blob: 5b77faaaf2b9eac34d88e6e8d61fb2d996edeb63 [file] [log] [blame]
Viet-Trung Luu96b05c12016-01-11 11:26:36 -08001#include <time.h>
2#include <pthread.h>
3#include <errno.h>
4#include <stdio.h>
5#include <stdlib.h>
6
7int getdate_err;
8
George Kulakowski17e3b042016-02-18 15:59:50 -08009struct tm* getdate(const char* s) {
10 static struct tm tmbuf;
11 struct tm* ret = 0;
12 char* datemsk = getenv("DATEMSK");
13 FILE* f = 0;
14 char fmt[100], *p;
15 int cs;
Viet-Trung Luu96b05c12016-01-11 11:26:36 -080016
George Kulakowski17e3b042016-02-18 15:59:50 -080017 pthread_setcancelstate(PTHREAD_CANCEL_DEFERRED, &cs);
Viet-Trung Luu96b05c12016-01-11 11:26:36 -080018
George Kulakowski17e3b042016-02-18 15:59:50 -080019 if (!datemsk) {
20 getdate_err = 1;
21 goto out;
22 }
Viet-Trung Luu96b05c12016-01-11 11:26:36 -080023
George Kulakowski17e3b042016-02-18 15:59:50 -080024 f = fopen(datemsk, "rbe");
25 if (!f) {
26 if (errno == ENOMEM)
27 getdate_err = 6;
28 else
29 getdate_err = 2;
30 goto out;
31 }
Viet-Trung Luu96b05c12016-01-11 11:26:36 -080032
George Kulakowski17e3b042016-02-18 15:59:50 -080033 while (fgets(fmt, sizeof fmt, f)) {
34 p = strptime(s, fmt, &tmbuf);
35 if (p && !*p) {
36 ret = &tmbuf;
37 goto out;
38 }
39 }
Viet-Trung Luu96b05c12016-01-11 11:26:36 -080040
George Kulakowski17e3b042016-02-18 15:59:50 -080041 getdate_err = 7;
Viet-Trung Luu96b05c12016-01-11 11:26:36 -080042out:
George Kulakowski17e3b042016-02-18 15:59:50 -080043 if (f)
44 fclose(f);
45 pthread_setcancelstate(cs, 0);
46 return ret;
Viet-Trung Luu96b05c12016-01-11 11:26:36 -080047}