Thread: making a timestamp

  1. #1
    karmatic
    Guest

    making a timestamp

    Can anyone suggest a way to convert the result from gettimeofday() which is number of seconds to a UNIX integer timestamp that looks like for example, 20030411021539, on April 11, 2003 at 2:15:39am?

    Thanks!

  2. #2
    karmatic
    Guest
    Thanks..I actually wanted the timestamp to look a UNIX integer, i.e, 20030411021539.

    Any other ideas are appreciated!

  3. #3
    karmatic
    Guest
    Thanks! One more thing, how would I add milliseconds to that?
    :-)

    Thanks again.

  4. #4
    karmatic
    Guest
    I can't get the milliseconds to be part of the long long double, i.e 20030411110933.456, where 456 is the milliseconds. Maybe I am making this more complicated than it needs to be...

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #include <sys/time.h>
    
    int main( ) {
       struct timeval tstr;
       struct tm      *tmstr;
       char           buf[128];
       long long double milli;
       long long double ll,num;
       if( gettimeofday( &tstr, NULL ) != 0 ) {
          perror( "gettimeofday" );
       return 1;
      }
      tmstr = localtime( &tstr.tv_sec);
      strftime( buf, sizeof( buf ), "%Y%m%d%I%M%S", tmstr );
      milli=(long long double)(tstr.tv_usec/1000.0);
      num=(long long double)strtoll(buf,NULL,10);
      ll =(long long double)(0.0+num);
      ll=(long long double)(ll+milli/1000.0);
      printf( "%.3llf\n",(long long double)ll );
      return 0;
    }

  5. #5
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    AFAIK, there is no long long double, just long double. Try that.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I don't know why they're bothering with floating point values anyway. A millisecond is 1/1000th. As such, since it itself is never a floating point value, just use an integer for it. Use an integer for minutes, one for seconds, one for milliseconds. Why bother with floats?

    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    karmatic
    Guest
    vVv: I ran the code, but I need the stamp to show milliseconds as well. long long is supported, but I thought since I want the milliseconds to appear after the decimal point, I have to make it a double as well...since it won't work with just long long.

    quzah: I want the output to look like: 20030411110933.456.
    How do I get that unless I make the type some sort of float?

    Thanks guys.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. making sprites
    By DavidP in forum Game Programming
    Replies: 9
    Last Post: 02-20-2010, 07:00 AM
  2. newbie needs help with code
    By compudude86 in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 08:54 PM
  3. Making great graphics
    By MadCow257 in forum Game Programming
    Replies: 1
    Last Post: 02-20-2006, 11:59 PM
  4. Making control...
    By Finchie_88 in forum C++ Programming
    Replies: 2
    Last Post: 09-07-2004, 01:42 PM
  5. Replies: 2
    Last Post: 01-13-2003, 01:28 PM