在python中将本地时间从UTC更改为UTC + 2

时间:2015-09-03 13:16:39

标签: python python-3.x

如何将此代码从localtime UTC更改为UTC + 2。现在hours()功能打印13但我需要写15。

import time;


def hours():
    localtime =  time.localtime(time.time())
    return localtime.tm_hour


def minutes():
    localtime =  time.localtime(time.time()) 
    return localtime.tm_min

def seconds():
   localtime =  time.localtime(time.time())     
   return localtime.tm_sec


print(hours())
    #minutes()
    #seconds()

2 个答案:

答案 0 :(得分:1)

您可以将pytz与datetime模块一起使用。 对于时区参考,我看here。 我会做一些这样的事情:

import datetime
import pytz
utc_dt = datetime.datetime.now(tz=pytz.utc)
amsterdam_tz = pytz.timezone("Europe/Amsterdam")
local_amsterdam_time = amsterdam_tz.normalize(utc_dt)
print local_amsterdam_time.hour
print local_amsterdam_time.minute
print local_amsterdam_time.second

答案 1 :(得分:1)

如何使用datetime模块:

import datetime;

today = datetime.datetime.now()
todayPlus2Hours = today + datetime.timedelta(hours=2)

print(todayPlus2Hours)

print(todayPlus2Hours.hour)
print(todayPlus2Hours.minute)
print(todayPlus2Hours.second)