如何获得给定时间和状态信息的本地时区 [英] How to get the local timezone given time and state info
问题描述
我正在使用 Python
和 Amazon Redshift
的组合.我收到了来自各个用户的点击量,以及有关这些点击量的一些数据.可以使用以下格式将其可视化:
I am using a combination of Python
and Amazon Redshift
.
I have hits from various users and some data around those hits. This may be visualized in the following format:
UTCTime State IP Country
<time> CA 10.1.1.1 US
<time> AZ 10.1.1.2 US
<time> NY 10.1.1.3 US
我想将此UTC时间转换为当地时间,因为上述匹配来自不同时区. Amazon Redshift
中有一个功能:
I would like to convert this UTC time to the local time, as the above hits are from various time zones. There's a function in Amazon Redshift
:
CONVERT_TIMEZONE ( ['source_timezone',] 'target_timezone', 'timestamp')
但这需要目标时区
.给定此信息,如何找到该目标时区.
But this requires Target timezone
. How do I find this target timezone given this info.
如果还有其他方法可以做到,我也很高兴知道这一点.
If there's any other way to do this, I will be happy to know that too.
任何帮助将不胜感激.
推荐答案
您可以使用我们包.通过点子添加它:
You could approach this problem by using us package. Add it via pip:
$ pip install us
状态查找方法允许按FIPS代码,缩写和名称进行匹配:
The state lookup method allows matching by FIPS code, abbreviation, and name:
>>> us.states.lookup('24')
<State:Maryland>
>>> us.states.lookup('MD')
<State:Maryland>
>>> us.states.lookup('md')
<State:Maryland>
>>> us.states.lookup('maryland')
<State:Maryland>
如我们的包装说明中所述,
State 对象具有 time_zones
和 capital_tz
属性,这些属性的名称是不言而喻的:
as mentioned in us package description, State
object has time_zones
and capital_tz
attributes, which names are self explanitory:
>>> us.states.lookup('MD').capital_tz
'America/New_York'
和
>>> us.states.lookup('MD').time_zones
['America/New_York']
现在,您可以轻松地将状态缩写转换为全时区名称.
Now you can easily translate state abbreviation to full time zone name.
这篇关于如何获得给定时间和状态信息的本地时区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!