below is the Scenario:
1. (Host-machine)User will create tasks on the server by sending task information to the rest services of server (task_name,task_owner etc)
2. (server-machine)server will receive information from the user and creates a timestamp(2017-08-07 08:42:07-04:00) along with timezone for the task and saves into a database
3. user will do list tasks and user must be able to see task_name,task_owner and task_creation time
Assuming Host-machine and server-machine are on different timezones.
when user does list tasks we trigger the rest-services and processes the tasks timestamps and show local timestamps equivalent to server timezone.
我的主机代码使用python来触发休息服务并解析结果..
so,
input: 2017-08-07 08:42:07-04:00
output : 2017-08-07 17:30:03
explanation: its converting from time stamp of one time zone to
another timezone.EDT is 2017-08-07 08:42:07-04:00
IST is 2017-08-07 08:42:07+5:30 .
so, assume Local is IST the time must be ~17:30
(8:42:07+9:30)
新蜜蜂到python帮帮我:)
答案 0 :(得分:0)
我认为Arrow会帮助您简化您尝试做的事情。服务器上的所有日期和时间都应该是UTC,并为每个用户保存正确的时区。使用arrow
,您只需使用:
import arrow
time = arrow.get(input_time, 'YYYY-MM-DD HH:mm:ssZZ')
print time.to('local')
答案 1 :(得分:-1)
fixed this using the package called **tzlocal**
def convert_date_format(self, server_time):
local_date =parse(server_time).astimezone(get_localzone())
return str(local_date.replace(microsecond=0, tzinfo=None))
**explanation**
input: "2017-08-08 15:35:27-4:00"
1.function takes server_time as input converts to datetime object using
parse()
2.get_localzone() will fetch the local time zone
3.using the astimezone() converted the server_time to local timestamp
4.removed the microseconds and timezone fields