【问题标题】:linux convert time(for different timezones) to UTClinux将时间(不同时区)转换为UTC
【发布时间】:2012-11-22 08:28:07
【问题描述】:

在 linux 中有没有办法以编程方式获取给定时间字符串的 UTC 时间,例如

Tue Dec  14 10:30:23 PST 2012
Tue Jan  4 11:30:23 EST 2013

到 UTC 时间,无论(并且不更改)本地时区设置?

【问题讨论】:

  • 我个人想知道正常时间和UTC时间有什么区别?
  • 是您当地时区的时间吗?通常,PST、EST 等缩写不足以明确地找到时区。

标签: c linux gcc timezone


【解决方案1】:

date -u -d "Tue Dec 14 10:30:23 PST 2012" 报告Fri Dec 14 18:30:23 UTC 2012。差异是因为 2012 年 12 月 14 日实际上是星期五,而不是星期二。它可能在有效输入下效果更好......

【讨论】:

【解决方案2】:

更新:与最近 tz 数据库的结果不同:EST yields the same utc offset for a given date(与 the previous result 比较)。虽然它不影响不同时区可能使用相同缩写的一般结论,因此相同的缩写可能对应不同的 UTC 偏移量。见Parsing date/time string with timezone abbreviated name in Python?


缩写的时区名称(例如 EST)可能不明确。

示例

#!/bin/sh
for tz in Australia/Brisbane Australia/Sydney America/New_York
do date -u -d"TZ=\":$tz\" Tue Jan  4 11:30:23 EST 2013"
done

Output

Fri Jan  4 16:30:23 UTC 2013
Fri Jan  4 00:30:23 UTC 2013
Fri Jan  4 16:30:23 UTC 2013

两件事:

  • 根据使用的时区,日期字符串可能会被解释为不同的时刻

  • date 默默地忽略应该是UTC+10Australia/Brisbane 时区,即dateEST 解释为属于不同的时区。如果没有EST,它会产生正确的时间:

      $ date -u -d 'TZ=":Australia/Brisbane" Tue Jan  4 11:30:23 2013'
      Fri Jan  4 01:30:23 UTC 2013
    

查找给定时间和时区缩写的所有可能的 UTC 时间,例如 'Tue Jan 4 11:30:23 EST 2013'

#!/usr/bin/env python
from collections import defaultdict
from datetime import datetime
import pytz # $ sudo apt-get install python-tz
            # or if you can't install system-wide
            # $ pip install --user pytz

## Tue Dec  14 10:30:23 PST 2012
#naive_dt, tzname = datetime(2012, 12, 14, 10, 30, 23), 'PST'
## -> Fri Dec 14 18:30:23 2012 UTC

# Tue Jan  4 11:30:23 EST 2013
naive_dt, tzname = datetime(2013, 1, 4, 11, 30, 23), 'EST'
# Fri Jan  4 01:30:23 2013 UTC
# Fri Jan  4 00:30:23 2013 UTC
# Fri Jan  4 16:30:23 2013 UTC
# ambiguous
 
utc_times = defaultdict(list)
for zone in pytz.all_timezones:
    dt = pytz.timezone(zone).localize(naive_dt, is_dst=None)
    if dt.tzname() == tzname: # same timezone abbreviation
        utc_times[dt.astimezone(pytz.utc)].append(zone)

for utc_dt, timezones in utc_times.items():
    print("%s:\n\t%s" % (utc_dt.strftime('%c %Z'), '\n\t'.join(timezones)))

输出

所有Tue Jan 4 11:30:23 EST 2013 解释为具有相应时区名称的UTC:

Fri Jan  4 01:30:23 2013 UTC:
    Australia/Brisbane
    Australia/Lindeman
    Australia/Queensland
Fri Jan  4 00:30:23 2013 UTC:
    Australia/ACT
    Australia/Canberra
    Australia/Currie
    Australia/Hobart
    Australia/Melbourne
    Australia/NSW
    Australia/Sydney
    Australia/Tasmania
    Australia/Victoria
Fri Jan  4 16:30:23 2013 UTC:
    America/Atikokan
    America/Cayman
    America/Coral_Harbour
    America/Detroit
    ...
    America/New_York
    ...
    America/Toronto
    Canada/Eastern
    EST
    EST5EDT
    Jamaica
    US/East-Indiana
    US/Eastern
    US/Michigan

【讨论】:

    猜你喜欢
    • 2016-07-06
    • 2013-06-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多