42 lines
886 B
C
42 lines
886 B
C
#define _XOPEN_SOURCE
|
|
#define _POSIX_C_SOURCE 1999309L
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
#include <unistd.h>
|
|
#include <stdlib.h>
|
|
#include <errno.h>
|
|
#include <time.h>
|
|
#include "parse_date.h"
|
|
|
|
time_t mu_parse_date(const char *prog_name, const char *str) {
|
|
time_t local = time(NULL);
|
|
struct tm *tm = localtime(&local);
|
|
|
|
char flag = 0;
|
|
for (size_t i = 0; i < sizeof(mu_date_fmts) / sizeof(char *); i++) {
|
|
char *res = strptime(str, mu_date_fmts[i], tm);
|
|
if (res && *res == '\0') {
|
|
flag = 1;
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (flag == 0 && prog_name) {
|
|
fprintf(stderr, "%s: parsing: invalid format\n", prog_name);
|
|
return -1;
|
|
}
|
|
|
|
time_t rt = mktime(tm);
|
|
if (rt < 0) {
|
|
if (rt == -1 && prog_name)
|
|
fprintf(stderr, "%s: parsing: %s\n", prog_name, strerror(errno));
|
|
|
|
else if (prog_name)
|
|
fprintf(stderr, "%s: parsing: invalid date\n", prog_name);
|
|
|
|
return -1;
|
|
}
|
|
|
|
return rt;
|
|
}
|