1
A
回答
0
您可以使用 “pytz” 来完成this.Try:
from string import atoi
from datetime import datetime
import pytz # Available http://sourceforge.net/project/showfiles.php?group_id=79122
thedate = "20080518"
thetime = "2210"
europe_tz = pytz.timezone('Europe/Paris') # Note that your local install timezone should be settings.py
brazil_tz = pytz.timezone('America/Sao_Paulo')
server_tz = pytz.timezone('America/Los_Angeles')
stat_time = datetime(atoi(thedate[0:4]), atoi(thedate[4:6]), atoi(thedate[6:8]), atoi(thetime[0:2]), atoi(thetime[2:4]), 0, tzinfo=europe_tz)
stat_time.astimezone(brazil_tz) # returns time for brazil
stat_time.astimezone(server_tz) # returns server time
来源:http://menendez.com/blog/python-timezone-conversion-example-using-pytz/
1
尝试是这样的:
>>> import datetime
>>> my_time = datetime.datetime.strptime('04:05', '%H:%M')
>>> my_time
datetime.datetime(1900, 1, 1, 4, 5)
>>> offset_str = '-0100'
>>> offset = datetime.timedelta(hours=int(offset_str.lstrip('-')[:2]), minutes=int(offset_str.lstrip('-')[2:])) * (-1 if offset_str.startswith('-') else 1)
>>> offset
datetime.timedelta(-1, 82800)
>>> my_time + offset
datetime.datetime(1900, 1, 1, 3, 5)
>>> (my_time + offset).time()
datetime.time(3, 5)
简而言之:
>>> import datetime
>>> my_time = datetime.datetime.strptime('04:05', '%H:%M')
>>> offset_str = '-0100'
>>> offset = datetime.timedelta(hours=int(offset_str.lstrip('-')[:2]), minutes=int(offset_str.lstrip('-')[2:])) * (-1 if offset_str.startswith('-') else 1)
>>> (my_time + offset).time()
datetime.time(3, 5)
+0
10x!那将会完成这项工作,但任何人都可以想到一种更加pythonic的方式? – 2010-06-13 10:49:36
相关问题
- 1. 如何计算python中两个时区之间的时差?
- 2. Python中的区间计算
- 3. 时区计算
- 4. 时区计算
- 5. 时区计算
- 6. 如何计算Ruby中时区的时差
- 7. 如何计算python中分区字符的出现次数?
- 8. 与时区计算时差
- 9. 如何计算trapizoid区域?
- 10. 如何在mysql中的不同时区中计算总行数
- 11. 如何使用Python计算时间
- 12. 你如何计算皮尔逊的Python在Python中的置信区间?
- 13. 如何处理PHP中的时区差分计算?
- 14. Drupal时区计算器
- 15. 时区偏移计算
- 16. C#时区计算问题
- 17. 如何计算Python中的NTLM哈希?
- 18. 计算python中的代码的计算时间和内存
- 19. 从php的时区计算时间
- 20. 计算值从Python中的圆形区域中心开始计算值
- 21. 如何计算在python
- 22. 如何计算在python
- 23. Python如何计算指数?
- 24. 如何计算timedelta python
- 25. 计算区域中的R
- 26. python中的时钟倒数计算
- 27. 计算python中的小时和分钟
- 28. 如何计算n的分区数?
- 29. 如何计算python中的程序运行时间?
- 30. 我如何计算python中的时间浮点微秒?
它不是为我好,我得到了2 strigns-“04:05”和“-0100”和我whant使用它们来calc下新的时间,但10倍! – 2010-06-13 09:04:49