2016-05-14 7 views
5

प्राप्त करें, इसलिए डेटटाइम ऑब्जेक्ट दिया गया है, और एक निश्चित समय, मैं दिए गए डेटटाइम ऑब्जेक्ट के बाद निश्चित समय की अगली घटना प्राप्त करना चाहता हूं।रूबी डेटटाइम: अगले 5:15 बजे (या इसी तरह)

  • उदाहरण के लिए

    , 14 मार्च, 2016 4:00 बजे की तारीख, और 5:15 बजे के समय को देखते हुए, मैं 14 वीं मार्च लौटना चाहते, 2016 5:15 बजे

  • हालांकि, 14 वीं मार्च, 2016, 6:00 तिथि, और 5:15 बजे के समय को देखते हुए, मैं 15 वीं मार्च, 2016, 5:15 बजे वापस जाने के लिए, चाहते हैं के बाद से है कि अगले घटना है।

अब तक, मैं इस कोड लिखा है:

# Given fixed_time and date_time 

new_time = date_time 
if fixed_time.utc.strftime("%H%M%S%N") >= date_time.utc.strftime("%H%M%S%N") 
    new_time = DateTime.new(
    date_time.year, 
    date_time.month, 
    date_time.day, 
    fixed_time.hour, 
    fixed_time.min, 
    fixed_time.sec 
) 
else 
    next_day = date_time.beginning_of_day + 1.day 
    new_time = DateTime.new(
    next_day.year, 
    next_day.month, 
    next_day.day, 
    fixed_time.hour, 
    fixed_time.min, 
    fixed_time.sec 
) 
end 

# Return new_time 

यह काम करता है, लेकिन वहाँ एक बेहतर तरीका है?

उत्तर

6

मैं सिर्फ एक बार नई तारीख समय का निर्माण और 1 दिन जोड़ने अगर जरूरत होगी: यह पुनर्रचना पर

# Given fixed_time and date_time 
new_date_time = DateTime.new(
    date_time.year, 
    date_time.month, 
    date_time.day, 
    fixed_time.hour, 
    fixed_time.min, 
    fixed_time.sec 
) 

# add 1 day if new date prior to the given date 
new_date_time += 1.day if new_date_time < date_time 
1

यह एक छोटा सा चाकू है अतिरेक में से कुछ को हटाने के लिए:

# Given fixed_time and date_time 

base_date = date_time.to_date 
if fixed_time.to_time.utc.strftime("%T%N") <= date_time.to_time.utc.strftime("%T%N") 
    base_date = base_date.next_day 
end 

new_time = DateTime.new(
    base_date.year, 
    base_date.month, 
    base_date.day, 
    fixed_time.hour, 
    fixed_time.min, 
    fixed_time.sec 
) 

# Return new_time 

सबसे बड़ी परिवर्तन यह है कि base_date को नया_टाइम बनाया जाने से पहले निर्धारित किया जाता है, ताकि इसका उपयोग किया जा सके।

मैं भी DATETIME को next_day विधि का इस्तेमाल अगले दिन प्राप्त करने के लिए, और के लिए एक शॉर्टकट के रूप में "% टी" फॉर्मेट स्पेसिफायर इस्तेमाल किया "% एच:% एम:% s"

यहाँ एक छोटे से परीक्षण कार्यक्रम है कि पता चलता है कि यह काम करता है:

require "date" 

def next_schedule(fixed_time, date_time) 
    # Given fixed_time and date_time 

    base_date = date_time.to_date 
    if fixed_time.to_time.utc.strftime("%T%N") <= date_time.to_time.utc.strftime("%T%N") 
    base_date = base_date.next_day 
    end 

    new_time = DateTime.new(
    base_date.year, 
    base_date.month, 
    base_date.day, 
    fixed_time.hour, 
    fixed_time.min, 
    fixed_time.sec 
) 

    # Return new_time 
end 

StartTime = DateTime.strptime("2016-02-14 17:15:00", "%F %T") 
Dates = [ 
    "2016-03-14 16:00:00", 
    "2016-03-14 18:00:00" 
] 

Dates.each do |current_date| 
    scheduled = next_schedule(StartTime, DateTime.strptime(current_date, "%F %T")) 
    puts "Scheduled: #{scheduled.strftime('%F %T')}" 
end 

इस के उत्पादन में है:

Scheduled: 2016-03-14 17:15:00 
Scheduled: 2016-03-15 17:15:00 

यह परीक्षण मामलों प्रश्न में वर्णित उपयोग कर रहा है, और यह उम्मीद जवाब हो जाता है।

+0

धन्यवाद! यह फ़ंक्शन को बेहतर और छोटा दिखता है, हालांकि दृष्टिकोण समान है। अगर मुझे बेहतर दृष्टिकोण के साथ कोई जवाब नहीं मिलता है, तो मैं निश्चित रूप से स्वीकृत उत्तर के रूप में आपका निशान चिन्हित करूंगा। – shashwat

+0

@shashwat बस यह देखने के लिए जांच कर रहा है कि क्या आप बेहतर दृष्टिकोण ढूंढ पाए हैं, या वह है जिसके साथ आप जा रहे हैं? –

संबंधित मुद्दे