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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
|
/*
* Copyright (C) 2004-2006, Eric Lund
* http://www.mvpmc.org/
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/*
* timestamp.c - functions to manage MythTV timestamps. Primarily,
* these allocate timestamps and convert between string
* and cmyth_timestamp_t and between time_t and
* cmyth_timestamp_t.
*/
#include <sys/types.h>
#include <stdlib.h>
#ifndef _MSC_VER
#include <unistd.h>
#endif
#include <ctype.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <cmyth_local.h>
#include <time.h>
/*
* cmyth_timestamp_create(void)
*
* Scope: PUBLIC
*
* Description
*
* Create a timestamp structure and return a pointer to the structure.
*
* Return Value:
*
* Success: A non-NULL cmyth_timestamp_t (this type is a pointer)
*
* Failure: A NULL cmyth_timestamp_t
*/
cmyth_timestamp_t
cmyth_timestamp_create(void)
{
cmyth_timestamp_t ret = ref_alloc(sizeof(*ret));
cmyth_dbg(CMYTH_DBG_DEBUG, "%s\n", __FUNCTION__);
if (!ret) {
return(NULL);
}
ret->timestamp_year = 0;
ret->timestamp_month = 0;
ret->timestamp_day = 0;
ret->timestamp_hour = 0;
ret->timestamp_minute = 0;
ret->timestamp_second = 0;
ret->timestamp_isdst = 0;
return ret;
}
/*
* cmyth_timestamp_from_string(char *str)
*
* Scope: PUBLIC
*
* Description
*
* Create and fill out a timestamp structure using the string 'str'.
* The string must be a timestamp of the forn:
*
* yyyy-mm-ddThh:mm:ss
*
* Return Value:
*
* Success: A timestamp structure (this is a pointer type)
*
* Failure: NULL
*/
cmyth_timestamp_t
cmyth_timestamp_from_string(char *str)
{
cmyth_timestamp_t ret;
unsigned int i;
int datetime = 1;
char *yyyy = &str[0];
char *MM = &str[5];
char *dd = &str[8];
char *hh = &str[11];
char *mm = &str[14];
char *ss = &str[17];
if (!str) {
cmyth_dbg(CMYTH_DBG_ERROR, "%s: NULL string\n", __FUNCTION__);
return NULL;
}
ret = cmyth_timestamp_create();
if (!ret) {
cmyth_dbg(CMYTH_DBG_ERROR, "%s: NULL timestamp\n",
__FUNCTION__);
return NULL;
}
if (strlen(str) != CMYTH_TIMESTAMP_LEN) {
datetime = 0;
if (strlen(str) != CMYTH_DATESTAMP_LEN) {
cmyth_dbg(CMYTH_DBG_ERROR,
"%s: string is not a timestamp '%s'\n",
__FUNCTION__, str);
goto err;
}
}
if ((datetime == 1) &&
((str[4] != '-') || (str[7] != '-') || (str[10] != 'T') ||
(str[13] != ':') || (str[16] != ':'))) {
cmyth_dbg(CMYTH_DBG_ERROR, "%s: string is badly formed '%s'\n",
__FUNCTION__, str);
goto err;
}
if ((datetime == 0) &&
((str[4] != '-') || (str[7] != '-'))) {
cmyth_dbg(CMYTH_DBG_ERROR, "%s: string is badly formed '%s'\n",
__FUNCTION__, str);
goto err;
}
str[4] = '\0';
str[7] = '\0';
if (datetime) {
str[10] = '\0';
str[13] = '\0';
str[16] = '\0';
}
for (i = 0;
i < (datetime ? CMYTH_TIMESTAMP_LEN : CMYTH_DATESTAMP_LEN);
++i) {
if (str[i] && !isdigit(str[i])) {
cmyth_dbg(CMYTH_DBG_ERROR,
"%s: expected numeral at '%s'[%d]\n",
__FUNCTION__, str, i);
goto err;
}
}
ret->timestamp_year = atoi(yyyy);
ret->timestamp_month = atoi(MM);
if (ret->timestamp_month > 12) {
cmyth_dbg(CMYTH_DBG_ERROR, "%s: month value too big'%s'\n",
__FUNCTION__, str);
goto err;
}
ret->timestamp_day = atoi(dd);
if (ret->timestamp_day > 31) {
cmyth_dbg(CMYTH_DBG_ERROR, "%s: day value too big'%s'\n",
__FUNCTION__, str);
goto err;
}
if (datetime == 0)
return ret;
ret->timestamp_hour = atoi(hh);
if (ret->timestamp_hour > 23) {
cmyth_dbg(CMYTH_DBG_ERROR, "%s: hour value too big'%s'\n",
__FUNCTION__, str);
goto err;
}
ret->timestamp_minute = atoi(mm);
if (ret->timestamp_minute > 59) {
cmyth_dbg(CMYTH_DBG_ERROR, "%s: minute value too big'%s'\n",
__FUNCTION__, str);
goto err;
}
ret->timestamp_second = atoi(ss);
if (ret->timestamp_second > 59) {
cmyth_dbg(CMYTH_DBG_ERROR, "%s: second value too big'%s'\n",
__FUNCTION__, str);
goto err;
}
return ret;
err:
ref_release(ret);
return NULL;
}
cmyth_timestamp_t
cmyth_timestamp_from_tm(struct tm * tm_datetime)
{
cmyth_timestamp_t ret;
ret = cmyth_timestamp_create();
if (!ret) {
cmyth_dbg(CMYTH_DBG_ERROR, "%s: NULL timestamp\n",
__FUNCTION__);
return NULL;
}
ret->timestamp_year = tm_datetime->tm_year + 1900;
ret->timestamp_month = tm_datetime->tm_mon + 1;
ret->timestamp_day = tm_datetime->tm_mday;
ret->timestamp_hour = tm_datetime->tm_hour;
ret->timestamp_minute = tm_datetime->tm_min;
ret->timestamp_second = tm_datetime->tm_sec;
ret->timestamp_isdst = tm_datetime->tm_isdst;
return ret;
}
/*
* cmyth_timestamp_from_unixtime(time_t l)
*
* Scope: PUBLIC
*
* Description
*
* Create and fill out a timestamp structure using the time_t 'l'.
*
* Return Value:
*
* Success: cmyth_timestamp_t object
*
* Failure: -(ERRNO)
*/
cmyth_timestamp_t
cmyth_timestamp_from_unixtime(time_t l)
{
struct tm tm_datetime;
localtime_r(&l,&tm_datetime);
return cmyth_timestamp_from_tm(&tm_datetime);
}
/*
* cmyth_timestamp_to_longlong( cmyth_timestamp_t ts)
*
* Scope: PUBLIC
*
* Description
*
* Create a time_t value from the timestamp structure 'ts' and
* return the result.
*
*
* Return Value:
*
* Success: time_t value > 0 (seconds from January 1, 1970)
*
* Failure: (time_t) -1
*/
time_t
cmyth_timestamp_to_unixtime(cmyth_timestamp_t ts)
{
struct tm tm;
tm.tm_sec = ts->timestamp_second;
tm.tm_min = ts->timestamp_minute;
tm.tm_hour = ts->timestamp_hour;
tm.tm_mday = ts->timestamp_day;
tm.tm_mon = ts->timestamp_month-1;
tm.tm_year = ts->timestamp_year-1900;
tm.tm_isdst = ts->timestamp_isdst;
return mktime(&tm);
}
/*
* cmyth_timestamp_to_string(char *str, cmyth_timestamp_t ts)
*
* Scope: PUBLIC
*
* Description
*
* Create a string from the timestamp structure 'ts' and put it in the
* user supplied buffer 'str'. The size of 'str' must be
* CMYTH_TIMESTAMP_LEN + 1 or this will overwrite beyond 'str'.
*
*
* Return Value:
*
* Success: 0
*
* Failure: -(ERRNO)
*/
int
cmyth_timestamp_to_string(char *str, cmyth_timestamp_t ts)
{
if (!str) {
cmyth_dbg(CMYTH_DBG_ERROR, "%s: NULL output string provided\n",
__FUNCTION__);
return -EINVAL;
}
if (!ts) {
cmyth_dbg(CMYTH_DBG_ERROR, "%s: NULL timestamp provided\n",
__FUNCTION__);
return -EINVAL;
}
sprintf(str,
"%4.4ld-%2.2ld-%2.2ldT%2.2ld:%2.2ld:%2.2ld",
ts->timestamp_year,
ts->timestamp_month,
ts->timestamp_day,
ts->timestamp_hour,
ts->timestamp_minute,
ts->timestamp_second);
return 0;
}
/*
* cmyth_timestamp_to_isostring(char *str, cmyth_timestamp_t ts)
*
* Scope: PUBLIC
*
* Description
*
* Create a string from the timestamp structure 'ts' and put it in the
* user supplied buffer 'str'. The size of 'str' must be
* CMYTH_TIMESTAMP_LEN + 1 or this will overwrite beyond 'str'.
*
*
* Return Value:
*
* Success: 0
*
* Failure: -(ERRNO)
*/
int
cmyth_timestamp_to_isostring(char *str, cmyth_timestamp_t ts)
{
if (!str) {
cmyth_dbg(CMYTH_DBG_ERROR, "%s: NULL output string provided\n",
__FUNCTION__);
return -EINVAL;
}
if (!ts) {
cmyth_dbg(CMYTH_DBG_ERROR, "%s: NULL timestamp provided\n",
__FUNCTION__);
return -EINVAL;
}
sprintf(str,
"%4.4ld-%2.2ld-%2.2ld",
ts->timestamp_year,
ts->timestamp_month,
ts->timestamp_day);
return 0;
}
int
cmyth_timestamp_to_display_string(char *str, cmyth_timestamp_t ts,
int time_format_12)
{
if (!str) {
cmyth_dbg(CMYTH_DBG_ERROR, "%s: NULL output string provided\n",
__FUNCTION__);
return -EINVAL;
}
if (!ts) {
cmyth_dbg(CMYTH_DBG_ERROR, "%s: NULL timestamp provided\n",
__FUNCTION__);
return -EINVAL;
}
if (time_format_12)
{
unsigned long hour = ts->timestamp_hour;
int pm = 0;
if (hour > 11)
{
pm = 1;
hour -= 12;
}
if (hour == 0)
hour = 12;
sprintf(str,
"%4.4ld-%2.2ld-%2.2ldT%2.2ld:%2.2ld:%2.2ld %s",
ts->timestamp_year,
ts->timestamp_month,
ts->timestamp_day,
hour,
ts->timestamp_minute,
ts->timestamp_second,
pm ? "PM" : "AM");
}
else
{
sprintf(str,
"%4.4ld-%2.2ld-%2.2ldT%2.2ld:%2.2ld:%2.2ld",
ts->timestamp_year,
ts->timestamp_month,
ts->timestamp_day,
ts->timestamp_hour,
ts->timestamp_minute,
ts->timestamp_second);
}
return 0;
}
/*
* cmyth_datetime_to_string(char *str, cmyth_timestamp_t ts)
*
* Scope: PUBLIC
*
* Description
*
* Create a string from the timestamp structure 'ts' and put it in the
* user supplied buffer 'str'. The size of 'str' must be
* CMYTH_DATETIME_LEN + 1 or this will overwrite beyond 'str'.
*
*
* Return Value:
*
* Success: 0
*
* Failure: -(ERRNO)
*/
int
cmyth_datetime_to_string(char *str, cmyth_timestamp_t ts)
{
struct tm tm_datetime;
time_t t_datetime;
if (!str) {
cmyth_dbg(CMYTH_DBG_ERROR, "%s: NULL output string provided\n",
__FUNCTION__);
return -EINVAL;
}
if (!ts) {
cmyth_dbg(CMYTH_DBG_ERROR, "%s: NULL timestamp provided\n",
__FUNCTION__);
return -EINVAL;
}
memset(&tm_datetime, 0, sizeof(tm_datetime));
tm_datetime.tm_year = ts->timestamp_year - 1900;
tm_datetime.tm_mon = ts->timestamp_month - 1;
tm_datetime.tm_mday = ts->timestamp_day;
tm_datetime.tm_hour = ts->timestamp_hour;
tm_datetime.tm_min = ts->timestamp_minute;
tm_datetime.tm_sec = ts->timestamp_second;
tm_datetime.tm_isdst = ts->timestamp_isdst;
t_datetime = mktime(&tm_datetime);
sprintf(str,
"%4.4ld-%2.2ld-%2.2ldT%2.2ld:%2.2ld:%2.2ld",
ts->timestamp_year,
ts->timestamp_month,
ts->timestamp_day,
ts->timestamp_hour,
ts->timestamp_minute,
ts->timestamp_second);
cmyth_dbg(CMYTH_DBG_ERROR, "original timestamp string: %s \n",str);
sprintf(str,"%lu",(unsigned long) t_datetime);
cmyth_dbg(CMYTH_DBG_ERROR, "time in seconds: %s \n",str);
return 0;
}
/*
* cmyth_timestamp_compare(cmyth_timestamp_t ts1, cmyth_timestamp_t ts2)
*
* Scope: PUBLIC
*
* Description
*
* Compare ts1 to ts2 and indicate whether ts1 is less than, equal to
* or greater than ts1.
*
*
* Return Value:
*
* -1: ts1 is less than (erlier than) ts2
* 0: ts1 is the same as ts2
* 1: ts1 is greater than (later than) ts2
*/
int
cmyth_timestamp_compare(cmyth_timestamp_t ts1, cmyth_timestamp_t ts2)
{
/*
* If either timestamp is NULL it is 'less than' the non-NULL one
* (this is a stretch, but it shouldn't happen). If they are both
* NULL, they are equal.
*/
if (!ts1) {
if (!ts2) {
return 0;
}
return -1;
}
if (!ts2) {
return 1;
}
if (ts1->timestamp_year != ts2->timestamp_year) {
return (ts1->timestamp_year > ts2->timestamp_year) ? 1 : -1;
}
if (ts1->timestamp_month != ts2->timestamp_month) {
return (ts1->timestamp_month > ts2->timestamp_month) ? 1 : -1;
}
if (ts1->timestamp_day != ts2->timestamp_day) {
return (ts1->timestamp_day > ts2->timestamp_day) ? 1 : -1;
}
if (ts1->timestamp_hour != ts2->timestamp_hour) {
return (ts1->timestamp_hour > ts2->timestamp_hour) ? 1 : -1;
}
if (ts1->timestamp_minute != ts2->timestamp_minute) {
return (ts1->timestamp_minute > ts2->timestamp_minute)
? 1
: -1;
}
if (ts1->timestamp_second != ts2->timestamp_second) {
return (ts1->timestamp_second > ts2->timestamp_second)
? 1
: -1;
}
return 0;
}
int
cmyth_timestamp_diff(cmyth_timestamp_t ts1, cmyth_timestamp_t ts2)
{
struct tm tm_datetime;
time_t start, end;
memset(&tm_datetime, 0, sizeof(tm_datetime));
tm_datetime.tm_year = ts1->timestamp_year - 1900;
tm_datetime.tm_mon = ts1->timestamp_month - 1;
tm_datetime.tm_mday = ts1->timestamp_day;
tm_datetime.tm_hour = ts1->timestamp_hour;
tm_datetime.tm_min = ts1->timestamp_minute;
tm_datetime.tm_sec = ts1->timestamp_second;
tm_datetime.tm_isdst = ts1->timestamp_isdst;
start = mktime(&tm_datetime);
memset(&tm_datetime, 0, sizeof(tm_datetime));
tm_datetime.tm_year = ts2->timestamp_year - 1900;
tm_datetime.tm_mon = ts2->timestamp_month - 1;
tm_datetime.tm_mday = ts2->timestamp_day;
tm_datetime.tm_hour = ts2->timestamp_hour;
tm_datetime.tm_min = ts2->timestamp_minute;
tm_datetime.tm_sec = ts2->timestamp_second;
tm_datetime.tm_isdst = ts2->timestamp_isdst;
end = mktime(&tm_datetime);
return (int)(end - start);
}
|