working with daylight saving time

This is the place for queries that don't fit in any of the other categories.

working with daylight saving time

Postby metulburr » Sun Mar 31, 2013 9:33 am

originally i didnt bother to add daylight savings time. Prob because when i first wrote it it wasnt daylight savings time. and i didnt even think about it. Well when it did start. I noticed my counters were an hour short of each day. -1 hour accuratte time.

So at the time, i was thinking. OK i just need to add an hour then. So i did.
Code: Select all
      if time.localtime().tm_isdst:
         diff += 3600 #add daylight savings time hour (in seconds)

from the function:
Code: Select all
   def time_since_stamp(self, time_stamp):
      '''update time since the timestamp'''
      current_time = time.time()
      diff = current_time - float(time_stamp)
      if time.localtime().tm_isdst:
         diff += 3600 #add daylight savings time hour (in seconds)

      sincetime_sec = int(diff)

      sincetime_min = (diff / 60)
      sincetime_min_rem = int((diff % 60))

      sincetime_hour = (sincetime_min / 60)
      sincetime_hour_rem = int((sincetime_min % 60))

      sincetime_day = (sincetime_hour / 24)
      sincetime_day_rem = int((sincetime_hour % 24))

      data = {
         'day':int(sincetime_day),
         'hour':sincetime_day_rem,
         'sec':sincetime_min_rem,
         'min':sincetime_hour_rem,
         'total_sec':sincetime_sec
         }
      return data


now i just for some reason decided to do this, but i reset the counter, and realized that "dah" its suppose to be 0 hours and now i have 1 hour because of that.

so an example output would be (in restarting 2 minutes and 38 seconds ago, in so that since time last smoked should be only 2 min, 38 sec, and life saved should be just 48 seconds):
Quit Date: 03/31/2013 05:21 AM
Cigarettes avoided: 0
Money saved: $0.00
Since last smoke: 0 days, 1 hours, 2 minutes, 38 seconds
Life saved: 0 days, 1 hours, 0 minutes, 48 seconds
You are on day: 1

[####-----------------------------] 13% 20 minutes



So i am not sure how to calculate daylight savings time in this







EDIT: actually i am quite confused as restarting it, it makes since without that adding 3600 seconds, but puttings one that is running with it and without it side by side, the one with daylight on makes sense as the since last smoke time and hours is correct.

with daylight:
Code: Select all
Quit Date: 02/16/2013 02:30 AM
Cigarettes avoided: 1723
Money saved: $74.12
Since last smoke: 43 days, 3 hours, 15 minutes, 29 seconds
Life saved: 13 days, 5 hours, 1 minutes, 23 seconds
You are on day: 44

[#################################] 100%  20 minutes
[#################################] 100%  8 Hours
[#################################] 100%  12 Hours
[#################################] 100%  24 Hours
[#################################] 100%  48 Hours
[#################################] 100%  72 Hours
[#################################] 100%  5-8 days
[#################################] 100%  10 days
[#################################] 100%  10 Days - 2 Weeks
[#################################] 100%  21 Days
[#################################] 100%  2-4 Weeks
[#########################--------] 76%  8 Weeks
[###############------------------] 46%  2 Weeks - 3 Months
[###############------------------] 46%  3 Weeks - 3 Months
[#####----------------------------] 15%  1-9 Months
[###------------------------------] 11%  1 Year
[---------------------------------] 1%  10 Years
[---------------------------------] 0%  13 Years
[---------------------------------] 0%  5-15 Years
[---------------------------------] 0%  15 Years
[---------------------------------] 0%  20 Years


wihtout daylight:
Code: Select all
Quit Date: 02/16/2013 02:30 AM
Cigarettes avoided: 1723
Money saved: $74.12
Since last smoke: 43 days, 2 hours, 15 minutes, 28 seconds
Life saved: 13 days, 4 hours, 1 minutes, 23 seconds
You are on day: 44

[#################################] 100%  20 minutes
[#################################] 100%  8 Hours
[#################################] 100%  12 Hours
[#################################] 100%  24 Hours
[#################################] 100%  48 Hours
[#################################] 100%  72 Hours
[#################################] 100%  5-8 days
[#################################] 100%  10 days
[#################################] 100%  10 Days - 2 Weeks
[#################################] 100%  21 Days
[#################################] 100%  2-4 Weeks
[#########################--------] 76%  8 Weeks
[###############------------------] 46%  2 Weeks - 3 Months
[###############------------------] 46%  3 Weeks - 3 Months
[#####----------------------------] 15%  1-9 Months
[###------------------------------] 11%  1 Year
[---------------------------------] 1%  10 Years
[---------------------------------] 0%  13 Years
[---------------------------------] 0%  5-15 Years
[---------------------------------] 0%  15 Years
[---------------------------------] 0%  20 Years
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1562
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: working with daylight saving time

Postby tnknepp » Mon Apr 01, 2013 7:58 pm

This is probably 100% not useful, but it is for these types of issues that we always record our data in UTC. UTC time never shifts for daylight saving.
Python: 2.7 via Anaconda
Numpy: 1.7
Pandas: 0.11
OS: Windows 7
IDE: Spyder/IPython
User avatar
tnknepp
 
Posts: 145
Joined: Mon Mar 11, 2013 7:41 pm


Return to General Coding Help

Who is online

Users browsing this forum: Google [Bot], Keepsake and 2 guests