Problem with date in Python
I'm developing a web application and would like to display user's current date basing on his timezone. Here is my code:
userTimezone = -5 #EAST is positive, WEST negative
utcTimestamp = time.mktime(time.gmtime())
userDate = time.gmtime(utcTimestamp+userTimezone*60*60)
I think the prob开发者_如何学JAVAlem is with gmtime() since it does some conversions automatically. If I could, I would replace gmtime with function which doesn't convert anything, but haven't found any.
you are probably looking for time.localtime(seconds)
. gmtime always returns utc time.
Set time.timezone
to the user's timezone, then display it using localtime().
精彩评论