Python: formatting date, time

I need to create a local timestamp as YYYYMMDDHHmmSSOHH'mm '. This OHH'mm 'is one of +, -, Z, and then there is an hour and minutes followed by'.

How can I get such a timestamp, indicating both the local time zone and possible summer time?

+18
python datetime
Mar 21 '10 at 12:52
source share
2 answers
import time localtime = time.localtime() timeString = time.strftime("%Y%m%d%H%M%S", localtime) # is DST in effect? timezone = -(time.altzone if localtime.tm_isdst else time.timezone) timeString += "Z" if timezone == 0 else "+" if timezone > 0 else "-" timeString += time.strftime("%H'%M'", time.gmtime(abs(timezone))) 
+27
Mar 21 '10 at 13:12
source share

time.strftime will do for this

And on linux %z will just give you the -HHMM format if the environment variable is set correctly.

 >>> os.environ['TZ'] = 'EST' >>> time.strftime('%x %X %z') '03/21/10 08:16:33 -0500' 
+8
Mar 21 '10 at 12:55
source share



All Articles