epoch seconds from a datetime

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Richard Rossel

    epoch seconds from a datetime

    Hi friends,
    I need a little help here, I 'm stuck with epoch calculation issue.
    I have this datetime:
    date_new = datetime(*time. strptime('20080 101T000000','%Y %m%dT%H%M%S')
    [0:6])
    This date_new is in UTC
    Now I need to know the seconds since epoch of this new date, so I run
    this:
    seconds = int(time.mktime (date_new.timet uple()))
    but the seconds returned belongs to :
    Tue, 01 Jan 2008 03:00:00 GMT
    because the localtime is in timezone 'America/Santiago': -3

    I fix this trying to alter the TZ with time.tzset():
    os.environ['TZ'] = 'UTC'
    time.tzset()

    ..... and now I can gets the right epoch, but I can't restore the
    previous TimeZone, I try with:
    os.environ['TZ'] = '', but the time.tzset() doesn't back to the
    original ( America/Santiago)

    A solution should be set the os.environ['TZ'] to 'America/Santiago'
    but I can't make a TZ hardcode because
    the software should works on different timezones.

    So the question, how can restore the system into original timezone, or
    how to know the seconds since epoch
    from UTC datetime without change the local system TIMEZONE.

    please help

  • Chris Rebert

    #2
    Re: epoch seconds from a datetime

    On Thu, Aug 28, 2008 at 10:18 AM, Richard Rossel <henhiskan@gmai l.comwrote:
    Hi friends,
    I need a little help here, I 'm stuck with epoch calculation issue.
    I have this datetime:
    date_new = datetime(*time. strptime('20080 101T000000','%Y %m%dT%H%M%S')
    [0:6])
    This date_new is in UTC
    Now I need to know the seconds since epoch of this new date, so I run
    this:
    seconds = int(time.mktime (date_new.timet uple()))
    but the seconds returned belongs to :
    Tue, 01 Jan 2008 03:00:00 GMT
    because the localtime is in timezone 'America/Santiago': -3
    >
    I fix this trying to alter the TZ with time.tzset():
    os.environ['TZ'] = 'UTC'
    time.tzset()
    >
    .... and now I can gets the right epoch, but I can't restore the
    previous TimeZone, I try with:
    os.environ['TZ'] = '', but the time.tzset() doesn't back to the
    original ( America/Santiago)
    I think you need to del os.environ['TZ'] rather than setting it to the
    empty string.

    On my box:
    Python 2.5.1 (r251:54863, Feb 4 2008, 21:48:13)
    [GCC 4.0.1 (Apple Inc. build 5465)] on darwin
    >>import os, time
    >>time.asctime( )
    'Thu Aug 28 11:19:57 2008'
    >>#that's my correct local time
    >>time.tzname
    ('PST', 'PDT')
    >>#that's my correct timezone
    >>os.environ['TZ'] = 'UTC'
    >>time.tzset( )
    >>time.tzname
    ('UTC', 'UTC')
    >>time.asctime( )
    'Thu Aug 28 18:20:33 2008'
    >>#we're clearly in UTC now
    >>del os.environ['TZ'] #this is the key line
    >>time.tzset( )
    >>time.tzname
    ('PST', 'PDT')
    >>time.asctime( )
    'Thu Aug 28 11:21:05 2008'
    >>#and now we're back to my original timezone
    Regards,
    Chris
    ========
    Follow the path of the Iguana...
    Rebertia: http://rebertia.com
    Blog: http://blog.rebertia.com
    >
    A solution should be set the os.environ['TZ'] to 'America/Santiago'
    but I can't make a TZ hardcode because
    the software should works on different timezones.
    >
    So the question, how can restore the system into original timezone, or
    how to know the seconds since epoch
    from UTC datetime without change the local system TIMEZONE.
    >
    please help
    >
    --

    >

    Comment

    • Richard Rossel

      #3
      Re: epoch seconds from a datetime

      On 28 ago, 14:25, "Chris Rebert" <cvrebert+...@g mail.comwrote:
      On Thu, Aug 28, 2008 at 10:18 AM, Richard Rossel <henhis...@gmai l.comwrote:
      Hi friends,
      I need a little help here, I 'm stuck with epoch calculation issue.
      I have this datetime:
      date_new = datetime(*time. strptime('20080 101T000000','%Y %m%dT%H%M%S')
      [0:6])
      This date_new is in UTC
      Now I need to know the seconds since epoch of this new date, so I run
      this:
      seconds = int(time.mktime (date_new.timet uple()))
      but the seconds returned belongs to :
      Tue, 01 Jan 2008 03:00:00 GMT
      because the  localtime is in timezone 'America/Santiago': -3
      >
      I fix this trying to alter the TZ with time.tzset():
       os.environ['TZ'] = 'UTC'
      time.tzset()
      >
      .... and now I can gets the right epoch, but I can't restore the
      previous TimeZone, I try with:
      os.environ['TZ'] = '', but the time.tzset() doesn't back to the
      original ( America/Santiago)
      >
      I think you need to del os.environ['TZ'] rather than setting it to the
      empty string.
      >
      On my box:
      Python 2.5.1 (r251:54863, Feb  4 2008, 21:48:13)
      [GCC 4.0.1 (Apple Inc. build 5465)] on darwin>>import os, time
      >time.asctime ()
      >
      'Thu Aug 28 11:19:57 2008'>>#that's my correct local time
      >time.tzname
      ('PST', 'PDT')
      >#that's my correct timezone
      >os.environ['TZ'] = 'UTC'
      >time.tzset()
      >time.tzname
      ('UTC', 'UTC')
      >time.asctime ()
      >
      'Thu Aug 28 18:20:33 2008'>>#we're clearly in UTC now
      >del os.environ['TZ'] #this is the key line
      >time.tzset()
      >time.tzname
      ('PST', 'PDT')
      >time.asctime ()
      >
      'Thu Aug 28 11:21:05 2008'
      >
      >#and now we're back to my original timezone

      Thanks Chris, and also I found that with reload(time) works too

      --
      Richard Rossel
      Ing. Civil Informatico
      Valparaiso, Chile

      Comment

      Working...