Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save dkorolev/9b27cfc7f38d6b5edd8b3d4f16752868 to your computer and use it in GitHub Desktop.
Save dkorolev/9b27cfc7f38d6b5edd8b3d4f16752868 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <string.h>
#include <time.h>
int parse(const char* timestamp_as_string) {
struct tm struct_tm;
time_t t;
memset(&struct_tm, 0, sizeof(struct tm));
if (strptime(timestamp_as_string, "%m/%d/%Y %H:%M", &struct_tm)) {
printf("strptime() returned `%d/%02d/%02d %02d:%02d:%02d; is_dst = %d`, ",
struct_tm.tm_year + 1900,
struct_tm.tm_mon,
struct_tm.tm_mday,
struct_tm.tm_hour,
struct_tm.tm_min,
struct_tm.tm_sec,
struct_tm.tm_isdst);
struct_tm.tm_isdst = -1;
t = mktime(&struct_tm);
printf("mktime() returned `%d`.\n", (int)t);
return (int)t;
} else {
fprintf(stderr, "Failed to parse date: `%s`.\n", timestamp_as_string);
return 0;
}
}
int main() {
const char* timestamp_of_interest = "11/06/2016 01:00";
const char* previous_call_one = "11/06/2016 0:00";
const char* previous_call_two = "11/06/2016 2:00";
int t1;
int t2;
parse(previous_call_one);
t1 = parse(timestamp_of_interest);
parse(previous_call_two);
t2 = parse(timestamp_of_interest);
if (t1 == t2) {
printf("OK\n");
} else {
printf("`%s` : %d != %d\n", timestamp_of_interest, t1, t2);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment