469,898 Members | 1,471 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,898 developers. It's quick & easy.

covert time from date Hour min sec format to epoch time i.e time since 1 jan 1970 in C

HI I have to convert a time in the format

i have
year
month
day
hour
min and seconds

I need to convert this into time in seconds since 1 jan 1970 i.e the
epoch time

do some modifications i.e add some time in seconds and then bring back
to the origi8nal format i.e year month day and hour min seconds

I could see some functions in time.h but dont know how exactly to use
them as time structure tm has some more fields which i am not sure are
mandatoryor not.

If you can suggest some way to do this it will be great

Regards
sumeet

Jun 6 '06 #1
5 2650
Summu82 said:
HI I have to convert a time in the format

i have
year
month
day
hour
min and seconds

I need to convert this into time in seconds since 1 jan 1970 i.e the
epoch time
Actually, you probably don't need to do this, and it's just as well, since C
doesn't guarantee that 1/1/1970 is the epoch. Nor does it guarantee a
resolution in seconds (but see below).
do some modifications i.e add some time in seconds and then bring back
to the origi8nal format i.e year month day and hour min seconds


Pick up your K&R (2nd edition) and turn to p255.

Instantiate a struct tm object like this:

struct tm foo = {0};

to ensure that any unused objects are properly initialised. [F/X - nurses
bite wound.]

Now populate as many of the fields of the struct tm as you can, and
particularly tm_sec and tm_min (0-59), tm_hour (0-23), tm_mday (1-31),
tm_mon (0-11 - it's an offset from January), and tm_year (years since 1900,
so if you mean 2006, you put 106 here).

That's all you /have/ to fill in.

Now add your time-in-seconds to tm_sec. Yes, I know - this will give you a
ludicrous value. It doesn't matter.

Now do this:

mktime(&foo);

and check out foo's fields - you will discover that everything is all
magically sorted out for you. Isn't C marvellous?

--
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)
Jun 6 '06 #2
Thanks Richard for the update

I tried to do the folowing coding but am Getting Bus Error

Can You send me the code with actual syntax
as i am not vey good with structures and pointers
see my code below if you can find some error

Thanks & Regards
Sumeet

#include <stdio.h>
#include <time.h>
int main()
{
int
tm_sec1,tm_min1,tm_hour1,tm_mday1,tm_mon1,tm_year1 ,tm_wday1,tm_yday1,tm_isdst1;
int sec1,min1,hour1,day1,month1,year1;
char timestr[40];
char tstring[40];
struct tm *tmp,*tmp2,*tmp3;
time_t t,t2,t3;
// code to see the actual values in a time structure works fine
t = time(NULL);
tmp = localtime(&t); /* or gmtime, if you want GMT^H^H^HUTC */

tm_sec1=(tmp->tm_sec);
tm_min1=(tmp->tm_min);
tm_hour1=(tmp->tm_hour);
tm_mday1=(tmp->tm_mday);
tm_mon1=(tmp->tm_mon);
tm_year1=(tmp->tm_year);
tm_wday1=(tmp->tm_wday);
tm_yday1=(tmp->tm_yday);
tm_isdst1=(tmp->tm_isdst);
printf("value of tm_sec1 is %d\n",tm_sec1);
printf("value of tm_min1 is %d\n",tm_min1);
printf("value of tm_hour1 is %d\n",tm_hour1);
printf("value of tm_mday1 is %d\n",tm_mday1);
printf("value of tm_mon1 is %d\n",tm_mon1);
printf("value of tm_year1 is %d\n",tm_year1);
printf("value of tm_wday1 is %d\n",tm_wday1);
printf("value of tm_yday1 is %d\n",tm_yday1);
printf("value of tm_isdst1 is %d\n",tm_isdst1);

// code to see the addition of time in seconds
// giving bus error dont know how to debug
tmp2->tm_sec=20+3037734;
tmp2->tm_min=20;
tmp2->tm_hour=10;
tmp2->tm_mday=5;
tmp2->tm_mon=5;
tmp2->tm_year=106;
//tmp2->tm_wday=0;
//tmp2->tm_yday=0;
//tmp2->tm_isdst=-1;

t3=mktime(tmp2);

tmp3 = localtime(&t3); /* or gmtime, if you want GMT^H^H^HUTC */
tm_sec1=(tmp3->tm_sec);
tm_min1=(tmp3->tm_min);
tm_hour1=(tmp3->tm_hour);
tm_mday1=(tmp3->tm_mday);
tm_mon1=(tmp3->tm_mon);
tm_year1=(tmp3->tm_year);
tm_wday1=(tmp3->tm_wday);
tm_yday1=(tmp3->tm_yday);
tm_isdst1=(tmp3->tm_isdst);
printf("value of tm_sec1 is %d\n",tm_sec1);
printf("value of tm_min1 is %d\n",tm_min1);
printf("value of tm_hour1 is %d\n",tm_hour1);
printf("value of tm_mday1 is %d\n",tm_mday1);
printf("value of tm_mon1 is %d\n",tm_mon1);
printf("value of tm_year1 is %d\n",tm_year1);
printf("value of tm_wday1 is %d\n",tm_wday1);
printf("value of tm_yday1 is %d\n",tm_yday1);
printf("value of tm_isdst1 is %d\n",tm_isdst1);


return 0;
}

