- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我有一个应该在 EST 中的时间戳:
2014-10-06T18:06:40-04:56
我理解第一部分:2014-10-06T18:06:40
,但不理解 -04:56
。
-04:56
在这里是什么意思?`
我是这样得到时间戳的:
import datetime
start_time = datetime.datetime(year = 2014,
month = 10,
day = 6,
hour = 18,
tzinfo = pytz.timezone('US/Eastern'))
end_time = start_time + datetime.timedelta(seconds=400)
然后:
end_time.isoformat()
返回:
2014-10-06T18:06:40-04:56
最佳答案
问题是 pytz
:
… differs from the documented Python API for tzinfo implementations; if you want to create local wallclock times you need to use the
localize()
method documented in this document …
再往下,它说:
Unfortunately using the tzinfo argument of the standard datetime constructors "does not work" with pytz for many timezones.
>>> datetime(2002, 10, 27, 12, 0, 0, tzinfo=amsterdam).strftime(fmt)
'2002-10-27 12:00:00 LMT+0020'
因此,您需要按照文档的建议进行操作 — 使用 normalize
、构建 UTC 时间并使用 astimezone
等。您想要哪一个取决于您的具体内容正在尝试做。例如:
>>> from datetime import datetime
>>> from pytz import timezone
>>> utc = timezone('UTC')
>>> eastern = timezone('US/Eastern')
>>> datetime(2014, 10, 6, 18, tzinfo=eastern).isoformat()
'2014-10-06T18:00:00-04:56'
>>> eastern.normalize(datetime(2014, 10, 6, 18, tzinfo=eastern)).isoformat()
'2014-10-06T18:56:00-04:00'
>>> datetime(2014, 10, 6, 18, tzinfo=utc).astimezone(eastern).isoformat()
'2014-10-06T14:00:00-04:00'
>>> eastern.localize(datetime(2014, 10, 6, 18)).isoformat()
'2014-10-06T18:00:00-04:00'
我想这是你想要的最后一个。正如 localize
的文档所说:
Convert naive time to local time.
This method should be used to construct localtimes, rather than passing a tzinfo argument to a datetime constructor.
而且我认为构造本地时间正是您在这里想要的。
如果您想知道为什么……好吧,如果您查看 Olson 数据库中的数据,或者只是打印出 eastern._utcoffset
,您会看到-1 天,+68640 分钟。那是 19.0166+ 小时,而不是 19。为什么?因为每个时区都是用它的起始偏移量定义的,并从那里进行调整。东部基于 1883 年 11 月 18 日 12:03:58 的纽约时区,此时距格林威治标准时间为 -04:56:02。对从 1920 年开始的日期进行了调整,减去了额外的 00:03:58。当然,DST 每年来回调整一小时。所以,截至目前,东部时间是 -04:00,但不知道它应该代表什么日期,它是 -04:56。而且,因为 datetime
只是询问时区的偏移量,而不是它在特定时间的偏移量,所以它就是这样。
最后一件事:EST 是东部标准时间,即 -05:00。这不是 2014 年 10 月 6 日美国任何地点的时区,因为在 2014 年,美国的夏令时为 11 月 2 日。 (印第安纳州曾经有一些县在夏季使用 EST,但现在不再有。)您正在寻找的是 EDT,东部夏令时,即 -04:00。或者,当然,ET,这是夏季的 EDT 和冬季的 EST,这是您通过查找 'US/Eastern'
或 'America/New_York'
.
关于python - 等格式的时区字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26264897/
在我的 Rails (3.0) 应用程序中,我使用助手 time_zone_select() 来使用时区。它生成时区名称,如“(GMT+01:00) Paris”... 但这些名称与 MySQL 中的
我正在解析格式为的日期: SimpleDateFormat formatter = new SimpleDateFormat("EE MMM dd HH:mm:ss Z yyyy"); 当我这样做的时
我在 Android 上使用 ACTION_TIMEZONE_CHANGED Intent 过滤器来响应时区更改。 我注意到 Jodas 当前时区此时没有更新,使用:DateTimeZone.getD
我有一个旧版 Windows 应用程序,它从数据库读取数据。其中一列是“TimeZoneInfoId”。在传统世界中,它是由另一个 Windows 应用程序编写的,因此它存储 Windows 字符串:
我是数据库开发新手,目前从事 MySQL 工作。我有一列包含美国的所有时区。我希望能够获取每行中每个时区的当前时间(只有时间,没有日期)。 时区数据如下:+05:00、-03:00 等等 这就是我尝试
我正在尝试了解 session 时区在 PostgreSQL 中的工作原理。 当我将 session 时区设置为“01:00”时,返回日期为“-01:00”。符号总是颠倒的,我不理解这种行为。 这怎么
我写入我的 mongoDB 数据库,例如开始日期,开始日期始终是一周中的星期一(我使用并且我在欧洲中部时区)。 "startDate" : ISODate("2014-12-28T23:00:00Z"
是否有详尽的 MySQL 时区列表? 似乎 MySQL 设置中 time_zone 的有效值取决于主机操作系统,但我一直无法找到可能值的列表。 我需要时间显示卡尔加里本地时间。 最佳答案 默认情况下,
我已经在 config/app.php 中配置了时区至 Europe/Lisbon . 如果我做 return date_default_timezone_get(); , 返回 Europe/Lis
我在整个脚本中广泛使用 unix 时间,通常我将时区设置如下: date_default_timezone_set('Europe/London'); 这一直工作正常,直到上周末时钟回退一个小时,我怀
我有一个字符串,2013-10-07T23:59:51.205-07:00,想将其转换为 Java 日期对象。我收到解析错误。 date = new SimpleDateFormat("yyyy-MM
如何更改日志文件名中的 log4j 时区? 我的 log4j.xml 文件:
在JAVA中,如何确定所有日期都作为GMT日期返回? 例如,即使我尝试使用GMT语言环境强制使用DateFormat,它也会应用某种逻辑来检索BST日期。 public static void mai
好吧,这是我遇到过的最奇怪的错误之一! 首先:我不是 Python 程序员,该脚本是由 friend 编写的(我认为他的大部分内容来自示例)。 该脚本的用途:将日历信息(CET 时间)从 XML 文件
我们有一个网站,目前在某个时间(由用户选择)运行拍卖,这一切都工作正常,因为当服务器到达该时间时,拍卖就会开始。然后我们必须添加时区,具体取决于用户居住的地方。然后,根据所选的下拉菜单,这将增加或减少
我陷入了这个逻辑。我正在开发一个消息调度项目,我的客户来自不同的国家。让我们考虑一下亚洲(不遵守夏令时)和美国/纽约(遵守夏令时)。 现在,我正在编写以下查询来获取距当前时间 10 分钟间隔内的时间表
我尝试返回特定时区的值,但我遇到了一些奇怪的响应行为: SELECT created_at AT TIME ZONE 'US/Pacific' - created_at, NOW() A
我正在尝试将不带时区值(例如“31.05.2015”)的字符串转换为 NSDate。 我的 DateFormatter 有什么问题?它返回日期 2015-05-31 22:00:00 +0000当我尝
我已经成功地将我的本地 tzinfo 导入到 mysql 中,我几乎已经了解了关于如何存储和使用这些数据的所有细节,除了一个字段。有一个名为 Transition_type_id 的数字字段(在表 t
为什么以下行返回的 TimeZone 显示不正确的时间: TimeZone.getTimeZone("America/Ottawa") 现在显示晚上 10:26 [亚特兰大时间现在是 6:26,Ott
我是一名优秀的程序员,十分优秀!