51 lines
1.2 KiB
C
51 lines
1.2 KiB
C
#define _XOPEN_SOURCE
|
|
#include <time.h>
|
|
#define __USE_UNIX98
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
|
|
int mktime_test(const char* str)
|
|
{
|
|
struct tm local_tm, local_tm2;
|
|
time_t local_time;
|
|
char buffer[128];
|
|
|
|
memset(buffer, 0, sizeof(buffer));
|
|
puts(str);
|
|
if (strptime(str, "%Y-%m-%d %H:%M:%S", &local_tm) == NULL)
|
|
{
|
|
fprintf(stderr, "failed to convert `%s' to struct tm\n", str);
|
|
return 1;
|
|
}
|
|
snprintf(buffer, sizeof(buffer), "%d-%02d-%02d %02d:%02d:%02d",
|
|
local_tm.tm_year + 1900, local_tm.tm_mon + 1, local_tm.tm_mday,
|
|
local_tm.tm_hour, local_tm.tm_min, local_tm.tm_sec);
|
|
puts(buffer);
|
|
if (strcmp(str, buffer) != 0)
|
|
{
|
|
fprintf(stderr, "strptime() failed\n");
|
|
return 1;
|
|
}
|
|
local_tm.tm_isdst = -1;
|
|
local_time = mktime(&local_tm);
|
|
localtime_r(&local_time, &local_tm2);
|
|
snprintf(buffer, sizeof(buffer), "%d-%02d-%02d %02d:%02d:%02d",
|
|
local_tm2.tm_year + 1900, local_tm2.tm_mon + 1, local_tm2.tm_mday,
|
|
local_tm2.tm_hour, local_tm2.tm_min, local_tm2.tm_sec);
|
|
puts(buffer);
|
|
if (strcmp(str, buffer) != 0)
|
|
{
|
|
fprintf(stderr, "mktime() or localtime_r() failed\n");
|
|
return 1;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
int main()
|
|
{
|
|
mktime_test("1981-07-01 00:00:00");
|
|
mktime_test("1981-07-01 00:00:00");
|
|
return 0;
|
|
}
|
|
|