different time tuple format

Discussion in 'Python' started by Maksim Kasimov, Jun 7, 2005.

  1. hi all, sorry if i'm reposting

    why time.strptime and time.localtime returns tuple with different DST (9 item of the tuple)?
    is there some of setting to fix the problem?


    Python 2.2.3 (#1, May 31 2005, 11:33:52)
    [GCC 2.95.4 20020320 [FreeBSD]] on freebsd4
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import time
    >>> time.strptime("2005-06-07 21:00:00", "%Y-%m-%d %H:%M:%S")

    (2005, 6, 7, 21, 0, 0, 6, 1, 0)
    >>> time.localtime()

    (2005, 6, 7, 21, 2, 39, 1, 158, 1)
    >>>



    --
    Best regards,
    Maksim Kasimov
    mailto:
    Maksim Kasimov, Jun 7, 2005
    #1
    1. Advertising

  2. Maksim Kasimov

    Guest

    http://docs.python.org/lib/module-time.html tells us the last element
    is the DST flag, on your computer that applies for localtime(). To get
    this with strptime() you have to tweak the %Z formatter - this is
    platform specific.
    , Jun 7, 2005
    #2
    1. Advertising

  3. Maksim Kasimov

    Rick Holbert Guest

    Like the last poster said, use %Z. On my Mandriva Linux system I get the
    following results:

    >>> time.localtime()

    (2005, 6, 7, 15, 7, 12, 1, 158, 1)
    >>> time.strptime("2005-06-07 15:07:12 EDT", "%Y-%m-%d %H:%M:%S %Z")

    (2005, 6, 7, 15, 7, 12, 1, 158, 1)

    Rick

    Maksim Kasimov wrote:

    > hi all, sorry if i'm reposting
    >
    > why time.strptime and time.localtime returns tuple with different DST (9
    > item of the tuple)? is there some of setting to fix the problem?
    >
    >
    > Python 2.2.3 (#1, May 31 2005, 11:33:52)
    > [GCC 2.95.4 20020320 [FreeBSD]] on freebsd4
    > Type "help", "copyright", "credits" or "license" for more information.
    > >>> import time
    > >>> time.strptime("2005-06-07 21:00:00", "%Y-%m-%d %H:%M:%S")

    > (2005, 6, 7, 21, 0, 0, 6, 1, 0)
    > >>> time.localtime()

    > (2005, 6, 7, 21, 2, 39, 1, 158, 1)
    > >>>

    >
    >
    Rick Holbert, Jun 7, 2005
    #3
  4. Rick Holbert wrote:
    > Like the last poster said, use %Z. On my Mandriva Linux system I get the
    > following results:
    >
    >
    >>>>time.localtime()

    >
    > (2005, 6, 7, 15, 7, 12, 1, 158, 1)
    >
    >>>>time.strptime("2005-06-07 15:07:12 EDT", "%Y-%m-%d %H:%M:%S %Z")

    > (2005, 6, 7, 15, 7, 12, 1, 158, 1)


    does not work at all: "ValueError: format mismatch"

    i've check the value of time.tzname:
    ('EET', 'EEST')

    and get the following (again):

    >>> time.strptime("2005-06-07 15:07:12 EET", "%Y-%m-%d %H:%M:%S %Z")

    (2005, 6, 7, 15, 7, 12, 6, 1, 0)









    --
    Best regards,
    Maksim Kasimov
    mailto:
    Maksim Kasimov, Jun 7, 2005
    #4
  5. Maksim Kasimov

    Guest

    In your case it is the EEST, as this is the DST timezone (see again:
    http://docs.python.org/lib/module-time.html)

    ** martin@ubuntu:~ $ python
    ** Python 2.4.1 (#2, Mar 30 2005, 21:51:10)
    ** [GCC 3.3.5 (Debian 1:3.3.5-8ubuntu2)] on linux2
    ** Type "help", "copyright", "credits" or "license" for more
    information.
    ** >>> import time
    ** >>> print time.tzname
    ** ('CET', 'CEST')
    ** >>> time.strptime("2005-06-07 15:07:12 CET", "%Y-%m-%d %H:%M:%S %Z")
    ** (2005, 6, 7, 15, 7, 12, 1, 158, 0)
    ** >>> time.strptime("2005-06-07 15:07:12 CEST", "%Y-%m-%d %H:%M:%S
    %Z")
    ** (2005, 6, 7, 15, 7, 12, 1, 158, 1)
    ** >>>
    , Jun 7, 2005
    #5
  6. seems like it is not a platform specific,
    i think to solve the problem i need put settings in order (in php it is php.ini file) thus i'll have a portable code.
    i've check the following code on my various servers, and it gives me different results:

    import time
    time.tzname
    time.daylight
    time.strptime("2005-06-07 15:07:12", "%Y-%m-%d %H:%M:%S")
    time.strptime("2005-06-07 15:07:12 EEST", "%Y-%m-%d %H:%M:%S %Z")




    Python 2.3.3 (#1, Feb 28 2004, 20:35:22)
    [GCC 2.95.4 20020320 [FreeBSD]] on freebsd4
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import time
    >>> time.tzname

    ('EET', 'EEST')
    >>> time.daylight

    1
    >>> time.strptime("2005-06-07 15:07:12", "%Y-%m-%d %H:%M:%S")

    (2005, 6, 7, 15, 7, 12, 1, 158, -1)
    >>> time.strptime("2005-06-07 15:07:12 EEST", "%Y-%m-%d %H:%M:%S %Z")

    (2005, 6, 7, 15, 7, 12, 1, 158, 1)



    Python 2.2.3 (#1, Oct 22 2004, 03:10:44)
    [GCC 2.95.4 20020320 [FreeBSD]] on freebsd4
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import time
    >>> time.tzname

    ('EET', 'EEST')
    >>> time.daylight

    1
    >>> time.strptime("2005-06-07 15:07:12", "%Y-%m-%d %H:%M:%S")

    (2005, 6, 7, 15, 7, 12, 6, 1, 0)
    >>> time.strptime("2005-06-07 15:07:12 EEST", "%Y-%m-%d %H:%M:%S %Z")

    (2005, 6, 7, 15, 7, 12, 6, 1, 1)



    wrote:
    > In your case it is the EEST, as this is the DST timezone (see again:
    > http://docs.python.org/lib/module-time.html)
    >
    > ** martin@ubuntu:~ $ python
    > ** Python 2.4.1 (#2, Mar 30 2005, 21:51:10)
    > ** [GCC 3.3.5 (Debian 1:3.3.5-8ubuntu2)] on linux2
    > ** Type "help", "copyright", "credits" or "license" for more
    > information.
    > ** >>> import time
    > ** >>> print time.tzname
    > ** ('CET', 'CEST')
    > ** >>> time.strptime("2005-06-07 15:07:12 CET", "%Y-%m-%d %H:%M:%S %Z")
    > ** (2005, 6, 7, 15, 7, 12, 1, 158, 0)
    > ** >>> time.strptime("2005-06-07 15:07:12 CEST", "%Y-%m-%d %H:%M:%S
    > %Z")
    > ** (2005, 6, 7, 15, 7, 12, 1, 158, 1)
    > ** >>>
    >



    --
    Best regards,
    Maksim Kasimov
    mailto:
    Maksim Kasimov, Jun 8, 2005
    #6
  7. Maksim Kasimov

    Guest

    The names are at least platform specific, see below the names of the
    timezones on my Windows NT 4 box

    *** Python 2.3.4 (#53, May 25 2004, 21:17:02) [MSC v.1200 32 bit
    (Intel)] on win32
    *** Type "help", "copyright", "credits" or "license" for more
    information.
    *** >>> import time
    *** >>> print time.tzname
    *** ('W. Europe Standard Time', 'W. Europe Daylight Time')
    *** >>>
    , Jun 8, 2005
    #7
  8. yes, i agree, on my WinXP it gives another values.

    but my question is how to setup the python (or OS) to make it gives the same results when i call
    time.strptime("2005-06-07 15:07:12", "%Y-%m-%d %H:%M:%S")
    on various servers (and maybe with various OS)?

    for now, i can't get it even with the same OS.
    and i would like to set time string exactly as "2005-06-07 15:07:12", without "CEST", "EEST" and so on, because as you've notice before, it is different on a variuos systems


    wrote:
    > The names are at least platform specific, see below the names of the
    > timezones on my Windows NT 4 box
    >
    > *** Python 2.3.4 (#53, May 25 2004, 21:17:02) [MSC v.1200 32 bit
    > (Intel)] on win32
    > *** Type "help", "copyright", "credits" or "license" for more
    > information.
    > *** >>> import time
    > *** >>> print time.tzname
    > *** ('W. Europe Standard Time', 'W. Europe Daylight Time')
    > *** >>>
    >



    --
    Best regards,
    Maksim Kasimov
    mailto:
    Maksim Kasimov, Jun 8, 2005
    #8
  9. Maksim Kasimov

    Guest

    It is probably the best to calculate back to UTC.

    Assume "2005-06-07 15:07:12" the local time, then convert it as
    follows to UTC. Use the UTC time to store/manipulate/whatever you want
    to do.

    import time
    t = time.mktime(time.strptime("2005-06-07 15:07:12", "%Y-%m-%d
    %H:%M:%S"))

    print time.ctime(t)

    offset = time.timezone
    if time.daylight:
    offset = time.altzone
    t += offset
    print time.ctime(t)
    , Jun 8, 2005
    #9
  10. maybe you are right, i've searched in google groups - such a question was posted to comp.lang.python many times and i has not found (yet) the answer on "how to tune up the output of time.strptime()?"


    wrote:
    > It is probably the best to calculate back to UTC.
    >
    > Assume "2005-06-07 15:07:12" the local time, then convert it as
    > follows to UTC. Use the UTC time to store/manipulate/whatever you want
    > to do.
    >
    > import time
    > t = time.mktime(time.strptime("2005-06-07 15:07:12", "%Y-%m-%d
    > %H:%M:%S"))
    >
    > print time.ctime(t)
    >
    > offset = time.timezone
    > if time.daylight:
    > offset = time.altzone
    > t += offset
    > print time.ctime(t)
    >



    --
    Best regards,
    Maksim Kasimov
    mailto:
    Maksim Kasimov, Jun 9, 2005
    #10
  11. Maksim Kasimov

    Magnus Lycka Guest

    Maksim Kasimov wrote:
    > hi all, sorry if i'm reposting
    >
    > why time.strptime and time.localtime returns tuple with different DST (9
    > item of the tuple)?


    I've been bitten by the quirks in the time modules so many times
    that I would advice against using it for any date handling. It's
    ok for time measurement as long as you understand the differences
    between time.clock and time.time on your particular platform. You
    should be aware that it's just a thin wrapper around the c libs,
    and they seem to disagree wildly among platforms on how things
    should be done. On NT 4 for instance, the C time libs was a few
    weeks off concerning when DST starts in Europe (but the win32 API
    was correct), time zone names vary among platforms etc. There is
    an inverse of localtime() in mktime(), but no inverse of gmtime()
    etc. Yuk!

    I would suggest that you either update Python to 2.4 (or 2.3) and
    use the datetime module, or that you get mxDateTime if you are
    stuck with 2.2.
    Magnus Lycka, Jun 10, 2005
    #11
    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. Michal Mikolajczyk
    Replies:
    1
    Views:
    792
    Larry Bates
    Apr 20, 2004
  2. Jeff Epler
    Replies:
    0
    Views:
    924
    Jeff Epler
    Apr 20, 2004
  3. Bill Scherer
    Replies:
    0
    Views:
    602
    Bill Scherer
    Apr 20, 2004
  4. Gregor Horvath

    Why tuple with one item is no tuple

    Gregor Horvath, Mar 15, 2005, in forum: Python
    Replies:
    37
    Views:
    793
    Antoon Pardon
    Mar 30, 2005
  5. Steve
    Replies:
    1
    Views:
    945
    Fredrik Lundh
    Dec 13, 2005
Loading...

Share This Page