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

Discussion in 'C Programming' started by Summu82, Jun 6, 2006.

  1. Summu82

    Summu82 Guest

    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
     
    Summu82, Jun 6, 2006
    #1
    1. Advertising

  2. 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)
     
    Richard Heathfield, Jun 6, 2006
    #2
    1. Advertising

  3. Summu82

    Summu82 Guest

    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)
     
    Summu82, Jun 6, 2006
    #3
  4. 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)
     
    Richard Heathfield, Jun 6, 2006
    #4
  5. Summu82

    Summu82 Guest

    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)
     
    Summu82, Jun 7, 2006
    #5
  6. 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)
     
    Richard Heathfield, Jun 7, 2006
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. vertigo

    days since 1 Jan 1970

    vertigo, Jul 7, 2004, in forum: Perl
    Replies:
    6
    Views:
    7,346
    Joe Smith
    Jul 8, 2004
  2. Peter Kleiweg
    Replies:
    2
    Views:
    1,125
    Peter Kleiweg
    Jul 6, 2005
  3. Grey Alien

    Unix time (predating epoch of Jan-1970)

    Grey Alien, Jul 19, 2007, in forum: C Programming
    Replies:
    5
    Views:
    601
    Keith Thompson
    Jul 20, 2007
  4. JC
    Replies:
    4
    Views:
    3,267
    Marcel Müller
    Jan 7, 2009
  5. Tyler Cruz

    Time with Epoch earlier than 1970 question

    Tyler Cruz, Feb 8, 2004, in forum: Perl Misc
    Replies:
    4
    Views:
    131
    Gregory Toomey
    Feb 8, 2004
Loading...

Share This Page