1: Primero convierta la hora del sistema en un formato de hora estándar y luego en una marca de tiempo.
#include "iostream"
#include "time.h"
#include "string.h"
using namespace std;
int main()
{
time_t rawtime ;
struct tm * timeinfo;
char s[100];
time ( &rawtime );
timeinfo = localtime ( &rawtime );
time_t tick = mktime(timeinfo);
strftime(s, sizeof(s), "%Y-%m-%d %H:%M:%S", timeinfo);
printf("%d: %s\n", (int)tick, s);
int Year = timeinfo->tm_year+1900;
int Mon = timeinfo->tm_mon+1;
int Day = timeinfo->tm_mday;
int Hour = timeinfo->tm_hour;
int Min = timeinfo->tm_min;
int Second = timeinfo->tm_sec;
cout << Year << ":" << Mon << ":" << Day << "-" << Hour << ":" << Min << ":" << Second << endl;
return 0;
}
entre ellos:
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.
Los resultados de la operación son los siguientes:
2: Primero convierta la hora del sistema en una marca de tiempo y luego en un formato estándar de hora.
#include "iostream"
#include "time.h"
#include "string.h"
using namespace std;
int main()
{
time_t now;
int unixTime = (int)time(&now);
time_t tick = (time_t)unixTime;
struct tm tm;
char s[100];
tm = *localtime(&tick);
strftime(s, sizeof(s), "%Y-%m-%d %H:%M:%S", &tm);
printf("%d: %s\n", (int)unixTime, s);
return 0;
}
Los resultados de la operación son los siguientes:
Gracias: https://gameinstitute.qq.com/community/detail/126521