Generate the following pseudo-timestamp as workaround on limitation of flot graphing library
currently, I have the following current timestamp value example in seconds.
开发者_运维百科1299196800
. It means
4 March 2011 0:0:0 in Greenwich (GMT)
4 March 2011 8:0:0 in Malaysia (GMT +8)
Currently, if my Python script pass in 1299196800
to a flot graphing javascript library, it will always show Greenwich's time 4 March 2011 0:0:0
instead of Malaysia's time 4 March 2011 8:0:0
, although the client machine is using Malaysia timezone.
My python server will always having same time zone as client machine. We want 4 March 2011 8:0:0
to be shown in client side.
Hence, for workaround purpose on the limitation of flot graphing library, instead of passing 1299196800
, I would like to pass in
# 8 is timezone offset.
# 1299196800 is current timestamp.
pseudo_timestamp = 1299196800 + 8 * 60 * 60
May I know how can I generate the above "pseudo timestamp" in python? Is manipulate with a constant timezone a good idea? Will it encounter any problem when comes to day light saving?
The UTC time in flot is by design: look for timestamp here, and fixing the time offset is the recommended solution.
Consider that the flot timestamp are expressed in milliseconds, so you'll have to multiply your example offset by 1000.
It is not a good idea, though, to use a constant timestamp: in general you'd try to discover the client offset, but since you are sure that it's the same of the server, you can use the server data to do that.
Use time.daytime
to discover if a daylight saving is active, and then use time.timezome
or time.altzone
accordingly to get the offset in hours.
Python doc on time is here
Given that, you can adjust your timestamps converting the offset in milliseconds and adding it to you timestamps (which you already did :) )
You use datatime object with tzinfo which is time-zone aware. Here is a short-snippet of how you actually do it.
import datetime
from datetime import tzinfo, timedelta
class GMT8(tzinfo):
def utcoffset(self, dt):
return timedelta(hours=8)
def tzname(self, dt):
return "GMT +8"
def dst(self,dt):
return timedelta(0)
malaysiatime = GMT8()
o = datetime.datetime.fromtimestamp(1299196800,malaysiatime)
print o
# prints 2011-03-04 08:00:00+08:00
精彩评论