Saturday, 15 March 2014

Ruby on Rails parsing time from datetime, not as string -



Ruby on Rails parsing time from datetime, not as string -

i'm trying extract time component datetime object (which represented "at" in example). how do this, absolutely stumped? (i don't want parse string strftime did here):

@session_date.at.strftime("%h:%m")

i homecoming hours , minutes time object.

is there specific reason want time object?

just we're clear, time class in ruby isn't "datetime without date." "what's difference between datetime , time in ruby?" explains, "time wrapper around posix-standard time_t, or seconds since jan 1, 1970." datetime, time object still has year, month, , day, don't gain using time instead. there's not way represent hr , min using either time or datetime.

the best time, think, this:

date_time = datetime.now seconds = date_time.hour * 60 * 60 + date_time.minute * 60 time = time.at(seconds) # => 1970-01-01 09:58

...but still have phone call time.hour , time.min @ hr , minute.

if you're looking lightweight info construction represent hr , min pair, though, might roll own:

hourandminute = struct.new(:hour, :minute) def self.from_datetime(date_time) new(date_time.hour, date_time.minute) end end hm = hourandminute.from_datetime(datetime.now) # => #<struct hourandminute hour=15, minute=58> hm.to_h # => { :hour => 15, :minute => 58 } hm.to_a # => [ 15, 58 ] edit re:

i have variable stores appointment -- variable datetime object. have 2 table fields store start , end times of location. need check if time scheduled appointment lies between start , end times.

ah, seems had bit of xy problem. makes lot more sense now.

absent more information, i'm going assume "fields store start , end times of location" mysql time columns called start_time , start_time. given mysql time columns, rails casts values time objects with date component set 1/1/2000. if database has values start_time = '09:00' , end_time = '17:00', rails give time objects this:

start_time = time.new(2000, 1, 1, 9, 0) # => 2000-01-01 09:00:00 ... end_time = time.new(2000, 1, 1, 17, 0) # => 2000-01-01 17:00:00 ...

now appointment time datetime, let's phone call appointment_datetime , suppose it's @ 10:30am tomorrow:

appointment_datetime = datetime.new(2014, 11, 18, 10, 30) # => 2014-11-18 10:30:00 ...

so rephrase question: how tell if time part of appointment_datetime between time part of start_time , end_time. reply is, need either alter date part of start_time , end_time match date part of appointment_datetime, or other way around. since it's easier alter 1 thing two, let's other way around , alter appointment_datetime match start_time , end_time (and, since 2 time objects, we'll create time object):

appointment_time = datetime.new(2000, 1, 1, appointment_datetime.hour, appointment_datetime.minute) # => 2000-01-01 10:30:00 ...

now can compare them directly:

if appointment_time >= start_time && appointment_time <= end_time puts "appointment time good!" end # or, more succinctly: if (start_time..end_time).cover?(appointment_time) puts "appointment time good!" end

you would, of course, want wrap of in method, perhaps in location model (which, again, i'm assuming has start_time , end_time attributes):

class location < activerecord::base # ... def appointment_time_good?(appointment_datetime) appointment_time = datetime.new(2000, 1, 1, appointment_datetime.hour, appointment_datetime.minute) (start_time..end_time).cover?(appointment_time) end end location = location.find(12) # => #<location id: 12, ...> location.appointment_time_good?(appointment_time) # => true

i hope that's helpful!

p.s. way implement ditch date/time objects exclusively , straight numeric comparison:

def appointment_time_good?(appointment_datetime) appointment_hour_min = [ appointment_datetime.hour, appointment_datetime.minute ] appointment_hour_min >= [ start_time.hour, start_time.min ] && appointment_hour_min <= [ end_time.hour, end_time.min ] end

ruby-on-rails ruby datetime

No comments:

Post a Comment