Richard Heathfield wrote:
Summu82 said:
HI I have to convert a time in the format

i have
year
month
day
hour
min and seconds

I need to convert this into time in seconds since 1 jan 1970 i.e the
epoch time


Actually, you probably don't need to do this, and it's just as well, since C
doesn't guarantee that 1/1/1970 is the epoch. Nor does it guarantee a
resolution in seconds (but see below).
do some modifications i.e add some time in seconds and then bring back
to the origi8nal format i.e year month day and hour min seconds


Pick up your K&R (2nd edition) and turn to p255.

Instantiate a struct tm object like this:

struct tm foo = {0};

to ensure that any unused objects are properly initialised. [F/X - nurses
bite wound.]

Now populate as many of the fields of the struct tm as you can, and
particularly tm_sec and tm_min (0-59), tm_hour (0-23), tm_mday (1-31),
tm_mon (0-11 - it's an offset from January), and tm_year (years since 1900,
so if you mean 2006, you put 106 here).

That's all you /have/ to fill in.

Now add your time-in-seconds to tm_sec. Yes, I know - this will give you a
ludicrous value. It doesn't matter.

Now do this:

mktime(&foo);

and check out foo's fields - you will discover that everything is all
magically sorted out for you. Isn't C marvellous?

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


Jun 6 '06 #3
Summu82 said:
struct tm *tmp,*tmp2,*tmp3;
These aren't pointing anywhere.
tmp = localtime(&t); /* or gmtime, if you want GMT^H^H^HUTC */
Okay, tmp is now pointing to a struct tm object (or is a null pointer).
tmp2->tm_sec=20+3037734;


But tmp2 still isn't pointing to any struct tm object, so dereferencing it
invokes undefined behaviour.

You need a struct tm object, not just a pointer of the right type.

--
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)
Jun 6 '06 #4
Thanks Rechard

I tried as per your suggestion and have a workin code right now

here is the working code

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

void main(){
static char *const wday[] = {
"Sunday", "Monday", "Tuesday", "Wednesday",
"Thursday", "Friday", "Saturday", "-unknown-"
};
struct tm time_str;
unsigned long int tt1;
/*...*/
time_str.tm_year = 2006 - 1900;
time_str.tm_mon = 7 - 1;
time_str.tm_mday = 4;
time_str.tm_hour = 0;
time_str.tm_min = 0;
time_str.tm_sec = 1+3602;
time_str.tm_isdst = -1;

tt1 =(int)mktime(&time_str);
printf("value of time in sec is %d\n",tt1);

if (mktime(&time_str)== -1)
time_str.tm_wday=7;
printf("%s\n", wday[time_str.tm_wday]);

printf("year %d\n", time_str.tm_year);
printf(" month %d\n", time_str.tm_mon);
printf(" day %d\n", time_str.tm_mday);
printf(" hour %d\n", time_str.tm_hour);
printf(" minutes %d\n", time_str.tm_min);
printf("seconds %d\n", time_str.tm_sec);
printf(" daytimesavings %d\n", time_str.tm_isdst);

}
Thanks Again for the suggestions

I am able to achieve the desired reults without any converions into
seconds and back to time structure

Regards
Sumeet

Richard Heathfield wrote:
Summu82 said:
struct tm *tmp,*tmp2,*tmp3;


These aren't pointing anywhere.
tmp = localtime(&t); /* or gmtime, if you want GMT^H^H^HUTC */


Okay, tmp is now pointing to a struct tm object (or is a null pointer).
tmp2->tm_sec=20+3037734;


But tmp2 still isn't pointing to any struct tm object, so dereferencing it
invokes undefined behaviour.

You need a struct tm object, not just a pointer of the right type.

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


Jun 7 '06 #5
Summu82 said:
Thanks Rechard

I tried as per your suggestion and have a workin code right now
Well, you have code that doesn't appear to break on your current combination
of compiler/library/operating system/hardware, at any rate.

here is the working code

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

void main(){
In C, main returns int. Not void, not double, not FILE *, and ldiv_t - just
int.
static char *const wday[] = {
"Sunday", "Monday", "Tuesday", "Wednesday",
"Thursday", "Friday", "Saturday", "-unknown-"
};
struct tm time_str;
unsigned long int tt1;
/*...*/
time_str.tm_year = 2006 - 1900;
Well done. This code is far superior to:

time_str.tm_year = 106;

which is functionally equivalent but more obscure.

<snip>
tt1 =(int)mktime(&time_str);
Unwise. If you're going to capture the return value of mktime(), capture it
in a time_t. And lose that pointless cast.
printf("value of time in sec is %d\n",tt1);
Not necessarily. The C Standard does not guarantee that the resolution of
time_t is 1 second.
if (mktime(&time_str)== -1)


You just normalised it once, and you haven't changed it since then. Why are
you normalising it again?

--
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)
Jun 7 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Sorisio, Chris | last post: by
6 posts views Thread by David Graham | last post: by
4 posts views Thread by Ike | last post: by
3 posts views Thread by Tomas | last post: by
1 post views Thread by Ron Harter | last post: by
18 posts views Thread by moni | last post: by
5 posts views Thread by Grey Alien | last post: by
6 posts views Thread by Jeremy Sanders | last post: by
1 post views Thread by Waqarahmed | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.