completely stuck understanding how to use time.h

This is a discussion on completely stuck understanding how to use time.h within the C++ Programming forums, part of the General Programming Boards category; hi, im a begginer programmer and i am trying to convert a unix timestamp into a readable formatt ie. 45d732f6 ...

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    99

    Unhappy completely stuck understanding how to use time.h

    hi, im a begginer programmer and i am trying to convert a unix timestamp into a readable formatt ie. 45d732f6 ---> Sat, 17 February 2007 16:53:10 UTC.


    Ive looked on the internet for examples and i cant find any simple ones that can explain to me what is going on. All the examples i can find are just retrieving current time and so on but nothing that shows how to preform a conversion.

    can anyone of you guys help me out i am completely stuck and dnt know how to proceed


    thanks

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >and i am trying to convert a unix timestamp into a readable formatt ie. 45d732f6 ---> Sat, 17 February 2007 16:53:10 UTC.
    Assuming the timestamp came from a time_t, you can do this:
    Code:
    	time_t timestamp = 0x45d732f6;
    	struct tm t = *localtime(&timestamp);
    	char buf[80];
    	if (strftime(buf, sizeof buf, "%a, %d %B %Y %H:%M:%S UTC", &t) == 0)
    	{
    		std::cerr << "Error calling strftime." << std::endl;
    	}
    	std::cout << buf << std::endl;

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    99
    THANK YOU!!! this is exactly the sort of thing i was after. The only problem i can im having is that my 8 byte time stamp is stored as below in the variable unixtime. I seem to get an error whenever i try to initialise

    time_t timestamp = unixtime[];

    This is the only way that i know how to get and store the timestamp i hope its not going to be to much of a problem??

    Code:
    unsigned char _1,_2,_3,_4,_5,_6,_7,_8;
    int unixtime [8]; 
    	
    unixtime[0]=_1;
    unixtime[1]=_2;
    unixtime[2]=_3;
    unixtime[3]=_4;
    unixtime[4]=_5;
    unixtime[5]=_6;
    unixtime[6]=_7;
    unixtime[7]=_8;

    thanks again

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Is each digit stored in a separate location of the array?
    Code:
    unixtime[0]=0x4;
    unixtime[1]=0x5;
    unixtime[2]=0xd;
    unixtime[3]=0x7;
    unixtime[4]=0x3;
    unixtime[5]=0x2;
    unixtime[6]=0xf;
    unixtime[7]=0x6;

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    I guess the question is, how does 45d732f6 relate to your unixtime[] array?

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    I'm going to assume that unsigned char _1, _2, _3, _4, _5, _6, _7, _8 contain the char codes for each hex value. Then a simple idea would be to use a std::stringstream to convert this to a time_t.
    Code:
    #include <sstream>
    .
    .
    	char time_array[] = {_1,_2,_3,_4,_5,_6,_7,_8,'\0'};
    
    	std::string timestamp_s = time_array;
    	std::stringstream converter(timestamp_s);
    
    	time_t timestamp;
    	converter >> std::hex >> timestamp;
    Now the variable timestamp would contain the time stamp as a time_t, and you could use the code posted earlier (which utilizes the two functions localtime() and strftime()) to convert this to a printable string.

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Right, swoopy's solution won't work.

    Normally, time_t is a 64-bit integer, so I suppose your 8 char values are actually the bytes of a 64-bit integer. In which case, you'll need to know the byte-order. It is either the highest byte first or the lowest byte first [most likely at least]:
    Code:
    // Lowest byte first (little endian):
    time_t lebytestotime(char *buf)
    {
       time_t t;
       char *tp = (char *)&t;
       for(i = 0; i < 8; i++)
       {
          tp[i] = buf[i];
       }
       return t;
    }
    
    // highest byte first, big endian:
    time_t bebytestotime(char *buf)
    {
       time_t t;
       char *tp = (char *)&t;
       for(i = 0; i < 8; i++)
       {
          tp[7-i] = buf[i];
       }
       return t;
    }
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Registered User
    Join Date
    Apr 2008
    Posts
    99
    hi my timestamp is big endian therefore it is as it comes, thers no need for me to reverse the bytes. anyway i have to following code:



    Code:
    
    unsigned char _1,_2,_3,_4,_5,_6,_7,_8;
    
    
    infile.read (&_1,sizeof (_1)) ;		
    infile.read (&_2,sizeof (_2)) ;
    infile.read (&_3,sizeof (_3)) ;		
    infile.read (&_4,sizeof (_4)) ;
    infile.read (&_5,sizeof (_5)) ;		
    infile.read (&_6,sizeof (_6)) ;
    infile.read (&_7,sizeof (_7)) ;		
    infile.read (&_8,sizeof (_8)) ;
    
    unsigned char unixtime [9]; 
    
    unixtime[0]=_1;
    unixtime[1]=_2;
    unixtime[2]=_3;
    unixtime[3]=_4;
    unixtime[4]=_5;
    unixtime[5]=_6;
    unixtime[6]=_7;
    unixtime[7]=_8;
    unixtime[8]='/0';
    
    std::string timestamp_s = unixtime;
    	std::stringstream converter(timestamp_s);
    
    	time_t timestamp;
    	converter >> std::hex >> timestamp;
    
    
    	struct tm t = *localtime(&timestamp);
    	char buf[80];
    	if (strftime(buf, sizeof buf, "%a, %d %B %Y %H:%M:%S UTC", &t) == 0)
    	{
    		std::cerr << "Error calling strftime." << std::endl;
    	}
    	std::cout << buf << std::endl;
    
    cout << buf << endl;

    its gives me some strange results. In my unixtime array i am just storing each byte of my 8 byte time stamp in the order they should be translated. ANother strange thing that just started to happen is that my string terminating charcter for unixtime has started to give me garbage on the output, i dnt know if ive changed something without noticing?



    I have started getting confused after the point of storing my bytes in the unixtime[]. If anyone ould guide me from this point to where i want to be i would be appreciative!

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I expect that you can do:
    Code:
    time_t ts;
    
    infile.read (&ts,sizeof (ts)) ;
    
    struct tm t = *localtime(&ts);
    ...
    You are trying to make a string out of binary data, and that will NOT work.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #10
    Registered User
    Join Date
    Apr 2008
    Posts
    99
    thanks matsp for your quick reply, you beat me to it but i think ive sorted my problem and it seems to be giving the output i want. Also thanks to swoopy, you input was also a great help to me!! thank you both guys.




    ..............one more quick question if thats ok. Im trying to write this buffer to my file now but im getting an error. Its asking for more parameters in my .write statement but i thought what i have put below would be sufficient??


    Code:
    of.write(buf);   // of is the name of my ofstream


    thanks again

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    In likeness with read, you need to specify how long your data (buf) is.

    E.g.
    Code:
    of.write(buf, strlen(buf)+1);
    But I'm thinking that you may want to actually use, since I expect your output is a text file:
    Code:
    of << buf;
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  12. #12
    Registered User
    Join Date
    Apr 2008
    Posts
    99
    Thanks for all your help, that sorted my problem


    MUCH APPRECIATED guys!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. New, not completely understanding
    By Time in forum C++ Programming
    Replies: 10
    Last Post: 05-08-2008, 10:22 AM
  2. Complete C noob...and completely stuck lol
    By JST212 in forum C Programming
    Replies: 6
    Last Post: 03-02-2008, 09:54 AM
  3. Replies: 6
    Last Post: 10-23-2006, 07:22 PM
  4. understanding recursive functions
    By houler in forum C Programming
    Replies: 7
    Last Post: 12-09-2004, 11:56 AM
  5. I'm completely stuck!
    By tek in forum C++ Programming
    Replies: 0
    Last Post: 08-23-2003, 06:43 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21