c 時間轉移函數

來源:互聯網
上載者:User

標籤:

              /*
                 @param       date
                 @param       formart  of date
                 @return      time_t
                 @author      [email protected]
               */
              time_t strtotime(char* const date,char* const format="%Y%m%d%H%M%S")
              {
                      struct tm tm;
                      strptime(date,format, &tm) ;
                      time_t ft=mktime(&tm);
                      return ft;
              }



string   timetodate(time_t const timer)
              {
                      struct tm *l=localtime(&timer);

                      char buf[128];
                      snprintf(buf,sizeof(buf),"%04d-%02d-%02d %02d:%02d:%02d",l->tm_year+1900,l->tm_mon+1,l->tm_mday,l->tm_hour,l->tm_min,l->tm_sec);
                      string s(buf);
                      return s;


              }
//該程式碼片段來自於: http://www.sharejs.com/codes/cpp/5675

 

 

#include <stdio.h>
#include <time.h>
int strtotime(char datetime[])
{
struct tm tm_time;
int unixtime;
strptime(datetime, "%Y-%m-%d %H:%M:%S", &tm_time);
 
unixtime = mktime(&tm_time);
return unixtime;
}
 
另附上幾個時間相關的函數,做個筆記:
 
//目前時間
char* get_curr_time()
{
char* strtime = (char *)malloc(sizeof(char)*20);
memset(strtime, 0, sizeof(char)*20);
time_t now;
time(&now);
strftime(strtime, 20, "%Y-%m-%d %H:%M:%S", gmtime(&now));
printf("Info: current time %s\n", strtime);
return strtime;
}
 
//目前時間的unix時間戳記
int get_curr_unixtime(void)
{
time_t now;
int unixtime = time(&now);
return unixtime;
}
 
//字元轉unix時間戳記
int strtotime(char datetime[])
{
struct tm tm_time;
int unixtime;
strptime(datetime, “%Y-%m-%d %H:%M:%S”, &tm_time);
 
unixtime = mktime(&tm_time);
return unixtime;
}

c 時間轉移函數

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.