Time Conversion

M

moni

I have 2 time values:


System time and an input from the user.


1) System time is in the form of seconds from 1/1/1970 calculated by
using


time_t secs;
SYSTEMTIME stime;


time(&secs);


2) The input from the user is in form hr:min:sec which is a string
value.


But the seperate values have been obtained by using


sscanf(storedTimeValue, "%d:%d:%d", &hour,&minutes,&seconds);


So now I have 3 integer values for hour,minutes and seconds.


3) I have a function which calculates the time difference between 2
time values,
but both these time values need to be seconds from 1/1/1970.


Is there any way by which,I could convert the user given time into time

in seconds from 1/1/1970,
so that I can get the difference between the system time and the user
given time.
 
R

Richard Bos

moni said:
I have 2 time values: System time and an input from the user.
1) System time is in the form of seconds from 1/1/1970

ISO C doesn't guarantee that. Nor does it give you any way to do maths
on time_t. But all is not lost...
time_t secs;
SYSTEMTIME stime;

This type doesn't exist in C. It's also not necessary.
time(&secs);
2) The input from the user is in form hr:min:sec which is a string
value. But the seperate values have been obtained by using
sscanf(storedTimeValue, "%d:%d:%d", &hour,&minutes,&seconds);
So now I have 3 integer values for hour,minutes and seconds.

Good. Note that you'll still need the relevant date, but that can be had
from the time_t you got from time().
3) I have a function which calculates the time difference between 2
time values, but both these time values need to be seconds from 1/1/1970.

Then you have the wrong function. The right function to calculate the
difference between two time_t's is in <time.h>, and it's called
difftime(). This must work regardless of the format of a time_t, and is
therefore superior to a home-made function which assumes an epoch of
1970/01/01 and a resolution of 1 second.
Is there any way by which,I could convert the user given time into time
in seconds from 1/1/1970,

No. However, there _is_ a way to convert it into a time_t. It's a bit
roundabout, but it does work anywhere.
First you convert your time_t into a struct tm, using either gmtime() or
localtime(). Next you set the tm_hour, tm_min and tm_sec members of this
struct tm to the values you got from sscanf(). Then you convert this
struct tm back to a second time_t, using mktime(). Finally you subtract
both time_t's using difftime(), and Bob's your uncle. All of these
functions are ISO Standard C, and all (except sscanf(), obviously) can
be found in <time.h>.

Richard
 
M

moni

struct tm *newtime, *oldtime;

time_t result;
time_t long_time;
double elapsed_time;


sscanf(stringtoconvert, "%d:%d:%d", &hour,&minutes,&seconds);


time( &long_time ); /* Get time as long integer. */

newtime = localtime( &long_time ); /* Convert to local time. */
oldtime = localtime( &long_time );




newtime->tm_isdst = 0;
newtime->tm_hour = hour;
newtime->tm_min = minutes;
newtime->tm_sec = seconds;
//newtime->tm_year = 2006;

result = mktime(&newtime);



elapsed_time = difftime( result, long_time );
printf("time is %d, %d", result, long_time);



Here the result always come s to -1, ie. mktime is always returning -1.

Can you tell me the reason?

Thanx alot..

Richard said:
Richard Bos said:

ISO C doesn't [...] give you any way to do maths on time_t.

...except for difftime.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
 
A

attn.steven.kuo

moni said:
struct tm *newtime, *oldtime;

time_t result;
time_t long_time;
double elapsed_time;


sscanf(stringtoconvert, "%d:%d:%d", &hour,&minutes,&seconds);


time( &long_time ); /* Get time as long integer. */

newtime = localtime( &long_time ); /* Convert to local time. */
oldtime = localtime( &long_time );




newtime->tm_isdst = 0;
newtime->tm_hour = hour;
newtime->tm_min = minutes;
newtime->tm_sec = seconds;
//newtime->tm_year = 2006;

result = mktime(&newtime);



