gmtime()的正确行为是什么? [英] What is the proper behavior of gmtime()?

查看:82
本文介绍了gmtime()的正确行为是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一份C ++标准。不幸的是,我没有C89标准的副本

,我的问题与某些内容有关。

通过引用合并。


具体来说,当参数为负时(即在纪元之前),标准对gmtime()

的行为有什么看法?


是UB吗? gmtime()应该返回NULL / 0吗?它应该返回一个指向

a time_t的指针,并带有有效的pre-epoch值吗?

I have a copy of the C++ standard. Unfortunately, I don''t have a copy
of the C89 standard, and the question I have is related to something
incorporated by reference.

Specifically, what does the Standard say about the behavior of gmtime()
when the argument is negative (i.e. before the epoch)?

Is it UB? Should gmtime() return NULL/0? Should it return a pointer to
a time_t with valid pre-epoch values?

推荐答案

red floyd写道:
red floyd wrote:
我有一份C ++标准。不幸的是,我没有C89标准的副本,而我的问题与通过引用合并的内容有关。


或许在comp.lang.c中提问会更有意义,不是吗?

具体来说,标准对gmtime的行为有什么看法()
当论证为负时(即在纪元之前)?


''gmtime''的参数不算算。它是time_t *。没有

epoch在C90(C89)。你似乎处于特定于实现的

领域。


有一件事是肯定的,如果''时间''功能不能起作用,因为缺席

(或不可用)的系统日历,它返回(time_t)( - 1)。你可以从''mktime''获得
''time_t'',但是,如果时间不能代表
,你会得到(time_t) (-1)。不要把这个值传递给''gmtime''。

是UB吗? gmtime()应该返回NULL / 0吗?它应该返回一个指向
具有有效的pre-epoch值的time_t吗?
I have a copy of the C++ standard. Unfortunately, I don''t have a copy
of the C89 standard, and the question I have is related to something
incorporated by reference.
Perhaps asking in comp.lang.c would make more sense in that case, no?
Specifically, what does the Standard say about the behavior of gmtime()
when the argument is negative (i.e. before the epoch)?
The argument of ''gmtime'' is not arithmetic. It''s "time_t*". There is no
"epoch" in C90 (C89). You seem to be in the implementation-specific
territory.

One thing is certain that if ''time'' function can''t function due to absence
(or unavailability) of a system calendar, it returns (time_t)(-1). You
can get ''time_t'' from ''mktime'', but, again, if the time cannot be
represented, you''d get (time_t)(-1). Don''t pass that value to ''gmtime''.
Is it UB? Should gmtime() return NULL/0? Should it return a pointer to
a time_t with valid pre-epoch values?



''gmtime''如果无法转换你传递的值,则返回NULL对它来说。


V



''gmtime'' returns NULL if it cannot convert the value you pass to it.

V


Victor Bazarov写道:
Victor Bazarov wrote:
red floyd写道:
red floyd wrote:
我有一份C ++标准。不幸的是,我没有C89标准的副本,而我的问题与通过引用合并的内容有关。
I have a copy of the C++ standard. Unfortunately, I don''t have a copy
of the C89 standard, and the question I have is related to something
incorporated by reference.



也许是在问在comp.lang.c中会更有意义吗,不是吗?



Perhaps asking in comp.lang.c would make more sense in that case, no?




或许,但我没看过comp.lang.c,don''我想花时间

在那里筛选任何不相关的谷壳,并且由于C89标准是(b)标准(通过引用)并入C ++标准,它似乎是

在这里相关和主题。


我很清楚gmtime()需要time_t *。我想知道如果其参数*指向的*值是

pre-epoch(负数),那么gbtime应该做什么。我认为这是隐含的理解。我已经忘记了-1是无效的时间_t。价值。


所以它是特定的实现(什么是负时间)。这就是我想要的
。由于我正在寻找可移植性,我会检查

的预调用负值,或者从gmtime返回0。



Perhaps, but I don''t read comp.lang.c, don''t want to spend the time
sifting through any unrelated chaff there, and since the C89 standard is
incorporated (by reference) into the C++ Standard, it would seem to be
relevant and on-topic here.

I''m well aware that gmtime() takes a time_t*. I was wondering what
gmtime is supposed to do if the *value pointed to by its parameter* is
pre-epoch (negative). I had figured that was implicitly understood. I
had forgotten that -1 is the "invalid time_t" value.

So it''s implementation specific (what "negative time" does). That''s
what I was looking for. Since I''m looking for portability, I''ll check
for negative values pre-call, or 0 returns from gmtime.

red floyd写道:
red floyd wrote:
Victor Bazarov写道:
Victor Bazarov wrote:
red floyd写道:
red floyd wrote:
我有C ++标准的副本。不幸的是,我没有C89标准的副本,我的问题与
通过引用合并的内容有关。

或许在comp.lang中提问在这种情况下.c会更有意义吗,不是吗?
I have a copy of the C++ standard. Unfortunately, I don''t have a
copy of the C89 standard, and the question I have is related to
something incorporated by reference.

Perhaps asking in comp.lang.c would make more sense in that case, no?



也许,但我没看过comp.lang.c,不想花时间在那里筛选任何不相关的箔条,并且由于C89标准被纳入(通过引用)到C ++标准中,它似乎是相关的和在这里的主题。
<我很清楚gmtime()需要time_t *。我想知道gmtime应该做什么,如果它的参数*指向的*值是
pre-epoch(负数)。



Perhaps, but I don''t read comp.lang.c, don''t want to spend the time
sifting through any unrelated chaff there, and since the C89 standard is
incorporated (by reference) into the C++ Standard, it would seem to be
relevant and on-topic here.

I''m well aware that gmtime() takes a time_t*. I was wondering what
gmtime is supposed to do if the *value pointed to by its parameter* is
pre-epoch (negative).




你在这里再去!没有时代。除了

单个负值(-1)之外的负值仍然有效。标准中没有任何内容
表示''time_t''也是算术(或积分)类型。可能在C ++中,''time_t''类型是用户定义的类型,只需

就可以转换为''int'',并且提供(-1)创建一个

类型''time_t''的对象,它以某种方式无效。因为你不应该在便携式程序中查看''time_t''的内容,并且只能使用

来回传递时间函数,任何实现的

类型都是可能的。

我认为这是隐含的理解。


标准方面没有任何暗示。我认为这是隐含的理解



我已经忘记了-1是无效的time_t。值。

所以它是特定的实现(什么是负时间)。那就是我在寻找的东西。由于我正在寻找可移植性,我会检查
是否有预先调用的值,或者从gmtime返回0。



Here you go again!.. There is no "epoch". Negative values, except the
single negative value of (-1) can still be valid. Nothing in the Standard
says that ''time_t'' is an arithmetic (or integral) type, either. It is
possible that in C++ the type ''time_t'' is a user-defined type that simply
has a conversion from, say, ''int'', and supplying (-1) creates an object of
type ''time_t'' that is somehow "invalid". Since you''re not supposed to be
looking at the contents of ''time_t'' in a portable program, and only use it
to pass back and forth between time functions, any implementation of that
type is possible.
I had figured that was implicitly understood.
There is nothing implicit when it comes to Standards. I thought that was
implicitly understood.
I
had forgotten that -1 is the "invalid time_t" value.

So it''s implementation specific (what "negative time" does). That''s
what I was looking for. Since I''m looking for portability, I''ll check
for negative values pre-call, or 0 returns from gmtime.




你可以' 便携式检查负值。未指明

_any_''time_t'的负值是不好的。它指定只有

(time_t)( - 1)是无效,这意味着没有可用的日历时间。


有两个函数在返回time_t

值的C标准库:''time''和''mktime''。我们知道他们_mey_返回一些东西

喜欢(time_t)( - 7777)它应该被认为是_valid_值(在那个

特定的实现中)。


V



You can''t portably "check for negative values". It is unspecified that
_any_ negative value of ''time_t'' is bad. It specifies that only
(time_t)(-1) is "invalid", meaning that no calendar time is available.

There are two functions in the C Standard Library that return a time_t
value: ''time'' and ''mktime''. For all we know they _may_ return something
like (time_t)(-7777) and it should be considered a _valid_ value (in that
particular implementation).

V


这篇关于gmtime()的正确行为是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