时间处理 c++ 獲取當前系統時間 1. 時間戳形式 2. char *形式[轉]

原文链接 

 /* ctime example */
 #include <stdio.h>
 #include <time.h>
 #include <iostream>
 
 using namespace std;
 
 int main ()
 {
   time_t rawtime;
 
   time ( &rawtime );
   printf("%ld\n", &rawtime);
   printf ( "The current local time is: %s", ctime (&rawtime) );
   
   char * time = ctime(&rawtime);
   //ctime(&rawtime) : time_t/timestampe -> "Www Mmm dd hh:mm:ss yyyy" format
   cout<< time << endl;
   printf("%s", asctime(gmtime(&rawtime)));// asctime(gmtime(&rawtime)) = ctime(&rawtime);
   //here gmtime(&rawtime) : time_t(timpstampe) -> struct tm
   //here asctime(gmtime) : struct tm -> "Www Mmm dd hh:mm:ss yyyy" format 
   
   
   struct tm * ptm;
   ptm = gmtime(&rawtime);
   
   cout<<(ptm->tm_year + 1900)<<"year "<<(ptm->tm_mon + 1)<<"month "<<(ptm->tm_mday)<<"day "<<(ptm
   ->tm_hour)<<":"<<(ptm->tm_min)<<":"<<(ptm->tm_sec)<<endl;
   
 
   return 0;
 }

猜你喜欢

转载自blog.csdn.net/qq_31918961/article/details/106470874