gwenhywfar 5.12.0
timestamp.c
Go to the documentation of this file.
1/***************************************************************************
2 begin : Wed Mar 22 2023
3 copyright : (C) 2023 by Martin Preuss
4 email : martin@libchipcard.de
5
6 ***************************************************************************
7 * *
8 * This library is free software; you can redistribute it and/or *
9 * modify it under the terms of the GNU Lesser General Public *
10 * License as published by the Free Software Foundation; either *
11 * version 2.1 of the License, or (at your option) any later version. *
12 * *
13 * This library is distributed in the hope that it will be useful, *
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
16 * Lesser General Public License for more details. *
17 * *
18 * You should have received a copy of the GNU Lesser General Public *
19 * License along with this library; if not, write to the Free Software *
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, *
21 * MA 02111-1307 USA *
22 * *
23 ***************************************************************************/
24
25#ifdef HAVE_CONFIG_H
26# include <config.h>
27#endif
28
29#include "./timestamp_p.h"
30
31#include <gwenhywfar/misc.h>
32#include <gwenhywfar/debug.h>
33
34
35
36/* ------------------------------------------------------------------------------------------------
37 * forward declarations
38 * ------------------------------------------------------------------------------------------------
39 */
40
41static int _calcJulian(int y, int m, int d);
42static void _writeAsString(GWEN_TIMESTAMP *tstamp);
43static void _setDate(GWEN_TIMESTAMP *tstamp, int year, int month, int day);
44static void _setTime(GWEN_TIMESTAMP *tstamp, int hour, int minute, int second);
45static void _setFromInt64(GWEN_TIMESTAMP *tstamp, int64_t i);
46
47
48
49/* ------------------------------------------------------------------------------------------------
50 * implementations
51 * ------------------------------------------------------------------------------------------------
52 */
53
54
55GWEN_TIMESTAMP *GWEN_Timestamp_new(int year, int month, int day,
56 int hour, int minute, int second)
57{
58 GWEN_TIMESTAMP *tstamp=NULL;
59
61 GWEN_Timestamp_SetDateAndTime(tstamp, year, month, day, hour, minute, second);
62 return tstamp;
63}
64
65
66
68{
69 if (tstampSrc) {
70 GWEN_TIMESTAMP *tstamp;
71
73 memmove(tstamp, tstampSrc, sizeof(GWEN_TIMESTAMP));
74 return tstamp;
75 }
76 return NULL;
77}
78
79
80
82{
83 if (tstamp) {
84 GWEN_FREE_OBJECT(tstamp);
85 }
86}
87
88
89
90const char *GWEN_Timestamp_GetString(const GWEN_TIMESTAMP *tstamp)
91{
92 return tstamp->asString;
93}
94
95
96
98{
99 int64_t result;
100
101 result=
102 (tstamp->second)+
103 ((int64_t)(tstamp->minute)*60)+
104 ((int64_t)(tstamp->hour)*60*60)+
105 ((int64_t)(tstamp->julian)*24*60*60);
106
107 return result;
108}
109
110
111
113{
114 GWEN_TIMESTAMP *tstamp=NULL;
115
117 _setFromInt64(tstamp, i);
118 return tstamp;
119}
120
121
122
123void _setFromInt64(GWEN_TIMESTAMP *tstamp, int64_t i)
124{
125 if (tstamp) {
126 tstamp->second=i%60;
127 i/=60;
128 tstamp->minute=i%60;
129 i/=60;
130 tstamp->hour=i%24;
131 i/=24;
133 }
134}
135
136
137
139{
140 if (ltm) {
141 GWEN_TIMESTAMP *tstamp;
142
143 tstamp=GWEN_Timestamp_new(ltm->tm_year+1900, ltm->tm_mon+1, ltm->tm_mday,
144 ltm->tm_hour, ltm->tm_min, ltm->tm_sec);
145 return tstamp;
146 }
147
148 return NULL;
149}
150
151
152
154{
155 return GWEN_Timestamp_fromStructTm(localtime(&ti));
156}
157
158
159
161{
162 return GWEN_Timestamp_fromStructTm(gmtime(&ti));
163}
164
165
166
168{
169 struct tm ti;
170 struct tm *tp;
171 time_t tt;
172
173 tt=time(NULL);
174 tp=localtime(&tt);
175 assert(tp);
176 memmove(&ti, tp, sizeof(ti));
177
178 ti.tm_sec=tstamp->second;
179 ti.tm_min=tstamp->minute;
180 ti.tm_hour=tstamp->hour;
181
182 ti.tm_year=tstamp->year-1900;
183 ti.tm_mon=tstamp->month-1;
184 ti.tm_mday=tstamp->day;
185 ti.tm_yday=0;
186 ti.tm_wday=0;
187 tt=mktime(&ti);
188 assert(tt!=(time_t)-1);
189 return tt;
190}
191
192
193
195{
196 time_t ti;
197
198 ti=time(NULL);
199 return GWEN_Timestamp_fromStructTm(localtime(&ti));
200}
201
202
203
205{
206 time_t ti;
207
208 ti=time(NULL);
209 return GWEN_Timestamp_fromStructTm(gmtime(&ti));
210}
211
212
213
215 int year, int month, int day,
216 int hour, int minute, int second)
217{
218 _setDate(tstamp, year, month, day);
219 _setTime(tstamp, hour, minute, second);
220 _writeAsString(tstamp);
221}
222
223
224
225void GWEN_Timestamp_SetDate(GWEN_TIMESTAMP *tstamp, int year, int month, int day)
226{
227 _setDate(tstamp, year, month, day);
228 _writeAsString(tstamp);
229}
230
231
232
234{
235 int l, n, i, j;
236
237 l=julian+68569;
238 n=(4*l)/146097;
239 l=l-(146097*n+3)/4;
240 i=(4000*(l+1))/1461001;
241 l=l-(1461*i)/4+31;
242 j=(80*l)/2447;
243
244 tstamp->day=l-(2447*j)/80;
245 l=j/11;
246 tstamp->month=j+2-(12*l);
247 tstamp->year=100*(n-49)+i+l;
248 tstamp->julian=julian;
249
250 _writeAsString(tstamp);
251}
252
253
254
255void GWEN_Timestamp_SetTime(GWEN_TIMESTAMP *tstamp, int hour, int minute, int second)
256{
257 _setTime(tstamp, hour, minute, second);
258 _writeAsString(tstamp);
259}
260
261
262
264{
265 return tstamp->year;
266}
267
268
269
271{
272 return tstamp->month;
273}
274
275
276
278{
279 return tstamp->day;
280}
281
282
283
285{
286 return tstamp->hour;
287}
288
289
290
292{
293 return tstamp->minute;
294}
295
296
297
299{
300 return tstamp->second;
301}
302
303
304
305int GWEN_Timestamp_Compare(const GWEN_TIMESTAMP *tstamp1, const GWEN_TIMESTAMP *tstamp0)
306{
307 if (tstamp0 && tstamp1) {
308 int64_t v1, v0;
309
310 v1=GWEN_Timestamp_toInt64(tstamp1);
311 v0=GWEN_Timestamp_toInt64(tstamp0);
312 if (v1==v0)
313 return 0;
314 else if (v1>v0)
315 return 1;
316 else
317 return -1;
318
319 }
320 else if (tstamp0)
321 return 1;
322 else if (tstamp1)
323 return -1;
324 else
325 return 0;
326}
327
328
329
330
331int _calcJulian(int y, int m, int d)
332{
333 return (1461*(y+4800+(m-14)/12))/4+
334 (367*(m-2-12*((m-14)/12)))/12-
335 (3*((y+4900+(m-14)/12)/100))/4+
336 d-32075;
337}
338
339
340
341void _setDate(GWEN_TIMESTAMP *tstamp, int year, int month, int day)
342{
343 tstamp->year=year;
344 tstamp->month=month;
345 tstamp->day=day;
346 tstamp->julian=_calcJulian(year, month, day);
347}
348
349
350
351void _setTime(GWEN_TIMESTAMP *tstamp, int hour, int minute, int second)
352{
353 tstamp->hour=hour;
354 tstamp->minute=minute;
355 tstamp->second=second;
356}
357
358
359
361{
362 char *ptr;
363 int x;
364
365 ptr=tstamp->asString+14;
366 *(ptr--)=0;
367
368 x=tstamp->second;
369 *(ptr--)='0'+(x%10);
370 x/=10;
371 *(ptr--)='0'+(x%10);
372
373 x=tstamp->minute;
374 *(ptr--)='0'+(x%10);
375 x/=10;
376 *(ptr--)='0'+(x%10);
377
378 x=tstamp->hour;
379 *(ptr--)='0'+(x%10);
380 x/=10;
381 *(ptr--)='0'+(x%10);
382
383 x=tstamp->day;
384 *(ptr--)='0'+(x%10);
385 x/=10;
386 *(ptr--)='0'+(x%10);
387
388 x=tstamp->month;
389 *(ptr--)='0'+(x%10);
390 x/=10;
391 *(ptr--)='0'+(x%10);
392
393 x=tstamp->year;
394 *(ptr--)='0'+(x%10);
395 x/=10;
396 *(ptr--)='0'+(x%10);
397 x/=10;
398 *(ptr--)='0'+(x%10);
399 x/=10;
400 *ptr='0'+(x%10);
401}
402
403
404
406{
407 if (s && strlen(s)>=14) {
408 int year, month, day, hour, min, sec;
409 GWEN_TIMESTAMP *result;
410 const char *originalPtr;
411
412 originalPtr=s;
413 year=*(s++)-'0';
414 year*=10;
415 year+=*(s++)-'0';
416 year*=10;
417 year+=*(s++)-'0';
418 year*=10;
419 year+=*(s++)-'0';
420
421 month=*(s++)-'0';
422 month*=10;
423 month+=*(s++)-'0';
424
425 day=*(s++)-'0';
426 day*=10;
427 day+=*(s++)-'0';
428
429 hour=*(s++)-'0';
430 hour*=10;
431 hour+=*(s++)-'0';
432
433 min=*(s++)-'0';
434 min*=10;
435 min+=*(s++)-'0';
436
437 sec=*(s++)-'0';
438 sec*=10;
439 sec+=*(s++)-'0';
440
441 result=GWEN_Timestamp_new(year, month, day, hour, min, sec);
442 if (!result) {
443 DBG_INFO(GWEN_LOGDOMAIN, "Bad timestamp string [%s]", originalPtr);
444 }
445 return result;
446 }
447 else {
448 DBG_INFO(GWEN_LOGDOMAIN, "Bad timestamp string [%s]", s?s:"<empty>");
449 return NULL;
450 }
451}
452
453
454
456{
457 const char *s;
458
459 s=GWEN_Timestamp_GetString(tstamp);
461 return 0;
462}
463
464
465
467{
468 const char *s;
469
470 s=GWEN_DB_GetCharValue(db, "timestamp", 0, NULL);
471 if (s && *s) {
472 GWEN_TIMESTAMP *tstamp;
473
475 if (tstamp==NULL) {
476 DBG_INFO(GWEN_LOGDOMAIN, "Invalid timestamp [%s]", s);
477 return NULL;
478 }
479 return tstamp;
480 }
481 else {
482 DBG_VERBOUS(GWEN_LOGDOMAIN, "no or empty timestamp");
483 return NULL;
484 }
485
486}
487
488
490{
491 if (tstamp && seconds!=0) {
492 int64_t ti;
493
494 ti=GWEN_Timestamp_toInt64(tstamp)+seconds;
495 _setFromInt64(tstamp, ti);
496 }
497}
498
499
500
501
502
503/* include test code */
504#include "timestamp-t.c"
505
506
507
508
#define NULL
Definition binreloc.c:300
const char * GWEN_DB_GetCharValue(GWEN_DB_NODE *n, const char *path, int idx, const char *defVal)
Definition db.c:971
int GWEN_DB_SetCharValue(GWEN_DB_NODE *n, uint32_t flags, const char *path, const char *val)
Definition db.c:997
#define GWEN_DB_FLAGS_OVERWRITE_VARS
Definition db.h:121
struct GWEN_DB_NODE GWEN_DB_NODE
Definition db.h:228
#define DBG_VERBOUS(dbg_logger, format,...)
Definition debug.h:224
#define DBG_INFO(dbg_logger, format,...)
Definition debug.h:181
#define GWEN_LOGDOMAIN
Definition logger.h:35
#define GWEN_FREE_OBJECT(varname)
Definition memory.h:61
#define GWEN_NEW_OBJECT(typ, varname)
Definition memory.h:55
static void _setFromInt64(GWEN_TIMESTAMP *tstamp, int64_t i)
Definition timestamp.c:123
int GWEN_Timestamp_Compare(const GWEN_TIMESTAMP *tstamp1, const GWEN_TIMESTAMP *tstamp0)
Definition timestamp.c:305
GWEN_TIMESTAMP * GWEN_Timestamp_fromLocalTime(time_t ti)
Definition timestamp.c:153
int GWEN_Timestamp_toDb(const GWEN_TIMESTAMP *tstamp, GWEN_DB_NODE *db)
Definition timestamp.c:455
int GWEN_Timestamp_GetMinute(const GWEN_TIMESTAMP *tstamp)
Definition timestamp.c:291
GWEN_TIMESTAMP * GWEN_Timestamp_fromGmTime(time_t ti)
Definition timestamp.c:160
void GWEN_Timestamp_SetDateAndTime(GWEN_TIMESTAMP *tstamp, int year, int month, int day, int hour, int minute, int second)
Definition timestamp.c:214
void GWEN_Timestamp_SetJulianDate(GWEN_TIMESTAMP *tstamp, int julian)
Definition timestamp.c:233
GWEN_TIMESTAMP * GWEN_Timestamp_fromString(const char *s)
Definition timestamp.c:405
GWEN_TIMESTAMP * GWEN_Timestamp_fromDb(GWEN_DB_NODE *db)
Definition timestamp.c:466
int GWEN_Timestamp_GetHour(const GWEN_TIMESTAMP *tstamp)
Definition timestamp.c:284
GWEN_TIMESTAMP * GWEN_Timestamp_new(int year, int month, int day, int hour, int minute, int second)
Definition timestamp.c:55
int GWEN_Timestamp_GetYear(const GWEN_TIMESTAMP *tstamp)
Definition timestamp.c:263
static void _setDate(GWEN_TIMESTAMP *tstamp, int year, int month, int day)
Definition timestamp.c:341
static int _calcJulian(int y, int m, int d)
Definition timestamp.c:331
int GWEN_Timestamp_GetSecond(const GWEN_TIMESTAMP *tstamp)
Definition timestamp.c:298
GWEN_TIMESTAMP * GWEN_Timestamp_dup(const GWEN_TIMESTAMP *tstampSrc)
Definition timestamp.c:67
const char * GWEN_Timestamp_GetString(const GWEN_TIMESTAMP *tstamp)
Definition timestamp.c:90
void GWEN_Timestamp_SetTime(GWEN_TIMESTAMP *tstamp, int hour, int minute, int second)
Definition timestamp.c:255
int GWEN_Timestamp_GetMonth(const GWEN_TIMESTAMP *tstamp)
Definition timestamp.c:270
GWEN_TIMESTAMP * GWEN_Timestamp_fromInt64(int64_t i)
Definition timestamp.c:112
void GWEN_Timestamp_AddSeconds(GWEN_TIMESTAMP *tstamp, int seconds)
Definition timestamp.c:489
static void _setTime(GWEN_TIMESTAMP *tstamp, int hour, int minute, int second)
Definition timestamp.c:351
int GWEN_Timestamp_GetDay(const GWEN_TIMESTAMP *tstamp)
Definition timestamp.c:277
void GWEN_Timestamp_SetDate(GWEN_TIMESTAMP *tstamp, int year, int month, int day)
Definition timestamp.c:225
void GWEN_Timestamp_free(GWEN_TIMESTAMP *tstamp)
Definition timestamp.c:81
static void _writeAsString(GWEN_TIMESTAMP *tstamp)
Definition timestamp.c:360
GWEN_TIMESTAMP * GWEN_Timestamp_NowInGmTime()
Definition timestamp.c:204
time_t GWEN_Timestamp_toTimeT(const GWEN_TIMESTAMP *tstamp)
Definition timestamp.c:167
GWEN_TIMESTAMP * GWEN_Timestamp_fromStructTm(const struct tm *ltm)
Definition timestamp.c:138
GWEN_TIMESTAMP * GWEN_Timestamp_NowInLocalTime()
Definition timestamp.c:194
int64_t GWEN_Timestamp_toInt64(const GWEN_TIMESTAMP *tstamp)
Definition timestamp.c:97
struct GWEN_TIMESTAMP GWEN_TIMESTAMP
Definition timestamp.h:36