elapsed_time = difftime( result, long_time );
printf("time is %d, %d", result, long_time);



Here the result always come s to -1, ie. mktime is always returning -1.

Can you tell me the reason?


The argument to mktime is a pointer to struct tm:

So try:

result = mktime(newtime);

instead of

result = mktime(&newtime);

If you want to throw in error checking, then:

if ( (result = mktime(newtime)) == (time_t)-1)
{
fprintf(stderr, "Bad mktime\n");
return EXIT_FAILURE;
}
 
M

moni

hey...

Thanx alot...

that worked..!


The argument to mktime is a pointer to struct tm:

So try:

result = mktime(newtime);

instead of

result = mktime(&newtime);

If you want to throw in error checking, then:

if ( (result = mktime(newtime)) == (time_t)-1)
{
fprintf(stderr, "Bad mktime\n");
return EXIT_FAILURE;
}
 
J

Jordan Abel

2006-11-07 said:
hey...

Thanx alot...

that worked..!

Top-posting isn't good.

Anyway - you should be aware a tm_year value of 2006 refers to the year
3906. The proper tm_year value for this year is 106.
 
M

moni

Why is that?

i dint get it...

thanx..


Jordan said:
Top-posting isn't good.

Anyway - you should be aware a tm_year value of 2006 refers to the year
3906. The proper tm_year value for this year is 106.
 
?

=?iso-8859-1?q?Asbj=F8rn_S=E6b=F8?=

The book "C Unleashed" seems to be out of print and somewhat hard to
come by. Can anybody (Mr. Heathfield, for example) tell me whether
there are any plans for a reprint or a new edition?

