Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #157731 > unrolled thread
| Started by | T <T@invalid.invalid> |
|---|---|
| First post | 2020-12-25 03:08 -0800 |
| Last post | 2021-02-02 11:29 +0000 |
| Articles | 18 — 14 participants |
Back to article view | Back to comp.lang.c
time.h time_t question T <T@invalid.invalid> - 2020-12-25 03:08 -0800
Re: time.h time_t question fir <profesor.fir@gmail.com> - 2020-12-25 03:28 -0800
Re: time.h time_t question fir <profesor.fir@gmail.com> - 2020-12-25 03:39 -0800
Re: time.h time_t question Bart <bc@freeuk.com> - 2020-12-25 12:34 +0000
Re: time.h time_t question gazelle@shell.xmission.com (Kenny McCormack) - 2020-12-25 14:03 +0000
Re: time.h time_t question Richard Damon <Richard@Damon-Family.org> - 2020-12-25 10:09 -0500
Re: time.h time_t question James Kuyper <jameskuyper@alumni.caltech.edu> - 2020-12-25 11:46 -0500
Re: time.h time_t question fir <profesor.fir@gmail.com> - 2020-12-25 09:47 -0800
Re: time.h time_t question Ben Bacarisse <ben.usenet@bsb.me.uk> - 2020-12-25 20:17 +0000
Re: time.h time_t question William Ahern <william@25thandClement.com> - 2020-12-31 02:32 -0800
Re: time.h time_t question Tim Rentsch <tr.17687@z991.linuxsc.com> - 2021-01-01 08:26 -0800
Re: time.h time_t question Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2020-12-31 12:07 -0700
Re: time.h time_t question Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2020-12-31 14:08 -0800
Re: time.h time_t question John Bode <jfbode1029@gmail.com> - 2021-01-28 12:02 -0600
Re: time.h time_t question James Kuyper <jameskuyper@alumni.caltech.edu> - 2021-01-28 14:14 -0500
Re: time.h time_t question Andrey Tarasevich <andreytarasevich@hotmail.com> - 2021-01-29 13:05 -0800
Re: time.h time_t question Jorgen Grahn <grahn+nntp@snipabacken.se> - 2021-01-30 06:45 +0000
Re: time.h time_t question gazelle@shell.xmission.com (Kenny McCormack) - 2021-02-02 11:29 +0000
| From | T <T@invalid.invalid> |
|---|---|
| Date | 2020-12-25 03:08 -0800 |
| Subject | time.h time_t question |
| Message-ID | <rs4h84$4j9$1@dont-email.me> |
Hi All,
Okay, I give up. I can't read C for my life!
1) What is the structure of time_t below?
/usr/include/bits/types/time_t.h
#ifndef __time_t_defined
#define __time_t_defined 1
#include <bits/types.h>
/* Returned by `time'. */
typedef __time_t time_t;
#endif
"time_t" is used here:
/usr/include/bits/types/time_t.h
/* Return the current time and put it in *TIMER if TIMER is not
NULL. */
extern time_t time (time_t *__timer) __THROW;
"__THROW" is throw an exception.
2) is the first "time_t" the returned value? What is
its "structure"? Is it the mystical time_t.h that I
can't figure out?
3) what is the "time"? Is that the name of the function?
Is the first "time_t" defining the structure of the
returned value of "time"?
4) what is the second "time_t"? Why two of them?
5) I presume "*___timer " is a pointers, but to what
structure?
6) is tm.h used anywhere in the above?
/usr/include/bits/types/struct_tm.h
struct tm
{
int tm_sec; /* Seconds. [0-60] (1 leap second) */
int tm_min; /* Minutes. [0-59] */
int tm_hour; /* Hours. [0-23] */
int tm_mday; /* Day. [1-31] */
int tm_mon; /* Month. [0-11] */
int tm_year; /* Year - 1900. */
int tm_wday; /* Day of week. [0-6] */
int tm_yday; /* Days in year.[0-365] */
int tm_isdst; /* DST. [-1/0/1]*/
# ifdef __USE_MISC
long int tm_gmtoff; /* Seconds east of UTC. */
const char *tm_zone; /* Timezone abbreviation. */
# else
long int __tm_gmtoff; /* Seconds east of UTC. */
const char *__tm_zone; /* Timezone abbreviation. */
# endif
};
#endif
Yours in confusion,
-T
[toc] | [next] | [standalone]
| From | fir <profesor.fir@gmail.com> |
|---|---|
| Date | 2020-12-25 03:28 -0800 |
| Message-ID | <eaa655b4-1495-4d20-acfe-2202ff233bfdn@googlegroups.com> |
| In reply to | #157731 |
piątek, 25 grudnia 2020 o 12:09:10 UTC+1 T napisał(a):
> Hi All,
>
> Okay, I give up. I can't read C for my life!
>
>
> 1) What is the structure of time_t below?
>
> /usr/include/bits/types/time_t.h
>
> #ifndef __time_t_defined
> #define __time_t_defined 1
>
> #include <bits/types.h>
>
> /* Returned by `time'. */
> typedef __time_t time_t;
>
> #endif
>
>
> "time_t" is used here:
>
> /usr/include/bits/types/time_t.h
>
> /* Return the current time and put it in *TIMER if TIMER is not
> NULL. */
> extern time_t time (time_t *__timer) __THROW;
>
>
> "__THROW" is throw an exception.
>
>
> 2) is the first "time_t" the returned value? What is
> its "structure"? Is it the mystical time_t.h that I
> can't figure out?
>
> 3) what is the "time"? Is that the name of the function?
> Is the first "time_t" defining the structure of the
> returned value of "time"?
>
> 4) what is the second "time_t"? Why two of them?
>
> 5) I presume "*___timer " is a pointers, but to what
> structure?
>
> 6) is tm.h used anywhere in the above?
> /usr/include/bits/types/struct_tm.h
>
> struct tm
> {
> int tm_sec; /* Seconds. [0-60] (1 leap second) */
> int tm_min; /* Minutes. [0-59] */
> int tm_hour; /* Hours. [0-23] */
> int tm_mday; /* Day. [1-31] */
> int tm_mon; /* Month. [0-11] */
> int tm_year; /* Year - 1900. */
> int tm_wday; /* Day of week. [0-6] */
> int tm_yday; /* Days in year.[0-365] */
> int tm_isdst; /* DST. [-1/0/1]*/
>
> # ifdef __USE_MISC
> long int tm_gmtoff; /* Seconds east of UTC. */
> const char *tm_zone; /* Timezone abbreviation. */
> # else
> long int __tm_gmtoff; /* Seconds east of UTC. */
> const char *__tm_zone; /* Timezone abbreviation. */
> # endif
> };
>
> #endif
>
what it is? this is genarally some symptom of sicknes who often occupies c++ people, it is a need to overcoding things around... then you get 17 names for int like in c++ 10 names for string etc
[toc] | [prev] | [next] | [standalone]
| From | fir <profesor.fir@gmail.com> |
|---|---|
| Date | 2020-12-25 03:39 -0800 |
| Message-ID | <b2adcc50-02db-4bf1-a076-5f3de4b74002n@googlegroups.com> |
| In reply to | #157732 |
piątek, 25 grudnia 2020 o 12:28:38 UTC+1 fir napisał(a):
> piątek, 25 grudnia 2020 o 12:09:10 UTC+1 T napisał(a):
> > Hi All,
> >
> > Okay, I give up. I can't read C for my life!
> >
> >
> > 1) What is the structure of time_t below?
> >
> > /usr/include/bits/types/time_t.h
> >
> > #ifndef __time_t_defined
> > #define __time_t_defined 1
> >
> > #include <bits/types.h>
> >
> > /* Returned by `time'. */
> > typedef __time_t time_t;
> >
> > #endif
> >
> >
> > "time_t" is used here:
> >
> > /usr/include/bits/types/time_t.h
> >
> > /* Return the current time and put it in *TIMER if TIMER is not
> > NULL. */
> > extern time_t time (time_t *__timer) __THROW;
> >
> >
> > "__THROW" is throw an exception.
> >
> >
> > 2) is the first "time_t" the returned value? What is
> > its "structure"? Is it the mystical time_t.h that I
> > can't figure out?
> >
> > 3) what is the "time"? Is that the name of the function?
> > Is the first "time_t" defining the structure of the
> > returned value of "time"?
> >
> > 4) what is the second "time_t"? Why two of them?
> >
> > 5) I presume "*___timer " is a pointers, but to what
> > structure?
> >
> > 6) is tm.h used anywhere in the above?
> > /usr/include/bits/types/struct_tm.h
> >
> > struct tm
> > {
> > int tm_sec; /* Seconds. [0-60] (1 leap second) */
> > int tm_min; /* Minutes. [0-59] */
> > int tm_hour; /* Hours. [0-23] */
> > int tm_mday; /* Day. [1-31] */
> > int tm_mon; /* Month. [0-11] */
> > int tm_year; /* Year - 1900. */
> > int tm_wday; /* Day of week. [0-6] */
> > int tm_yday; /* Days in year.[0-365] */
> > int tm_isdst; /* DST. [-1/0/1]*/
> >
> > # ifdef __USE_MISC
> > long int tm_gmtoff; /* Seconds east of UTC. */
> > const char *tm_zone; /* Timezone abbreviation. */
> > # else
> > long int __tm_gmtoff; /* Seconds east of UTC. */
> > const char *__tm_zone; /* Timezone abbreviation. */
> > # endif
> > };
> >
> > #endif
> >
> what it is? this is genarally some symptom of sicknes who often occupies c++ people, it is a need to overcoding things around... then you get 17 names for int like in c++ 10 names for string etc
so dont name it c if you donk want to piss of some people who can say "i know c"
its a code written by people eho dont know c
btw I could say that using this kind of 'trick' that is giving some user some handles/things to use which they dont fully know it is is kinda a nonethical work
it maybe may give some more power to library maintainer but brings this power out of user in imo non ethical way (its not only takes away the 'power' but gives down a poison)
[toc] | [prev] | [next] | [standalone]
| From | Bart <bc@freeuk.com> |
|---|---|
| Date | 2020-12-25 12:34 +0000 |
| Message-ID | <D%kFH.140138$C7R9.79436@fx14.ams4> |
| In reply to | #157731 |
On 25/12/2020 11:08, T wrote:
> Hi All,
>
> Okay, I give up. I can't read C for my life!
Nobody else can either, not the C you find in system header files. It's
just a ghastly mess of nested includes, macros, typedefs and if/ifdef
blocks. Every struct also seems to need a private int type for each field.
Once I needed to find out what type 'clock_t' was, and I finally found
it buried under SIX levels of typedefs and macros.
This sort of code is intended only for a C compiler to disentangle.
But try tricks like the following, althoough this will require using a C
compiler (if you are on Linux, you should have gcc).
I'm on Windows so it might work a little differently. I created a
program like this (say in test.c) which deliberately passes the wrong
type to a function:
#include <time.h>
void func(char*);
int main(void) {
time_t x;
func(x);
}
I then try to compile with gcc like this:
gcc -c test.c
This produces these warning messages:
C:\c>gcc -c test.c
test.c: In function 'main':
test.c:8:10: warning: passing argument 1 of 'func' makes pointer from
integer without a cast [-Wint-conversion]
func(x);
^
test.c:3:11: note: expected 'char *' but argument is of type 'time_t'
{aka 'long long int'}
void func(char*);
^~~~~
The important bit is "{aka 'long long int'}". This tells you that time_t
in this compiler on this platform is really just 'long long int', which
is almost certainly int64 (signed 64 bits).
>
> 1) What is the structure of time_t below?
>
> /usr/include/bits/types/time_t.h
>
> #ifndef __time_t_defined
> #define __time_t_defined 1
Yes, this is very common to have lot of names, frequently aliases,
starting with _, __ or ___. I used to call this 'underscore-itus'.
> extern time_t time (time_t *__timer) __THROW;
>
>
> "__THROW" is throw an exception.
No exceptions in C. So it might be an empty macro when compiled as C.
>
>
> 2) is the first "time_t" the returned value? What is
> its "structure"? Is it the mystical time_t.h that I
> can't figure out?
>
> 3) what is the "time"? Is that the name of the function?
> Is the first "time_t" defining the structure of the
> returned value of "time"?
>
> 4) what is the second "time_t"? Why two of them?
>
> 5) I presume "*___timer " is a pointers, but to what
> structure?
> extern time_t time (time_t *__timer) __THROW;
Ignore the __THROW. The structure of this is:
extern RETTYPE time(PARAMTYPE name);
So:
* The function has external linkage (means not defined in your program
but imported)
* If has RETTYPE time_t (returns a time_t value)
* The function name is 'time'
* It has one parameter with PARAMTYPE time_t* (pointer to a time_t)
* That parameter is called __timer. (Why parameters are named in headers
is not clear, since you can't do anything with it. Perhaps make it
easier to read? I don't think so! Parameter names can be anything so
what's with the __ prefix?)
(On Windows I developed a special tool, which processes C headers and
generates matching declarations in my language. Not perfect, but
provides a useful start point. For this time function, it produces:
clang function _time64(ref int64)int64
since on Windows, 'time' is a macro alias for the actual function which
is called '_time64'. This is the name to use to access via a FFI.
Long ago it would have been able to read header files from Linux, but I
don't think it can do that anymore, and using Linux now is far too
complicated on my machine.)
[toc] | [prev] | [next] | [standalone]
| From | gazelle@shell.xmission.com (Kenny McCormack) |
|---|---|
| Date | 2020-12-25 14:03 +0000 |
| Message-ID | <rs4rfo$jn6j$1@news.xmission.com> |
| In reply to | #157731 |
In article <rs4h84$4j9$1@dont-email.me>, T <T@invalid.invalid> wrote: >Hi All, > >Okay, I give up. I can't read C for my life! Then you should not try. You should go back to political trolling. -- Which of these is the crazier bit of right wing lunacy? 1) We've just had another mass shooting; now is not the time to be talking about gun control. 2) We've just had a massive hurricane; now is not the time to be talking about climate change.
[toc] | [prev] | [next] | [standalone]
| From | Richard Damon <Richard@Damon-Family.org> |
|---|---|
| Date | 2020-12-25 10:09 -0500 |
| Message-ID | <3hnFH.62096$x92.8455@fx48.iad> |
| In reply to | #157731 |
System headers can be hard to read, as they often use a multi-layered
approach that works well to define the things based on basic principles,
but without the internal design docuements may be hard to unravel.
On 12/25/20 6:08 AM, T wrote:
> Hi All,
>
> Okay, I give up. I can't read C for my life!
>
>
> 1) What is the structure of time_t below?
>
> /usr/include/bits/types/time_t.h
>
> #ifndef __time_t_defined
> #define __time_t_defined 1
>
> #include <bits/types.h>
>
> /* Returned by `time'. */
> typedef __time_t time_t;
>
> #endif
So now time_t is an alias for the type __time_t, that probably got
defined in <bits/types.h> (or something it included)
>
>
> "time_t" is used here:
>
> /usr/include/bits/types/time_t.h
>
> /* Return the current time and put it in *TIMER if TIMER is not
> NULL. */
> extern time_t time (time_t *__timer) __THROW;
>
>
> "__THROW" is throw an exception.
>
>
> 2) is the first "time_t" the returned value? What is
> its "structure"? Is it the mystical time_t.h that I
> can't figure out?
Yes, the first time_t is the return type, that you need to dig deeper to
find out how it is actually built.
>
> 3) what is the "time"? Is that the name of the function?
> Is the first "time_t" defining the structure of the
> returned value of "time"?
Yes, time is the name of the function being declared here. It is defined
to return a time_t (an alias for __time_t) and taking as a parameter a
pointer to a time_t.
>
> 4) what is the second "time_t"? Why two of them?
The type of pointer that the parameter to the function will take. time
takes as a parameter a pointer to an objet of type time_t (or possibly a
NULL pointer)
>
> 5) I presume "*___timer " is a pointers, but to what
> structure?
>
__timer is the name of the parameter, that name doesn't actually matter,
but might be used for error messages. The type of the parameter is a
pointer to a time_t.
> 6) is tm.h used anywhere in the above?
> /usr/include/bits/types/struct_tm.h
>
> struct tm
> {
> int tm_sec; /* Seconds. [0-60] (1 leap second) */
> int tm_min; /* Minutes. [0-59] */
> int tm_hour; /* Hours. [0-23] */
> int tm_mday; /* Day. [1-31] */
> int tm_mon; /* Month. [0-11] */
> int tm_year; /* Year - 1900. */
> int tm_wday; /* Day of week. [0-6] */
> int tm_yday; /* Days in year.[0-365] */
> int tm_isdst; /* DST. [-1/0/1]*/
>
> # ifdef __USE_MISC
> long int tm_gmtoff; /* Seconds east of UTC. */
> const char *tm_zone; /* Timezone abbreviation. */
> # else
> long int __tm_gmtoff; /* Seconds east of UTC. */
> const char *__tm_zone; /* Timezone abbreviation. */
> # endif
> };
>
> #endif
From what you have posted, it could be, You will need to dig through the
headers to see if that happens. It might go through several levels of
typeef. You need to trace down through it.
>
>
> Yours in confusion,
> -T
The key is you are expected to read the documentation for time. Note
that since (time_t)(-1) is a possible value, it is actually unlikely
that time_t will map to your struct tm, as that case wouldn't then be
valid. time_t by the standard will be an 'arithmetic type' (something
like an int or a float).
There is defined a function mktime which will convert a struct tm to a
time_t
[toc] | [prev] | [next] | [standalone]
| From | James Kuyper <jameskuyper@alumni.caltech.edu> |
|---|---|
| Date | 2020-12-25 11:46 -0500 |
| Message-ID | <rs551p$utc$1@dont-email.me> |
| In reply to | #157746 |
On 12/25/20 10:09 AM, Richard Damon wrote: ... > The key is you are expected to read the documentation for time. Note > that since (time_t)(-1) is a possible value, it is actually unlikely > that time_t will map to your struct tm, as that case wouldn't then be > valid. time_t by the standard will be an 'arithmetic type' (something > like an int or a float). The standard used to say "arithmetic type". I had fun pointing out that this allowed time_t to be a typedef for _Imaginary double, which would have some interesting consequences. I'd like to think that my comment was the reason why it was changed to "real type".
[toc] | [prev] | [next] | [standalone]
| From | fir <profesor.fir@gmail.com> |
|---|---|
| Date | 2020-12-25 09:47 -0800 |
| Message-ID | <fcadaf86-7285-46cd-9702-cb0a9cc30073n@googlegroups.com> |
| In reply to | #157746 |
piątek, 25 grudnia 2020 o 16:10:05 UTC+1 Richard Damon napisał(a):
> System headers can be hard to read, as they often use a multi-layered
> approach that works well to define the things based on basic principles,
> but without the internal design docuements may be hard to unravel.
> On 12/25/20 6:08 AM, T wrote:
> > Hi All,
> >
> > Okay, I give up. I can't read C for my life!
> >
> >
> > 1) What is the structure of time_t below?
> >
> > /usr/include/bits/types/time_t.h
> >
> > #ifndef __time_t_defined
> > #define __time_t_defined 1
> >
> > #include <bits/types.h>
> >
> > /* Returned by `time'. */
> > typedef __time_t time_t;
> >
> > #endif
> So now time_t is an alias for the type __time_t, that probably got
> defined in <bits/types.h> (or something it included)
> >
> >
> > "time_t" is used here:
> >
> > /usr/include/bits/types/time_t.h
> >
> > /* Return the current time and put it in *TIMER if TIMER is not
> > NULL. */
> > extern time_t time (time_t *__timer) __THROW;
> >
> >
> > "__THROW" is throw an exception.
> >
> >
> > 2) is the first "time_t" the returned value? What is
> > its "structure"? Is it the mystical time_t.h that I
> > can't figure out?
> Yes, the first time_t is the return type, that you need to dig deeper to
> find out how it is actually built.
> >
> > 3) what is the "time"? Is that the name of the function?
> > Is the first "time_t" defining the structure of the
> > returned value of "time"?
> Yes, time is the name of the function being declared here. It is defined
> to return a time_t (an alias for __time_t) and taking as a parameter a
> pointer to a time_t.
> >
> > 4) what is the second "time_t"? Why two of them?
> The type of pointer that the parameter to the function will take. time
> takes as a parameter a pointer to an objet of type time_t (or possibly a
> NULL pointer)
> >
> > 5) I presume "*___timer " is a pointers, but to what
> > structure?
> >
> __timer is the name of the parameter, that name doesn't actually matter,
> but might be used for error messages. The type of the parameter is a
> pointer to a time_t.
> > 6) is tm.h used anywhere in the above?
> > /usr/include/bits/types/struct_tm.h
> >
> > struct tm
> > {
> > int tm_sec; /* Seconds. [0-60] (1 leap second) */
> > int tm_min; /* Minutes. [0-59] */
> > int tm_hour; /* Hours. [0-23] */
> > int tm_mday; /* Day. [1-31] */
> > int tm_mon; /* Month. [0-11] */
> > int tm_year; /* Year - 1900. */
> > int tm_wday; /* Day of week. [0-6] */
> > int tm_yday; /* Days in year.[0-365] */
> > int tm_isdst; /* DST. [-1/0/1]*/
> >
> > # ifdef __USE_MISC
> > long int tm_gmtoff; /* Seconds east of UTC. */
> > const char *tm_zone; /* Timezone abbreviation. */
> > # else
> > long int __tm_gmtoff; /* Seconds east of UTC. */
> > const char *__tm_zone; /* Timezone abbreviation. */
> > # endif
> > };
> >
> > #endif
> From what you have posted, it could be, You will need to dig through the
> headers to see if that happens. It might go through several levels of
> typeef. You need to trace down through it.
>
> >
> >
> > Yours in confusion,
> > -T
>
> The key is you are expected to read the documentation for time. Note
> that since (time_t)(-1) is a possible value, it is actually unlikely
> that time_t will map to your struct tm, as that case wouldn't then be
> valid. time_t by the standard will be an 'arithmetic type' (something
> like an int or a float).
>
> There is defined a function mktime which will convert a struct tm to a
> time_t
some could printf sizeof on it to see what size it is at least that work
i remember in more begining years of me programming such things as say sfml Image was scarry to me (i mean structures which you dont clearly know what it is in ram, or structures which you got by handle)..now i rather know what it is without looking (few ints like width, height and pointer to mallocked ram)
[toc] | [prev] | [next] | [standalone]
| From | Ben Bacarisse <ben.usenet@bsb.me.uk> |
|---|---|
| Date | 2020-12-25 20:17 +0000 |
| Message-ID | <878s9lbpyc.fsf@bsb.me.uk> |
| In reply to | #157731 |
T <T@invalid.invalid> writes: > Okay, I give up. I can't read C for my life! I takes time. But you are looking in the wrong place. The place to find out about functions is in the documentation, not the implementation. You do need a bit more than most users if you want to call a C function from Raku, but only a tiny bit more. > 1) What is the structure of time_t below? There's not "structure". time_t is a "real type" (in recent versions of C). In your case it is almost certainly just another name for a 64-bit integer type. > /usr/include/bits/types/time_t.h > > #ifndef __time_t_defined > #define __time_t_defined 1 > > #include <bits/types.h> > > /* Returned by `time'. */ > typedef __time_t time_t; > > #endif > > "time_t" is used here: > > /usr/include/bits/types/time_t.h > > /* Return the current time and put it in *TIMER if TIMER is not NULL. */ > extern time_t time (time_t *__timer) __THROW; > > "__THROW" is throw an exception. > > > 2) is the first "time_t" the returned value? Yes. > What is its "structure"? Is it the mystical time_t.h that I can't > figure out? You have not yet found where it is defined. It could take you a long time. Your best bet is to ask "what Raku type do I need to make a native call that uses C's time_t?". > 3) what is the "time"? Is that the name of the function? Yes. > Is the first "time_t" defining the structure of the > returned value of "time"? It's just the return type. time_t is just a name for some other type, most likely long int or long long int. > 4) what is the second "time_t"? Why two of them? It is part of the type of a pointer argument to the time function. The time function is odd. It dates from an era when C could not necessarily return a value of a type with enough range and precision, so a pointer to an object that could hold the result was passed. Things have moved on, but the ability to have the value stored into a variable rather than simply returned has been kept. > 5) I presume "*___timer " is a pointers, but to what > structure? Just a pointer to a 64-bit integer into which the function stores the time. > 6) is tm.h used anywhere in the above? Not that I can see. But try to find some documentation for the C implementation you are using. That will give you much clearer information that poking about in the header files. -- Ben.
[toc] | [prev] | [next] | [standalone]
| From | William Ahern <william@25thandClement.com> |
|---|---|
| Date | 2020-12-31 02:32 -0800 |
| Message-ID | <0vl0ch-68j1.ln1@wilbur.25thandClement.com> |
| In reply to | #157731 |
T <T@invalid.invalid> wrote: > Hi All, > > Okay, I give up. I can't read C for my life! As mentioned elsethread, system headers can be quite difficult to read, particularly because of excessive use of the preprocessor. This isn't always the case--see musl libc, for example. > > 1) What is the structure of time_t below? > > /usr/include/bits/types/time_t.h > > #ifndef __time_t_defined > #define __time_t_defined 1 > > #include <bits/types.h> > > /* Returned by `time'. */ > typedef __time_t time_t; > > #endif > One technique (at least on POSIX systems) to help find an underlying type is to run a command like: printf "#include <time.h>\n" | c99 -E - | grep time_t Per POSIX, the -E flag tells the compiler to "[c]opy C-language source files to standard output, executing all preprocessor directives; no compilation shall be performed." (If you don't have the `c99` command, try `cc` or whatever your local compiler command is--e.g. `gcc` or `clang`.) On an Ubuntu Linux 20.04 system I find the answer in the first three lines, typedef long int __time_t; # 1 "/usr/include/x86_64-linux-gnu/bits/types/time_t.h" 1 3 4 typedef __time_t time_t; on Solaris 11.4 in the first line, typedef long time_t; and on Alpine Linux 3.12 in the first line, typedef long time_t; My AIX 7.1 environment only has gcc available, but the answer is also on the first line, $ printf "#include <time.h>\n" | gcc -E - | grep time_t typedef int time_t; OpenBSD doesn't have c99, either, and the answer isn't as conclusive, but the first two lines are sufficient, $ printf "#include <time.h>\n" | cc -E - | grep time_t typedef __int64_t __time_t; typedef __time_t time_t; Note that on multi-arch systems or when cross-compiling you may need to include additional compiler flags as types might differ across architectures.
[toc] | [prev] | [next] | [standalone]
| From | Tim Rentsch <tr.17687@z991.linuxsc.com> |
|---|---|
| Date | 2021-01-01 08:26 -0800 |
| Message-ID | <867dowtyh3.fsf@linuxsc.com> |
| In reply to | #157936 |
William Ahern <william@25thandClement.com> writes: > T <T@invalid.invalid> wrote: > > As mentioned elsethread, system headers can be quite difficult to read, > particularly because of excessive use of the preprocessor. This isn't always > the case--see musl libc, for example. > >> 1) What is the structure of time_t below? > > One technique (at least on POSIX systems) to help find an underlying type is > to run a command like: > > printf "#include <time.h>\n" | c99 -E - | grep time_t This is a great tip. Thank you!
[toc] | [prev] | [next] | [standalone]
| From | Joe Pfeiffer <pfeiffer@cs.nmsu.edu> |
|---|---|
| Date | 2020-12-31 12:07 -0700 |
| Message-ID | <1b5z4hztez.fsf@pfeifferfamily.net> |
| In reply to | #157731 |
T <T@invalid.invalid> writes:
> Hi All,
>
> Okay, I give up. I can't read C for my life!
>
>
> 1) What is the structure of time_t below?
As other have pointed out, trying to figure this out from the header
files is an exercise in frustration. Better to look in the appropriate
man page, in this case time(2)
DESCRIPTION
time() returns the time as the number of seconds since the Epoch,
1970-01-01 00:00:00 +0000 (UTC).
If tloc is non-NULL, the return value is also stored in the memory
pointed to by tloc.
RETURN VALUE
On success, the value of time in seconds since the Epoch is returned.
On error, ((time_t) -1) is returned, and errno is set appropriately.
So time_t is something like an int (I'm sure others can give more
detailed and technically correct responses than this -- and will!).
> 2) is the first "time_t" the returned value? What is
> its "structure"? Is it the mystical time_t.h that I
> can't figure out?
It is the returned value. It has no structure, as it is basically
an int. You can use the time() function by having a
#include <time.h>
up at the top of your file (more information from the man page).
> 3) what is the "time"? Is that the name of the function?
> Is the first "time_t" defining the structure of the
> returned value of "time"?
Yes, time() is the name of the function, and it returns something of
type time_t. You can declare and use it with something like
#include <time.h> /* somewhere up at the top, to get the definitions of
time and time_t */
time_t mytime; /* declare a variable of type time_t, in the function
where you need it */
mytime = time(NULL); /* get the current time, measured in seconds from
1970-01-01 00:00:00 +0000 (UTC) */
> 4) what is the second "time_t"? Why two of them?
If you pass a pointer to a time_t in, it will also fill that location in
with the time. I've got no idea in the world why you would do that.
> 5) I presume "*___timer " is a pointers, but to what
> structure?
Do you mean *__timer? If so, it's a pointer to a time_t.
> 6) is tm.h used anywhere in the above?
No. There are functions that will interpret a time_t in terms of date,
time of day, etc, and fill a tm structure in with the the values (for
instance, gmtime()).
[toc] | [prev] | [next] | [standalone]
| From | Keith Thompson <Keith.S.Thompson+u@gmail.com> |
|---|---|
| Date | 2020-12-31 14:08 -0800 |
| Message-ID | <87zh1t7hny.fsf@nosuchdomain.example.com> |
| In reply to | #157958 |
Joe Pfeiffer <pfeiffer@cs.nmsu.edu> writes:
> T <T@invalid.invalid> writes:
>> Okay, I give up. I can't read C for my life!
>>
>> 1) What is the structure of time_t below?
>
> As other have pointed out, trying to figure this out from the header
> files is an exercise in frustration. Better to look in the appropriate
> man page, in this case time(2)
>
> DESCRIPTION
> time() returns the time as the number of seconds since the Epoch,
> 1970-01-01 00:00:00 +0000 (UTC).
>
> If tloc is non-NULL, the return value is also stored in the memory
> pointed to by tloc.
>
> RETURN VALUE
> On success, the value of time in seconds since the Epoch is returned.
> On error, ((time_t) -1) is returned, and errno is set appropriately.
>
> So time_t is something like an int (I'm sure others can give more
> detailed and technically correct responses than this -- and will!).
C specifies time_t as a real type (integer or floating-point)
capable of representing times. POSIX imposes stricter requirements.
It's typically a 32-bit or 64-bit signed integer representing
seconds since 1970. (32-bit signed time_t will run out in 2038.)
[...]
>> 4) what is the second "time_t"? Why two of them?
>
> If you pass a pointer to a time_t in, it will also fill that location in
> with the time. I've got no idea in the world why you would do that.
Historical reasons. If the time function were being defined today, it
would presumably be specified as:
time_t time(void);
But in very old implementations, time_t needed to be 32 bits, but int
was only 16 bits and "long" had not yet been invented, so time_t took an
int* argument which it treated as a pointer to the initial element of a
2-element arrary:
time(int arr[2]);
time(int *arr); /* equivalent */
When implementations gained support for integers wide enough to hold a
time_t value, the return value was added, but changing the parameter
would have broken existing code. The usual way to invoke it is:
const time_t now = time(NULL);
[...]
--
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
Working, but not speaking, for Philips Healthcare
void Void(void) { Void(); } /* The recursive call of the void */
[toc] | [prev] | [next] | [standalone]
| From | John Bode <jfbode1029@gmail.com> |
|---|---|
| Date | 2021-01-28 12:02 -0600 |
| Message-ID | <8cfee797-98df-30bc-dfc1-1125586ff54f@gmail.com> |
| In reply to | #157731 |
On 12/25/20 5:08 AM, T wrote:
> Hi All,
>
> Okay, I give up. I can't read C for my life!
>
>
> 1) What is the structure of time_t below?
>
> /usr/include/bits/types/time_t.h
>
> #ifndef __time_t_defined
> #define __time_t_defined 1
>
> #include <bits/types.h>
>
> /* Returned by `time'. */
> typedef __time_t time_t;
>
'time_t' is a typedef name for '__time_t', which is defined
in 'bits/types.h'.
To find how how '__time_t' is defined, you'll need to look in
'bits/types.h'.
This isn't uncommon - for compilers like gcc that have been
implemented on a wide variety of platforms, types that are very
platform-dependent are often not defined directly in the standard
library header, but in a platform-specific header which gets
#included in the standard header. In this case, the standard
'time.h' header is including 'bits/types/time_t.h' and 'bits/types.h',
which is where the ultimate definition is.
On my MacOS system, 'time.h' #includes 'sys/_types/_time_t.h',
which defines 'time_t' as
typedef __darwin_time_t time_t;
'sys/_types/_time_t.h' #includes 'machine/types.h', which in turn
#includes 'i386/types.h', which #includes 'i386/_types.h', which
defines '__darwin_time_t' as
typedef long __darwin_time_t;
So, ultimately, on my system, 'time_t' has the same representation
as a 'long'.
The language definition only requires that 'time_t' is a real (scalar,
as opposed to complex) type capable of storing time values (commonly,
the number of seconds elapsed since 1970-01-01 00:00:00).
> #endif
>
>
> "time_t" is used here:
>
> /usr/include/bits/types/time_t.h
>
> /* Return the current time and put it in *TIMER if TIMER is not
> NULL. */
> extern time_t time (time_t *__timer) __THROW;
>
>
> "__THROW" is throw an exception.
>
>
> 2) is the first "time_t" the returned value? What is
> its "structure"? Is it the mystical time_t.h that I
> can't figure out?
>
Yes, the 'time' function returns a value of type 'time_t'.
> 3) what is the "time"? Is that the name of the function?
> Is the first "time_t" defining the structure of the
> returned value of "time"?
>
Yes, 'time' is the function name.
> 4) what is the second "time_t"? Why two of them?
>
The time function accepts a pointer to a 'time_t' object;
it both returns the current time *and* updates a 'time_t'
object at the same time. You can use it as
/**
* Assign the result to curtime, the
* NULL argument is ignored;
*/
time_t curtime = time( NULL );
or
time_t curtime;
/**
* Write a new value to curtime through
* the pointer argument
*/
time( &curtime );
or even
time_t one_object;
/**
* Write the current time to both one_object through
* the pointer argument and another_object from
* the return value.
*/
time_t another_object = time( &one_object );
> 5) I presume "*___timer " is a pointers, but to what
> structure?
>
'*__timer' is a pointer to whatever 'time_t' object you
want to update - see the examples above.
> 6) is tm.h used anywhere in the above?
> /usr/include/bits/types/struct_tm.h
>
> struct tm
> {
> int tm_sec; /* Seconds. [0-60] (1 leap second) */
> int tm_min; /* Minutes. [0-59] */
> int tm_hour; /* Hours. [0-23] */
> int tm_mday; /* Day. [1-31] */
> int tm_mon; /* Month. [0-11] */
> int tm_year; /* Year - 1900. */
> int tm_wday; /* Day of week. [0-6] */
> int tm_yday; /* Days in year.[0-365] */
> int tm_isdst; /* DST. [-1/0/1]*/
>
> # ifdef __USE_MISC
> long int tm_gmtoff; /* Seconds east of UTC. */
> const char *tm_zone; /* Timezone abbreviation. */
> # else
> long int __tm_gmtoff; /* Seconds east of UTC. */
> const char *__tm_zone; /* Timezone abbreviation. */
> # endif
> };
>
> #endif
>
No - the 'tm' struct can be set from a 'time_t' value (using the
'localtime' or 'gmtime' functions), or it can be used
to set a 'time_t' value using the 'mktime' function, but it's not being
used in the 'time' call above.
>
> Yours in confusion,
> -T
[toc] | [prev] | [next] | [standalone]
| From | James Kuyper <jameskuyper@alumni.caltech.edu> |
|---|---|
| Date | 2021-01-28 14:14 -0500 |
| Message-ID | <ruv2f3$13f$1@dont-email.me> |
| In reply to | #158693 |
On 1/28/21 1:02 PM, John Bode wrote: ... > The language definition only requires that 'time_t' is a real (scalar, > as opposed to complex) type capable of storing time values (commonly, > the number of seconds elapsed since 1970-01-01 00:00:00). "The real floating and complex types are collectively called the floating types." (6.2.5p11). On implementations which support imaginary types, they also qualify as floating types (G.1p2). "The integer and real floating types are collectively called real types" (6.2.5p17). time_t must be one of those types. "Integer and floating types are collectively called arithmetic types." (6.2.5p18) "Arithmetic types and pointer types are collectively called scalar types. Array and structure types are collectively called aggregate types." (6.2.5p21) "scalar types" is therefore a bigger category than "real types", and the opposite category is "aggregate types", not "complex types".
[toc] | [prev] | [next] | [standalone]
| From | Andrey Tarasevich <andreytarasevich@hotmail.com> |
|---|---|
| Date | 2021-01-29 13:05 -0800 |
| Message-ID | <rv1tbe$ogh$1@dont-email.me> |
| In reply to | #157731 |
On 12/25/2020 3:08 AM, T wrote:
>
> Okay, I give up. I can't read C for my life!
>
The library specification make one important guarantee: names of
standard types are _not_ reserved until you include a header that is
specified to define that name.
This means that name 'time_t' is available to the user to use as they
see fit, provided the translation unit does not include <time.h>.
For example, this is a perfectly valid C program
#include <stdio.h>
int time_t;
int main() {}
which will immediately become invalid if one includes <time.h> instead
of <stdio.h>.
This in turn means that if <stdio.h> need to refer to 'time_t'
internally for whatever reason, it is not allowed to declare it as
'time_t' (and, of course, it is not allowed to include <time.h>
internally). It has to somehow access the proper datatype under a
different name.
This is the reason standard headers are typically split into
"implementation" and "interface" parts. The former declares standard
entities under some invented internal names, like '__time_t'. The latter
is responsible for exposing these entities under their standard names,
e.g. by doing
typedef __time_t time_t;
--
Best regards,
Andrey Tarasevich
[toc] | [prev] | [next] | [standalone]
| From | Jorgen Grahn <grahn+nntp@snipabacken.se> |
|---|---|
| Date | 2021-01-30 06:45 +0000 |
| Message-ID | <slrns1a04o.2ptb.grahn+nntp@frailea.sa.invalid> |
| In reply to | #157731 |
On Fri, 2020-12-25, T wrote: > Hi All, > > Okay, I give up. I can't read C for my life! That's because you cannot learn C by asking questions, one by one, on Usenet. You should get a book, and read it. (Or maybe a web tutorial. I learned C a long time ago, and I have no idea what's good and what's not, today.) /Jorgen -- // Jorgen Grahn <grahn@ Oo o. . . \X/ snipabacken.se> O o .
[toc] | [prev] | [next] | [standalone]
| From | gazelle@shell.xmission.com (Kenny McCormack) |
|---|---|
| Date | 2021-02-02 11:29 +0000 |
| Message-ID | <rvbd3k$29dgm$3@news.xmission.com> |
| In reply to | #158775 |
In article <slrns1a04o.2ptb.grahn+nntp@frailea.sa.invalid>, Jorgen Grahn <grahn+nntp@snipabacken.se> wrote: >On Fri, 2020-12-25, T wrote: >> Hi All, >> >> Okay, I give up. I can't read C for my life! > >That's because you cannot learn C by asking questions, one by one, on >Usenet. You should get a book, and read it. "T" has made it abundantly clear that he has no desire or intent to learn C. And that's not just generic insult; it is what he has repeatedly claimed in his own words. All he cares about is how to use the "C" interface in Perl. -- The book "1984" used to be a cautionary tale; Now it is a "how-to" manual.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.c
csiph-web