473,756 Members | 3,111 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(storedTi meValue, "%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 3118
"moni" <mo*******@gmai l.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(storedTi meValue, "%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(stringto convert, "%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(stringto convert, "%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(stringto convert, "%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************ *********@i42g2 000cwa.googlegr oups.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************ *********@i42g2 000cwa.googlegr oups.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>.or g.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************ *********@k70g2 000cwa.googlegr oups.com"moni" <mo*******@gmai l.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.co m I think that you could be trusted not to shaft
him." -- Robert Chang, rec.games.board

Nov 7 '06 #10

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

Similar topics

4
11716
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 set my cookie with: setcookie('CookieName', $SomeValue, time()+3600, "/");
2
2813
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, #include <map> #include <iostream> #include <iomanip> #include <string> #include <time.h>
1
11685
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 field. I need the converted time to show up in a report. I also need the corresponding date to increment if necessary. Any ideas on how to do this would be greatly appreciated.
6
2069
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 char myArray={ (unsigned char) (foo&0xFF), (unsigned char) ((foo&0xFF00)>>8), (unsigned char) ((foo&0xFF0000)>>16),
5
4676
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 there anything in VB.net that can make this conversion? If not, is it possible to hit a time server of some sort to get the conversion? I know there are time conversion websites all over the place Im just wondering if any of them allow an...
3
2889
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
2758
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 have a time value,
3
2476
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 dates/times, and is used by a number of other programs as well. I need to convert times that use this convention to python time representations, and because Python does not support the %z time conversion character I cannot simply use the time.strptime...
5
3175
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 following code that attempts to remove an item from the list: class shm_objptr_list : public std::list < void*, SharedMemAlloc<void * > {
5
8522
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 expect positive offsets from this date and are incapable of working on dates preceeding the epoch (i.e. negative offsets) - which IMHO shows a remarkable lack of foresight - and is *just* a little bit annoying. Does anyone know of an algo I can...
0
9152
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9716
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9571
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8569
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6410
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4996
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5180
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3676
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3185
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.