473,396 Members | 1,998 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

How to compute time into integer?

Hi All,

I need to compute the current time value in seconds: the current time
is 12/16/2003, and it's time value in integer should around 1071560616
seconds since 1/1/1970. It is easy to get such a value in Tcl, but i
noticed it is uneasy to do in C. Well, ANSI provides functions such as
time(), asctime(), localtime(), gmtime(), ctime() in time.h file. But,
I can't convert the returns into integer in seconds.

I did think of the way as computing by:

year*365 + month * 12 + day *24 + .....

with the values derived from struct tm members. but I can't handle
the leap year and leap month well. Anybody can help? Thanks!
Huey
Nov 14 '05 #1
13 20604

"Huey" <hu********@yahoo.com> schrieb im Newsbeitrag
news:ae**************************@posting.google.c om...
Hi All,

I need to compute the current time value in seconds: the current time
is 12/16/2003, and it's time value in integer should around 1071560616
seconds since 1/1/1970. It is easy to get such a value in Tcl, but i
noticed it is uneasy to do in C. Well, ANSI provides functions such as
time(), asctime(), localtime(), gmtime(), ctime() in time.h file. But,
I can't convert the returns into integer in seconds.

I did think of the way as computing by:

year*365 + month * 12 + day *24 + .....


Assuming your input is in the format mm/dd/yyyy:
Checks, necessary headers and calling function omitted, may/must be expanded
eventually to handle hours, minutes and seconds as well as daylight saving.
I suggest you read the description of a struct tm carefully :)

time_t StringToTimestamp(const char *time_string)
{
struct tm time_in = {0};
char *tmp = NULL;

time_in.tm_year = strtoul(time_string + 6, &tmp, 10) - 1900;
time_in.tm_mon = strtoul(time_string, &tmp, 10) - 1;
time_in.tm_mday = strtoul(time_string + 3, &tmp, 10);
/* Convert the date in seconds (since 1/1/1970) */
return mktime(&time_in);
} /* StringToTimestamp */

HTH
Robert
Nov 14 '05 #2
nrk
Huey wrote:
Hi All,

I need to compute the current time value in seconds: the current time
is 12/16/2003, and it's time value in integer should around 1071560616
seconds since 1/1/1970. It is easy to get such a value in Tcl, but i
noticed it is uneasy to do in C. Well, ANSI provides functions such as
time(), asctime(), localtime(), gmtime(), ctime() in time.h file. But,
I can't convert the returns into integer in seconds.

I did think of the way as computing by:

year*365 + month * 12 + day *24 + .....

with the values derived from struct tm members. but I can't handle
the leap year and leap month well. Anybody can help? Thanks!
Huey


Here's one way:

#include <stdio.h>
#include <time.h>
#include <stdlib.h>

int main(void) {
struct tm epoch_strt;
time_t basetime;
time_t curtime;
double nsecs;

epoch_strt.tm_sec = 0;
epoch_strt.tm_min = 0;
epoch_strt.tm_hour = 0;
epoch_strt.tm_mday = 1;
epoch_strt.tm_mon = 1;
epoch_strt.tm_year = 70;
epoch_strt.tm_isdst = -1;

basetime = mktime(&epoch_strt);
if ( basetime == (time_t)-1 ) {
fprintf(stderr, "Cannot convert time\n");
exit(EXIT_FAILURE);
}

curtime = time(NULL);
if ( curtime == (time_t)-1 ) {
fprintf(stderr, "Cannot get current time\n");
exit(EXIT_FAILURE);
}

nsecs = difftime(curtime, basetime);

printf("Seconds since start of epoch: %.0f\n", nsecs);

return 0;
}

-nrk.
Nov 14 '05 #3
nrk
Robert Stankowic wrote:

"Huey" <hu********@yahoo.com> schrieb im Newsbeitrag
news:ae**************************@posting.google.c om...
Hi All,

I need to compute the current time value in seconds: the current time
is 12/16/2003, and it's time value in integer should around 1071560616
seconds since 1/1/1970. It is easy to get such a value in Tcl, but i
noticed it is uneasy to do in C. Well, ANSI provides functions such as
time(), asctime(), localtime(), gmtime(), ctime() in time.h file. But,
I can't convert the returns into integer in seconds.

I did think of the way as computing by:

year*365 + month * 12 + day *24 + .....


Assuming your input is in the format mm/dd/yyyy:
Checks, necessary headers and calling function omitted, may/must be
expanded eventually to handle hours, minutes and seconds as well as
daylight saving. I suggest you read the description of a struct tm
carefully :)

