Printing current system time in C++

This is a discussion on Printing current system time in C++ within the C++ Programming forums, part of the General Programming Boards category; hi all, I'm trying to print out the current time using C++. I'm using Microsoft visual studio 2008.. Code: #include ...

  1. #1
    Registered User
    Join Date
    Aug 2010
    Posts
    13

    Question Printing current system time in C++

    hi all,

    I'm trying to print out the current time using C++. I'm using Microsoft visual studio 2008..

    Code:
    #include <windows.h>
    #include <stdio.h>
    #include <time.h>
    
    #ifndef WINDOWS
    #include <sys/time.h>
    #endif
    
    
    #ifdef WINDOWS
      SYSTEMTIME      beg;
    #else
      struct timeval  beg;  
      struct tm      *t;        
    #endif
    
    
    #ifdef WINDOWS
      GetLocalTime( &beg );
    
      printf( "%02d:%02d:%02d:%06d\n", beg.wHour,
        beg.wMinute, beg.wSecond, beg.wMilliseconds * 1000 );
    #else
      gettimeofday( &beg, NULL );
      t = localtime( &beg.tv_sec );
    
      printf( "%02d:%02d:%02d.%06d\n",
        t->tm_hour, t->tm_min, t->tm_sec, (int) beg.tv_usec );
      #endif
    Getlocaltime() functin doesn't seem to work.. What's the problem? Any help is appreciated..

  2. #2
    Registered User
    Join Date
    Mar 2008
    Posts
    10
    First, you need a main function in your code (c code):
    Code:
    #include <windows.h>
    #include <stdio.h>
    #include <time.h>
    
    #ifndef WINDOWS
    #include <sys/time.h>
    #endif
    
    
    int main(){
    
    #ifdef WINDOWS
    	SYSTEMTIME      beg;
    #else
    	struct timeval  beg;  
    	struct tm      *t;        
    #endif
    
    
    #ifdef WINDOWS
    	GetLocalTime( &beg );
    
    	printf( "%02d:%02d:%02d:%06d\n", beg.wHour,
    		beg.wMinute, beg.wSecond, beg.wMilliseconds * 1000 );
    #else
    	gettimeofday( &beg, NULL );
    	t = localtime( &beg.tv_sec );
    
    	printf( "%02d:%02d:%02d.%06d\n",
    		t->tm_hour, t->tm_min, t->tm_sec, (int) beg.tv_usec );
    #endif
    	return 0;
    }
    Next, open the project properties (Alt+F7) in your Visual Studio and select "Configuration Properties>C/C++>Preprocessor" and add WINDOWS to the Preprocessor Definitions or, add directly in the code
    Code:
    #include <windows.h>
    #include <stdio.h>
    #include <time.h>
    #define WINDOWS
    ...

  3. #3
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    If you're gonna have those #ifdefs for WINDOWS then you might as well put this inside too:
    Code:
    #ifdef WINDOWS
    #include <windows.h>
    #endif
    Although I'm not sure why you need any platform specific code just to print the time. These standard functions should be all you need: Standard C Date & Time [C++ Reference]
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Immediate programming help! Please!
    By xMEGANx in forum C++ Programming
    Replies: 6
    Last Post: 02-20-2008, 11:52 AM
  2. Printing System time and 30 minute alarm
    By kevndale79 in forum C Programming
    Replies: 16
    Last Post: 10-17-2006, 05:10 AM
  3. compiler build error
    By KristTlove in forum C++ Programming
    Replies: 2
    Last Post: 11-30-2003, 09:16 AM
  4. current time
    By lithium in forum C Programming
    Replies: 3
    Last Post: 01-26-2003, 04:42 PM
  5. get system precise time
    By crystalike in forum C++ Programming
    Replies: 6
    Last Post: 09-05-2002, 12:51 AM

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