Linux clock_gettime 是 UTC 时间还是本地时区?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8531845/
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
Is clock_gettime a UTC time, or local time zone?
提问by edA-qa mort-ora-y
Does the function clock_gettime
return a timestamp measured from the epoch in UTC or in the local time zone?
该函数是否clock_gettime
返回从 UTC 或本地时区的纪元开始测量的时间戳?
I know that time
is supposed to be from the UTC epoch, but I can't find any reference saying the same is true of clock_gettime
.
我知道这time
应该来自 UTC 时代,但我找不到任何参考资料说clock_gettime
.
采纳答案by NPE
回答by Basile Starynkevitch
Actually, the question does not make a lot of sense. See time(7)for an overview of time-related functions.
其实,这个问题没有多大意义。有关时间相关函数的概述,请参见time(7)。
Any clock device returns a time measured from some origin event in the past. Unix (and Posix) convention is to measure it from the Epoch(start of 1970, as aix's answerexplains).
任何时钟设备都会返回从过去某个原始事件测量的时间。Unix(和 Posix)约定是从Epoch(从 1970 年开始,正如aix 的回答所解释的那样)来衡量它。
Displayingsome time in UTC, or local time, or using the French revolutionary calendar, or using the Maya calendar, or any other calendar from whatever culture you are interested in, does not change that time. Only the display (or shown form) of that time changes.
以 UTC 或本地时间显示某个时间,或使用法国革命日历,或使用玛雅日历,或您感兴趣的任何文化的任何其他日历,都不会更改该时间。只有那个时间的显示(或显示形式)改变。
It is a bit like saying that two, deux, 1+1, or 2, or 10b -with bmeaning binary- are all representations of the samenumber.
这有点像说2、deux、1+1或2或 10b - b表示二进制 - 都是相同数字的表示。
Back to the question, the man page of clock_gettimegives the precise answer to the question. It depends upon the clk_id
you are asking for, and for CLOCK_REALTIME
, the time is measured since the Unix Epoch. For other clocks (e.g. CLOCK_MONOTONIC
), the used origin is not specified.
回到问题,clock_gettime的手册页给出了这个问题的准确答案。这取决于clk_id
您所要求CLOCK_REALTIME
的时间,以及自 Unix 时代以来的时间。对于其他时钟(例如CLOCK_MONOTONIC
),未指定使用的原点。
(I'm quite sadly surprised by the number of questions here which could be answered very quickly by looking into the man. I don't understand the logic of people taking more time to ask the question here that to look -just by typing man clock_gettime
on their Linux box- into the man pages).
(我很遗憾的是由可通过查看很快得到回答的问题在这里的数量感到惊讶的人。我不明白的人的逻辑花更多的时间在这里提出的问题,要看看-只是通过打字 man clock_gettime
的他们的 Linux 盒子 - 进入手册页)。
The notion of time zone is only relevant for struct tm
as returned by localtime& gmtime
(and related) functions. A time (e.g. some time_t
) measured from the Epoch (like the result of time(2), gettimeofday, clock_gettime
with CLOCK_REALTIME
) has no time zone.
时区的概念仅与localtime& (和相关)函数struct tm
返回的内容相关。从 Epoch 测量的时间(例如 some )(如time(2)、gettimeofday、with 的结果)没有时区。gmtime
time_t
clock_gettime
CLOCK_REALTIME
The Unix Epoch is january 01, 1970 0:00 UTC (by definition of (time_t)0
), In my time zone (MET= Paris/France) the same Epoch is Thu Jan 1 01:00:00 MET 1970.
Unix 纪元是 1970 年 1 月 1 日 0:00 UTC(根据定义(time_t)0
),在我的时区(MET= 巴黎/法国),相同的纪元是 1970 年 1 月 1 日星期四 01:00:00 MET。
回答by mykhal
it depends..
这取决于..
if you call clock_gettime(1)
, it is likely, the the epoch is the time when your linux has started.
如果你打电话clock_gettime(1)
,很可能,epoch 是你的 linux 开始的时间。