Comparing offset-aware and offset-naive datetimes?

Roy Smith roy at panix.com
Sun Jan 27 10:17:59 EST 2013


In article <mailman.1101.1359263568.2939.python-list at python.org>,
 Ben Finney <ben+python at benfinney.id.au> wrote:

> Roy Smith <roy at panix.com> writes:
> 
> > I have two datetimes.  One is offset-naive.  The other is offset-aware, 
> > but I happen to know its offset is 0 (i.e. GMT).
> 
> Do you know the timezone of the offset-naive value?
> 
> Or is the above mixed up, and you mean “one is offset-aware; the other
> is offset-naive, but I happen to know its offset is UTC+0”?

Well, actually, what I wrote was correct, but incomplete.

The naive value came from a unix timestamp, so I know it is UTC.  The 
aware value came from dateutil's parsing of a string that looks like:

Sat, 26 Jan 2013 20:10:34 GMT

so I know that one's UTC too :-)  Unfortunately, the boto documentation 
describes it only as "The string timestamp representing the last time 
this object was modified in S3" without specifying the exact format.  
I'm not sure if it's showing up in GMT because my server happens to be 
running on GMT, or if it always does, so I'd rather let dateutil figure 
it out for me.

> In which case, you can create a new timezone-aware value using the
> timezone-naive value and the timezone you've decided to apply::
> 
>     >>> timestamp_c = timestamp_a.replace(tzinfo=timezone_for_a)

That's what I ended up with, thanks:

    s3_time = dateutil.parser.parse(s3_key.last_modified)
    info = os.stat(file)
    file_time = datetime.utcfromtimestamp(info.st_mtime)
    utc = dateutil.tz.tzutc()
    file_time = file_time.replace(tzinfo=utc)
    if s3_time < file_time:

> What I'm not fine with is politicians who think it's a fun game to
> fiddle with the specific timezone definitions with little advance notice
> and leave we programmers to clean up the mess they keep making. Those
> people are sorely in need of a nasal infestation of parrot fleas.

Yes, indeed.  I've been around long enough to remember when calendar 
code had the "Nixon flag" and the "Ford flag", named for the first two 
yahoos who started playing timekeeper for a day.  That was before the 
Olson database existed.



More information about the Python-list mailing list