libamxc  1.10.3
C Generic Data Containers
Timestamp

Data Structures

struct  _timestamp
 The timestamp structure (unix epoch time). More...
 

Typedefs

typedef struct _timestamp amxc_ts_t
 The timestamp structure (unix epoch time). More...
 

Functions

int amxc_ts_now (amxc_ts_t *tsp)
 Takes current time as unix epoch time. More...
 
int amxc_ts_parse (amxc_ts_t *tsp, const char *str, size_t len)
 Transforms the given string in to unix epoch time. More...
 
size_t amxc_ts_format (const amxc_ts_t *tsp, char *dst, size_t len)
 Transforms unix epoch time to a string. More...
 
size_t amxc_ts_format_precision (const amxc_ts_t *tsp, char *dst, size_t len, int precision)
 Transforms unix epoch time to a string. More...
 
int amxc_ts_compare (const amxc_ts_t *tsp1, const amxc_ts_t *tsp2)
 Checks if tsp1 comes after tsp2. More...
 
bool amxc_ts_is_valid (const amxc_ts_t *tsp)
 Checks if a timestamp is valid. More...
 
int amxc_ts_to_tm_utc (const amxc_ts_t *tsp, struct tm *tmp)
 Converts timestamp in unix epoch time to a struct tm type in UTC time. More...
 
int amxc_ts_to_tm_local (const amxc_ts_t *tsp, struct tm *tmp)
 Converts timestamp in unix epoch time to a struct tm type in local time. More...
 
int amxc_ts_to_local (amxc_ts_t *tsp)
 Adds the local time offset to the timestamp structure. More...
 
int amxc_ts_from_tm (amxc_ts_t *const tsp, struct tm *tmp)
 Converts a broken down time in a struct tm to a timestamp structure. More...
 

Detailed Description

Typedef Documentation

◆ amxc_ts_t

typedef struct _timestamp amxc_ts_t

The timestamp structure (unix epoch time).

Function Documentation

◆ amxc_ts_compare()

int amxc_ts_compare ( const amxc_ts_t tsp1,
const amxc_ts_t tsp2 
)

Checks if tsp1 comes after tsp2.

Parameters
tsp1a pointer to a timestamp structure
tsp2a pointer to a timestamp structure
Returns
0 if input is invalid 1 if tsp1 comes after tsp2 -1 if tsp2 comes after tsp1

Definition at line 506 of file amxc_timestamp.c.

506  {
507  when_null(tsp1, exit);
508  when_null(tsp2, exit);
509 
510  if(tsp1->sec < tsp2->sec) {
511  return -1;
512  }
513  if(tsp1->sec > tsp2->sec) {
514  return 1;
515  }
516  if(tsp1->nsec < tsp2->nsec) {
517  return -1;
518  }
519  if(tsp1->nsec > tsp2->nsec) {
520  return 1;
521  }
522 
523 exit:
524  return 0;
525 }
#define when_null(x, l)
Definition: amxc_macros.h:126
int32_t nsec

◆ amxc_ts_format()

size_t amxc_ts_format ( const amxc_ts_t tsp,
char *  dst,
size_t  len 
)

Transforms unix epoch time to a string.

Transforms unix epoch time to string in RFC3339 compatible format.

Use amxc_ts_format_precision to specify the precision.

Note
The length of the provided string buffer must be at least 21 bytes. The buffer length must be 36 bytes to be able to store nanoseconds and time zone offset. The buffer must be pre-allocated.
Parameters
tspa pointer to a timestamp structure
dsta pointer to a string
lensize of the string buffer
Returns
length of string dst

Definition at line 461 of file amxc_timestamp.c.

463  {
464  size_t retval = 0;
465  uint32_t f;
466  int precision;
467 
468  when_null(tsp, exit);
469  when_null(dst, exit);
470  when_true(!amxc_ts_is_valid(tsp), exit);
471 
472  f = tsp->nsec;
473  if(f == 0) {
474  precision = 0;
475  } else {
476  if((f % 1000000) == 0) {
477  precision = 3;
478  } else if((f % 1000) == 0) {
479  precision = 6;
480  } else {
481  precision = 9;
482  }
483  }
484  retval = timestamp_format_internal(dst, len, tsp, precision);
485 
486 exit:
487  return retval;
488 }
#define when_true(x, l)
Definition: amxc_macros.h:134
static size_t timestamp_format_internal(char *dst, size_t len, const amxc_ts_t *tsp, const int precision)
bool amxc_ts_is_valid(const amxc_ts_t *tsp)
Checks if a timestamp is valid.

◆ amxc_ts_format_precision()

