Python - 设置特定时区的日期时间(没有 UTC 转换)
需要明确的是,这是 python 2.6,我使用的是 pytz。
这是一个仅处理美国时区的应用程序,我需要能够锚定一个日期(今天),并仅获取太平洋标准时间晚上 8 点和晚上 11 点的 unix 时间戳(纪元时间)。
这让我发疯。
> pacific = pytz.timezone("US/Pacific")
> datetime(2011,2,11,20,0,0,0,pacific)
datetime.datetime(2011, 2, 11, 20, 0, tzinfo=<DstTzInfo 'US/Pacific' PST-1 day, 16:00:0 STD>)
> datetime(2011,2,11,20,0,0,0,pacific).strftime("%s")
'1297454400'
zsh> date -d '@1297454400'
Fri Feb 11 12:00:00 PST 2011
因此,即使我正在设置一个时区,并使用该时区创建日期时间,它仍然会将其创建为 UTC,然后将其转换。这更是一个问题,因为当我尝试进行计算时,UTC 将提前一天。
有没有一种简单(或至少合理)的方法来生成太平洋标准时间今天晚上 8 点的时间戳?
(需要明确的是,我确实了解在大多数情况下使用 UTC 的价值,例如数据库时间戳或一般存储。这不是其中一种情况,我特别需要 PST 晚上的时间戳,并且 UTC 不必进入其中。)
Just to be clear, this is python 2.6, I am using pytz.
This is for an application that only deals with US timezones, I need to be able to anchor a date (today), and get a unix timestamp (epoch time) for 8pm and 11pm in PST only.
This is driving me crazy.
> pacific = pytz.timezone("US/Pacific")
> datetime(2011,2,11,20,0,0,0,pacific)
datetime.datetime(2011, 2, 11, 20, 0, tzinfo=<DstTzInfo 'US/Pacific' PST-1 day, 16:00:0 STD>)
> datetime(2011,2,11,20,0,0,0,pacific).strftime("%s")
'1297454400'
zsh> date -d '@1297454400'
Fri Feb 11 12:00:00 PST 2011
So, even though I am setting up a timezone, and creating the datetime with that time zone, it is still creating it as UTC and then converting it. This is more of a problem since UTC will be a day ahead when I am trying to do the calculations.
Is there an easy (or at least sensical) way to generate a timestamp for 8pm PST today?
(to be clear, I do understand the value of using UTC in most situations, like database timestamps, or for general storage. This is not one of those situations, I specifically need a timestamp for evening in PST, and UTC should not have to enter into it.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
至少存在两个问题:
"US/Pacific"
)作为 tzinfo 参数。您应该使用pytz.timezone("US/Pacific").localize()
方法 相反.strftime('%s')
不可移植,它忽略 tzinfo,并且它始终使用本地时区。请改用datetime.timestamp()
或旧版 Python 上的类似方法。要在给定时区中创建时区感知日期时间:
要获取 POSIX 时间戳:(
在 Python 2.6 上,请参阅
totimestamp()< /code> 函数如何模拟
.total_seconds()
方法)。
There are at least two issues:
"US/Pacific"
as tzinfo parameter directly. You should usepytz.timezone("US/Pacific").localize()
method instead.strftime('%s')
is not portable, it ignores tzinfo, and it always uses the local timezone. Usedatetime.timestamp()
or its analogs on older Python versions instead.To make a timezone-aware datetime in the given timezone:
To get POSIX timestamp:
(On Python 2.6, see
totimestamp()
function on how to emulate.total_seconds()
method).为 UTC 时区创建一个 tzinfo 对象
utc
,然后尝试以下操作:编辑: 正如评论中指出的,将时区放入
datetime
> 构造函数并不总是健壮的。 首选方法是:使用 pytz 文档的 评论说
strftime("%s")
不可靠,它忽略时区信息(甚至 UTC)并假定其运行的系统的时区。它依赖于底层 C 库实现,并且在某些系统(例如 Windows)上根本不起作用。Create a tzinfo object
utc
for the UTC time zone, then try this:Edit: As pointed out in the comments, putting the timezone into the
datetime
constructor isn't always robust. The preferred method using the pytz documentation would be:Also note from the comments that
strftime("%s")
isn't reliable, it ignores the time zone information (even UTC) and assumes the time zone of the system it's running on. It relies on an underlying C library implementation and doesn't work at all on some systems (e.g. Windows).