time.mktime problem

Discussion in 'Python' started by McBooCzech, Aug 30, 2005.

  1. McBooCzech

    McBooCzech Guest

    Hi, on Linux (Fedora FC4) and Python 2.4.1
    I am trying to know the time delta in seconds between two times given
    in the HHMMSS format. My code looks like:

    import datetime, time
    ta1=(time.strptime('000001', '%H%M%S'))
    ta2=(time.strptime('230344', '%H%M%S'))
    t1=time.mktime(ta1)
    t2=time.mktime(ta2)
    print t1, t2

    -2147483648.0 -2147483648.0

    I just can not figure out, why the t1 and t2 are the same?


    Thanks for your comments

    Petr Jakes
    McBooCzech, Aug 30, 2005
    #1
    1. Advertising

  2. McBooCzech

    Matt Hammond Guest

    I don't get the same results:

    >>> import datetime, time
    >>> ta1=(time.strptime('000001', '%H%M%S'))
    >>> ta2=(time.strptime('230344', '%H%M%S'))
    >>> t1=time.mktime(ta1)
    >>> t2=time.mktime(ta2)
    >>> print t1, t2

    -2208988799.0 -2208905776.0
    >>> print t1-t2

    -83023.0

    Suse 9.3, python 2.4 (all 64bit)


    Matt

    --

    | Matt Hammond
    | R&D Engineer, BBC Research and Development, Tadworth, Surrey, UK.
    Matt Hammond, Aug 30, 2005
    #2
    1. Advertising

  3. "McBooCzech" <> writes:

    > Hi, on Linux (Fedora FC4) and Python 2.4.1
    > I am trying to know the time delta in seconds between two times given
    > in the HHMMSS format. My code looks like:
    >
    > import datetime, time
    > ta1=(time.strptime('000001', '%H%M%S'))
    > ta2=(time.strptime('230344', '%H%M%S'))
    > t1=time.mktime(ta1)
    > t2=time.mktime(ta2)
    > print t1, t2
    >
    > -2147483648.0 -2147483648.0
    >
    > I just can not figure out, why the t1 and t2 are the same?


    Hm. You are trying to convert (1900, 1, 1, 0, 0, 1, 0, 1, -1) to epoch.
    However, epochs start from 1970-01-01 00:00. So that at least is not right.

    Hint... see what var ta1 is. With python2.3 you'll get overflow error, becuase
    mktime argument is out of range.

    --
    # Edvard Majakari Software Engineer
    # PGP PUBLIC KEY available Soli Deo Gloria!

    $_ = '456476617264204d616a616b6172692c20612043687269737469616e20'; print
    join('',map{chr hex}(split/(\w{2})/)),uc substr(crypt(60281449,'es'),2,4),"\n";
    Edvard Majakari, Aug 30, 2005
    #3
  4. McBooCzech

    McBooCzech Guest

    according to the Python documentation:
    http://docs.python.org/lib/module-time.html

    ===snip===
    Values 100-1899 are always illegal.
    ..
    ..
    strptime(string[, format])
    ..
    ..
    The default values used to fill in any missing data are:
    (1900, 1, 1, 0, 0, 0, 0, 1, -1)
    ===snip===

    BTW, check the following code:
    >>import datetime, time
    >>print time.gmtime(time.mktime((1900, 1, 1, 0, 0, 0, 0, 1, -1)))

    (1901, 12, 13, 20, 45, 52, 4, 347, 0)

    but (1900, 1, 1, 0, 0, 0, 0, 1, -1) is (IMHO) expected.... Hmmm. But I
    am just a newbie!!! :)

    Anyway, maybe I am just using a wrong way how to calculate time delta
    between two time values given in the format "HHMMSS".

    Does Python provide some other ways how to calculate it?

    Petr Jakes
    McBooCzech, Aug 30, 2005
    #4
  5. "McBooCzech" <> writes:


    > ===snip===
    > Values 100-1899 are always illegal.
    > .
    > .
    > strptime(string[, format])
    > .
    > .
    > The default values used to fill in any missing data are:
    > (1900, 1, 1, 0, 0, 0, 0, 1, -1)
    > ===snip===
    >
    > BTW, check the following code:
    >>>import datetime, time
    >>>print time.gmtime(time.mktime((1900, 1, 1, 0, 0, 0, 0, 1, -1)))

    > (1901, 12, 13, 20, 45, 52, 4, 347, 0)
    >
    > but (1900, 1, 1, 0, 0, 0, 0, 1, -1) is (IMHO) expected.... Hmmm. But I
    > am just a newbie!!! :)


    You are comparing apples and oranges here. You checked documentation of
    strptime, and the problem is in the use of time.mktime().

    The point: time.mktime() returns Epoch time (seconds since 1970) and you are
    passing it a tuple which is (way before) 1970. There is no such thing as
    negative epoch. It is like computing packaging day of milk which hasn't been
    milked from the cow yet :)

    I really wonder what version of Python you are running:

    >>> import datetime, time
    >>> print time.gmtime(time.mktime((1900, 1, 1, 0, 0, 0, 0, 1, -1)))

    Traceback (most recent call last):
    File "<stdin>", line 1, in ?
    OverflowError: mktime argument out of range

    Python 2.3 and 2.4 both give the same error. As for the python version 2.2, no
    datetime module was implemented.

    --
    # Edvard Majakari Software Engineer
    # PGP PUBLIC KEY available Soli Deo Gloria!

    $_ = '456476617264204d616a616b6172692c20612043687269737469616e20'; print
    join('',map{chr hex}(split/(\w{2})/)),uc substr(crypt(60281449,'es'),2,4),"\n";
    Edvard Majakari, Sep 5, 2005
    #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. Florian Quetting

    mktime segfault

    Florian Quetting, Dec 31, 2004, in forum: C++
    Replies:
    2
    Views:
    501
    Florian Quetting
    Dec 31, 2004
  2. Bengt Richter

    time.mktime memory access violation bug

    Bengt Richter, Nov 18, 2003, in forum: Python
    Replies:
    6
    Views:
    680
    Bengt Richter
    Nov 21, 2003
  3. KW
    Replies:
    1
    Views:
    1,797
    Eric Sosman
    Dec 8, 2004
  4. saurabh

    time issue (mktime time_t tm)

    saurabh, Apr 14, 2009, in forum: C Programming
    Replies:
    3
    Views:
    1,846
  5. Replies:
    1
    Views:
    250
    Daniel Fischer
    May 24, 2006
Loading...

Share This Page