size_t amxc_ts_format_precision ( const amxc_ts_t tsp,
char *  dst,
size_t  len,
int  precision 
)

Transforms unix epoch time to a string.

Transforms unix epoch time to string in RFC3339 compatible format.

Note
The length of the provided string buffer must be at least 21 bytes. The buffer length must be 36 bytes to be able to store nanoseconds and time zone offset. The buffer must be pre-allocated.
Parameters
tspa pointer to a timestamp structure
dsta pointer to a string storing the UTC time
lensize of the string buffer
precisionnumber of digits used to express nanoseconds
Returns
length of string dst

Definition at line 490 of file amxc_timestamp.c.

493  {
494  int retval = 0;
495 
496  when_null(tsp, exit);
497  when_null(dst, exit);
498 
499  when_true(!amxc_ts_is_valid(tsp) || precision < 0 || precision > 9, exit);
500  retval = timestamp_format_internal(dst, len, tsp, precision);
501 
502 exit:
503  return retval;
504 }

◆ amxc_ts_from_tm()

int amxc_ts_from_tm ( amxc_ts_t *const  tsp,
struct tm *  tmp 
)

Converts a broken down time in a struct tm to a timestamp structure.

Using this function a timestamp structure is initialized using a struct tm as input.

Note
As struct tm doesn't contain timezone information, the offset will be set to 0.
Parameters
tspa pointer to a timestamp structure
tmppointer to struct tm, containing the broken down time.
Returns
-1 if an error occurs 0 if the action succeeded

Definition at line 574 of file amxc_timestamp.c.

574  {
575  int rv = -1;
576  time_t epoch = 0;
577 
578  when_null(tsp, exit);
579  when_null(tmp, exit);
580 
581  epoch = mktime(tmp);
582  when_true(epoch == -1, exit);
583 
584  tsp->sec = epoch;
585  tsp->nsec = 0;
586  tsp->offset = 0;
587 
588  rv = 0;
589 
590 exit:
591  return rv;
592 }
int16_t offset

◆ amxc_ts_is_valid()

bool amxc_ts_is_valid ( const amxc_ts_t tsp)

Checks if a timestamp is valid.

Parameters
tspa pointer to a timestamp structure
Returns
true if the timestamp is valid false if the timestamp is invalid

Definition at line 527 of file amxc_timestamp.c.

527  {
528  bool retval = false;
529  int64_t sec = 0;
530  when_null(tsp, exit);
531 
532  sec = tsp->sec + tsp->offset * 60;
533  if((sec < MIN_SEC) || (sec > MAX_SEC) ||
534  ( tsp->nsec < 0) || ( tsp->nsec > 999999999) ||
535  ( tsp->offset < -1439) || ( tsp->offset > 1439)) {
536  goto exit;
537  }
538 
539  retval = true;
540 
541 exit:
542  return retval;
543 }
#define MAX_SEC
#define MIN_SEC

◆ amxc_ts_now()

int amxc_ts_now ( amxc_ts_t tsp)

Takes current time as unix epoch time.

Fills the given amxc_ts_t structure with the current time in seconds from the unix epoch time.

Parameters
tspa pointer to the timestamp structure
Returns
-1 if an error occurred. 0 on success.

Definition at line 301 of file amxc_timestamp.c.

301  {
302  int retval = -1;
303  struct timespec ts = {0, 0};
304 
305  when_null(tsp, exit);
306  retval = clock_gettime(CLOCK_REALTIME, &ts);
307  tsp->sec = ts.tv_sec;
308  tsp->nsec = ts.tv_nsec;
309 
310  if(retval != 0) {
311  tsp->sec = MIN_SEC;
312  }
313  tsp->offset = 0;
314 
315 exit:
316  return retval ? -1 : 0;
317 }

◆ amxc_ts_parse()

int amxc_ts_parse ( amxc_ts_t tsp,
const char *  str,
size_t  len 
)

Transforms the given string in to unix epoch time.

Transforms the time stored in a string in https://tools.ietf.org/html/rfc3339 format into unix epoch time stored as a struct.

Parameters
tspa pointer to a timestamp structure
strstring containing time in RFC3339 format
lenlength of the string
Returns
-1 if an error occurred. 0 on success.

Definition at line 369 of file amxc_timestamp.c.

