C ++でのタイムスタンプと標準時の間の変換

最初の部分は次のサイトからのものです:https//blog.csdn.net/wangqing_12345/article/details/52092728

1.標準時間はタイムスタンプに置き換える必要があります

int standard_to_stamp(char * str_time) 

        struct tm stm; 
        int iY、iM、iD、iH、iMin、iS; 

        memset(&stm、0、sizeof(stm)); 
        iY = atoi(str_time); 
        iM = atoi(str_time + 5); 
        iD = atoi(str_time + 8); 
        iH = atoi(str_time + 11); 
        iMin = atoi(str_time + 14); 
        iS = atoi(str_time + 17); 

        stm.tm_year = iY-1900; 
        stm.tm_mon = iM-1; 
        stm.tm_mday = iD; 
        stm.tm_hour = iH; 
        stm.tm_min = iMin; 
        stm.tm_sec = iS; 

        / * printf( "%d-%0d-%0d%0d:%0d:%0d \ n"、iY、iM、iD、iH、iMin、iS); * / // png准時間格式例如:2016:08 :02 12:12:30
        return(int)mktime(&stm); 


2、タイムスタンプは標準時間に変換されます

typedef struct times
{         int Year;         int Mon;         int Day;         int Hour;         int Min;         int Second; }タイムズ; タイムスタンプ_to_standard(intstampTime){         time_tティック=(time_t)stampTime;         struct tm tm;         char s [100];         タイムズスタンダード;         //ダニ=時間(NULL);         tm = * localtime(&tick);         strftime(s、sizeof(s)、 "%Y-%m-%d%H:%M:%S"、&tm);         printf( "%d:%s \ n"、(int)tick、s);         standard.Year = atoi(s);         standard.Mon = atoi(s + 5);         standard.Day = atoi(s + 8);
























        standard.Hour = atoi(s + 11);
        standard.Min = atoi(s + 14);
        standard.Second = atoi(s + 17);
    
        return standard;
}

3。システムの標準時間を取得する方法

time_t rawtime;

struct tm * timeinfo;

時間(&rawtime);

timeinfo = localtime(&rawtime);

メリー中:

int Year = timeinfo-> tm_year + 1900;

int Mon = timeinfo-> tm_mon + 1;

時間、分、秒は、その日の残りの時間は同じままです。

 

4.システムの現在のタイムスタンプを取得する方法

 time_t now;                                                                                                                     
 int unixTime =(int)time(&now);

 

第二部、個人的なアプリケーション

#include <iostream>
#include <string>
#include <vector>
#include <time.h>
/ *
    関数:時間比較の
    説明:タイムスタンプの比較
    return:bool true、false
* /
inline bool CompareStampTime(long StampTimeStart、long StampTimeEnd)
{     if(stampTimeStart> StampTimeEnd)         return false;     return true; };




/ *
    関数:時間アキュムレータ
    説明:サイズ、時間間隔タイプ:時間タイプ0:分レベル、1:時間レベル、2:日レベル
    戻り値:long、累積後のタイムスタンプ
* /
inline long addTime(long strTime、int size、 int type)
{     switch(type)     {     case 0://         minutestrTime + = size * 60;         break;     case 1://         hourstrTime + = size * 60 * 60;         break;     case 2://         daystrTime + = size * 24 * 60 * 60;         break;     デフォルト:         break;     }     return strTime; };















/ *
    関数:タイムスタンプまでの標準時間
    説明:次のような標準時間としてパラメーターを渡します:2021-02-03 10:10:00
    戻り値:int型タイムスタンプ
* /
int standard_to_stamp(const char * str_time)
{     struct tm stm;     int iY、iM、iD、iH、iMin、iS;

    memset(&stm、0、sizeof(stm));
    iY = atoi(str_time);
    iM = atoi(str_time + 5);
    iD = atoi(str_time + 8);
    iH = atoi(str_time + 11);
    iMin = atoi(str_time + 14);
    iS = atoi(str_time + 17);

    stm.tm_year = iY-1900;
    stm.tm_mon = iM-1;
    stm.tm_mday = iD;
    stm.tm_hour = iH;
    stm.tm_min = iMin;
    stm.tm_sec = iS;

    return(int)mktime(&stm);
}


/ *
    関数:タイムスタンプを標準時間に変換します
    説明:
* /
std :: stringtamp_to_standard(intstampTime)
{     time_t tick =(time_t)stampTime;     struct tm tm;     char s [100];     tm = * localtime(&tick);     strftime(s、sizeof(s)、 "%Y-%m-%d%H:%M:%S"、&tm);     return s; };







 

おすすめ

転載: blog.csdn.net/xiaoshunzi111/article/details/113615645