parsing a date string

Discussion in 'Python' started by MikeyG, Mar 6, 2005.

  1. MikeyG

    MikeyG Guest

    Hi,

    I have a date string in the ctime() format ('Sat Mar 5 10:38:07 2005')
    and I want to know how long ago that was in whole days.

    So far I have:

    import time
    import datetime
    age =
    (datetime.date.fromtimestamp(time.mktime(time.strptime(date.strip()))) -
    datetime.date.today()).days

    Which is an absurd number of function calls and is possibly not even
    correct (something in the docs about mktime() taking localtime whereas
    my string is in UTC)

    Any suggestions?

    Thanks
    MikeG
    MikeyG, Mar 6, 2005
    #1
    1. Advertising

  2. Am Sun, 06 Mar 2005 19:35:23 +0000 schrieb MikeyG:

    > Hi,
    >
    > I have a date string in the ctime() format ('Sat Mar 5 10:38:07 2005')
    > and I want to know how long ago that was in whole days.
    >
    > So far I have:
    >
    > import time
    > import datetime
    > age =
    > (datetime.date.fromtimestamp(time.mktime(time.strptime(date.strip()))) -
    > datetime.date.today()).days
    >
    > Which is an absurd number of function calls and is possibly not even
    > correct (something in the docs about mktime() taking localtime whereas
    > my string is in UTC)


    Try %Z:

    time.mktime(time.strptime("Sat Mar 5 10:38:07 2005 UTC",
    "%a %b %d %H:%M:%S %Y %Z"))
    --> 1110015487.0

    time.mktime(time.strptime("Sat Mar 5 10:38:07 2005 CEST",
    "%a %b %d %H:%M:%S %Y %Z"))
    --> 1110011887.0

    HTH,
    Thomas


    --
    Thomas G├╝ttler, http://www.thomas-guettler.de/
    Thomas Guettler, Mar 7, 2005
    #2
    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. Matt
    Replies:
    1
    Views:
    564
    Matthew Speed
    Nov 8, 2003
  2. .Net Sports
    Replies:
    1
    Views:
    2,639
    intrader
    Jun 25, 2005
  3. Peter Grison

    Date, date date date....

    Peter Grison, May 28, 2004, in forum: Java
    Replies:
    10
    Views:
    3,183
    Michael Borgwardt
    May 30, 2004
  4. Matt
    Replies:
    2
    Views:
    491
    Pete Becker
    Nov 8, 2003
  5. davyb
    Replies:
    7
    Views:
    194
    Gregory Brown
    Nov 18, 2005
Loading...

Share This Page