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 | |
| 22 | #include "libnetconf.h" |
Radek Krejci | d0d1952 | 2015-09-02 13:49:25 +0200 | [diff] [blame] | 23 | |
| 24 | API time_t |
Michal Vasko | 231e4b2 | 2015-12-10 10:10:59 +0100 | [diff] [blame] | 25 | nc_datetime2time(const char *datetime) |
Radek Krejci | d0d1952 | 2015-09-02 13:49:25 +0200 | [diff] [blame] | 26 | { |
| 27 | struct tm time; |
Michal Vasko | 231e4b2 | 2015-12-10 10:10:59 +0100 | [diff] [blame] | 28 | char *dt; |
Radek Krejci | d0d1952 | 2015-09-02 13:49:25 +0200 | [diff] [blame] | 29 | int i; |
| 30 | long int shift, shift_m; |
| 31 | time_t retval; |
| 32 | |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 33 | if (!datetime) { |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 34 | ERRARG("datetime"); |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 35 | return -1; |
Radek Krejci | d0d1952 | 2015-09-02 13:49:25 +0200 | [diff] [blame] | 36 | } |
| 37 | |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 38 | dt = strdup(datetime); |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 39 | if (!dt) { |
| 40 | ERRMEM; |
| 41 | return -1; |
| 42 | } |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 43 | |
Radek Krejci | d0d1952 | 2015-09-02 13:49:25 +0200 | [diff] [blame] | 44 | 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 Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 89 | return retval; |
Radek Krejci | d0d1952 | 2015-09-02 13:49:25 +0200 | [diff] [blame] | 90 | } |
| 91 | |
Michal Vasko | 231e4b2 | 2015-12-10 10:10:59 +0100 | [diff] [blame] | 92 | API char * |
Michal Vasko | 8c1bfab | 2016-05-25 11:17:02 +0200 | [diff] [blame] | 93 | nc_time2datetime(time_t time, const char *tz, char *buf) |
Radek Krejci | d0d1952 | 2015-09-02 13:49:25 +0200 | [diff] [blame] | 94 | { |
Michal Vasko | 231e4b2 | 2015-12-10 10:10:59 +0100 | [diff] [blame] | 95 | char *date = NULL; |
| 96 | char *zoneshift = NULL; |
Radek Krejci | d0d1952 | 2015-09-02 13:49:25 +0200 | [diff] [blame] | 97 | int zonediff, zonediff_h, zonediff_m; |
| 98 | struct tm tm, *tm_ret; |
| 99 | char *tz_origin; |
| 100 | |
| 101 | if (tz) { |
Radek Krejci | a507d1a | 2016-02-26 15:26:44 +0100 | [diff] [blame] | 102 | tz_origin = getenv("TZ"); |
| 103 | if (tz_origin) { |
| 104 | tz_origin = strdup(tz_origin); |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 105 | if (!tz_origin) { |
| 106 | ERRMEM; |
| 107 | return NULL; |
| 108 | } |
Radek Krejci | a507d1a | 2016-02-26 15:26:44 +0100 | [diff] [blame] | 109 | } |
Radek Krejci | d0d1952 | 2015-09-02 13:49:25 +0200 | [diff] [blame] | 110 | setenv("TZ", tz, 1); |
Radek Krejci | bd16d45 | 2016-05-31 16:00:32 +0200 | [diff] [blame] | 111 | tzset(); /* apply timezone change */ |
Radek Krejci | d0d1952 | 2015-09-02 13:49:25 +0200 | [diff] [blame] | 112 | tm_ret = localtime_r(&time, &tm); |
Radek Krejci | a507d1a | 2016-02-26 15:26:44 +0100 | [diff] [blame] | 113 | if (tz_origin) { |
| 114 | setenv("TZ", tz_origin, 1); |
| 115 | free(tz_origin); |
| 116 | } else { |
| 117 | unsetenv("TZ"); |
| 118 | } |
Radek Krejci | bd16d45 | 2016-05-31 16:00:32 +0200 | [diff] [blame] | 119 | tzset(); /* apply timezone change */ |
Radek Krejci | d0d1952 | 2015-09-02 13:49:25 +0200 | [diff] [blame] | 120 | |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 121 | if (!tm_ret) { |
| 122 | return NULL; |
Radek Krejci | d0d1952 | 2015-09-02 13:49:25 +0200 | [diff] [blame] | 123 | } |
| 124 | } else { |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 125 | if (!gmtime_r(&time, &tm)) { |
| 126 | return NULL; |
Radek Krejci | d0d1952 | 2015-09-02 13:49:25 +0200 | [diff] [blame] | 127 | } |
| 128 | } |
| 129 | |
Radek Krejci | fcf561b | 2016-05-31 16:02:37 +0200 | [diff] [blame] | 130 | /* years cannot be negative */ |
| 131 | if (tm.tm_year < -1900) { |
| 132 | ERRARG("time"); |
| 133 | return NULL; |
| 134 | } |
| 135 | |
| 136 | if (tm.tm_gmtoff == 0) { |
| 137 | /* time is Zulu (UTC) */ |
| 138 | if (asprintf(&zoneshift, "Z") == -1) { |
| 139 | ERRMEM; |
| 140 | return NULL; |
| 141 | } |
Radek Krejci | d0d1952 | 2015-09-02 13:49:25 +0200 | [diff] [blame] | 142 | } else { |
Radek Krejci | fcf561b | 2016-05-31 16:02:37 +0200 | [diff] [blame] | 143 | zonediff = tm.tm_gmtoff; |
| 144 | zonediff_h = zonediff / 60 / 60; |
| 145 | zonediff_m = zonediff / 60 % 60; |
Michal Vasko | 55fc5ef | 2016-06-03 12:07:27 +0200 | [diff] [blame] | 146 | if (asprintf(&zoneshift, "%+03d:%02d", zonediff_h, zonediff_m) == -1) { |
Radek Krejci | fcf561b | 2016-05-31 16:02:37 +0200 | [diff] [blame] | 147 | ERRMEM; |
| 148 | return NULL; |
Radek Krejci | d0d1952 | 2015-09-02 13:49:25 +0200 | [diff] [blame] | 149 | } |
| 150 | } |
Michal Vasko | 8c1bfab | 2016-05-25 11:17:02 +0200 | [diff] [blame] | 151 | |
| 152 | if (buf) { |
| 153 | sprintf(buf, "%04d-%02d-%02dT%02d:%02d:%02d%s", |
| 154 | tm.tm_year + 1900, |
| 155 | tm.tm_mon + 1, |
| 156 | tm.tm_mday, |
| 157 | tm.tm_hour, |
| 158 | tm.tm_min, |
| 159 | tm.tm_sec, |
Radek Krejci | fcf561b | 2016-05-31 16:02:37 +0200 | [diff] [blame] | 160 | zoneshift); |
Michal Vasko | 8c1bfab | 2016-05-25 11:17:02 +0200 | [diff] [blame] | 161 | } else if (asprintf(&date, "%04d-%02d-%02dT%02d:%02d:%02d%s", |
| 162 | tm.tm_year + 1900, |
| 163 | tm.tm_mon + 1, |
| 164 | tm.tm_mday, |
| 165 | tm.tm_hour, |
| 166 | tm.tm_min, |
| 167 | tm.tm_sec, |
Radek Krejci | fcf561b | 2016-05-31 16:02:37 +0200 | [diff] [blame] | 168 | zoneshift) == -1) { |
Radek Krejci | d0d1952 | 2015-09-02 13:49:25 +0200 | [diff] [blame] | 169 | free(zoneshift); |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 170 | ERRMEM; |
| 171 | return NULL; |
Radek Krejci | d0d1952 | 2015-09-02 13:49:25 +0200 | [diff] [blame] | 172 | } |
| 173 | free(zoneshift); |
| 174 | |
Michal Vasko | 8c1bfab | 2016-05-25 11:17:02 +0200 | [diff] [blame] | 175 | return (buf ? buf : date); |
Radek Krejci | d0d1952 | 2015-09-02 13:49:25 +0200 | [diff] [blame] | 176 | } |