pacemaker  2.1.6-802a72226b
Scalable High-Availability cluster resource manager
iso8601.h
Go to the documentation of this file.
1 /*
2  * Copyright 2005-2020 the Pacemaker project contributors
3  *
4  * The version control history for this file may have further details.
5  *
6  * This source code is licensed under the GNU Lesser General Public License
7  * version 2.1 or later (LGPLv2.1+) WITHOUT ANY WARRANTY.
8  */
9 
10 #ifndef PCMK__CRM_COMMON_ISO8601__H
11 # define PCMK__CRM_COMMON_ISO8601__H
12 
13 # include <time.h>
14 # include <ctype.h>
15 # include <stdint.h> // uint32_t
16 # include <stdbool.h> // bool
17 
18 #ifdef __cplusplus
19 extern "C" {
20 #endif
21 
28 /*
29  * See https://en.wikipedia.org/wiki/ISO_8601
30  */
31 
32 typedef struct crm_time_s crm_time_t;
33 
34 typedef struct crm_time_period_s {
39 
40 /* Creates a new date/time object conforming to ISO 8601, for example:
41  * Ordinal: 2010-01 12:00:00 +10:00
42  * Gregorian: 2010-01-01 12:00:00 +10:00
43  * ISO Week: 2010-W53-6 12:00:00 +10:00
44  *
45  * Notes:
46  * Only one of date, time is required
47  * If date or timezone is unspecified, they default to the current one
48  * Supplying NULL results in the current date/time
49  * Dashes may be omitted from dates
50  * Colons may be omitted from times and timezones
51  * A timezone of 'Z' denotes UTC time
52  */
53 crm_time_t *crm_time_new(const char *string);
55 void crm_time_free(crm_time_t * dt);
56 
57 bool crm_time_is_defined(const crm_time_t *t);
58 char *crm_time_as_string(const crm_time_t *dt, int flags);
59 
60 #define crm_time_log(level, prefix, dt, flags) \
61  crm_time_log_alias(level, __FILE__, __func__, __LINE__, prefix, dt, flags)
62 
63 void crm_time_log_alias(int log_level, const char *file, const char *function,
64  int line, const char *prefix,
65  const crm_time_t *date_time, int flags);
66 
67 # define crm_time_log_date 0x001
68 # define crm_time_log_timeofday 0x002
69 # define crm_time_log_with_timezone 0x004
70 # define crm_time_log_duration 0x008
71 
72 # define crm_time_ordinal 0x010
73 # define crm_time_weeks 0x020
74 # define crm_time_seconds 0x100
75 # define crm_time_epoch 0x200
76 # define crm_time_usecs 0x400
77 
78 crm_time_t *crm_time_parse_duration(const char *duration_str);
80  const crm_time_t *value);
81 crm_time_period_t *crm_time_parse_period(const char *period_str);
83 
84 int crm_time_compare(const crm_time_t *a, const crm_time_t *b);
85 
86 int crm_time_get_timeofday(const crm_time_t *dt, uint32_t *h, uint32_t *m,
87  uint32_t *s);
88 int crm_time_get_timezone(const crm_time_t *dt, uint32_t *h, uint32_t *m);
89 int crm_time_get_gregorian(const crm_time_t *dt, uint32_t *y, uint32_t *m,
90  uint32_t *d);
91 int crm_time_get_ordinal(const crm_time_t *dt, uint32_t *y, uint32_t *d);
92 int crm_time_get_isoweek(const crm_time_t *dt, uint32_t *y, uint32_t *w,
93  uint32_t * d);
94 
95 /* Time in seconds since 0000-01-01 00:00:00Z */
96 long long crm_time_get_seconds(const crm_time_t *dt);
97 
98 /* Time in seconds since 1970-01-01 00:00:00Z */
99 long long crm_time_get_seconds_since_epoch(const crm_time_t *dt);
100 
101 void crm_time_set(crm_time_t *target, const crm_time_t *source);
102 void crm_time_set_timet(crm_time_t *target, const time_t *source);
103 
104 /* Returns a new time object */
105 crm_time_t *pcmk_copy_time(const crm_time_t *source);
106 crm_time_t *crm_time_add(const crm_time_t *dt, const crm_time_t *value);
107 crm_time_t *crm_time_subtract(const crm_time_t *dt, const crm_time_t *value);
108 
109 /* All crm_time_add_... functions support negative values */
110 void crm_time_add_seconds(crm_time_t * dt, int value);
111 void crm_time_add_minutes(crm_time_t * dt, int value);
112 void crm_time_add_hours(crm_time_t * dt, int value);
113 void crm_time_add_days(crm_time_t * dt, int value);
114 void crm_time_add_weeks(crm_time_t * dt, int value);
115 void crm_time_add_months(crm_time_t * dt, int value);
116 void crm_time_add_years(crm_time_t * dt, int value);
117 
118 /* Useful helper functions */
119 int crm_time_january1_weekday(int year);
120 int crm_time_weeks_in_year(int year);
121 int crm_time_days_in_month(int month, int year);
122 
123 bool crm_time_leapyear(int year);
124 bool crm_time_check(const crm_time_t *dt);
125 
126 #ifdef __cplusplus
127 }
128 #endif
129 
130 #endif
crm_time_t * crm_time_new_undefined(void)
Allocate memory for an uninitialized time object.
Definition: iso8601.c:126
void crm_time_add_years(crm_time_t *dt, int value)
Definition: iso8601.c:1654
int crm_time_get_isoweek(const crm_time_t *dt, uint32_t *y, uint32_t *w, uint32_t *d)
Definition: iso8601.c:406
void crm_time_add_seconds(crm_time_t *dt, int value)
Add a given number of seconds to a date/time or duration.
Definition: iso8601.c:1548
crm_time_t * crm_time_add(const crm_time_t *dt, const crm_time_t *value)
Definition: iso8601.c:1400
bool crm_time_is_defined(const crm_time_t *t)
Check whether a time object has been initialized yet.
Definition: iso8601.c:142
struct crm_time_s crm_time_t
Definition: iso8601.h:32
crm_time_period_t * crm_time_parse_period(const char *period_str)
Parse a time period from an ISO 8601 interval specification.
Definition: iso8601.c:1203
int crm_time_weeks_in_year(int year)
Definition: iso8601.c:191
crm_time_t * pcmk_copy_time(const crm_time_t *source)
Definition: iso8601.c:1374
crm_time_t * crm_time_parse_duration(const char *duration_str)
Parse a time duration from an ISO 8601 duration specification.
Definition: iso8601.c:1095
int crm_time_get_ordinal(const crm_time_t *dt, uint32_t *y, uint32_t *d)
Definition: iso8601.c:398
void crm_time_add_hours(crm_time_t *dt, int value)
Definition: iso8601.c:1642
bool crm_time_check(const crm_time_t *dt)
Check whether a time object represents a sensible date/time.
Definition: iso8601.c:1497
char * crm_time_as_string(const crm_time_t *dt, int flags)
Get a string representation of a crm_time_t object.
Definition: iso8601.c:694
int crm_time_get_timezone(const crm_time_t *dt, uint32_t *h, uint32_t *m)
Definition: iso8601.c:307
void crm_time_set_timet(crm_time_t *target, const time_t *source)
Definition: iso8601.c:1368
void crm_time_set(crm_time_t *target, const crm_time_t *source)
Definition: iso8601.c:1305
void crm_time_add_weeks(crm_time_t *dt, int value)
Definition: iso8601.c:1648
bool crm_time_leapyear(int year)
Definition: iso8601.c:236
crm_time_t * start
Definition: iso8601.h:35
void crm_time_add_months(crm_time_t *dt, int value)
Definition: iso8601.c:1593
crm_time_t * end
Definition: iso8601.h:36
void crm_time_free_period(crm_time_period_t *period)
Free a dynamically allocated time period object.
Definition: iso8601.c:1294
void crm_time_add_minutes(crm_time_t *dt, int value)
Definition: iso8601.c:1636
crm_time_t * crm_time_calculate_duration(const crm_time_t *dt, const crm_time_t *value)
Definition: iso8601.c:1428
crm_time_t * diff
Definition: iso8601.h:37
int crm_time_january1_weekday(int year)
Definition: iso8601.c:178
int crm_time_get_gregorian(const crm_time_t *dt, uint32_t *y, uint32_t *m, uint32_t *d)
Definition: iso8601.c:365
long long crm_time_get_seconds(const crm_time_t *dt)
Definition: iso8601.c:316
long long crm_time_get_seconds_since_epoch(const crm_time_t *dt)
Definition: iso8601.c:359
int crm_time_compare(const crm_time_t *a, const crm_time_t *b)
Definition: iso8601.c:1518
struct crm_time_period_s crm_time_period_t
const char * target
Definition: pcmk_fence.c:29
crm_time_t * crm_time_new(const char *string)
Definition: iso8601.c:109
void crm_time_log_alias(int log_level, const char *file, const char *function, int line, const char *prefix, const crm_time_t *date_time, int flags)
Definition: iso8601.c:261
crm_time_t * crm_time_subtract(const crm_time_t *dt, const crm_time_t *value)
Definition: iso8601.c:1462
int crm_time_get_timeofday(const crm_time_t *dt, uint32_t *h, uint32_t *m, uint32_t *s)
Definition: iso8601.c:299
void crm_time_add_days(crm_time_t *dt, int value)
Definition: iso8601.c:1568
uint64_t flags
Definition: remote.c:215
int crm_time_days_in_month(int month, int year)
Return number of days in given month of given year.
Definition: iso8601.c:224
void crm_time_free(crm_time_t *dt)
Definition: iso8601.c:150