在 Python 中设置时区 [英] Setting timezone in Python

查看:38
本文介绍了在 Python 中设置时区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Python 是否可以像这样在 PHP 中设置时区:

date_default_timezone_set("欧洲/伦敦");$Year = date('y');$Month = date('m');$Day = date('d');$Hour = date('H');$Minute = date('i');

我无法真正安装任何其他模块等,因为我使用的是共享虚拟主机.

有什么想法吗?

解决方案

>>>导入操作系统,时间>>>time.strftime('%X %x %Z')'12:45:20 08/19/09 CDT'>>>os.environ['TZ'] = '欧洲/伦敦'>>>时间.tzset()>>>time.strftime('%X %x %Z')'18:45:39 08/19/09 BST'

要获取您列出的特定值:

<预><代码>>>>年份 = time.strftime('%Y')>>>月 = time.strftime('%m')>>>day = time.strftime('%d')>>>小时 = time.strftime('%H')>>>分钟 = time.strftime('%M')

请参阅此处以获取完整的指令列表.请记住,strftime() 函数将始终返回字符串,而不是整数或其他类型.

Is it possible with Python to set the timezone just like this in PHP:

date_default_timezone_set("Europe/London");
$Year = date('y');
$Month = date('m');
$Day = date('d');
$Hour = date('H');
$Minute = date('i');

I can't really install any other modules etc as I'm using shared web hosting.

Any ideas?

解决方案

>>> import os, time
>>> time.strftime('%X %x %Z')
'12:45:20 08/19/09 CDT'
>>> os.environ['TZ'] = 'Europe/London'
>>> time.tzset()
>>> time.strftime('%X %x %Z')
'18:45:39 08/19/09 BST'

To get the specific values you've listed:

>>> year = time.strftime('%Y')
>>> month = time.strftime('%m')
>>> day = time.strftime('%d')
>>> hour = time.strftime('%H')
>>> minute = time.strftime('%M')

See here for a complete list of directives. Keep in mind that the strftime() function will always return a string, not an integer or other type.

这篇关于在 Python 中设置时区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