471,897 Members | 1,964 Online

# Time Conversion

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.

Nov 7 '06 #1
18 2962
"moni" <mo*******@gmail.comwrote:
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
Nov 7 '06 #2
Richard Bos said:

<snip>
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)
Nov 7 '06 #3

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 Heathfield wrote:
Richard Bos said:

<snip>
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)
Nov 7 '06 #4
moni wrote:
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);

result = mktime(&newtime);

If you want to throw in error checking, then:

if ( (result = mktime(newtime)) == (time_t)-1)
{
return EXIT_FAILURE;
}

--
Hope this helps,
Steven

Nov 7 '06 #5
hey...

Thanx alot...

that worked..!
at*************@gmail.com wrote:
moni wrote:
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);

result = mktime(&newtime);

If you want to throw in error checking, then:

if ( (result = mktime(newtime)) == (time_t)-1)
{
return EXIT_FAILURE;
}

--
Hope this helps,
Steven
Nov 7 '06 #6
moni wrote:
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.
Nov 7 '06 #7
Why is that?

i dint get it...

thanx..
Jordan Abel wrote:
moni wrote:
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.
Nov 7 '06 #8

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:

Reason: Illegal host/domain name found")

Asbjørn Sæbø
--
A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Nov 7 '06 #9
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.
Why is that?
i dint get it...
Because the definition of the tm_year field is "years since 1900", not
"years since 0".

--
John Gordon "... What with you being his parents and all,
go****@panix.com I think that you could be trusted not to shaft
him." -- Robert Chang, rec.games.board

Nov 7 '06 #10
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.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: er, none at the moment.
Nov 7 '06 #11
Richard Heathfield <in*****@invalid.invalidwrites:
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. :-(
OK, thanks for the information. I'll have to look around for a bit,
then.

Asbjørn
--
A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Nov 7 '06 #12
On Tue, 2006-11-07 at 22:23 +0100, AsbjÃ¸rn SÃ¦bÃ¸ wrote:
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:

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.

--
Andrew Poelstra <http://www.wpsoftware.net>
For email, use 'apoelstra' at the above site.
"You're only smart on the outside." -anon.

Nov 7 '06 #13
"moni" <mo*******@gmail.comwrites:
Why is that?

i dint get it...
You didn't get what? If you didn't get Jordan's advice to avoid

http://www.caliburn.nl/topposting.html
http://www.cpax.org.uk/prg/writings/topposting.php

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 7 '06 #14

On Nov 7, 5:53 pm, Asbjørn Sæbø <a...@stud.ntnu.nowrote:
Richard Heathfield <inva...@invalid.invalidwrites:
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. :-(
OK, thanks for the information. I'll have to look around for a bit,
then.
They still have it on Amazon if you have nothing against used books.

--
Ioan - Ciprian Tandau
tandau _at_ freeshell _dot_ org (hope it's not too late)
(... and that it still works...)

Nov 8 '06 #15
Jordan Abel wrote:
<snip>
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.
On Nov 7, 4:02 pm, "moni" <mons.2...@gmail.comwrote:
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.

--
Ioan - Ciprian Tandau
tandau _at_ freeshell _dot_ org (hope it's not too late)
(... and that it still works...)

Nov 8 '06 #16
Andrew Poelstra wrote:
On Tue, 2006-11-07 at 22:23 +0100, AsbjÃ¸rn SÃ¦bÃ¸ wrote:
>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:

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.
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.d...MEWA%3AIT&rd=1

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

Good luck.
--
Nov 8 '06 #17
Andrew Poelstra <ap*******@false.sitewrites:
On Tue, 2006-11-07 at 22:23 +0100, Asbjørn Sæbø wrote:
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
--
A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Nov 13 '06 #18
On 7 Nov 2006 10:35:37 -0800, "moni" <mo*******@gmail.comwrote:
>
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.has 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 [u]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
Nov 20 '06 #19

### This discussion thread is closed

Replies have been disabled for this discussion.