Comparing offset-aware and offset-naive datetimes?

Discussion in 'Python' started by Roy Smith, Jan 27, 2013.

  1. Roy Smith

    Roy Smith Guest

    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). How can I compare
    these?

    May the flies of a thousand Norwegian Blue parrots infest the armpits of
    whoever invented timezones.
     
    Roy Smith, Jan 27, 2013
    #1
    1. Advertising

  2. Vito De Tullio, Jan 27, 2013
    #2
    1. Advertising

  3. Re: The utter mess of current timezone definitions (was: Comparingoffset-aware and offset-naive datetimes?)

    On Sun, Jan 27, 2013 at 4:25 PM, Ben Finney <> wrote:
    >> but I happen to know its offset is 0 (i.e. GMT).

    >
    > As further fuel for your hatred: GMT is not the same thing as UTC+0, and
    > never has been. (See the definitions of those two separate timezones for
    > more; Wikipedia's articles are probably a good start.)


    For most people's purposes, GMT and UTC are equivalent. I tell people
    that my D&D sessions are Sundays from 2:00 UTC to roughly 6:00-7:00
    UTC, and if they treat that as GMT, they're not going to miss the
    session. It's a lot better than a mess of "local time" with DST.

    Timezones aren't a problem. Daylight/Summer time (or Ireland's
    alternative, Winter time) is.

    ChrisA
     
    Chris Angelico, Jan 27, 2013
    #3
  4. Roy Smith

    Roy Smith Guest

    Re: The utter mess of current timezone definitions (was: Comparing offset-aware and offset-naive datetimes?)

    In article <>,
    Ben Finney <> wrote:

    > Roy Smith <> writes:
    >
    > > but I happen to know its offset is 0 (i.e. GMT).

    >
    > As further fuel for your hatred: GMT is not the same thing as UTC+0, and
    > never has been. (See the definitions of those two separate timezones for
    > more; Wikipedia's articles are probably a good start.)


    Yes, I am aware of the difference between GMT and UTC (and UT0, UT1,
    etc). None of which really matters for my purpose. In this case, it
    just so happens that the original string is:

    Sat, 26 Jan 2013 20:10:34 GMT

    so if anybody is conflating GMT and UTC, it's the dateutil parser :)
     
    Roy Smith, Jan 27, 2013
    #4
  5. Roy Smith

    Roy Smith Guest

    In article <>,
    Ben Finney <> wrote:

    > Roy Smith <> 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.
     
    Roy Smith, Jan 27, 2013
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Lance Riedel

    Translated Offset to Source Offset

    Lance Riedel, Oct 14, 2003, in forum: XML
    Replies:
    2
    Views:
    503
    Patrick TJ McPhee
    Oct 15, 2003
  2. John Hunter

    datetimes, tzinfo and gmtime epoch

    John Hunter, Apr 16, 2004, in forum: Python
    Replies:
    0
    Views:
    307
    John Hunter
    Apr 16, 2004
  3. ads

    DateTimes and MinOccurs

    ads, Apr 17, 2008, in forum: ASP .Net Web Services
    Replies:
    2
    Views:
    387
    Priya
    May 23, 2008
  4. noydb
    Replies:
    7
    Views:
    172
    noydb
    Dec 10, 2012
  5. Roy Smith
    Replies:
    8
    Views:
    186
Loading...

Share This Page