python - 远程主机时区与本地时区
我正在构建一个用于远程日志监控的 Web 前端,
必须管理大约 10 个不同的地理位置,我遇到过你们中的一些人已经拥有的三头地狱犬。
有没有办法从远程 HPUX shell 变量获取以下远程信息:
- zoneinfo(国家/城市)
- UTC + 偏移量(我可以轻松地从 zoneinfo 获取此信息)
到目前为止,我能得到的最好的是操作系统缩写时区 (这是否足以使用静态构建的 pytz.common_timezones 集合迭代地跨越远程时间并将缩写区域反向转换为国家/城市,否则我完全走错了路?)
在获取国家/地区后我可以轻松获得偏移量城市(我没有)
datetime.now(pytz.timezone('Asia/Dili')).strftime('%Z %z')
“TLT +0900”
- 获取远程缩写时区,
(Linux 具有更理智的
grep "ZONE=" /etc/sysconfig/clock
输出,例如,
ZONE="欧洲/伦敦"
而 HP-UX /etc/TIMEZONE 使用缩写时区,例如
TZ=CAT-2
我会使用 echo $TZ ,它会输出一些更有用的数据,例如 CAT-2,但一些远程 HP-UX 甚至没有配置此配置,因此迫使我依赖不明确的 RFC822 日期
date +%z
CAT
I已经研究了 pytz、datetime.datetime、email.Utils,但考虑到它无法直接从缩写时间转换为区域信息国家/城市(pytz 允许相反的情况)
我应该从自动发现远程时区的堂吉诃德任务开始吗? 只需在接受注册远程主机的用户输入时添加国家/城市下拉列表?
编辑(部分解决方案)
基于@Mike Pennington 答案
from datetime import datetime as dt
from datetime import timedelta as td
from dateutil.relativedelta import *
from email.Utils import mktime_tz, parsedate_tz
hpux_remote_date = 'Thu Apr 28 18:09:20 TLT 2011'
utctimestamp = mktime_tz(parsedate_tz( hpux_remote_date ))
hpux_dt = dt.fromtimestamp( utctimestamp )
delta_offset = relativedelta(dt.utcnow(), hpux_dt)
hpux_utc = hpux_dt + delta_offset
# Sanity checking to ensure we are correct...
hpux_dt
datetime.datetime(2011, 4, 28, 18, 9, 20)
hpux_utc
datetime.datetime(2011, 4, 28, 9, 9, 22, 229148)
I'm building a web frontend for remote logs monitoring,
having to manage about 10 different geographic locations I've bumped into the 3 headed hellhound some of you already have.
Is there any way to get from a remote HPUX shell variable the following remote info:
- zoneinfo (Country/City)
- UTC + offset (I can easily get this from the zoneinfo)
So far the best I could get is the OS abbreviated timezone
(is this enough to iteratively cross the remote time with a statically built pytz.common_timezones collection and reverse convert the abbreviated zones into Country/City or I'm completely going the wrong way?)
I can easily get the offset after getting the Country/City (which I haven't)
datetime.now(pytz.timezone('Asia/Dili')).strftime('%Z %z')
'TLT +0900'
- get remote abbreviated timezone,
(Linux has the far more sane
grep "ZONE=" /etc/sysconfig/clock
output like,
ZONE="Europe/London"
while HP-UX /etc/TIMEZONE uses abbreviated timezones like
TZ=CAT-2
I'd use echo $TZ which would output a little more useful data like CAT-2 but some remote HP-UXes don't even have this configured thus forcing me to rely on the ambiguous RFC822 date,
date +%z
CAT
I've looked both into pytz, datetime.datetime, email.Utils but considering it's a no can do directly converting from abbreviated time into the zoneinfo Country/City (pytz allows the opposite)
should I just scratch this Don Quixote quest of autodiscovering the remote timezone and
just add a Country/City dropdown list when accepting the user input registering the remote host?
EDIT (Partial solution)
building on @Mike Pennington answer
from datetime import datetime as dt
from datetime import timedelta as td
from dateutil.relativedelta import *
from email.Utils import mktime_tz, parsedate_tz
hpux_remote_date = 'Thu Apr 28 18:09:20 TLT 2011'
utctimestamp = mktime_tz(parsedate_tz( hpux_remote_date ))
hpux_dt = dt.fromtimestamp( utctimestamp )
delta_offset = relativedelta(dt.utcnow(), hpux_dt)
hpux_utc = hpux_dt + delta_offset
# Sanity checking to ensure we are correct...
hpux_dt
datetime.datetime(2011, 4, 28, 18, 9, 20)
hpux_utc
datetime.datetime(2011, 4, 28, 9, 9, 22, 229148)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该能够像这样找到 GMT 偏移量...
作为 GMT 偏移量,忽略 DST
(time.localtime()[3] - time.localtime()[8]) - time.gmtime()[ 3]
我现在处于中部时间 (GMT - 6),因此,这在我的系统上会产生
-6
。作为 GMT 偏移量,包括 DST 补偿
(time.localtime()[3]) - time.gmtime()[3]
这在我的系统上产生
-5
。使用第二个选项可能是最简单的,并使用它将本地 HPUX 时间转换为 GMT;然后根据需要使用 pytz 进行 mangle 操作。
编辑
如果您正在使用远程(非 GMT)时间戳的文本表示,那么直接使用日期时间对象可能更容易...我没有方便的 HPUX,但我假设日期字符串类似于我的 Debian 挤压系统。
You should be able to find your GMT offset like this...
As a GMT offset, disregarding DST
(time.localtime()[3] - time.localtime()[8]) - time.gmtime()[3]
I am in Central time (GMT - 6) so, this yields
-6
on my system.As a GMT offset, including DST compensation
(time.localtime()[3]) - time.gmtime()[3]
This yields
-5
on my system.It's probably easiest to go with the second option and use it to convert those local HPUX times into GMT; then mangle with
pytz
as-required.EDIT
If you are working with a text representation of remote (non-GMT) timestamps, it is probably easier to work directly with datetime objects... I don't have HPUX handy, but I'll assume the date string is similar to my debian squeeze system.