4

I have a process where I read in a bunch of strings in ISO 8601 format at Zulu or UTC time. For example,

2012-06-20T21:15:00Z
2012-06-20T21:16:00Z
2012-06-20T21:17:00Z
2012-06-20T21:18:00Z

I convert the strings into timezone aware python datetime objects and then save them in a binary format as integers by converting them to Unix Timestamps. For example,

dt_str = '2012-06-20T21:15:00Z'
ts = int(mktime(datetime.strptime(dt_str, '%Y-%m-%dT%H:%M:%SZ').timetuple()))
# ts = 1340241300

When I read these timestamps back into another process I would like to instantiate a numpy.datetime64 object directly from the timestamp. The problem is that datetime64 sets the timezone to my local timezone.

np_dt = np.datetime64(ts,'s')
# np_dt = numpy.datetime64('2012-06-20T21:15:00-0400')

Does anyone know of a way that I could read in my timestamp so that it is UTC time? Would I would like for np_dt to equal is numpy.datetime64('2012-06-20T21:15:00-0000')...I think.

Regards

jterrace
  • 64,866
  • 22
  • 157
  • 202
aquil.abdullah
  • 3,059
  • 3
  • 21
  • 40

3 Answers3

4

What about setting the timezone for your code.

import os, time
os.environ['TZ'] = 'GMT'
time.tzset()
# then your code 
np_dt = np.datetime64(ts,'s')
karlcow
  • 6,977
  • 4
  • 38
  • 72
  • Looks like this requires numpy >= 1.7.0. Creation of datetime64 does not appear to allow for specifying seconds in 1.6.2: import pkg_resources pkg_resources.get_distribution('numpy').version '1.6.2' np.datetime64(1338624706, 's') TypeError: function takes at most 1 argument (2 given) – Kurt Schwehr Feb 24 '13 at 15:55
  • Code seems wrong to me: Is utc and gmt the same? (what about day-light savings, utc does NOT follow day-light savings: http://stackoverflow.com/questions/5495803/does-utc-observe-daylight-saving-time....) – ntg Aug 11 '15 at 12:58
  • UTC is more recent and more precise than GMT. It's better to use UTC. They both do not follow Daylight savings. http://geography.about.com/od/timeandtimezones/a/gmtutc.htm – karlcow Aug 11 '15 at 23:34
1

You can use the dateutil module to help out. First, create a datetime object from the timestamp integer you saved:

>>> ts = 1340241300
>>> import datetime
>>> from dateutil import tz
>>> dt = datetime.datetime.fromtimestamp(ts).replace(tzinfo=tz.tzutc())
>>> dt
datetime.datetime(2012, 6, 20, 21, 15, tzinfo=tzutc())

Then pass it to numpy, which will convert it to the local timezone (I am in -4):

>>> import numpy as np
>>> np.datetime64(dt.isoformat())
numpy.datetime64('2012-06-20T17:15:00-0400')
jterrace
  • 64,866
  • 22
  • 157
  • 202
  • So the idea is to convert the UTC to local timezone so that when numpy attaches the local timezone it will at least reflect the correct time? I wonder how well that works with daylight savings transitions. – Mark Ransom Jun 20 '12 at 22:49
  • Yes, exactly. It should work fine regardless of the local timezone – jterrace Jun 21 '12 at 00:28
  • Thanks for the reply. It seems like I may be better off just leaving the timestamp as a string. I'll have to do some more reading on datetime64, but ideally I would always like to work with UTC time. – aquil.abdullah Jun 21 '12 at 14:08
  • I don't think there's any way for the datetim64 to show as UTC. The time above **is** the same as your UTC time, but in the local timezone. – jterrace Jun 21 '12 at 14:16
  • Ah yes, understood. It seems as though the best way to go may be to leave the timestamps as strings. – aquil.abdullah Jun 21 '12 at 21:55
0

Judging by the documentation, the only way to do it is to create it from a string that specifies the time zone directly. Thus you'll need to create a datetime.datetime object first and format it to a string with 'Z' appended, then construct the numpy.datetime64 from that.

Mark Ransom
  • 299,747
  • 42
  • 398
  • 622
  • Thanks for the reply. I'll have to determine what I gain from storing the timestamp as an integer vs what it cost to convert it back to a string append the 'Z' and then instantiate a datetime64 object. – aquil.abdullah Jun 21 '12 at 14:04