371  {
372  int retval = -1;
373  const unsigned char* cur, * end;
374  unsigned char ch;
375  uint16_t year, month, day, hour, min, sec;
376  uint32_t rdn, sod, nsec;
377  int16_t offset;
378 
379  when_null(tsp, exit);
380  when_null(str, exit);
381  when_true(len < 20, exit);
382  /*
383  * 1
384  * 01234567890123456789
385  * 2013-12-31T23:59:59Z
386  */
387  cur = (const unsigned char*) str;
388  when_true(cur[4] != '-' || cur[7] != '-' ||
389  cur[13] != ':' || cur[16] != ':', exit);
390 
391  ch = cur[10];
392  when_true(!(ch == 'T' || ch == ' ' || ch == 't'), exit);
393  when_failed(amxc_ts_parse_date(cur, &year, &month, &day), exit);
394  when_failed(amxc_ts_parse_time(cur, &hour, &min, &sec), exit);
395  when_true(day > 28 && day > month_days(year, month), exit);
396 
397  if(month < 3) {
398  year--;
399  }
400 
401  rdn = (1461 * year) / 4 - year / 100 + year / 400 + DayOffset[month] + day - 306;
402  sod = hour * 3600 + min * 60 + sec;
403  end = cur + len;
404  cur = cur + 19;
405  offset = nsec = 0;
406 
407  ch = *cur++;
408  if(ch == '.') {
409  const unsigned char* start;
410  size_t ndigits;
411 
412  start = cur;
413  for(; cur < end; cur++) {
414  const unsigned char digit = *cur - '0';
415  if(digit > 9) {
416  break;
417  }
418  nsec = nsec * 10 + digit;
419  }
420 
421  ndigits = cur - start;
422  when_true(ndigits < 1 || ndigits > 9, exit);
423 
424  nsec *= Pow10[9 - ndigits];
425 
426  when_true(cur == end, exit);
427 
428  ch = *cur++;
429  }
430 
431  if(!((ch == 'Z') || (ch == 'z'))) {
432  when_failed(amxc_ts_parse_offset(cur - 1, end, &offset), exit);
433  cur += 5;
434  }
435 
436  when_true(cur != end, exit);
437 
438  tsp->sec = ((int64_t) rdn - 719163) * 86400 + sod - offset * 60;
439  tsp->nsec = nsec;
440  tsp->offset = offset;
441 
442  retval = 0;
443 
444 exit:
445  return retval;
446 }
#define when_failed(x, l)
Definition: amxc_macros.h:142
static int amxc_ts_parse_offset(const unsigned char *cur, const unsigned char *end, int16_t *offset)
static const uint16_t DayOffset[13]
static const uint32_t Pow10[10]
static unsigned char month_days(uint16_t y, uint16_t m)
static int amxc_ts_parse_date(const unsigned char *cur, uint16_t *year, uint16_t *month, uint16_t *day)
static int amxc_ts_parse_time(const unsigned char *cur, uint16_t *hour, uint16_t *min, uint16_t *sec)

◆ amxc_ts_to_local()

int amxc_ts_to_local ( amxc_ts_t tsp)

Adds the local time offset to the timestamp structure.

Parameters
tspa pointer to a timestamp structure
Returns
-1 if an error occurs 0 if the action succeeded

Definition at line 553 of file amxc_timestamp.c.

553  {
554  int retval = -1;
555  time_t rawtime = time(NULL);
556  struct tm* ptm = gmtime(&rawtime);
557  time_t gmt = 0;
558 
559  when_null(ptm, exit);
560  when_null(tsp, exit);
561 
562  gmt = mktime(ptm);
563  ptm = localtime(&rawtime);
564 
565  when_null(ptm, exit);
566 
567  tsp->offset = (rawtime - gmt + (ptm->tm_isdst ? 3600 : 0)) / 60;
568  retval = 0;
569 
570 exit:
571  return retval;
572 }

◆ amxc_ts_to_tm_local()

int amxc_ts_to_tm_local ( const amxc_ts_t tsp,
struct tm *  tmp 
)

Converts timestamp in unix epoch time to a struct tm type in local time.

Parameters
tspa pointer to a timestamp structure
tmpa pointer to a struct tm type
Returns
-1 if an error occurs 0 if the action succeeded

Definition at line 549 of file amxc_timestamp.c.

549  {
550  return timestamp_to_tm(tsp, tmp, true);
551 }
static int timestamp_to_tm(const amxc_ts_t *tsp, struct tm *tmp, const bool local)

◆ amxc_ts_to_tm_utc()

int amxc_ts_to_tm_utc ( const amxc_ts_t tsp,
struct tm *  tmp 
)

Converts timestamp in unix epoch time to a struct tm type in UTC time.

Parameters
tspa pointer to a timestamp structure
tmpa pointer to a struct tm type
Returns
-1 if an error occurs 0 if the action succeeded

Definition at line 545 of file amxc_timestamp.c.

545  {
546  return timestamp_to_tm(tsp, tmp, false);
547 }