Skip to content

Instantly share code, notes, and snippets.

@dkorolev
Created May 2, 2018 02:26
Show Gist options
  • Save dkorolev/c5e98e1de5af1a02a18aaf0f10380efa to your computer and use it in GitHub Desktop.
Save dkorolev/c5e98e1de5af1a02a18aaf0f10380efa 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)) {
struct_tm.tm_isdst = -1;
t = mktime(&struct_tm);
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