473,386 Members | 1,602 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,386 software developers and data experts.

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 3075
"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);

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;
}

--
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);

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;
}

--
Hope this helps,
Steven
Nov 7 '06 #6
2006-11-07 <11*********************@i42g2000cwa.googlegroups. com>,
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:
2006-11-07 <11*********************@i42g2000cwa.googlegroups. com>,
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:

Recipient address: <xxx>@<xxxx>.org.uk
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
In <11*********************@k70g2000cwa.googlegroups. com"moni" <mo*******@gmail.comwrites:
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:

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.

--
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
top-posting, read the following:

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:
2006-11-07 <1162926494.233238.42...@i42g2000cwa.googlegroups. com>,
<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:

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.
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.
--
Reclaim Your Inbox! http://www.mozilla.org/products/thunderbird
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: dan glenn | last post by:
Say, I want to set a cookie and have it expire an hour after it's set. It's looking like this is only possible for browsers which are in the same time zone as my server?? In other words, if I...
2
by: learning_C++ | last post by:
I programmed this code with a function "get_current_time" in the begining. When I compiled with the command g++ -Wall -g xxx.xpp -o xxx there are so many errors. please help me and thanks, ...
1
by: heirou | last post by:
I'm a novice in this subject....I've made a database that requires a time conversion. For example, if local time is 1200, determine the time in Korea. I use two fields: a date field, and a time...
6
by: DCSudolcan | last post by:
I know that a program can create and properly initialize an array of pointers to functions at build time, but can something like the following be done at build time? void foo(void); unsigned...
5
by: Paulers | last post by:
Hello, I'm working on an app that requires the functionality to convert the time in Austrailia to the time in New York (EST). I am wondering, what is the bestway to approach this in vb.net? Is...
3
by: Jason S | last post by:
is there any way to use templates to bind integer/floating point constants to a template for compile-time use? e.g. template <double conversion> class meters { const factor = conversion;
3
by: moni | last post by:
Hi, I wanted to convert a time value in the form of time_t into a readable form in C# or vice versa, in order to be able to subtract two time values and give the result in msecs. eg. I...
3
by: Evan Klitzke | last post by:
Although it is not present in ANSI C, the GNU version of stftime supports the conversion character %z, which is a time offset from GMT. The four digit time offset is required in RFC 2822...
5
by: fimarn | last post by:
I am trying to get rid of compile time error that I am getting only in RHEL5 (not in RHEL4) apparently due to the changes in the stl_list.h file. The error that I am getting is coming from the...
5
by: Grey Alien | last post by:
I need to convert timestamps that are given as the number of seconds that have elapsed since midnight UTC of January 1, 1970, (not counting leap seconds). It seems all of the std C functions...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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,...

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.