Radek Krejci | d0d1952 | 2015-09-02 13:49:25 +0200 | [diff] [blame] | 1 | /** |
| 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 Krejci | 9b81f5b | 2016-02-24 13:14:49 +0100 | [diff] [blame] | 8 | * 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 Vasko | afd416b | 2016-02-25 14:51:46 +0100 | [diff] [blame] | 11 | * |
Radek Krejci | 9b81f5b | 2016-02-24 13:14:49 +0100 | [diff] [blame] | 12 | * https://opensource.org/licenses/BSD-3-Clause |
Radek Krejci | d0d1952 | 2015-09-02 13:49:25 +0200 | [diff] [blame] | 13 | */ |
| 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 | |
Michal Vasko | 316c963 | 2020-04-15 11:06:57 +0200 | [diff] [blame] | 22 | #include "compat.h" |
Radek Krejci | d0d1952 | 2015-09-02 13:49:25 +0200 | [diff] [blame] | 23 | #include "libnetconf.h" |
Radek Krejci | d0d1952 | 2015-09-02 13:49:25 +0200 | [diff] [blame] | 24 | |
| 25 | API time_t |
Michal Vasko | 231e4b2 | 2015-12-10 10:10:59 +0100 | [diff] [blame] | 26 | nc_datetime2time(const char *datetime) |
Radek Krejci | d0d1952 | 2015-09-02 13:49:25 +0200 | [diff] [blame] | 27 | { |
| 28 | struct tm time; |
Michal Vasko | 231e4b2 | 2015-12-10 10:10:59 +0100 | [diff] [blame] | 29 | char *dt; |
Radek Krejci | d0d1952 | 2015-09-02 13:49:25 +0200 | [diff] [blame] | 30 | int i; |
| 31 | long int shift, shift_m; |
| 32 | time_t retval; |
| 33 | |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 34 | if (!datetime) { |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 35 | ERRARG("datetime"); |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 36 | return -1; |
Radek Krejci | d0d1952 | 2015-09-02 13:49:25 +0200 | [diff] [blame] | 37 | } |
| 38 | |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 39 | dt = strdup(datetime); |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 40 | if (!dt) { |
| 41 | ERRMEM; |
| 42 | return -1; |
| 43 | } |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 44 | |
Radek Krejci | d0d1952 | 2015-09-02 13:49:25 +0200 | [diff] [blame] | 45 | if (strlen(dt) < 20 || dt[4] != '-' || dt[7] != '-' || dt[13] != ':' || dt[16] != ':') { |
| 46 | ERR("Wrong date time format not compliant to RFC 3339."); |
| 47 | free(dt); |
| 48 | return (-1); |
| 49 | } |
| 50 | |
| 51 | memset(&time, 0, sizeof(struct tm)); |
| 52 | time.tm_year = atoi(&dt[0]) - 1900; |
| 53 | time.tm_mon = atoi(&dt[5]) - 1; |
| 54 | time.tm_mday = atoi(&dt[8]); |
| 55 | time.tm_hour = atoi(&dt[11]); |
| 56 | time.tm_min = atoi(&dt[14]); |
| 57 | time.tm_sec = atoi(&dt[17]); |
| 58 | |
| 59 | retval = timegm(&time); |
| 60 | |
| 61 | /* apply offset */ |
| 62 | i = 19; |
| 63 | if (dt[i] == '.') { /* we have fractions to skip */ |
| 64 | for (i++; isdigit(dt[i]); i++) |
| 65 | ; |
| 66 | } |
| 67 | if (dt[i] == 'Z' || dt[i] == 'z') { |
| 68 | /* zero shift */ |
| 69 | shift = 0; |
| 70 | } else if (dt[i + 3] != ':') { |
| 71 | /* wrong format */ |
| 72 | ERR("Wrong date time shift format not compliant to RFC 3339."); |
| 73 | free(dt); |
| 74 | return (-1); |
| 75 | } else { |
| 76 | shift = strtol(&dt[i], NULL, 10); |
| 77 | shift = shift * 60 * 60; /* convert from hours to seconds */ |
| 78 | shift_m = strtol(&dt[i + 4], NULL, 10) * 60; /* includes conversion from minutes to seconds */ |
| 79 | /* correct sign */ |
| 80 | if (shift < 0) { |
| 81 | shift_m *= -1; |
| 82 | } |
| 83 | /* connect hours and minutes of the shift */ |
| 84 | shift = shift + shift_m; |
| 85 | } |
| 86 | /* we have to shift to the opposite way to correct the time */ |
| 87 | retval -= shift; |
| 88 | |
| 89 | free(dt); |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 90 | return retval; |
Radek Krejci | d0d1952 | 2015-09-02 13:49:25 +0200 | [diff] [blame] | 91 | } |
| 92 | |
Michal Vasko | 231e4b2 | 2015-12-10 10:10:59 +0100 | [diff] [blame] | 93 | API char * |
Michal Vasko | 8c1bfab | 2016-05-25 11:17:02 +0200 | [diff] [blame] | 94 | nc_time2datetime(time_t time, const char *tz, char *buf) |
Radek Krejci | d0d1952 | 2015-09-02 13:49:25 +0200 | [diff] [blame] | 95 | { |
Michal Vasko | 231e4b2 | 2015-12-10 10:10:59 +0100 | [diff] [blame] | 96 | char *date = NULL; |
| 97 | char *zoneshift = NULL; |
Radek Krejci | d0d1952 | 2015-09-02 13:49:25 +0200 | [diff] [blame] | 98 | int zonediff, zonediff_h, zonediff_m; |
| 99 | struct tm tm, *tm_ret; |
| 100 | char *tz_origin; |
| 101 | |
| 102 | if (tz) { |
Radek Krejci | a507d1a | 2016-02-26 15:26:44 +0100 | [diff] [blame] | 103 | tz_origin = getenv("TZ"); |
| 104 | if (tz_origin) { |
| 105 | tz_origin = strdup(tz_origin); |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 106 | if (!tz_origin) { |
| 107 | ERRMEM; |
| 108 | return NULL; |
| 109 | } |
Radek Krejci | a507d1a | 2016-02-26 15:26:44 +0100 | [diff] [blame] | 110 | } |
Radek Krejci | d0d1952 | 2015-09-02 13:49:25 +0200 | [diff] [blame] | 111 | setenv("TZ", tz, 1); |
Radek Krejci | bd16d45 | 2016-05-31 16:00:32 +0200 | [diff] [blame] | 112 | tzset(); /* apply timezone change */ |
Radek Krejci | d0d1952 | 2015-09-02 13:49:25 +0200 | [diff] [blame] | 113 | tm_ret = localtime_r(&time, &tm); |
Radek Krejci | a507d1a | 2016-02-26 15:26:44 +0100 | [diff] [blame] | 114 | if (tz_origin) { |
| 115 | setenv("TZ", tz_origin, 1); |
| 116 | free(tz_origin); |
| 117 | } else { |
| 118 | unsetenv("TZ"); |
| 119 | } |
Radek Krejci | bd16d45 | 2016-05-31 16:00:32 +0200 | [diff] [blame] | 120 | tzset(); /* apply timezone change */ |
Radek Krejci | d0d1952 | 2015-09-02 13:49:25 +0200 | [diff] [blame] | 121 | |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 122 | if (!tm_ret) { |
| 123 | return NULL; |
Radek Krejci | d0d1952 | 2015-09-02 13:49:25 +0200 | [diff] [blame] | 124 | } |
| 125 | } else { |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 126 | if (!gmtime_r(&time, &tm)) { |
| 127 | return NULL; |
Radek Krejci | d0d1952 | 2015-09-02 13:49:25 +0200 | [diff] [blame] | 128 | } |
| 129 | } |
| 130 | |
Radek Krejci | fcf561b | 2016-05-31 16:02:37 +0200 | [diff] [blame] | 131 | /* years cannot be negative */ |
| 132 | if (tm.tm_year < -1900) { |
| 133 | ERRARG("time"); |
| 134 | return NULL; |
| 135 | } |
| 136 | |
| 137 | if (tm.tm_gmtoff == 0) { |
| 138 | /* time is Zulu (UTC) */ |
| 139 | if (asprintf(&zoneshift, "Z") == -1) { |
| 140 | ERRMEM; |
| 141 | return NULL; |
| 142 | } |
Radek Krejci | d0d1952 | 2015-09-02 13:49:25 +0200 | [diff] [blame] | 143 | } else { |
Radek Krejci | fcf561b | 2016-05-31 16:02:37 +0200 | [diff] [blame] | 144 | zonediff = tm.tm_gmtoff; |
| 145 | zonediff_h = zonediff / 60 / 60; |
| 146 | zonediff_m = zonediff / 60 % 60; |
Michal Vasko | 55fc5ef | 2016-06-03 12:07:27 +0200 | [diff] [blame] | 147 | if (asprintf(&zoneshift, "%+03d:%02d", zonediff_h, zonediff_m) == -1) { |
Radek Krejci | fcf561b | 2016-05-31 16:02:37 +0200 | [diff] [blame] | 148 | ERRMEM; |
| 149 | return NULL; |
Radek Krejci | d0d1952 | 2015-09-02 13:49:25 +0200 | [diff] [blame] | 150 | } |
| 151 | } |
Michal Vasko | 8c1bfab | 2016-05-25 11:17:02 +0200 | [diff] [blame] | 152 | |
| 153 | if (buf) { |
| 154 | sprintf(buf, "%04d-%02d-%02dT%02d:%02d:%02d%s", |
| 155 | tm.tm_year + 1900, |
| 156 | tm.tm_mon + 1, |
| 157 | tm.tm_mday, |
| 158 | tm.tm_hour, |
| 159 | tm.tm_min, |
| 160 | tm.tm_sec, |
Radek Krejci | fcf561b | 2016-05-31 16:02:37 +0200 | [diff] [blame] | 161 | zoneshift); |
Michal Vasko | 8c1bfab | 2016-05-25 11:17:02 +0200 | [diff] [blame] | 162 | } else if (asprintf(&date, "%04d-%02d-%02dT%02d:%02d:%02d%s", |
| 163 | tm.tm_year + 1900, |
| 164 | tm.tm_mon + 1, |
| 165 | tm.tm_mday, |
| 166 | tm.tm_hour, |
| 167 | tm.tm_min, |
| 168 | tm.tm_sec, |
Radek Krejci | fcf561b | 2016-05-31 16:02:37 +0200 | [diff] [blame] | 169 | zoneshift) == -1) { |
Radek Krejci | d0d1952 | 2015-09-02 13:49:25 +0200 | [diff] [blame] | 170 | free(zoneshift); |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 171 | ERRMEM; |
| 172 | return NULL; |
Radek Krejci | d0d1952 | 2015-09-02 13:49:25 +0200 | [diff] [blame] | 173 | } |
| 174 | free(zoneshift); |
| 175 | |
Michal Vasko | 8c1bfab | 2016-05-25 11:17:02 +0200 | [diff] [blame] | 176 | return (buf ? buf : date); |
Radek Krejci | d0d1952 | 2015-09-02 13:49:25 +0200 | [diff] [blame] | 177 | } |