-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdate_utils.c
129 lines (118 loc) · 3.4 KB
/
date_utils.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/* xvisitors -- very fast web logs analyzer.
*
* Copyright (C) 2014 Wael BEN ZID <benzid.wael@hotmail.fr>
* All Rights Reserved.
*
* This software is released under the terms of the GPL license version 2.
* Read the COPYING file in this distribution for more details. */
#include "date_utils.h"
/* returns the time converted into a time_t value.
* On error (time_t) -1 is returned.
* Note that this function is specific for the following format:
* "10/May/2004:04:15:33". Works if the month is not an abbreviation, or if the
* year is abbreviated to only the last two digits.
* The time can be omitted like in "10/May/2004". */
time_t parse_date(char *s, struct tm *tmptr, int time_delta)
{
struct tm tm;
time_t t;
char *months[] = {
"jan", "feb", "mar", "apr", "may", "jun",
"jul", "aug", "sep", "oct", "nov", "dec",
};
char *day, *month, *year, *time = NULL;
char monthaux[32];
int i, len;
/* make a copy to mess with it */
len = strlen(s);
if (len >= 32) goto fmterr;
memcpy(monthaux, s, len);
monthaux[len] = '\0';
/* Inizialize the tm structure. We just fill three fields */
tm.tm_sec = 0;
tm.tm_min = 0;
tm.tm_hour = 0;
tm.tm_mday = 0;
tm.tm_mon = 0;
tm.tm_year = 0;
tm.tm_wday = 0;
tm.tm_yday = 0;
tm.tm_isdst = -1;
/* search delimiters */
day = monthaux;
if ((month = strchr(day, '/')) == NULL) goto fmterr;
*month++ = '\0';
if ((year = strchr(month, '/')) == NULL) goto fmterr;
*year++ = '\0';
/* time, optional for this parser. */
if ((time = strchr(year, ':')) != NULL) {
*time++ = '\0';
}
/* convert day */
tm.tm_mday = atoi(day);
if (tm.tm_mday < 1 || tm.tm_mday > 31) goto fmterr;
/* convert month */
if (strlen(month) < 3) goto fmterr;
month[0] = tolower(month[0]);
month[1] = tolower(month[1]);
month[2] = tolower(month[2]);
for (i = 0; i < 12; i++) {
if (memcmp(month, months[i], 3) == 0) break;
}
if (i == 12) goto fmterr;
tm.tm_mon = i;
/* convert year */
tm.tm_year = atoi(year);
if (tm.tm_year > 100) {
if (tm.tm_year < 1900 || tm.tm_year > 2500) goto fmterr;
tm.tm_year -= 1900;
} else {
/* if the year is in two-digits form, the 0 - 68 range
* is converted to 2000 - 2068 */
if (tm.tm_year < 69)
tm.tm_year += 100;
}
/* convert time */
if (time) { /* format is HH:MM:SS */
if (strlen(time) < 8) goto fmterr;
tm.tm_hour = ((time[0]-'0')*10)+(time[1]-'0');
if (tm.tm_hour < 0 || tm.tm_hour > 23) goto fmterr;
tm.tm_min = ((time[3]-'0')*10)+(time[4]-'0');
if (tm.tm_min < 0 || tm.tm_min > 59) goto fmterr;
tm.tm_sec = ((time[6]-'0')*10)+(time[7]-'0');
if (tm.tm_sec < 0 || tm.tm_sec > 60) goto fmterr;
}
t = mktime(&tm);
if (t == (time_t)-1) goto fmterr;
t += time_delta * 3600;
if (tmptr) {
struct tm *auxtm;
if ((auxtm = localtime(&t)) != NULL)
*tmptr = *auxtm;
}
return t;
fmterr: /* format error */
return (time_t) -1;
}
/* returns 1 if the given date is Saturday or Sunday.
* Zero is otherwise returned. */
int vi_is_weekend(char *s, int time_delta)
{
struct tm tm;
if (parse_date(s, &tm, time_delta) != (time_t)-1) {
if (tm.tm_wday == 0 || tm.tm_wday == 6)
return 1;
}
return 0;
}
#if 0
/* Returns true if 'year' is a leap year. */
int isleap(int year)
{
int conda, condb, condc;
conda = (year % 4) == 0;
condb = (year % 100) == 0;
condc = (year % 400) == 0;
return conda && !(condb && !condc);
}
#endif