0

I have a list of that contains times. Each time is the number of seconds from Jan 1, 1904. This is not Unix or any other conversion I have familiar with. How can I convert this to the date?

An example of this is 3438012868.0 which is 3438012868.0 seconds from Jan 1, 1904. I want this date to be Dec 11, 2012, 1:00.30 pm

GlassSeaHorse
  • 429
  • 2
  • 7
  • 14

1 Answers1

5

Use the datetime and timedelta classes for this kind of thing.

from datetime import datetime, timedelta

date = datetime(1904,1,1) # January 1st, 1904 at midnight

delta = timedelta(seconds = 10000000)

newdate = date + delta

newdate will evaulate to datetime.datetime(1904, 4, 26, 17, 46, 40), which is April 26, 1904,

Docs on these classes: https://docs.python.org/3/library/datetime.html

gcarvelli
  • 1,580
  • 1
  • 10
  • 15
  • The technique is perfect, the details are not - the question specified *1904*, not *1901*. – Mark Ransom Nov 20 '14 at 03:54
  • Whoops, fixing that now. – gcarvelli Nov 20 '14 at 03:59
  • I'm a little confused on this. I thought timedelta gives the time between two different times, what is this timedelta giving the time between? Sorry, this might be simple. I'm still really new to python. I also edited my question to give an example of one of the times. They are in seconds, so would i put that time in timedelta(seconds = data_seconds)? – GlassSeaHorse Nov 21 '14 at 04:34
  • I just tried for data_seconds in time: date = datetime(1904,1,1) delta = timedelta(seconds = data_seconds) newdate = date + delta print newdate and it gave me the wrong date. It gave me 18 days ahead and several hours ahead. I tried manually putting in the first time from the time list and it gave me the right date but about 16 hours ahead. – GlassSeaHorse Nov 21 '14 at 04:53
  • I just realized this is the Mac Timestamp if that means anything useful. – GlassSeaHorse Nov 21 '14 at 05:04
  • I got it to work! I just had to do date = datetime(1904,1,1,5,26,02) I think the extra times are because of different timezones, leap seconds and leap years. I'm not sure exactly, but using this date made everything work perfectly. – GlassSeaHorse Nov 21 '14 at 05:28
  • @jakeowen is the timezone you're working with one of those ones that had an oddball number of minutes offset in the distant past? Otherwise it doesn't explain the unusual starting time you're using - leap seconds wouldn't account for it. – Mark Ransom Nov 21 '14 at 05:47
  • @MarkRansom It might be offset by an oddball number of minutes. I'd like to look more into this but don't have the time to right now. the datetime I used worked for all the times in the list, so I'm content. Once I finish this project I will look more into why this odd number of seconds makes it works. – GlassSeaHorse Nov 21 '14 at 16:32