Find the answer to your Linux question:
Results 1 to 5 of 5
I want to how get time in seconds. If input is 6/2/2009 , I want the output in seconds since an ``epoch.'' (from Jan 1,1970) I tried the following program,but ...
  1. #1
    Linux Guru Lakshmipathi's Avatar
    Join Date
    Sep 2006
    Location
    3rd rock from sun - Often seen near moon
    Posts
    1,568

    Post compute time in seconds + C program

    I want to how get time in seconds.
    If input is 6/2/2009 , I want the output in seconds since an ``epoch.'' (from Jan 1,1970)
    I tried the following program,but it's not working.
    Any help?
    Code:
    #include <time.h>
    main(){
    struct tm mytm;
    mytm.tm_mon=2;
    mytm.tm_mday=6;
    mytm.tm_year=2009;
    printf("\n In seconds : %u",ctime(&mytm));
    }
    - Lakshmipathi.G
    -------------------
    FOSS India Award winning ext3fs Undelete tool and tutorials www.giis.co.in
    First they criticize you,Then they laugh at you,Then they fight with you,Then you win. - M.K.Gandhi
    -------------------

  2. #2
    Linux User
    Join Date
    May 2008
    Location
    NYC, moved from KS & MO
    Posts
    251
    ctime is equivalent to asctime(localtime(t)) therefore calling it will always return the current time in a string format, and you should not just pass it to printf with format %u and hope it works.

    Code:
    #include <time.h>
    #include <stdio.h>
    main(){
    time_t rawtime;
    struct tm *mytm;
    time(&rawtime);
    mytm=localtime(&rawtime);
    mytm->tm_mon=1;
    mytm->tm_mday=6;
    mytm->tm_year=2009-1900; /*type man mktime to see the definition of tm_year */
    printf("Seconds since epoch: %u\n",mktime(mytm));
    }
    line #include <stdio.h> gets rid of the "incompatible implicit declaration of built-in function 'printf'" warning. Also please take note that:
    1. tm_mon is 0 based, therefore if you want February, you should use 1
    2. tm_year should be the elapsed number of years since 1900
    3. the above program uses the current hour, minute and second, if you want it to be exactly the beginning of that date, you'll have to initialise tm_sec, tm_min and tm_hour as well.

  3. #3
    Linux Guru Lakshmipathi's Avatar
    Join Date
    Sep 2006
    Location
    3rd rock from sun - Often seen near moon
    Posts
    1,568

    Smile

    Thanks secondmouse ..It worked perfectly.
    I have a doubt on this line

    time(&rawtime);
    What's the usage about above in program.I'm still getting same output when i commented out this line.

    #include <time.h>
    #include <stdio.h>
    main(){
    time_t rawtime;
    struct tm *mytm;
    /* time(&rawtime); */
    mytm=localtime(&rawtime);
    mytm->tm_mon=1;
    mytm->tm_mday=6;
    mytm->tm_year=2009-1900; /*type man mktime to see the definition of tm_year */
    printf("Seconds since epoch: %u\n",mktime(mytm));
    }
    Thanks for the notes too.
    - Lakshmipathi.G
    -------------------
    FOSS India Award winning ext3fs Undelete tool and tutorials www.giis.co.in
    First they criticize you,Then they laugh at you,Then they fight with you,Then you win. - M.K.Gandhi
    -------------------

  4. #4
    Linux User
    Join Date
    May 2008
    Location
    NYC, moved from KS & MO
    Posts
    251
    Glad I can help.

    As for the usage of time(), you can google for "c programming time()", here's one I found:
    time() - Standard C Time & Date - C Programming Reference - eLook.org

  5. #5
    Linux Guru Lakshmipathi's Avatar
    Join Date
    Sep 2006
    Location
    3rd rock from sun - Often seen near moon
    Posts
    1,568

    Lightbulb

    yes..thanks again for the links
    - Lakshmipathi.G
    -------------------
    FOSS India Award winning ext3fs Undelete tool and tutorials www.giis.co.in
    First they criticize you,Then they laugh at you,Then they fight with you,Then you win. - M.K.Gandhi
    -------------------

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...