getting timestamp from time() 
Author Message
 getting timestamp from time()

hello all,

   I was looking for a function that will give me the unix timestamp
from time() ie what I get by `date +'%s'`

 time() function returns time_t data type where can I see the
documentation for time_t data type

--
Ramprasad A Padmanabhan
Sr Software Engineer
Netcore Solns Pvt Ltd
Mumbai
ph - (022) 4628000



Mon, 09 May 2005 15:14:11 GMT  
 getting timestamp from time()

Quote:

>    I was looking for a function that will give me the unix timestamp
> from time() ie what I get by `date +'%s'`
>  time() function returns time_t data type where can I see the
> documentation for time_t data type

time_t is an arithmetic type capable of representing times. The range and
precision of times representable in time_t is implementation-defined.
Btw. I don't know what `date +'%s'` will give you. Can you be more specific as
it might be achieable using standard C.


Mon, 09 May 2005 18:17:11 GMT  
 getting timestamp from time()

Quote:
>   I was looking for a function that will give me the unix timestamp
>from time() ie what I get by `date +'%s'`

On a POSIX platform, this is exactly what time() returns.  No other
function is needed.

Quote:
> time() function returns time_t data type where can I see the
>documentation for time_t data type

On a Unix system, "man 2 time" (or something like this, not all Unix
systems agree on this command's interface) should provide you the
answer.

Note that, although this is a very common behaviour, even beyond the
realm of POSIX, the language doesn't require it.  There may be some
weird implementation that represents times in a different way.

Dan
--
Dan Pop
DESY Zeuthen, RZ group



Mon, 09 May 2005 21:34:56 GMT  
 getting timestamp from time()

Quote:

> hello all,

>    I was looking for a function that will give me the unix timestamp
> from time() ie what I get by `date +'%s'`

Do you mean the thing that looks like this:

Thu Nov 21 10:01:15 CST 2002

If so, then look into the  strftime() function, it probably will be able
to format a time for you appropriately.

Quote:
>  time() function returns time_t data type where can I see the
> documentation for time_t data type

That is implementation defined. For POSIX systems, and many others, it
is an integral type containing the number of seconds since "the epoch",
Jan 1, 1970.

For details, check your documentation or ask on newsgroup dedicated to
your platform.

Brian Rodenborn



Tue, 10 May 2005 00:09:44 GMT  
 getting timestamp from time()

Quote:
> hello all,

>    I was looking for a function that will give me the unix timestamp
> from time() ie what I get by `date +'%s'`

>  time() function returns time_t data type where can I see the
> documentation for time_t data type

You'll be needing strftime() then.

Mark.



Tue, 10 May 2005 00:59:47 GMT  
 getting timestamp from time()

Quote:

>> hello all,

>>    I was looking for a function that will give me the unix timestamp
>> from time() ie what I get by `date +'%s'`

>>  time() function returns time_t data type where can I see the
>> documentation for time_t data type

>You'll be needing strftime() then.

Nope, he doesn't.  Clue:

    fangorn:~/tmp 291> date +'%s'
    1037907391

Dan
--
Dan Pop
DESY Zeuthen, RZ group



Tue, 10 May 2005 03:37:31 GMT  
 getting timestamp from time()
in comp.lang.c i read:

Quote:

>>    I was looking for a function that will give me the unix timestamp
>> from time() ie what I get by `date +'%s'`

>Do you mean the thing that looks like this:

>Thu Nov 21 10:01:15 CST 2002

no.  seconds since an epoch.  the iso c solution requires that you use
difftime, mktime and time, and a presumption of how to select the epoch,
e.g.,

[warning: no error checking]

  time_t now = time(0);
  struct tm epoch = {0};
  unsigned long s = difftime(now, mktime(&epoch)) - 1;

--
bringing you boring signatures for 17 years



Tue, 10 May 2005 15:39:35 GMT  
 getting timestamp from time()

Quote:
>in comp.lang.c i read:

>>>    I was looking for a function that will give me the unix timestamp
>>> from time() ie what I get by `date +'%s'`

>>Do you mean the thing that looks like this:

>>Thu Nov 21 10:01:15 CST 2002

>no.  seconds since an epoch.  the iso c solution requires that you use
>difftime, mktime and time, and a presumption of how to select the epoch,
>e.g.,

>[warning: no error checking]

>  time_t now = time(0);
>  struct tm epoch = {0};
>  unsigned long s = difftime(now, mktime(&epoch)) - 1;

This is setting the epoch to 0th of January, 1900.  I doubt there are
many implementations where mktime will return anything but (time_t)-1.

Since the Unix epoch is well defined, no assumptions are needed about
the proper setting of epoch.  But, there is still no guarantee that
mktime(&epoch) will succeed on a non-POSIX system (and on a POSIX system,
a plain time() solves the problem).

Dan
--
Dan Pop
DESY Zeuthen, RZ group



Tue, 10 May 2005 18:27:03 GMT  
 getting timestamp from time()

Quote:

> in comp.lang.c i read:

> >>    I was looking for a function that will give me the unix timestamp
> >> from time() ie what I get by `date +'%s'`

> >Do you mean the thing that looks like this:

> >Thu Nov 21 10:01:15 CST 2002

> no.  seconds since an epoch.  the iso c solution requires that you use
> difftime, mktime and time, and a presumption of how to select the epoch,
> e.g.,

Since you aren't the OP, I don't think you are qualified to answer the
question. If you look at what he asked, it looks a LOT like the UNIX
shell date command. Plus, he mentions %s, which is normally for strings
(assuming a printf() like function.

None of that say the seconds since the epoch to me.

Brian Rodenborn



Wed, 11 May 2005 00:17:13 GMT  
 getting timestamp from time()

Quote:


> > in comp.lang.c i read:

> > >>    I was looking for a function that will give me the unix timestamp
> > >> from time() ie what I get by `date +'%s'`

> > >Do you mean the thing that looks like this:

> > >Thu Nov 21 10:01:15 CST 2002

> > no.  seconds since an epoch.  the iso c solution requires that you use
> > difftime, mktime and time, and a presumption of how to select the epoch,
> > e.g.,

>  Since you aren't the OP, I don't think you are qualified to answer the
>  question. If you look at what he asked, it looks a LOT like the UNIX
>  shell date command. Plus, he mentions %s, which is normally for strings
>  (assuming a printf() like function.

>  None of that say the seconds since the epoch to me.

>  Brian Rodenborn

--
Don't go around saying the world owes you a living.  The world owes you
nothing.  It was here first.
                -- Mark Twain


Wed, 11 May 2005 01:34:36 GMT  
 getting timestamp from time()

Quote:

>> in comp.lang.c i read:

>> >>    I was looking for a function that will give me the unix timestamp
>> >> from time() ie what I get by `date +'%s'`

>> >Do you mean the thing that looks like this:

>> >Thu Nov 21 10:01:15 CST 2002

>> no.  seconds since an epoch.  the iso c solution requires that you use
>> difftime, mktime and time, and a presumption of how to select the epoch,
>> e.g.,

>Since you aren't the OP, I don't think you are qualified to answer the
>question.

Bad thinking.

Quote:
>If you look at what he asked, it looks a LOT like the UNIX
>shell date command. Plus, he mentions %s, which is normally for strings
>(assuming a printf() like function.

>None of that say the seconds since the epoch to me.

Let's see:

    fangorn:~/tmp 313> date +'%s'
    1037985024

Does the output of "date +'%s'" (the OP's command) look like seconds
since the epoch to you?

Dan
--
Dan Pop
DESY Zeuthen, RZ group



Wed, 11 May 2005 01:12:38 GMT  
 getting timestamp from time()

Quote:



> >> in comp.lang.c i read:

> >> >>    I was looking for a function that will give me the unix timestamp
> >> >> from time() ie what I get by `date +'%s'`

> >> >Do you mean the thing that looks like this:

> >> >Thu Nov 21 10:01:15 CST 2002

> >> no.  seconds since an epoch.  the iso c solution requires that you use
> >> difftime, mktime and time, and a presumption of how to select the epoch,
> >> e.g.,

> >Since you aren't the OP, I don't think you are qualified to answer the
> >question.

> Bad thinking.

> >If you look at what he asked, it looks a LOT like the UNIX
> >shell date command. Plus, he mentions %s, which is normally for strings
> >(assuming a printf() like function.

> >None of that say the seconds since the epoch to me.

> Let's see:

>     fangorn:~/tmp 313> date +'%s'
>     1037985024

> Does the output of "date +'%s'" (the OP's command) look like seconds
> since the epoch to you?

> Dan
> --
> Dan Pop
> DESY Zeuthen, RZ group


1037972217 at this time. :-)

--

"Everything should be made as simple as possible, but not simpler."
                    --- Albert Einstein ---



Wed, 11 May 2005 02:32:52 GMT  
 getting timestamp from time()

Quote:

> >Since you aren't the OP, I don't think you are qualified to answer the
> >question.

> Bad thinking.

Why is that?

Quote:
> >If you look at what he asked, it looks a LOT like the UNIX
> >shell date command. Plus, he mentions %s, which is normally for strings
> >(assuming a printf() like function.

> >None of that say the seconds since the epoch to me.

> Let's see:

>     fangorn:~/tmp 313> date +'%s'
>     1037985024

> Does the output of "date +'%s'" (the OP's command) look like seconds
> since the epoch to you?

Why did you drop the backticks around date?

Again, only the OP can really say what the OP had in mind.

Brian Rodenborn



Wed, 11 May 2005 03:02:36 GMT  
 getting timestamp from time()
Thanks all ,
 And I am sorry my post had not been very clear. I wanted only the
number of seconds from epoch ( I assume that is 1st Jan 1970 ) since
this is the output of date +'%s' on a bash shell prompt

And about the documentation on time_t  "man 2 time " gives documentation
about time command but does not specify what is time_t

Any way I did this for my requirement;

int now;
now = (int) time(NULL);
and this works fine

Quote:


> > >Since you aren't the OP, I don't think you are qualified to answer the
> > >question.

> > Bad thinking.

> Why is that?

> > >If you look at what he asked, it looks a LOT like the UNIX
> > >shell date command. Plus, he mentions %s, which is normally for strings
> > >(assuming a printf() like function.

> > >None of that say the seconds since the epoch to me.

> > Let's see:

> >     fangorn:~/tmp 313> date +'%s'
> >     1037985024

> > Does the output of "date +'%s'" (the OP's command) look like seconds
> > since the epoch to you?

> Why did you drop the backticks around date?

> Again, only the OP can really say what the OP had in mind.

> Brian Rodenborn

--
Ramprasad A Padmanabhan
Sr Software Engineer
Netcore Solns Pvt Ltd
Mumbai
ph - (022) 4628000


Wed, 11 May 2005 14:27:12 GMT  
 getting timestamp from time()
in comp.lang.c i read:

Quote:
>the documentation on time_t  "man 2 time " gives documentation
>about time command but does not specify what is time_t

time_t is an arithmetic type.  exactly which is left for the implementation
to choose.

Quote:
>Any way I did this for my requirement;

>int now;
>now = (int) time(NULL);
>and this works fine

but it's wrong.  time_t is a typedef of a real type, so use it:

  time_t now;
  now = time(NULL);

note: if your implementation also follows posix/sus then the value returned
will be what you want, the number of seconds since the epoch.  further
discussion of this aspect of time() should be held elsewhere as it's off-
topic here, e.g., perhaps comp.unix.programmer.

--
bringing you boring signatures for 17 years



Thu, 12 May 2005 03:23:35 GMT  
 
 [ 16 post ]  Go to page: [1] [2]

 Relevant Pages 

1. Getting Assembly compilation timestamp

2. Getting File Timestamp

3. How to maintain TimeStamp consistency across Time Zones?

4. Implications of date/timestamping Updates from different time zones

5. Creating Timestamp with Sub-second time

6. Creating Timestamp with Sub-second time

7. Getting time zone information?

8. getting time/date

9. getting and useing system time

10. Getting time, portability..

11. Getting raw numerical time as a string.

12. Getting time in millisecond resolution

 

 
Powered by phpBB® Forum Software