from date/time string to seconds since epoch

Discussion in 'Python' started by Peter Kleiweg, Jul 6, 2005.

  1. Is there a safe and clean way to parse a date/time string into
    seconds since epoch?

    I have a string with date and time in GMT. I can get the correct
    value using this:


    #!/usr/bin/env python
    import os
    os.environ['TZ'] = 'UTC'
    import time
    s = 'Wed, 06 Jul 2005 16:49:38 GMT'
    seconds = time.mktime(time.strptime(s, '%a, %d %b %Y %H:%M:%S %Z'))


    However, I also need conversions to localtime. Setting TZ to UTC
    before importing the time module won't let me do this. Changing
    TZ after importing time has no effect.


    --
    Peter Kleiweg L:NL,af,da,de,en,ia,nds,no,sv,(fr,it) S:NL,de,en,(da,ia)
    info: http://www.let.rug.nl/~kleiweg/ls.html
    Peter Kleiweg, Jul 6, 2005
    #1
    1. Advertising

  2. Peter Kleiweg

    Guest

    You can calculate the offset of where you are like this:

    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.
    py> import time
    py> offset = 0
    py> if time.daylight:
    py. offset = time.altzone
    py. else:
    py. offset = time.timezone
    py.
    py> print offset
    -7200
    py>

    You can use time.time() to get the UTC time, then add the offset.
    , Jul 6, 2005
    #2
    1. Advertising

  3. schreef op de 6e dag van de hooimaand van het jaar 2005:

    > You can calculate the offset of where you are like this:
    >
    > 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.
    > py> import time
    > py> offset = 0
    > py> if time.daylight:
    > py. offset = time.altzone
    > py. else:
    > py. offset = time.timezone
    > py.
    > py> print offset
    > -7200
    > py>
    >
    > You can use time.time() to get the UTC time, then add the offset.


    The offset is for this moment, not for the date and time of the
    string parsed.

    --
    Peter Kleiweg L:NL,af,da,de,en,ia,nds,no,sv,(fr,it) S:NL,de,en,(da,ia)
    info: http://www.let.rug.nl/~kleiweg/ls.html
    Peter Kleiweg, Jul 6, 2005
    #3
    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. Summu82
    Replies:
    5
    Views:
    853
    Richard Heathfield
    Jun 7, 2006
  2. Alexander
    Replies:
    0
    Views:
    245
    Alexander
    Jul 20, 2010
  3. Rami Chowdhury

    Re: convert time to UTC seconds since epoch

    Rami Chowdhury, Jul 20, 2010, in forum: Python
    Replies:
    5
    Views:
    1,074
    Steve Allen
    Jul 21, 2010
  4. Ian Kelly
    Replies:
    0
    Views:
    443
    Ian Kelly
    Jul 21, 2010
  5. Victor Hooi
    Replies:
    8
    Views:
    169
    Barry Scott
    Jan 3, 2013
Loading...

Share This Page