blob: b2f0005cb4925b146d2dd9f96d6fb91652dd39ca [file] [log] [blame]
Radek Krejcid0d19522015-09-02 13:49:25 +02001/**
2 * \file time.c
3 * \author Radek Krejci <rkrejci@cesnet.cz>
4 * \brief libnetconf2 - time handling functions
5 *
6 * Copyright (c) 2015 CESNET, z.s.p.o.
7 *
Radek Krejci9b81f5b2016-02-24 13:14:49 +01008 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
Michal Vaskoafd416b2016-02-25 14:51:46 +010011 *
Radek Krejci9b81f5b2016-02-24 13:14:49 +010012 * https://opensource.org/licenses/BSD-3-Clause
Radek Krejcid0d19522015-09-02 13:49:25 +020013 */
14
15#define _GNU_SOURCE
16#include <ctype.h>
17#include <stdio.h>
18#include <stdlib.h>
19#include <string.h>
20#include <unistd.h>
21
22#include "libnetconf.h"
Radek Krejcid0d19522015-09-02 13:49:25 +020023
24API time_t
Michal Vasko231e4b22015-12-10 10:10:59 +010025nc_datetime2time(const char *datetime)
Radek Krejcid0d19522015-09-02 13:49:25 +020026{
27 struct tm time;
Michal Vasko231e4b22015-12-10 10:10:59 +010028 char *dt;
Radek Krejcid0d19522015-09-02 13:49:25 +020029 int i;
30 long int shift, shift_m;
31 time_t retval;
32
Michal Vasko7f1c78b2016-01-19 09:52:14 +010033 if (!datetime) {
Michal Vasko45e53ae2016-04-07 11:46:03 +020034 ERRARG("datetime");
Michal Vasko7f1c78b2016-01-19 09:52:14 +010035 return -1;
Radek Krejcid0d19522015-09-02 13:49:25 +020036 }
37
Michal Vasko7f1c78b2016-01-19 09:52:14 +010038 dt = strdup(datetime);
Michal Vasko4eb3c312016-03-01 14:09:37 +010039 if (!dt) {
40 ERRMEM;
41 return -1;
42 }
Michal Vasko7f1c78b2016-01-19 09:52:14 +010043
Radek Krejcid0d19522015-09-02 13:49:25 +020044 if (strlen(dt) < 20 || dt[4] != '-' || dt[7] != '-' || dt[13] != ':' || dt[16] != ':') {
45 ERR("Wrong date time format not compliant to RFC 3339.");
46 free(dt);
47 return (-1);
48 }
49
50 memset(&time, 0, sizeof(struct tm));
51 time.tm_year = atoi(&dt[0]) - 1900;
52 time.tm_mon = atoi(&dt[5]) - 1;
53 time.tm_mday = atoi(&dt[8]);
54 time.tm_hour = atoi(&dt[11]);
55 time.tm_min = atoi(&dt[14]);
56 time.tm_sec = atoi(&dt[17]);
57
58 retval = timegm(&time);
59
60 /* apply offset */
61 i = 19;
62 if (dt[i] == '.') { /* we have fractions to skip */
63 for (i++; isdigit(dt[i]); i++)
64 ;
65 }
66 if (dt[i] == 'Z' || dt[i] == 'z') {
67 /* zero shift */
68 shift = 0;
69 } else if (dt[i + 3] != ':') {
70 /* wrong format */
71 ERR("Wrong date time shift format not compliant to RFC 3339.");
72 free(dt);
73 return (-1);
74 } else {
75 shift = strtol(&dt[i], NULL, 10);
76 shift = shift * 60 * 60; /* convert from hours to seconds */
77 shift_m = strtol(&dt[i + 4], NULL, 10) * 60; /* includes conversion from minutes to seconds */
78 /* correct sign */
79 if (shift < 0) {
80 shift_m *= -1;
81 }
82 /* connect hours and minutes of the shift */
83 shift = shift + shift_m;
84 }
85 /* we have to shift to the opposite way to correct the time */
86 retval -= shift;
87
88 free(dt);
Michal Vasko7f1c78b2016-01-19 09:52:14 +010089 return retval;
Radek Krejcid0d19522015-09-02 13:49:25 +020090}
91
Michal Vasko231e4b22015-12-10 10:10:59 +010092API char *
93nc_time2datetime(time_t time, const char *tz)
Radek Krejcid0d19522015-09-02 13:49:25 +020094{
Michal Vasko231e4b22015-12-10 10:10:59 +010095 char *date = NULL;
96 char *zoneshift = NULL;
Radek Krejcid0d19522015-09-02 13:49:25 +020097 int zonediff, zonediff_h, zonediff_m;
98 struct tm tm, *tm_ret;
99 char *tz_origin;
100
101 if (tz) {
Radek Krejcia507d1a2016-02-26 15:26:44 +0100102 tz_origin = getenv("TZ");
103 if (tz_origin) {
104 tz_origin = strdup(tz_origin);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100105 if (!tz_origin) {
106 ERRMEM;
107 return NULL;
108 }
Radek Krejcia507d1a2016-02-26 15:26:44 +0100109 }
Radek Krejcid0d19522015-09-02 13:49:25 +0200110 setenv("TZ", tz, 1);
111 tm_ret = localtime_r(&time, &tm);
Radek Krejcia507d1a2016-02-26 15:26:44 +0100112 if (tz_origin) {
113 setenv("TZ", tz_origin, 1);
114 free(tz_origin);
115 } else {
116 unsetenv("TZ");
117 }
Radek Krejcid0d19522015-09-02 13:49:25 +0200118
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100119 if (!tm_ret) {
120 return NULL;
Radek Krejcid0d19522015-09-02 13:49:25 +0200121 }
122 } else {
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100123 if (!gmtime_r(&time, &tm)) {
124 return NULL;
Radek Krejcid0d19522015-09-02 13:49:25 +0200125 }
126 }
127
128 if (tm.tm_isdst < 0) {
129 zoneshift = NULL;
130 } else {
131 if (tm.tm_gmtoff == 0) {
132 /* time is Zulu (UTC) */
133 if (asprintf(&zoneshift, "Z") == -1) {
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100134 ERRMEM;
135 return NULL;
Radek Krejcid0d19522015-09-02 13:49:25 +0200136 }
137 } else {
138 zonediff = tm.tm_gmtoff;
139 zonediff_h = zonediff / 60 / 60;
140 zonediff_m = zonediff / 60 % 60;
141 if (asprintf(&zoneshift, "%s%02d:%02d", (zonediff < 0) ? "-" : "+", zonediff_h, zonediff_m) == -1) {
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100142 ERRMEM;
143 return NULL;
Radek Krejcid0d19522015-09-02 13:49:25 +0200144 }
145 }
146 }
147 if (asprintf(&date, "%04d-%02d-%02dT%02d:%02d:%02d%s",
148 tm.tm_year + 1900,
149 tm.tm_mon + 1,
150 tm.tm_mday,
151 tm.tm_hour,
152 tm.tm_min,
153 tm.tm_sec,
154 (zoneshift == NULL) ? "" : zoneshift) == -1) {
155 free(zoneshift);
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100156 ERRMEM;
157 return NULL;
Radek Krejcid0d19522015-09-02 13:49:25 +0200158 }
159 free(zoneshift);
160
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100161 return date;
Radek Krejcid0d19522015-09-02 13:49:25 +0200162}