gwenhywfar 5.11.2beta
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);
45
46
47
48/* ------------------------------------------------------------------------------------------------
49 * implementations
50 * ------------------------------------------------------------------------------------------------
51 */
52
53
54GWEN_TIMESTAMP *GWEN_Timestamp_new(int year, int month, int day,
55 int hour, int minute, int second)
56{
57 GWEN_TIMESTAMP *tstamp=NULL;
58
60 GWEN_Timestamp_SetDateAndTime(tstamp, year, month, day, hour, minute, second);
61 return tstamp;
62}
63
64
65
67{
68 if (tstampSrc) {
69 GWEN_TIMESTAMP *tstamp;
70
72 memmove(tstamp, tstampSrc, sizeof(GWEN_TIMESTAMP));
73 return tstamp;
74 }
75 return NULL;
76}
77
78
79
81{
82 if (tstamp) {
83 GWEN_FREE_OBJECT(tstamp);
84 }
85}
86
87
88
89const char *GWEN_Timestamp_GetString(const GWEN_TIMESTAMP *tstamp)
90{
91 return tstamp->asString;
92}
93
94
95
97{
98 int64_t result;
99
100 result=
101 (tstamp->second)+
102 ((int64_t)(tstamp->minute)*60)+
103 ((int64_t)(tstamp->hour)*60*60)+
104 ((int64_t)(tstamp->julian)*24*60*60);
105
106 return result;
107}
108
109
110
112{
113 GWEN_TIMESTAMP *tstamp=NULL;
114
116 tstamp->second=i%60;
117 i/=60;
118 tstamp->minute=i%60;
119 i/=60;
120 tstamp->hour=i%24;
121 i/=24;
122
124
125 return tstamp;
126}
127
128
129
131{
132 if (ltm) {
133 GWEN_TIMESTAMP *tstamp;
134
135 tstamp=GWEN_Timestamp_new(ltm->tm_year+1900, ltm->tm_mon+1, ltm->tm_mday,
136 ltm->tm_hour, ltm->tm_min, ltm->tm_sec);
137 return tstamp;
138 }
139
140 return NULL;
141}
142
143
144
146{
147 return GWEN_Timestamp_fromStructTm(localtime(&ti));
148}
149
150
151
153{
154 return GWEN_Timestamp_fromStructTm(gmtime(&ti));
155}
156
157
158
160{
161 struct tm ti;
162 struct tm *tp;
163 time_t tt;
164
165 tt=time(NULL);
166 tp=localtime(&tt);
167 assert(tp);
168 memmove(&ti, tp, sizeof(ti));
169
170 ti.tm_sec=tstamp->second;
171 ti.tm_min=tstamp->minute;
172 ti.tm_hour=tstamp->hour;
173
174 ti.tm_year=tstamp->year-1900;
175 ti.tm_mon=tstamp->month-1;
176 ti.tm_mday=tstamp->day;
177 ti.tm_yday=0;
178 ti.tm_wday=0;
179 tt=mktime(&ti);
180 assert(tt!=(time_t)-1);
181 return tt;
182}
183
184
185
187{
188 time_t ti;
189
190 ti=time(NULL);
191 return GWEN_Timestamp_fromStructTm(localtime(&ti));
192}
193
194
195
197{
198 time_t ti;
199
200 ti=time(NULL);
201 return GWEN_Timestamp_fromStructTm(gmtime(&ti));
202}
203
204
205
207 int year, int month, int day,
208 int hour, int minute, int second)
209{
210 _setDate(tstamp, year, month, day);
211 _setTime(tstamp, hour, minute, second);
212 _writeAsString(tstamp);
213}
214
215
216
217void GWEN_Timestamp_SetDate(GWEN_TIMESTAMP *tstamp, int year, int month, int day)
218{
219 _setDate(tstamp, year, month, day);
220 _writeAsString(tstamp);
221}
222
223
224
226{
227 int l, n, i, j;
228
229 l=julian+68569;
230 n=(4*l)/146097;
231 l=l-(146097*n+3)/4;
232 i=(4000*(l+1))/1461001;
233 l=l-(1461*i)/4+31;
234 j=(80*l)/2447;
235
236 tstamp->day=l-(2447*j)/80;
237 l=j/11;
238 tstamp->month=j+2-(12*l);
239 tstamp->year=100*(n-49)+i+l;
240 tstamp->julian=julian;
241
242 _writeAsString(tstamp);
243}
244
245
246
247void GWEN_Timestamp_SetTime(GWEN_TIMESTAMP *tstamp, int hour, int minute, int second)
248{
249 _setTime(tstamp, hour, minute, second);
250 _writeAsString(tstamp);
251}
252
253
254
256{
257 return tstamp->year;
258}
259
260
261
263{
264 return tstamp->month;
265}
266
267
268
270{
271 return tstamp->day;
272}
273
274
275
277{
278 return tstamp->hour;
279}
280
281
282
284{
285 return tstamp->minute;
286}
287
288
289
291{
292 return tstamp->second;
293}
294
295
296
297int GWEN_Timestamp_Compare(const GWEN_TIMESTAMP *tstamp1, const GWEN_TIMESTAMP *tstamp0)
298{
299 if (tstamp0 && tstamp1) {
300 int64_t v1, v0;
301
302 v1=GWEN_Timestamp_toInt64(tstamp1);
303 v0=GWEN_Timestamp_toInt64(tstamp0);
304 if (v1==v0)
305 return 0;
306 else if (v1>v0)
307 return 1;
308 else
309 return -1;
310
311 }
312 else if (tstamp0)
313 return 1;
314 else if (tstamp1)
315 return -1;
316 else
317 return 0;
318}
319
320
321
322
323int _calcJulian(int y, int m, int d)
324{
325 return (1461*(y+4800+(m-14)/12))/4+
326 (367*(m-2-12*((m-14)/12)))/12-
327 (3*((y+4900+(m-14)/12)/100))/4+
328 d-32075;
329}
330
331
332
333void _setDate(GWEN_TIMESTAMP *tstamp, int year, int month, int day)
334{
335 tstamp->year=year;
336 tstamp->month=month;
337 tstamp->day=day;
338 tstamp->julian=_calcJulian(year, month, day);
339}
340
341
342
343void _setTime(GWEN_TIMESTAMP *tstamp, int hour, int minute, int second)
344{
345 tstamp->hour=hour;
346 tstamp->minute=minute;
347 tstamp->second=second;
348}
349
350
351
353{
354 char *ptr;
355 int x;
356
357 ptr=tstamp->asString+14;
358 *(ptr--)=0;
359
360 x=tstamp->second;
361 *(ptr--)='0'+(x%10);
362 x/=10;
363 *(ptr--)='0'+(x%10);
364
365 x=tstamp->minute;
366 *(ptr--)='0'+(x%10);
367 x/=10;
368 *(ptr--)='0'+(x%10);
369
370 x=tstamp->hour;
371 *(ptr--)='0'+(x%10);
372 x/=10;
373 *(ptr--)='0'+(x%10);
374
375 x=tstamp->day;
376 *(ptr--)='0'+(x%10);
377 x/=10;
378 *(ptr--)='0'+(x%10);
379
380 x=tstamp->month;
381 *(ptr--)='0'+(x%10);
382 x/=10;
383 *(ptr--)='0'+(x%10);
384
385 x=tstamp->year;
386 *(ptr--)='0'+(x%10);
387 x/=10;
388 *(ptr--)='0'+(x%10);
389 x/=10;
390 *(ptr--)='0'+(x%10);
391 x/=10;
392 *ptr='0'+(x%10);
393}
394
395
396
398{
399 if (s && strlen(s)>=14) {
400 int year, month, day, hour, min, sec;
401 GWEN_TIMESTAMP *result;
402 const char *originalPtr;
403
404 originalPtr=s;
405 year=*(s++)-'0';
406 year*=10;
407 year+=*(s++)-'0';
408 year*=10;
409 year+=*(s++)-'0';
410 year*=10;
411 year+=*(s++)-'0';
412
413 month=*(s++)-'0';
414 month*=10;
415 month+=*(s++)-'0';
416
417 day=*(s++)-'0';
418 day*=10;
419 day+=*(s++)-'0';
420
421 hour=*(s++)-'0';
422 hour*=10;
423 hour+=*(s++)-'0';
424
425 min=*(s++)-'0';
426 min*=10;
427 min+=*(s++)-'0';
428
429 sec=*(s++)-'0';
430 sec*=10;
431 sec+=*(s++)-'0';
432
433 result=GWEN_Timestamp_new(year, month, day, hour, min, sec);
434 if (!result) {
435 DBG_INFO(GWEN_LOGDOMAIN, "Bad timestamp string [%s]", originalPtr);
436 }
437 return result;
438 }
439 else {
440 DBG_INFO(GWEN_LOGDOMAIN, "Bad timestamp string [%s]", s?s:"<empty>");
441 return NULL;
442 }
443}
444
445
446
448{
449 const char *s;
450
451 s=GWEN_Timestamp_GetString(tstamp);
453 return 0;
454}
455
456
457
459{
460 const char *s;
461
462 s=GWEN_DB_GetCharValue(db, "timestamp", 0, NULL);
463 if (s && *s) {
464 GWEN_TIMESTAMP *tstamp;
465
467 if (tstamp==NULL) {
468 DBG_INFO(GWEN_LOGDOMAIN, "Invalid timestamp [%s]", s);
469 return NULL;
470 }
471 return tstamp;
472 }
473 else {
474 DBG_VERBOUS(GWEN_LOGDOMAIN, "no or empty timestamp");
475 return NULL;
476 }
477
478}
479
480
481
482
483
484
485/* include test code */
486#include "timestamp-t.c"
487
488
489
490
#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
int GWEN_Timestamp_Compare(const GWEN_TIMESTAMP *tstamp1, const GWEN_TIMESTAMP *tstamp0)
Definition timestamp.c:297
GWEN_TIMESTAMP * GWEN_Timestamp_fromLocalTime(time_t ti)
Definition timestamp.c:145
int GWEN_Timestamp_toDb(const GWEN_TIMESTAMP *tstamp, GWEN_DB_NODE *db)
Definition timestamp.c:447
int GWEN_Timestamp_GetMinute(const GWEN_TIMESTAMP *tstamp)
Definition timestamp.c:283
GWEN_TIMESTAMP * GWEN_Timestamp_fromGmTime(time_t ti)
Definition timestamp.c:152
void GWEN_Timestamp_SetDateAndTime(GWEN_TIMESTAMP *tstamp, int year, int month, int day, int hour, int minute, int second)
Definition timestamp.c:206
void GWEN_Timestamp_SetJulianDate(GWEN_TIMESTAMP *tstamp, int julian)
Definition timestamp.c:225
GWEN_TIMESTAMP * GWEN_Timestamp_fromString(const char *s)
Definition timestamp.c:397
GWEN_TIMESTAMP * GWEN_Timestamp_fromDb(GWEN_DB_NODE *db)
Definition timestamp.c:458
int GWEN_Timestamp_GetHour(const GWEN_TIMESTAMP *tstamp)
Definition timestamp.c:276
GWEN_TIMESTAMP * GWEN_Timestamp_new(int year, int month, int day, int hour, int minute, int second)
Definition timestamp.c:54
int GWEN_Timestamp_GetYear(const GWEN_TIMESTAMP *tstamp)
Definition timestamp.c:255
static void _setDate(GWEN_TIMESTAMP *tstamp, int year, int month, int day)
Definition timestamp.c:333
static int _calcJulian(int y, int m, int d)
Definition timestamp.c:323
int GWEN_Timestamp_GetSecond(const GWEN_TIMESTAMP *tstamp)
Definition timestamp.c:290
GWEN_TIMESTAMP * GWEN_Timestamp_dup(const GWEN_TIMESTAMP *tstampSrc)
Definition timestamp.c:66
const char * GWEN_Timestamp_GetString(const GWEN_TIMESTAMP *tstamp)
Definition timestamp.c:89
void GWEN_Timestamp_SetTime(GWEN_TIMESTAMP *tstamp, int hour, int minute, int second)
Definition timestamp.c:247
int GWEN_Timestamp_GetMonth(const GWEN_TIMESTAMP *tstamp)
Definition timestamp.c:262
GWEN_TIMESTAMP * GWEN_Timestamp_fromInt64(int64_t i)
Definition timestamp.c:111
static void _setTime(GWEN_TIMESTAMP *tstamp, int hour, int minute, int second)
Definition timestamp.c:343
int GWEN_Timestamp_GetDay(const GWEN_TIMESTAMP *tstamp)
Definition timestamp.c:269
void GWEN_Timestamp_SetDate(GWEN_TIMESTAMP *tstamp, int year, int month, int day)
Definition timestamp.c:217
void GWEN_Timestamp_free(GWEN_TIMESTAMP *tstamp)
Definition timestamp.c:80
static void _writeAsString(GWEN_TIMESTAMP *tstamp)
Definition timestamp.c:352
GWEN_TIMESTAMP * GWEN_Timestamp_NowInGmTime()
Definition timestamp.c:196
time_t GWEN_Timestamp_toTimeT(const GWEN_TIMESTAMP *tstamp)
Definition timestamp.c:159
GWEN_TIMESTAMP * GWEN_Timestamp_fromStructTm(const struct tm *ltm)
Definition timestamp.c:130
GWEN_TIMESTAMP * GWEN_Timestamp_NowInLocalTime()
Definition timestamp.c:186
int64_t GWEN_Timestamp_toInt64(const GWEN_TIMESTAMP *tstamp)
Definition timestamp.c:96
struct GWEN_TIMESTAMP GWEN_TIMESTAMP
Definition timestamp.h:36