0
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來觸發REST的服務和解析結果..從服務器獲取的時間戳本地時間戳

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幫我出:)

+1

的可能的複製[?如何轉換字符串日期時間在Python時間戳(https://stackoverflow.com/questions/10957522/how-to-convert-字符串日期時間到時間戳在蟒蛇) – Thomas

+0

@ThomasMey這不是一個重複!因爲您共享的帖子將字符串轉換爲同一時間+偏移量的時間戳對象。但我的問題是從一個時區的時間戳轉換到另一個時區。 EDT是2017-08-07 08:42:07-04:00 IST是2017-08-07 08:42:07 + 5:30。可以說本地是IST,時間一定是〜9: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') 
+0

感謝您的評論。他們將在數據庫中使用UTC,但我們使用主機上的其餘服務查詢數據庫。時間戳將更改爲主機的本地時區。因此,服務器最終將返回帶有主機時區的時間戳。因此該值可以是任何時區。僅供參考,MySQL使用與UTC時間戳相同的方式存儲時間戳,在獲取結果時,它將修改計算機時區的時間戳在哪裏查詢數據庫可能是預期的行爲。讓我知道如果我出錯 –

+0

我真的不明白你最近的評論,也不明白我是否真的解決了你的困境:) – droravr

-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 
+0

怎麼比只用'arrow.get(input).to( '本地')'? – droravr

+0

@droravr它不容易,兩者都是類似的,我已經使用tzlocal來處理時區挑戰。我不想使用另一個包(箭頭),因爲我已經使用tzlocal服務於我的目的。 –