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

    Hi getting system time in C++

    Hi all,

    How do i get the system time in C++?


    thnks!

  2. #2
    Join Date
    Aug 2005
    Posts
    133

    Re: Hi getting system time in C++

    Code:
    # define TIME_SIZE 40
    
      const struct tm *tm;
      size_t len;
      time_t now;
      char *s;
    
      now = time ( NULL );
      tm = localtime ( &now );
    
      s = new char[TIME_SIZE];
    
      len = strftime ( s, TIME_SIZE, "%d %B %Y %I:%M:%S %p", tm );
    Louis-Philippe Frenette
    Arobas Informatique Granby
    http://www.arobasinformatique.com

  3. #3
    Join Date
    Sep 2005
    Location
    New Delhi, India
    Posts
    332

    Re: Hi getting system time in C++

    You can make use of strftime.
    Code:
    #include <iostream>
    #include <ctime>
    #include <cerrno>
     
    int main()
    {
         //Find the current time
         time_t curtime = time(0); 
          
          //convert it to tm
          tm now=*localtime(&curtime); 
         
         //BUFSIZ is standard macro that expands to a integer constant expression 
         //that is greater then or equal to 256. It is the size of the stream buffer 
         //used by setbuf()
         char dest[BUFSIZ]={0};
         
         //Format string determines the conversion specification's behaviour
         const char format[]="%A, %B %d %Y. The time is %X"; 
         
         //strftime - converts date and time to a string
         if (strftime(dest, sizeof(dest)-1, format, &now)>0)
           std::cout<<dest<<std::endl;
         else 
           std::cerr<<"strftime failed. Errno code: "<<errno<<std::endl;
    }
    You can make use of _strtime but it's non-portable and may not work on every system.
    Code:
    #include <iostream>
    #include <ctime>
     
    int main ()
    {
      char time [10];
      _strtime(time);
      std::cout<<"Current Time:"<<time;
      return 0;
    }
    Last edited by sunnypalsingh; August 8th, 2007 at 08:40 PM.
    Appreciate others by rating good posts

    "Only buy something that you'd be perfectly happy to hold if the market shut down for 10 years." - Warren Buffett

  4. #4
    Join Date
    Aug 2005
    Location
    India
    Posts
    67

    Re: Hi getting system time in C++

    On windows Using Win32 API we can make it very simple.
    Code:
    #include<iostream>
    #include<windows.h>
    
    int main(){
    	SYSTEMTIME st;
    	GetLocalTime(&st);
    	std::cout<<st.wHour<<":"<<st.wMinute<<":"<< st.wSecond;
    	return 0;
    }
    You can find details for all other related functions here

  5. #5
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: Hi getting system time in C++

    You can use GetSystemTime. Here is the list of all Windows time functions.
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

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