(Sorry for the somewhat off-topic post. I tried to contact
Mr. Heathfield at the email address he specifies at the bottom of his
posts, but it came back with a message saying:
"Your message cannot be delivered to the following recipients:

Recipient address: <xxx>@<xxxx>.org.uk
Reason: Illegal host/domain name found")

Asbjørn Sæbø
 
R

Richard Heathfield

Asbj?rn Sæb? said:
The book "C Unleashed" seems to be out of print and somewhat hard to
come by.

Yes, I'm afraid that's true. "C Unleashed" went out of print earlier this
year IIRC, and as far as I know there are no plans for a re-print or a new
edition. :-(
(Sorry for the somewhat off-topic post. I tried to contact
Mr. Heathfield at the email address he specifies at the bottom of his
posts, but it came back with a message saying:
"Your message cannot be delivered to the following recipients:

Mea culpa. The mail forwarding is broken at present, and I haven't got
around to contacting the forwarder to find out why. I'll try to remember to
do that tomorrow.
 
A

Andrew Poelstra

The book "C Unleashed" seems to be out of print and somewhat hard to
come by. Can anybody (Mr. Heathfield, for example) tell me whether
there are any plans for a reprint or a new edition?

(Sorry for the somewhat off-topic post. I tried to contact
Mr. Heathfield at the email address he specifies at the bottom of his
posts, but it came back with a message saying:
"Your message cannot be delivered to the following recipients:

Recipient address: <xxx>@<xxxx>.org.uk
Reason: Illegal host/domain name found")

I got my copy at Half Price Computer Books. I had requested it
from my library, but they denied it on the grounds that it was
"older" and "high priced".

You may find that contacting Sams Publishing will get you a
better answer than this group will.
 
?

=?iso-8859-1?q?Asbj=F8rn_S=E6b=F8?=

Richard Heathfield said:
Asbj?rn Sæb? said:


Yes, I'm afraid that's true. "C Unleashed" went out of print earlier this
year IIRC, and as far as I know there are no plans for a re-print or a new
edition. :-(

OK, thanks for the information. I'll have to look around for a bit,
then.

Asbjørn
 
N

Nelu

Why is that?

i dint get it...

thanx..

Please don't top post. I put some of Jordan's quote above your so it
makes sense.
tm_year is the number of years since 1900. If you set tm_year to 2006,
the year will be 2006+1900=3906.
 
J

Jonathan Pritchard

Andrew said:
I got my copy at Half Price Computer Books. I had requested it
from my library, but they denied it on the grounds that it was
"older" and "high priced".

You may find that contacting Sams Publishing will get you a
better answer than this group will.

Hi there, after a sale went through I have been scouring the internet
for these books. I have found them on ebay again though, brand new. You
might want to hurry as I think time is running out on this one.

http://cgi.ebay.co.uk/ws/eBayISAPI....50047652336&rd=1&sspagename=STRK:MEWA:IT&rd=1

Pretty darn cheap too, don't touch Amazon marketplace, they know this
book is rare so they overcharge.

Good luck.
 
?

=?iso-8859-1?q?Asbj=F8rn_S=E6b=F8?=

Andrew Poelstra said:
The book "C Unleashed" seems to be out of print and somewhat hard to
come by. [...]
I got my copy at Half Price Computer Books. [...]

I think I have managed to persuade Half Price Computer Books to sell
me a couple (one for my private bookshelf, one for work) now.
Shipping out of the US was not an option ("too large for an
"international priority envelope"), but fortunately I have relatives
over there.

Asbjørn
 
D

Dave Thompson

struct tm *newtime, *oldtime;

time_t result;
time_t long_time;
double elapsed_time;

sscanf(stringtoconvert, "%d:%d:%d", &hour,&minutes,&seconds);

time( &long_time ); /* Get time as long integer. */
Note that time_t must be _some_ arithmetic type, and is usually an
integer (and in Unix/POSIX is required to be), but it isn't required
to be 'long' specifically, so this name and comment could be wrong.
newtime = localtime( &long_time ); /* Convert to local time. */
oldtime = localtime( &long_time );
Warning: localtime() and also gmtime() returns (if no error) a pointer
that is permitted to be and usually is to 'static' (shared) memory.
Changing any one of these 'values', as you do just below, affects the
others. In your code you don't actually use oldtime for anything so
this error is masked, but if you want to actually use it you need to
allocate an actual struct tm, not just a pointer, and _copy_ to it.
newtime->tm_isdst = 0;

Do you really want to force DST off? Even if the current day is during
a DST/SummerTime period in a zone that uses it? If you are worried
about times on a transition day, you can set tm_isdst to -1 to tell
the library to figure out (from scratch) whether the (modified)
broken-down time you give it is during DST or not. If it is during
both and thus is ambiguous, e.g. 01:30 on the 'fall back' day under US
rules, you will lose and get the -1 return. But from the very limited
requirements statement you have given us so far, it's not at all clear
what if anything is the desired answer in that case.
newtime->tm_hour = hour;
newtime->tm_min = minutes;
newtime->tm_sec = seconds;
//newtime->tm_year = 2006;

result = mktime(&newtime);
This is wrong. You are passing the address of the pointer, not the
value of the pointer = the address of the struct. If you had
#include'd <time.h> as you must to make your program valid, your
compiler would (must) have detected this mismatch and given you a
'diagnostic' (in usual terms, an error or warning message).

If you do make newtime an actual struct tm, as I discussed above,
_then_ this would be right.
printf("time is %d, %d", result, long_time);

If time_t is indeed long, that is not the correct format specifier for
printf, you should use %ld. If you don't know whether it is or not,
and in portable code you cannot easily know, you can cast to and
format as the largest possible type; in C89 this is long or unsigned
long using %ld or %lu, in C99 intmax_t using %jd or %ju. Fully
conforming C99 implementations are still fairly rare; ones that
implement at least fairly basic/easy features including this are
becoming more common but still not ubiquitous.
Here the result always come s to -1, ie. mktime is always returning -1.

Can you tell me the reason?
See above.

- David.Thompson1 at worldnet.att.net
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top