sqlite - Why is database time value different from the value created in the ruby time object? -
i have following code in seeds.rb file:
# create 7 day wall day in 1..7 # create days d = day.create(day: day) # each day create time slots. use 15 minute timeslots start_time = time.new.beginning_of_day puts start_time end_time = start_time.end_of_day puts end_time begin d.time_slots << timeslot.create(time: start_time) start_time += 15.minutes puts start_time end while start_time <= end_time puts "start time after exit: #{start_time}" campaign.days << d end
it creates right output 1 expect
2013-04-19 00:00:00 -0700 2013-04-19 23:59:59 -0700 2013-04-19 00:15:00 -0700 2013-04-19 00:30:00 -0700 2013-04-19 00:45:00 -0700 2013-04-19 01:00:00 -0700 2013-04-19 01:15:00 -0700 2013-04-19 01:30:00 -0700 2013-04-19 01:45:00 -0700 2013-04-19 02:00:00 -0700 2013-04-19 02:15:00 -0700 2013-04-19 02:30:00 -0700 2013-04-19 02:45:00 -0700 2013-04-19 03:00:00 -0700 2013-04-19 03:15:00 -0700 2013-04-19 03:30:00 -0700 ....
when in db using sqlite browser, see too. when use rails console inspect database see:
1.9.3p194 :001 > timeslot.find(1) timeslot load (17.3ms) select "time_slots".* "time_slots" "time_slots"."id" = ? limit 1 [["id", 1]] => #<timeslot id: 1, time: "2000-01-01 07:00:00", created_at: "2013-04-19 21:56:58", updated_at: "2013-04-19 21:56:58", campaign_id: nil, day_id: 1>
why record in database showing wrong date? timestamps right, time field wrong. don't it.
there 2 things going on in problem:
- there nuance using
time
,datetime
types in rails. see time fields in rails coming blank - the time zone issue. db stores times in utc/gmt. when
time.new
called uses system's current timezone.
the code increment time object right. need take timezone account when i'm displaying times.
Comments
Post a Comment