summaryrefslogtreecommitdiff
path: root/lib/mu-date.c
blob: f7a6e9352f7debd64d94b450b4c8a71366f805ef (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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/*
** Copyright (C) 2012  <djcb@djcbsoftware.nl>
**
** This program 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; either version 3, or (at your option) any
** later version.
**
** This program 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, write to the Free Software Foundation,
** Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
**
*/

#include <string.h>
#include <stdlib.h>
#include <ctype.h>

#include "mu-util.h"
#include "mu-date.h"
#include "mu-str.h"

const char*
mu_date_str_s (const char* frm, time_t t)
{
	struct tm	*tmbuf;
	static char	 buf[128];
	static int	 is_utf8 = -1;
	size_t		 len;

	if (G_UNLIKELY(is_utf8 == -1))
		is_utf8 = mu_util_locale_is_utf8 () ? 1 : 0;

	g_return_val_if_fail (frm, NULL);

	tmbuf = localtime(&t);
	len = strftime (buf, sizeof(buf) - 1, frm, tmbuf);
	if (len == 0)
		return ""; /* not necessarily an error... */

	if (!is_utf8) {
		/* charset is _not_ utf8, so we need to convert it, so
		 * the date could contain locale-specific characters*/
		gchar *conv;
		GError *err;
		err = NULL;
		conv = g_locale_to_utf8 (buf, -1, NULL, NULL, &err);
		if (err) {
			g_warning ("conversion failed: %s", err->message);
			g_error_free (err);
			strcpy (buf, "<error>");
		} else
			strncpy (buf, conv, sizeof(buf));

		g_free (conv);
	}

	return buf;
}

char*
mu_date_str (const char *frm, time_t t)
{
	return g_strdup (mu_date_str_s(frm, t));
}


const char*
mu_date_display_s (time_t t)
{
	time_t now;
	static const time_t SECS_IN_DAY = 24 * 60 * 60;

	now = time (NULL);

	if (ABS(now - t) > SECS_IN_DAY)
		return mu_date_str_s ("%x", t);
	else
		return mu_date_str_s ("%X", t);
}



time_t
mu_date_parse_hdwmy (const char *nptr)
{
	long int num;
	char *endptr;
	time_t now, delta;
	time_t never = (time_t)-1;

	g_return_val_if_fail (nptr, never);

	num = strtol  (nptr, &endptr, 10);
	if (num <= 0 || num > 9999)
		return never;

	if (endptr == NULL || *endptr == '\0')
		return never;

	switch (endptr[0]) {
	case 'h': /* hour */
		delta = num * 60 * 60; break;
	case 'd': /* day */
		delta = num * 24 * 60 * 60; break;
	case 'w': /* week */
		delta = num * 7 * 24 * 60 * 60; break;
	case 'm': /* month */
		delta = num * 30 * 24 * 60 * 60; break;
	case 'y': /* year */
		delta = num * 365 * 24 * 60 * 60; break;
	default:
		return never;
	}

	now = time(NULL);
	return delta <= now ? now - delta : never;
}


/* clear a date of anything non-numberic; static string, non-reentrant */
static char*
clear_date_s (const char *date)
{
	static char cleandate [14 + 1];
	unsigned u1, u2;

	for (u1 = u2 = 0; date[u1] != '\0'; ++u1)
		if (isdigit(date[u1]))
			cleandate[u2++] = date[u1];

	cleandate[u2] = '\0';

	return cleandate;
}



const char*
mu_date_complete_s (const char *date, gboolean is_begin)
{
	static char fulldate[14 + 1];
	static const char* full_begin = "00000101000000";
	static const char* full_end   = "99991231235959";

	char *cleardate;

	g_return_val_if_fail (date, NULL);

	cleardate = clear_date_s (date);

	strncpy (fulldate, is_begin ? full_begin : full_end,
		sizeof(fulldate));
	memcpy (fulldate, cleardate, strlen(cleardate));

	return fulldate;
}


char*
mu_date_complete (const char *date, gboolean is_begin)
{
	const char *s;

	g_return_val_if_fail (date, NULL);

	s = mu_date_complete_s (date, is_begin);
	return s ? g_strdup (s) : NULL;
}


const char*
mu_date_interpret_s (const char *datespec, gboolean is_begin)
{
	static char fulldate[14 + 1];
	time_t now, t;

	g_return_val_if_fail (datespec, NULL);

	if (mu_str_is_empty (datespec) && is_begin)
		return "000000000000"; /* beginning of time*/

	now = time(NULL);
	if (strcmp (datespec, "today") == 0) {
		strftime(fulldate, sizeof(fulldate),
			 is_begin ? "%Y%m%d000000" : "%Y%m%d235959",
			 localtime(&now));
		return fulldate;
	}

	if (mu_str_is_empty (datespec) || strcmp (datespec, "now") == 0) {
		strftime(fulldate, sizeof(fulldate), "%Y%m%d%H%M%S",
			 localtime(&now));
		return fulldate;
	}

	t = mu_date_parse_hdwmy (datespec);
	if (t != (time_t)-1) {
		strftime(fulldate, sizeof(fulldate), "%Y%m%d%H%M%S",
			 localtime(&t));
		return fulldate;
	}

	return datespec; /* nothing changed */
}


char*
mu_date_interpret (const char *datespec, gboolean is_begin)
{
	char *s;

	g_return_val_if_fail (datespec, NULL);

	s = mu_date_interpret (datespec, is_begin);
	return s ? g_strdup(s) : NULL;
}


time_t
mu_date_str_to_time_t (const char* date, gboolean local)
{
	struct tm	tm;
	char		mydate[14 + 1]; /* YYYYMMDDHHMMSS */
	time_t		t;

	memset (&tm, 0, sizeof(struct tm));
	strncpy (mydate, date, 15);
	mydate[sizeof(mydate)-1] = '\0';

	g_return_val_if_fail (date, (time_t)-1);

	tm.tm_sec   = atoi (mydate + 12);     mydate[12] = '\0';
	tm.tm_min   = atoi (mydate + 10);     mydate[10] = '\0';
	tm.tm_hour  = atoi (mydate +  8);     mydate[8]  = '\0';
	tm.tm_mday  = atoi (mydate +  6);     mydate[6]  = '\0';
	tm.tm_mon   = atoi (mydate +  4) - 1; mydate[4]  = '\0';
	tm.tm_year  = atoi (mydate) - 1900;
	tm.tm_isdst = -1;
	/* let timegm/mktime figure out the dst */

	if (local)
		t = mktime (&tm);
	else
		t = timegm (&tm); /* GNU/BSD specific */

	return t;
}

const char*
mu_date_time_t_to_str_s (time_t t, gboolean local)
{
	/* static char datestr[14 + 1]; /\* YYYYMMDDHHMMSS *\/ */
	static char		 datestr[14+1];	/* YYYYMMDDHHMMSS */
	static const char	*frm = "%Y%m%d%H%M%S";
	size_t len;

	len = strftime (datestr, sizeof(datestr), frm,
			local ? localtime (&t) : gmtime(&t));
	if (len == 0) {
		g_warning ("bug: error converting time");
		return "00000000000000";
	}

	return datestr;
}


char*
mu_date_time_t_to_str (time_t t, gboolean local)
{
	const char* str;

	str = mu_date_time_t_to_str_s (t, local);

	return str ? g_strdup(str): NULL;
}