time_t StringToTimestamp(const char *time_string)
{
struct tm time_in = {0};
char *tmp = NULL;

time_in.tm_year = strtoul(time_string + 6, &tmp, 10) - 1900;
time_in.tm_mon = strtoul(time_string, &tmp, 10) - 1;
time_in.tm_mday = strtoul(time_string + 3, &tmp, 10);
/* Convert the date in seconds (since 1/1/1970) */
return mktime(&time_in);
} /* StringToTimestamp */

HTH
Robert


Is the return of mktime guaranteed to be seconds since epoch?

-nrk.
Nov 14 '05 #4
nrk <ra*********@deadbeef.verizon.net> writes:
Is the return of mktime guaranteed to be seconds since epoch?


No. 7.23.1#4: "The range and precision of times representable in clock_t
and time_t are implementation-defined. [...]"

<OT>
Even on systems where time_t does represent the seconds since epoch, it is
not guaranteed to take leap seconds into account (which may or may not be
a problem).
</OT>

Martin
Nov 14 '05 #5

"nrk" <ra*********@deadbeef.verizon.net> schrieb im Newsbeitrag
news:lL****************@nwrddc01.gnilink.net...
Robert Stankowic wrote:

"Huey" <hu********@yahoo.com> schrieb im Newsbeitrag
news:ae**************************@posting.google.c om...

[....]

Is the return of mktime guaranteed to be seconds since epoch?


No. Time for coffee and reading the standard again.. :)
Thank you for pointing that out.
cheers
Robert
Nov 14 '05 #6
Huey wrote:

I need to compute the current time value in seconds: the current
time is 12/16/2003, and it's time value in integer should around
1071560616 seconds since 1/1/1970. It is easy to get such a value
in Tcl, but i noticed it is uneasy to do in C. Well, ANSI provides
functions such as time(), asctime(), localtime(), gmtime(),
ctime() in time.h file. But, I can't convert the returns into
integer in seconds.

I did think of the way as computing by:

year*365 + month * 12 + day *24 + .....

with the values derived from struct tm members. but I can't
handle the leap year and leap month well.


Ignoring leapseconds, and restricting the range to 1901 .. 2099,
start by considering quadyears of 1461 days, deltayears in the
range 0..3, followed by months, days, hours, etc. You will need
two tables of monthlength versus month.

It may be convenient to do as did the Romans, and consider the
year to begin in March.

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!

Nov 14 '05 #7
hu********@yahoo.com (Huey) writes:
I need to compute the current time value in seconds: the current time
is 12/16/2003, and it's time value in integer should around 1071560616
seconds since 1/1/1970. It is easy to get such a value in Tcl, but i
noticed it is uneasy to do in C. Well, ANSI provides functions such as
time(), asctime(), localtime(), gmtime(), ctime() in time.h file. But,
I can't convert the returns into integer in seconds.


