【发布时间】:2012-11-19 07:34:48
【问题描述】:
如果我的 Unix 机器设置为 IST 时区,如何获取当前的 GMT 时间?
【问题讨论】:
-
在单行中使用 unix 命令,仅为该命令设置您的时区
TZ=gmt date
标签: linux date unix unix-timestamp
如果我的 Unix 机器设置为 IST 时区,如何获取当前的 GMT 时间?
【问题讨论】:
TZ=gmt date
标签: linux date unix unix-timestamp
您可以使用日期命令的-u 选项:
date -u
-u 以格林威治标准时间(格林威治标准时间)显示(或设置)日期,绕过与本地时间的正常转换。
【讨论】:
像这样使用date shell 命令:
date -u
【讨论】:
如果您是通过 shell 脚本执行此操作,则可以使用 date -u,它会为您提供 UTC。
在 C 语言中,您可以将 time() 与 gmtime() 结合使用,为自己提供所需数据的 struct tm(gmtime() 提供 UTC,与 localtime() 不同)。
【讨论】:
在命令行中,您可以将时区设置为您想查看的时区,并使用 date 命令检查时间,然后再返回原始时区。
#see current timezone
(date +%Z)
#change to a desired ie. London
export TZ=England/London
#or LA would be
export TZ=America/Los_Angeles
#check the time
date
当然,正如建议的那样,要查看世界时,您可以使用之前建议的时间
【讨论】: