473,471 Members | 4,625 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

What is the proper behavior of gmtime()?

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?
Jan 26 '06 #1
11 4930
red floyd wrote:
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' returns NULL if it cannot convert the value you pass to it.

V
Jan 26 '06 #2
Victor Bazarov wrote:
red floyd wrote:
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?


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.

Jan 26 '06 #3
red floyd wrote:
Victor Bazarov wrote:
red floyd wrote:
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?


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).


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.


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
Jan 26 '06 #4
red floyd wrote:
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?

Most of what you ask is implementation-specific. Check your
documentation. Here's what the last public draft of C89 (it's identical
in the C99 draft as well) said:

4.12.3.3 The gmtime function

Synopsis

#include <time.h>
struct tm *gmtime(const time_t *timer);

Description

The gmtime function converts the calendar time pointed to by timer
into a broken-down time, expressed as Coordinated Universal Time
(UTC).

Returns

The gmtime function returns a pointer to that object, or a null
pointer if UTC is not available.


Brian

Jan 26 '06 #5
red floyd wrote:
Victor Bazarov wrote:
red floyd wrote:
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?


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.


Perhaps, you should start from the start...the documentation:
http://www.opengroup.org/onlinepubs/...ns/gmtime.html

Cheers
--
Mateusz Łoskot
http://mateusz.loskot.net
Jan 26 '06 #6
Victor, Mateusz, and Default User,

Thank you for your help. Not having the C89 spec, I had some
misconceptions about the nature of time_t, based on POSIX experience.

I will make sure to validate returns from gmtime() (luckily it's
encapsulated in only one or two places).
Jan 26 '06 #7
Mateusz Łoskot wrote:

Perhaps, you should start from the start...the documentation:
http://www.opengroup.org/onlinepubs/...ns/gmtime.html

Their claims about being "aligned with the ISO C standard" are bogus.

"The gmtime() function shall convert the time in seconds since the
Epoch pointed to by timer into a broken-down time, expressed as
Coordinated Universal Time (UTC)."
There's NO "Epoch" in the C standard, nor does time_t have to represent
any kind of seconds at all. The only thing the standard says about
time_t is that it is an arithmetic type capable of representing times.
It could be a long double with the number of microseconds since the
founding of Rome.

In short, I wouldn't use this as a reference unless you were sure it
applied to your platform. A better source would be the system
documention, if available.


Brian
Jan 26 '06 #8
Victor Bazarov wrote:
Here you go again!.. There is no "epoch".

Then almost all related manual pages are wrong, see
http://tinyurl.com/93kcf, 4.14.
(http://www.opengroup.org/onlinepubs/...html#tag_04_14)

Regards, Stephan

Jan 26 '06 #9
Stephan Brnnimann wrote:
Victor Bazarov wrote:
Here you go again!.. There is no "epoch".
Then almost all related manual pages are wrong, see
http://tinyurl.com/93kcf, 4.14.


I don't see any reference to function 'time' there. Do you?
(http://www.opengroup.org/onlinepubs/...html#tag_04_14)

Regards, Stephan


V
Jan 26 '06 #10
Victor Bazarov wrote:
[..] Nothing in the Standard
says that 'time_t' is an arithmetic (or integral) type, either. [..]


My bad. It's arithmetic alright.

V
--
Please remove capital As from my address when replying by mail
Jan 26 '06 #11
Stephan Brnnimann wrote:
Victor Bazarov wrote:
Here you go again!.. There is no "epoch".
Then almost all related manual pages are wrong, see [snip]


That reflects implementation-specific information. That's a normal and
expected feature of manuals. That's why they exist.

The C standard (and so by inclusion C++), is deliberately vague in this
case. I suspect there were a few common implementations that didn't
handle time anything like the UNIX "epoch" approach at that time. So
they left all this up to the implementation.

Brian
Jan 27 '06 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

220
by: Brandon J. Van Every | last post by:
What's better about Ruby than Python? I'm sure there's something. What is it? This is not a troll. I'm language shopping and I want people's answers. I don't know beans about Ruby or have...
1
by: Florian Lindner | last post by:
Hello, I want to know if a certain duration is over. I try it like this with timedelta objects: d = datetime.timedelta(minutes = 2) t = time.gmtime() print (t + d < time.gmtime()) gives:
2
by: red floyd | last post by:
Is there a portable way to convert between a struct tm and a time_t, for UTC? I know that time_t is seconds since the epoch (1970-01-01T00:00:00Z). Now, you can convert a time_t to a struct tm...
121
by: typingcat | last post by:
First of all, I'm an Asian and I need to input Japanese, Korean and so on. I've tried many PHP IDEs today, but almost non of them supported Unicode (UTF-8) file. I've found that the only Unicode...
67
by: neilcancer | last post by:
i come from china,and i'm sorry that my english is very poor. now i'm studing data structure and i met some problem about c language. could you tell me what will happen after i use free()? i...
2
by: Nagaraj | last post by:
Hi all, I am new to Linux platform. I am writing some C programs on Linux platform. It gives the segmentation fault error, what is segmentation fault and how to remove it. please reply.
7
by: Neil | last post by:
What I am doing wrong This works batPointer = adaptors.adaptor->batData; adaptors.batteries = batPointer->battery; where: batData is a pointer to a struct batPointer is a pointer to a...
3
by: johnqsmith | last post by:
Problem background: gcc v 4.1 2 .cpp files, 2 .h files Files: main.cpp a.cpp a.h b.h
3
by: jorba101 | last post by:
Following prototypes apply for localtime and gmtime: struct tm *localtime(const time_t *timer); struct tm *gmtime(const time_t *timer); I wonder, should I understand that localtime and...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.