472,347 Members | 2,321 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,347 software developers and data experts.

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('20080101T000000','%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.timetuple()))
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

Aug 28 '08 #1
2 3442
On Thu, Aug 28, 2008 at 10:18 AM, Richard Rossel <he*******@gmail.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('20080101T000000','%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.timetuple()))
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

--
http://mail.python.org/mailman/listinfo/python-list
Aug 28 '08 #2
On 28 ago, 14:25, "Chris Rebert" <cvrebert+...@gmail.comwrote:
On Thu, Aug 28, 2008 at 10:18 AM, Richard Rossel <henhis...@gmail.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('20080101T000000','%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.timetuple()))
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
Aug 28 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: John Hunter | last post by:
I have a python2.3 datetime instance and a tzinfo instance (eg Eastern from the python library reference). What is the best way to convert that...
2
by: Sunil | last post by:
Can someone suggest me how do i write a code in VB for epoch time. I want if the value is entered it changes into time and if time is enetred i get...
3
by: eight02645999 | last post by:
hi how can i get the number of days since epoch using the time module? or do i have to manually do the arithmetic? thanks
5
by: Summu82 | last post by:
HI I have to convert a time in the format i have year month day hour min and seconds I need to convert this into time in seconds since 1...
5
by: Extremest | last post by:
I have a bunch of dates in a couple different formats. I would like to convert them to epoch. The dates are like this Mon, 12 Jun 2006 09:18:22...
3
by: Mark | last post by:
I want to simply get the unix style epoch time (number of secs to now from Jan 01, 1970 UTC) for use as a timestamp to track the staleness of some...
5
by: Grey Alien | last post by:
I need to convert timestamps that are given as the number of seconds that have elapsed since midnight UTC of January 1, 1970, (not counting leap...
11
by: usenet | last post by:
Is the epoch time the number of seconds elapsed since January 1st 1970 in my timezone or as per UTC? I mean, if A's program makes a call to time()...
6
by: pavanponnapalli | last post by:
hi, i am using Date::Time and using localtime function to convert a given timestamp to epoch seconds. Everything is fine. The problem comes...
0
by: concettolabs | last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
0
by: teenabhardwaj | last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.