Commonly, time_t is a count of seconds from 1970. You can't
depend on this portably, but it's true on many systems. This is
probably what Tcl is using.
--
int main(void){char p[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuv wxyz.\
\n",*q="kl BIcNBFr.NKEzjwCIxNJC";int i=sizeof p/2;char *strchr();int putchar(\
);while(*q){i+=strchr(p,*q++)-p;if(i>=(int)sizeof p)i-=sizeof p-1;putchar(p[i]\
);}return 0;}
Nov 14 '05 #8

"nrk" <ra*********@deadbeef.verizon.net> wrote in message
news:hI***************@nwrddc01.gnilink.net...
Huey wrote:
Hi All,

I need to compute the current time value in seconds: the current time
is 12/16/2003, and it's time value in integer should around 1071560616
seconds since 1/1/1970. It is easy to get such a value in Tcl, but i
noticed it is uneasy to do in C. Well, ANSI provides functions such as
time(), asctime(), localtime(), gmtime(), ctime() in time.h file. But,
I can't convert the returns into integer in seconds.

I did think of the way as computing by:

year*365 + month * 12 + day *24 + .....

with the values derived from struct tm members. but I can't handle
the leap year and leap month well. Anybody can help? Thanks!
Huey


Here's one way:

#include <stdio.h>
#include <time.h>
#include <stdlib.h>

int main(void) {
struct tm epoch_strt;
time_t basetime;
time_t curtime;
double nsecs;

epoch_strt.tm_sec = 0;
epoch_strt.tm_min = 0;
epoch_strt.tm_hour = 0;
epoch_strt.tm_mday = 1;
epoch_strt.tm_mon = 1;
epoch_strt.tm_year = 70;
epoch_strt.tm_isdst = -1;

basetime = mktime(&epoch_strt);
if ( basetime == (time_t)-1 ) {
fprintf(stderr, "Cannot convert time\n");
exit(EXIT_FAILURE);
}

curtime = time(NULL);
if ( curtime == (time_t)-1 ) {
fprintf(stderr, "Cannot get current time\n");
exit(EXIT_FAILURE);
}

nsecs = difftime(curtime, basetime);

printf("Seconds since start of epoch: %.0f\n", nsecs);

return 0;
}

-nrk.

almost
shouldn't it be?
epoch_strt.tm_mon = 0;
Nov 14 '05 #9
nrk
Peter Slootweg wrote:

"nrk" <ra*********@deadbeef.verizon.net> wrote in message
news:hI***************@nwrddc01.gnilink.net...
Huey wrote:
> Hi All,
>
> I need to compute the current time value in seconds: the current time
> is 12/16/2003, and it's time value in integer should around 1071560616
> seconds since 1/1/1970. It is easy to get such a value in Tcl, but i
> noticed it is uneasy to do in C. Well, ANSI provides functions such as
> time(), asctime(), localtime(), gmtime(), ctime() in time.h file. But,
> I can't convert the returns into integer in seconds.
>
> I did think of the way as computing by:
>
> year*365 + month * 12 + day *24 + .....
>
> with the values derived from struct tm members. but I can't handle
> the leap year and leap month well. Anybody can help? Thanks!
>
>
> Huey


Here's one way:

#include <stdio.h>
#include <time.h>
#include <stdlib.h>

int main(void) {
struct tm epoch_strt;
time_t basetime;
time_t curtime;
double nsecs;

epoch_strt.tm_sec = 0;
epoch_strt.tm_min = 0;
epoch_strt.tm_hour = 0;
epoch_strt.tm_mday = 1;
epoch_strt.tm_mon = 1;
epoch_strt.tm_year = 70;
epoch_strt.tm_isdst = -1;

basetime = mktime(&epoch_strt);
if ( basetime == (time_t)-1 ) {
fprintf(stderr, "Cannot convert time\n");
exit(EXIT_FAILURE);
}

curtime = time(NULL);
if ( curtime == (time_t)-1 ) {
fprintf(stderr, "Cannot get current time\n");
exit(EXIT_FAILURE);
}

nsecs = difftime(curtime, basetime);

printf("Seconds since start of epoch: %.0f\n", nsecs);

return 0;
}

-nrk.

almost
shouldn't it be?
epoch_strt.tm_mon = 0;


Absolutely. That's embarrassing. Thanks for catching it.

-nrk.
--
Remove devnull for email

Nov 14 '05 #10
On 16 Dec 2003 09:25:07 -0800, Ben Pfaff <bl*@cs.stanford.edu> wrote:
hu********@yahoo.com (Huey) writes:
I need to compute the current time value in seconds: the current time
is 12/16/2003, and it's time value in integer should around 1071560616
seconds since 1/1/1970. It is easy to get such a value in Tcl, but i
noticed it is uneasy to do in C. Well, ANSI provides functions such as
time(), asctime(), localtime(), gmtime(), ctime() in time.h file. But,
I can't convert the returns into integer in seconds.


Commonly, time_t is a count of seconds from 1970. You can't
depend on this portably, but it's true on many systems. This is
probably what Tcl is using.


Note that mktime() converts *local* time in struct tm (including DST
if applicable) called "broken down", to *UTC* time in time_t, which is
(as you say) almost always seconds from 1970-01-01midnight though that
is not actually required by the standard. (UTC is often referred to as
GMT, for example in the name of gmtime(), although technically that is
very slightly different.)

If this distinction matters to you, you may be able to set the (local)
timezone to UTC before using the conversion, often just by setting an
environment variable, although this is not addressed by the C standard
at all; if not, there is no standard mkgmtime() and you have to do the
calculation yourself along the lines stated by CBFalconer.

- David.Thompson1 at worldnet.att.net
Nov 14 '05 #11
Dave Thompson <da*************@worldnet.att.net> writes:
[...]
Note that mktime() converts *local* time in struct tm (including DST
if applicable) called "broken down", to *UTC* time in time_t, which is
(as you say) almost always seconds from 1970-01-01midnight though that
is not actually required by the standard. (UTC is often referred to as
GMT, for example in the name of gmtime(), although technically that is
very slightly different.)


Strictly speaking, a time_t value is neither local time nor UTC.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://www.sdsc.edu/~kst>
Schroedinger does Shakespeare: "To be *and* not to be"
(Note new e-mail address)
Nov 14 '05 #12
On Fri, 19 Dec 2003 09:31:03 GMT, Keith Thompson <ks***@mib.org>
wrote:
Dave Thompson <da*************@worldnet.att.net> writes:
[...]
Note that mktime() converts *local* time in struct tm (including DST
if applicable) called "broken down", to *UTC* time in time_t, which is
(as you say) almost always seconds from 1970-01-01midnight though that
is not actually required by the standard. (UTC is often referred to as
GMT, for example in the name of gmtime(), although technically that is
very slightly different.)


Strictly speaking, a time_t value is neither local time nor UTC.


You're right. It is UTC in Unix/POSIX (actually not-quite-UTC, but
let's not go there), and I think it must effectively be so in any
implementation with the switch-timezone-setting usage I was preparing
to talk about (and too focussed on), but is not required in general.

- David.Thompson1 at worldnet.att.net
Nov 14 '05 #13
Dave Thompson <da*************@worldnet.att.net> writes:
On Fri, 19 Dec 2003 09:31:03 GMT, Keith Thompson <ks***@mib.org>
wrote:
Dave Thompson <da*************@worldnet.att.net> writes:
[...]
Note that mktime() converts *local* time in struct tm (including DST
if applicable) called "broken down", to *UTC* time in time_t, which is
(as you say) almost always seconds from 1970-01-01midnight though that
is not actually required by the standard. (UTC is often referred to as
GMT, for example in the name of gmtime(), although technically that is
very slightly different.)


Strictly speaking, a time_t value is neither local time nor UTC.


You're right. It is UTC in Unix/POSIX (actually not-quite-UTC, but
let's not go there), and I think it must effectively be so in any
implementation with the switch-timezone-setting usage I was preparing
to talk about (and too focussed on), but is not required in general.


Well, sort of. In Unix/POSIX (and ignoring leap seconds and the
distinction between UTC and GMT), a time_t value has no inherent time
zone. It's a count of seconds since an arbitrary time in the past.
It happens to be defined as 1970-01-01 00:00:00 UTC, but it could as
validly be defined as 1969-12-31 16:00:00 PST (though using UTC/GMT
makes the math a bit easier). That was the point I was trying to
make.

If you just look at the C standard, of course, (almost) all bets are
off.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://www.sdsc.edu/~kst>
Schroedinger does Shakespeare: "To be *and* not to be"
(Note new e-mail address)
Nov 14 '05 #14

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

Similar topics

1
by: pentium77 | last post by:
Hi, Just wondering what's the algo used to compute hashcode in Java ? Does anybody here know ? Thanks, -MK.
1
by: skirkby | last post by:
This will be obvious to some - but not me I'm afraid... I am using an SQL data link from my ASP application to a SPROC - this all works fine on standard SELECT statements and JOIN in to a...
2
by: Maciej Bliziński | last post by:
Hello, I started to write the query that should compute the median. Surprisingly, I get following error message: "server closed the connection unexpectedly This probably means the server...
1
by: M P | last post by:
Hi! How can I compute time between two numbers? Time A: 16-Apr-2006 08:00:00 Time B: 17-Apr-2006 16:00:10 Answer: Days, Time, Sec (1 day, 8 hours, 0 mins, 10 secs)
6
by: Spoon | last post by:
Hello, Consider: #define BUFFER_SIZE 1234 /* or some other value */ uint8_t buffer; int do_stuff(uint8_t *buf); where do_stuff() does something with each octet in the buffer.
27
by: csledge | last post by:
Hi, I am trying to compute a 64 bit result from 2 32 bit registers, How do I get the carry into the higher word ? Also is %lld correct ? #include<stdio.h> long long int64( long x, int y);...
2
by: milirica | last post by:
I have a code, where I should compute the App.Compute Time, for n=1,5,10,20,30,40,50.Is there any GOOD EXPERT that can make this clear to me ? If yes than PLease write here The code is: #include...
4
by: Daikide | last post by:
Hey, noob here... (I'm only 13, so be cool :) I need to compute the time value in seconds: the time will be variable, but the format will be like this--> 2007.08.24 13:38:39 I need TO TURN...
0
by: ccarter45 | last post by:
I need to compute the differences in color between 2 pictures. Also, I need to write a method that computes a useful difference color of two colors (where the colors and their difference will be...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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 project—planning, coding, testing,...

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.