Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/308.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用python中的Pytz根据给定的UTC偏移量转换数据和时间&;德扬戈?_Python_Django_Utc_Pytz_Timezone Offset - Fatal编程技术网

如何使用python中的Pytz根据给定的UTC偏移量转换数据和时间&;德扬戈?

如何使用python中的Pytz根据给定的UTC偏移量转换数据和时间&;德扬戈?,python,django,utc,pytz,timezone-offset,Python,Django,Utc,Pytz,Timezone Offset,我想使用Pytz根据给定的UTC偏移量转换时间戳对象(从模型中获取)。在打印时间戳对象时将其呈现到模板之前,它使用以下格式: 2012-05-29 10:03:37 我通过以下行获得偏移: offset = datetime.datetime.now(pytz.timezone('Asia/Kolkata')).strftime('%z') 上述偏移量将给出+0530 .现在我想用这个偏移量对timestamp对象进行更改。例如: timestamp: `2012-05-29 10:03

我想使用Pytz根据给定的UTC偏移量转换时间戳对象(从模型中获取)。在打印时间戳对象时将其呈现到模板之前,它使用以下格式:

 2012-05-29 10:03:37
我通过以下行获得偏移:

 offset = datetime.datetime.now(pytz.timezone('Asia/Kolkata')).strftime('%z')
上述偏移量将给出
+0530
.现在我想用这个偏移量对timestamp对象进行更改。例如:

timestamp: `2012-05-29 10:03:37`
offset:   `+0530`
after change,
timestamp: `2012-05-29 15:33:37`
那么这个呢:

timestamp: `2012-05-29 23:03:37`
offset:   `+0530`
after change,
timestamp: `2012-05-30 4:33:37`
看看日期,它也随着偏移量而改变。操纵时间后,它甚至可以是12小时格式


任何帮助都将是可观的

我希望这就是您所寻求的:

d = datetime.now(pytz.timezone('UTC'))   # get date in UTC format - you'll be getting it from the database
local = d.astimezone('Asia/Kolkata')
print local.strftime("%Y-%m-%d %H:%M:%S %z")

我希望这就是你想要的:

d = datetime.now(pytz.timezone('UTC'))   # get date in UTC format - you'll be getting it from the database
local = d.astimezone('Asia/Kolkata')
print local.strftime("%Y-%m-%d %H:%M:%S %z")