473,395 Members | 2,079 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,395 software developers and data experts.

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 2886
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Sorisio, Chris | last post by:
Ladies and gentlemen, I've imported some data from a MySQL database into a Python dictionary. I'm attempting to tidy up the date fields, but I'm receiving a 'mx.DateTime.Error: cannot convert...
6
by: David Graham | last post by:
Hi I have asked this question in alt.php as the time() function as used in setcookie belongs to php - or does it belong equally in the javascript camp - bit confused about that. Anyway, can anyone...
4
by: Ike | last post by:
If I have an integer, like : 1078349279 representing seconds since the "epoch" (jan 1, 1970) is there a means in javascript to format that into the date and time? Thank you. Ike
3
by: Tomas | last post by:
I have a very interesting problem... Php says that the difference between these times is 1 hour. <?php $t1 = "2005-12-31 20:00"; $t2 = "2005-12-31 20:00"; $t1i = strtotime($t1); $t2i =...
1
by: Ron Harter | last post by:
I have inherited a project that where I need to convert time form the DataTime format to the old form of seconds since 1/1/70. I can't locate any functionality in the framework (2.0) that even...
18
by: moni | last post by:
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
4
by: Holmsey | last post by:
Hi I have to convert am today's date and time (now) to a webservice. The example the web service gives was "1163313527144#" which comes out to 12/31/1969 06:00 PM. How do I convert a date to a 13...
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...
6
by: Jeremy Sanders | last post by:
Hi - I need to add support to a program for dates and times. The built-in Python library seems to be okay for many purposes, but what I would like would be Unix epoch style times (seconds relative...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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,...
0
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.