CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Jun 2013
    Posts
    21

    UnixTimestamp to time

    I have the following code that takes a unix time stamp into a date.

    Code:
    #include "stdafx.h"
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #include <conio.h>
    //e.g. 1331812987 = Thu 15 Mar 2012 01:03:07 PM CET
    int main(int argc, char** argv) {
     struct tm * dt;
     char b[19];
     long ts;
     
     ts = 1331812987;
    
     
     dt = localtime(&ts);
     // use any strftime format spec here
     strftime(b, sizeof(b), "%c", dt);
     fprintf(stdout, "%s", b);
     getch();
     return 0;
    }
    Everything is right accept the HOUR of the timestamp which is 07 instead of 01

    is this fixable?

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: UnixTimestamp to time

    Quote Originally Posted by vaas View Post
    I have the following code that takes a unix time stamp into a date.

    Code:
    #include "stdafx.h"
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #include <conio.h>
    //e.g. 1331812987 = Thu 15 Mar 2012 01:03:07 PM CET
    int main(int argc, char** argv) {
     struct tm * dt;
     char b[19];
     long ts;
     
     ts = 1331812987;
    
     
     dt = localtime(&ts);
     // use any strftime format spec here
     strftime(b, sizeof(b), "%c", dt);
     fprintf(stdout, "%s", b);
     getch();
     return 0;
    }
    Everything is right accept the HOUR of the timestamp which is 07 instead of 01

    is this fixable?
    According to http://www.unixtimestamp.com/index.php, 7 is the correct hour. Looks like it may be adjusting to the time zone of the computer.

  3. #3
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    Re: UnixTimestamp to time

    Yes localtime is returning the local time
    If you don't want that use gmtime instead.
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    https://visualstudio.microsoft.com/vs

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured