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

3 Answers 3

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')
Sign up to request clarification or add additional context in comments.

3 Comments

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)
Code seems wrong to me: Is utc and gmt the same? (what about day-light savings, utc does NOT follow day-light savings: stackoverflow.com/questions/5495803/…)
UTC is more recent and more precise than GMT. It's better to use UTC. They both do not follow Daylight savings. geography.about.com/od/timeandtimezones/a/gmtutc.htm
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')

5 Comments

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.
Yes, exactly. It should work fine regardless of the local timezone
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.
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.
Ah yes, understood. It seems as though the best way to go may be to leave the timestamps as strings.
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.

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.