summaryrefslogtreecommitdiff
path: root/utils/time.c
blob: 6d53b16e1d318dde50e63b55c932963592275d62 (plain)
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/*
 * Copyright 2007 Rob Kendrick <rjek@netsurf-browser.org>
 * Copyright 2004-2007 James Bursa <bursa@users.sourceforge.net>
 * Copyright 2003 Phil Mellor <monkeyson@users.sourceforge.net>
 * Copyright 2003 John M Bell <jmb202@ecs.soton.ac.uk>
 * Copyright 2004 John Tytgat <joty@netsurf-browser.org>
 *
 * This file is part of NetSurf, http://www.netsurf-browser.org/
 *
 * NetSurf is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2 of the License.
 *
 * NetSurf is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * \file utils/time.c
 * \brief Implementation of time operations.
 */

#include <errno.h>
#include <stdio.h>
#include <stdlib.h>

#include <curl/curl.h>

#include "utils/errors.h"
#include "utils/time.h"



/**
 * Weekdays
 *
 * Must be calender order.
 */
enum nsc_time_weekdays {
	NSC_TIME_WEEKDAY_SUN,
	NSC_TIME_WEEKDAY_MON,
	NSC_TIME_WEEKDAY_TUE,
	NSC_TIME_WEEKDAY_WED,
	NSC_TIME_WEEKDAY_THU,
	NSC_TIME_WEEKDAY_FRI,
	NSC_TIME_WEEKDAY_SAT,
	NSC_TIME_WEEKDAY__COUNT
};
/**
 * Months
 *
 * Must be calender order.
 */
enum nsc_time_months {
	NSC_TIME_MONTH_JAN,
	NSC_TIME_MONTH_FEB,
	NSC_TIME_MONTH_MAR,
	NSC_TIME_MONTH_APR,
	NSC_TIME_MONTH_MAY,
	NSC_TIME_MONTH_JUN,
	NSC_TIME_MONTH_JUL,
	NSC_TIME_MONTH_AUG,
	NSC_TIME_MONTH_SEP,
	NSC_TIME_MONTH_OCT,
	NSC_TIME_MONTH_NOV,
	NSC_TIME_MONTH_DEC,
	NSC_TIME_MONTH__COUNT,
};


/**
 * Array of short weekday names.
 */
static const char * const weekdays_short[NSC_TIME_WEEKDAY__COUNT] = {
	[NSC_TIME_WEEKDAY_SUN] = "Sun",
	[NSC_TIME_WEEKDAY_MON] = "Mon",
	[NSC_TIME_WEEKDAY_TUE] = "Tue",
	[NSC_TIME_WEEKDAY_WED] = "Wed",
	[NSC_TIME_WEEKDAY_THU] = "Thu",
	[NSC_TIME_WEEKDAY_FRI] = "Fri",
	[NSC_TIME_WEEKDAY_SAT] = "Sat"
};
/**
 * Array of month names.
 */
static const char * const months[NSC_TIME_MONTH__COUNT] = {
	[NSC_TIME_MONTH_JAN] = "Jan",
	[NSC_TIME_MONTH_FEB] = "Feb",
	[NSC_TIME_MONTH_MAR] = "Mar",
	[NSC_TIME_MONTH_APR] = "Apr",
	[NSC_TIME_MONTH_MAY] = "May",
	[NSC_TIME_MONTH_JUN] = "Jun",
	[NSC_TIME_MONTH_JUL] = "Jul",
	[NSC_TIME_MONTH_AUG] = "Aug",
	[NSC_TIME_MONTH_SEP] = "Sep",
	[NSC_TIME_MONTH_OCT] = "Oct",
	[NSC_TIME_MONTH_NOV] = "Nov",
	[NSC_TIME_MONTH_DEC] = "Dec"
};


/* exported interface documented in utils/time.h */
const char *rfc1123_date(time_t t)
{
	static char ret[30];

	struct tm *tm = gmtime(&t);

	snprintf(ret, sizeof ret, "%s, %02d %s %d %02d:%02d:%02d GMT",
			weekdays_short[tm->tm_wday], tm->tm_mday,
			months[tm->tm_mon], tm->tm_year + 1900,
			tm->tm_hour, tm->tm_min, tm->tm_sec);

	return ret;
}


/* exported function documented in utils/time.h */
int nsc_sntimet(char *str, size_t size, time_t *timep)
{
#ifndef HAVE_STRFTIME
	long long val;
	val = (long long)*timep;

	return snprintf(str, size, "%lld", val);
#else
	struct tm *ltm;

	ltm = localtime(timep);
	if (ltm == NULL) {
		return -1;
	}

	return strftime(str, size, "%s", ltm);
#endif
}


/* exported function documented in utils/time.h */
nserror nsc_snptimet(const char *str, size_t size, time_t *timep)
{
	time_t time_out;

#ifndef HAVE_STRPTIME
	char *rstr;

	if (size < 1) {
		return NSERROR_BAD_PARAMETER;
	}

	errno = 0;
	time_out = (time_t)strtoll(str, &rstr, 10);

	/* The conversion may have a range faliure or no digits were found */
	if ((errno != 0) || (rstr == str)) {
		return NSERROR_BAD_PARAMETER;
	}

#else
	struct tm ltm;

	if (size < 1) {
		return NSERROR_BAD_PARAMETER;
	}

	if (strptime(str, "%s", &ltm) == NULL) {
		return NSERROR_BAD_PARAMETER;
	}

	time_out = mktime(&ltm);

#endif
	*timep = time_out;

	return NSERROR_OK;
}


/* exported function documented in utils/time.h */
nserror nsc_strntimet(const char *str, size_t size, time_t *timep)
{
	time_t result;

	result = curl_getdate(str, NULL);

	if (result == -1) {
		return NSERROR_INVALID;
	}

	*timep = result;

	return NSERROR_OK;
}