Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > A simple example calculating the number of seconds since 01/01/1970

Reply
Thread Tools

A simple example calculating the number of seconds since 01/01/1970

 
 
cyril
Guest
Posts: n/a
 
      08-22-2003
I saw that is a recurrent message but i couln't succed in founding the
solution ...

I'm developping under Linux

Does anyone have a code snippet that permit to get in a simple long
var the number of sec since 01/01/1970.... I come from the Java world
;o)

Thanks for all.

PS : the code I try give me a negative value :
tm is a tm struct
currenttime and t_70 are some time_t struct;

tm.tm_day = 1;
tm.tm_year = 1970;
t_70 = mktime(&tm); //normally initialized to 0

currenttime = time(NULL);

sec = difftime(t_70, currenttime);
 
Reply With Quote
 
 
 
 
Corne' Cornelius
Guest
Posts: n/a
 
      08-22-2003
tm.tm_year should be set to the number of years since 1900, not the
actual year you want.

so in effect, t_70 is after currenttime, which would give you a negative
value.

cyril wrote:
> I saw that is a recurrent message but i couln't succed in founding the
> solution ...
>
> I'm developping under Linux
>
> Does anyone have a code snippet that permit to get in a simple long
> var the number of sec since 01/01/1970.... I come from the Java world
> ;o)
>
> Thanks for all.
>
> PS : the code I try give me a negative value :
> tm is a tm struct
> currenttime and t_70 are some time_t struct;
>
> tm.tm_day = 1;
> tm.tm_year = 1970;
> t_70 = mktime(&tm); //normally initialized to 0
>
> currenttime = time(NULL);
>
> sec = difftime(t_70, currenttime);


 
Reply With Quote
 
 
 
 
Martijn
Guest
Posts: n/a
 
      08-23-2003
cyril wrote:
> Does anyone have a code snippet that permit to get in a simple long
> var the number of sec since 01/01/1970.... I come from the Java world
> ;o)


time_t t = time(NULL);

Depending on your implementation, time_t may be a typedef of long. If you
_really_ need a long, go cast it.

long l = (long)t;

Good luck,

--
Martijn
http://www.sereneconcepts.nl


 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      08-23-2003
Martijn wrote:

> cyril wrote:
>> Does anyone have a code snippet that permit to get in a simple long
>> var the number of sec since 01/01/1970.... I come from the Java world
>> ;o)

>
> time_t t = time(NULL);
>
> Depending on your implementation, time_t may be a typedef of long. If you
> _really_ need a long, go cast it.
>
> long l = (long)t;


Bad advice. Even if time_t is a long on his implementation, the resulting
code will not be portable. Indeed, it might not even work, since the
Standard does not require time_t to represent the number of seconds since
1/1/1970.

The OP would do better to build a struct tm like this:

struct tm firstjan1970 = {0};
firstjan1970.tm_year = 70;
firstjan1970.tm_mon = 0;
firstjan1970.tm_mday = 1;

and then call mktime to get a time_t.

The next steps are to get the current time using time(), and then call
difftime() to get the number of seconds difference as a double.


--
Richard Heathfield : http://www.velocityreviews.com/forums/(E-Mail Removed)
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
 
Reply With Quote
 
cyril
Guest
Posts: n/a
 
      08-25-2003
Thanks to all your advices, i succed in what I wanna do.

Thanks for all.

Regards, Cyril.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Obtaining number of seconds since Jan 1 1970. JC C++ 4 01-07-2009 09:27 PM
standard function for calculating the number of bits of a natural number? Frank Buss VHDL 3 07-31-2006 06:48 PM
1. Ruby result: 101 seconds , 2. Java result:9.8 seconds, 3. Perl result:62 seconds Michael Tan Ruby 32 07-21-2005 03:23 PM
Calculating number of seconds given two times Asad ASP .Net 2 04-27-2004 06:20 AM
Help! I am having problems with calculating the total seconds from two date/times. Tim ASP General 3 08-05-2003 07:45 PM



Advertisments