C++取得システム現在時刻

2839 ワード

#include <time.h>

//*    
 time_t tt = time(NULL);//           cuo
 tm* t= localtime(&tt);
 printf("%d-%02d-%02d %02d:%02d:%02d
", t->tm_year + 1900, t->tm_mon + 1, t->tm_mday, t->tm_hour, t->tm_min, t->tm_sec); //* SYSTEMTIME st = {0}; GetLocalTime(&st); printf("%d-%02d-%02d %02d:%02d:%02d
", st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond);

 
              ,        ~

            ,    second,     Milliseconds  ,       ,           minutes 。   , hours……

//  —   :   C   ;  :       
#include <time.h>
#include <stdio.h>
int main( void )
{
    time_t t = time(0);
    char tmp[64];
    strftime( tmp, sizeof(tmp), "%Y/%m/%d %X %A    %j  %z",localtime(&t) );
    puts( tmp );
    return 0;
}
size_t strftime(char *strDest, size_t maxsize, const char *format, const struct tm *timeptr);
            。
struct tm *localtime(const time_t *timer);
      ,localtime        tm  
                :
%a       。Eg:Tue
%A       。 Eg: Tuesday
%b        。
%B        。
%c               。
%d             (    00   31)。  
%H   24            (    00   23)。
%I   12            (    01   12)。
%j              (    001   366)。
%m       (    1   12)。
%M   。
%p   ''AM''   ''PM''        。
%S   。
%U             ,             。
%W             ,             。
%w             ( 0    )。
%x           。
%X           。 Eg: 15:26:30
%y          (    00   99)。
%Y          ,    。 Eg:2008
%Z(%z)        。Eg:      
%% %   。

//      :       ;  :   windows API
#include <windows.h>
#include <stdio.h>
int main( void )
{
SYSTEMTIME sys;
GetLocalTime( &sys );
printf( "%4d/%02d/%02d %02d:%02d:%02d.%03d   %1d
",sys.wYear,sys.wMonth,sys.wDay,sys.wHour,sys.wMinute, sys.wSecond,sys.wMilliseconds,sys.wDayOfWeek); return 0; } // , : , // c++ #include<stdlib.h> #include<iostream> using namespace std; void main() { system("time"); } // , , // c++ #include<iostream> #include<ctime> using namespace std; int main() { time_t now_time; now_time = time(NULL); cout<<now_time; return 0; }

 http://www.cppblog.com/Tongy0/archive/2011/11/23/160782.html