isoformat中的时区域 [英] Time zone field in isoformat

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

问题描述

我的时间戳应该在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 
pre>

解决方案

问题是 pytz


...与tzinfo实现的文档Python API不同;如果要创建本地的wallclock时间,您需要使用本文档中记录的 localize()方法...


进一步说,它说:


不幸的是使用标准datetime构造函数的tzinfo参数不起作用with pytz for many timezones。




 >>> datetime(2002,10,27,12,0,0,tzinfo = amsterdam).strftime(fmt)
'2002-10-27 12:00:00 LMT + 0020'






所以,您需要使用文档建议 - 使用规范化,构建UTC时间并使用 astimezone 等。您想要的是什么取决于您正在尝试做什么。例如:

 >>> from datetime import datetime 
>>>>来自pytz import timezone
>>>> utc = timezone('UTC')
>>>>东部=时区('美国/东部)
>>>> datetime(2014,10,6,18,tzinfo = east).isoformat()
'2014-10-06T18:00:00-04:56'
>>> east.normalize(datetime(2014,10,6,18,tzinfo = east))。isoformat()
'2014-10-06T18:56:00-04:00'
>> > datetime(2014,10,6,18,tzinfo = utc).astimezone(east).isoformat()
'2014-10-06T14:00:00-04:00'
>> > east.localize(datetime(2014,10,6,18))。isoformat()
'2014-10-06T18:00:00-04:00'
/ pre>

我认为这是你想要的最后一个。作为本地化的文档说:


将天真的时间转换为本地时间。 / p>

此方法应用于构建localtimes,而不是将tzinfo参数传递给datetime构造函数,而不是


我认为构建当地时间正是您想要的。






如果您想知道为什么 ...好吧,如果您查看Olson数据库中的数据,或者只打印出 east._utcoffset 你会看到-1天,+68640分钟。那是19.0166+小时,而不是19.为什么?因为每个时区都以起始偏移量定义,从那里进行调整。东方是根据纽约时间1883年11月18日12:03:58的时区,此时是GMT的-04:56:02。从1920年开始的日期调整将减去额外的00:03:58。当然,对于DST来说,每年都会有一个小时的调整。所以,截至目前,东方是-04:00,但不知道应该代表什么日期,是-04:56。而且,因为 datetime 只是要求一个时区作为其偏移量,而不是在特定时间的偏移量,那就是它获得的。






最后一件事:EST是东部标准时间,这是-05:00。这不是2014年10月6日美国任何地点的时区,因为在2014年,美国的夏令时到11月2日。 (以前在印第安那州曾经是美国东部时间在夏天的县,但不再是。)你要找的是EDT,东方夏令时,这是-04:00。或者,当然,ET是夏天的EDT,冬天的EST,这是你从查找'US / Eastern''America / New_York'


I have a timestamp that is supposed to be in EST:

2014-10-06T18:06:40-04:56

I understand this first part: 2014-10-06T18:06:40, but not -04:56.

What does -04:56 mean here?`

Here is how I got that timestamp:

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)

And then:

end_time.isoformat()

returns:

2014-10-06T18:06:40-04:56

解决方案

The problem is that 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 …

Further down, it says:

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'


So, you need to do what the docs suggest—using normalize, constructing UTC times and using astimezone, etc. Which one you want depends on exactly what you're trying to do. For example:

>>> 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'

I think it's the last you want. As the docs for localize say:

Convert naive time to local time.

This method should be used to construct localtimes, rather than passing a tzinfo argument to a datetime constructor.

And I think constructing a local time is exactly what you wanted here.


If you're wondering why… well, if you look at the data that's in your Olson database, or just print out eastern._utcoffset, you'll see -1 day, +68640 minutes. That's 19.0166+ hours, not 19. Why? Because every timezone is defined with its starting offset, with adjustments from there. Eastern is based on New York's timezone as of 1883 Nov 18 12:03:58, at which point it was -04:56:02 from GMT. There's an adjustment for dates starting in 1920 that subtracts the extra 00:03:58. And of course the yearly adjustments back and forth one hour for DST. So, as of right now, Eastern is -04:00, but without any idea of what date it's supposed to represent, it's -04:56. And, because datetime just asks a timezone for its offset, rather than its offset at a particular time, that's what it gets.


One last thing: EST is Eastern Standard Time, which is -05:00. That's not the time zone of any location in the US on 6 October 2014, because in 2014, the US's daylight saving time goes to 2 November. (There used to be counties in Indiana that were on EST during the summer, but there no longer are.) What you're looking for is EDT, Eastern Daylight Time, which is -04:00. Or, of course, ET, which is EDT during the summer and EST during the winter, which is what you get from looking up 'US/Eastern' or 'America/New_York'.

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

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