Linux 如何在 Unix 中获得 GMT 时间?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13449435/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I get GMT time in Unix?
提问by Sam
If my Unix machine is set to IST timezone, how can I get the current GMT time?
如果我的 Unix 机器设置为 IST 时区,我如何获得当前的 GMT 时间?
回答by codaddict
You can use the -u
option of date command:
您可以使用-u
date 命令的选项:
date -u
-u Display (or set) the date in Greenwich Mean Time (GMT-universal time), bypassing the normal conversion to (or from) local time.
-u 以格林威治标准时间(GMT-universal time)显示(或设置)日期,绕过与本地时间的正常转换。
回答by Gilles Quenot
Try doing this with date
shell command :
尝试使用date
shell 命令执行此操作:
date -u
回答by paxdiablo
If you're doing this from a shell script, you can use date -u
, which gives you UTC.
如果您从 shell 脚本执行此操作,则可以使用date -u
,它为您提供 UTC。
From C, you would use time()
in conjunction with gmtime()
to give yourself a struct tm
with the required data (gmtime()
gives UTC, unlike localtime()
).
从 C 中,您可以time()
结合使用gmtime()
给自己一个struct tm
所需的数据(gmtime()
给出 UTC,不像localtime()
)。
回答by julumme
In command line, you can set a timezone to the one you would like to see, and check the time with date command, before returning to the original one.
在命令行中,您可以将时区设置为您想要查看的时区,并在返回原始时区之前使用 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
Also of course, as suggested, to see just universal time, you can use the one suggested before
当然,正如建议的那样,要查看世界时,您可以使用之前建议的