Conversión entre marca de tiempo y hora estándar en c ++

La primera parte proviene de: https://blog.csdn.net/wangqing_12345/article/details/52092728

1. La hora estándar debe reemplazarse por la marca de tiempo

int standard_to_stamp (char * str_time) 

        struct tm stm; 
        int iY, iM, iD, iH, iMin, es; 

        memset (& stm, 0, tamaño de (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); * / // 标准 时间 格式 例如 : 2016: 08 : 02 12:12:30
        return (int) mktime (& stm); 


2, la marca de tiempo se convierte a la hora estándar

typedef struct times
{         int Año;         int Mon;         int Day;         int Hora;         int Min;         int Second; }Veces; Times stamp_to_standard (int stampTime) {         time_t tick = (time_t) stampTime;         struct tm tm;         char s [100];         Estándar de tiempos;         // tick = tiempo (NULO);         tm = * hora local (& tick);         strftime (s, sizeof (s), "% Y-% m-% d% H:% M:% S", & tm);         printf ("% d:% s \ n", (int) tick, s);         standard.Year = atoi (s);         estándar.Mon = atoi (s + 5);         día estándar = atoi (s + 8);
























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

3. Cómo obtener la hora estándar del sistema

time_t rawtime;

struct tm * timeinfo;

tiempo (y tiempo sin procesar);

timeinfo = localtime (& rawtime);

其中 :

int Year = timeinfo-> tm_year + 1900;

int Mon = timeinfo-> tm_mon + 1;

Las horas, los minutos y los segundos permanecen iguales durante el resto del día.

 

4. Cómo obtener la marca de tiempo actual del sistema

 time_t ahora;                                                                                                                     
 int unixTime = (int) tiempo (y ahora) ;

 

La segunda parte, aplicación personal.

#include <iostream>
#include <string>
#include <vector>
#include <time.h>
/ *
    Función:
    Descripción de comparación de tiempo : Compare timestamp
    return: bool true, false
* /
inline bool CompareStampTime (long stampTimeStart, long stampTimeEnd)
{     if (stampTimeStart> stampTimeEnd)         return false;     return true; };




/ *
    Función: Acumulador de tiempo
    Descripción: tamaño, tipo de intervalo de tiempo: tipo de tiempo 0: nivel de minuto, 1: nivel de hora, 2: nivel de día
    Retorno: largo, sello de tiempo después de la acumulación
* /
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;         descanso;     predeterminado:         descanso;     }     return strTime; };















/ *
    Función: Hora estándar hasta la marca de tiempo
    Descripción: Pasar el parámetro como hora estándar, como: 2021-02-03 10:10:00
    Retorno: tipo int sello de tiempo
* /
int standard_to_stamp (const char * str_time)
{     struct tm stm;     int iY, iM, iD, iH, iMin, es;

    memset (& stm, 0, tamaño de (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);
}


/ *
    Función: Convertir marca de tiempo a hora estándar
    Descripción:
* /
std :: string stamp_to_standard (int stampTime)
{     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; };







 

Supongo que te gusta

Origin blog.csdn.net/xiaoshunzi111/article/details/113615645
Recomendado
Clasificación