about summary refs log tree commit diff
path: root/sternenblog/timeutil.h
blob: 7d0270ec7996e75bb8e00c94dde71d2e2fdf5ccb (plain) (blame)
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
/*!
 * @file
 * @brief Utilities for rendering timestamps as strings
 */
enum time_format {
    RSS_TIME_FORMAT,          //!< RFC822 formatted time with 4 instead of 2 year digits
    ATOM_TIME_FORMAT,         //!< RFC3339 formatted time
    HTML_TIME_FORMAT_READABLE //!< like `ATOM_TIME_FORMAT`, but with space between date and time
};

/*!
 * @brief Maximum size necessary to contain the output of flocaltime()
 */
#define MAX_TIMESTR_SIZE 32
// max HTML/Atom: 24 + NUL byte
// max RSS:       31 + NUL byte

/*!
 * @brief Format given timestamp as a string in the local timezone
 *
 * flocaltime() is a wrapper around `strftime()` which supports
 * a specific set of output formats. In contrast to `strftime()`
 * it can output correct RFC3339 time strings and does localtime
 * resolution for you.
 *
 * Example usage to print a RFC3339 formatted timestamp:
 *
 * ```
 * time_t some_time;
 * char strtime[MAX_TIMESTR_SIZE];
 *
 * if(flocaltime(strtime, ATOM_TIME_FORMAT, MAX_TIMESTR_SIZE, &some_time) > 0) {
 *   puts(strtime);
 * }
 * ```
 *
 * @param b output buffer
 * @param type time format to use for output
 * @param size number of `char`s the buffer can hold
 * @param time pointer to timestamp
 * @return `0` on error, otherwise length of the string placed in `b` excluding terminating `NUL` byte
 */
size_t flocaltime(char *b, enum time_format type, size_t size, const time_t *time);