datetime.c (3928B)
1 /* 2 * libdivecomputer 3 * 4 * Copyright (C) 2010 Jef Driesen 5 * 6 * This library is free software; you can redistribute it and/or 7 * modify it under the terms of the GNU Lesser General Public 8 * License as published by the Free Software Foundation; either 9 * version 2.1 of the License, or (at your option) any later version. 10 * 11 * This library is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 * Lesser General Public License for more details. 15 * 16 * You should have received a copy of the GNU Lesser General Public 17 * License along with this library; if not, write to the Free Software 18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 19 * MA 02110-1301 USA 20 */ 21 22 #ifdef HAVE_CONFIG_H 23 #include "config.h" 24 #endif 25 26 #include <time.h> 27 28 #include <libdivecomputer/datetime.h> 29 30 static struct tm * 31 dc_localtime_r (const time_t *t, struct tm *tm) 32 { 33 #ifdef HAVE_LOCALTIME_R 34 return localtime_r (t, tm); 35 #else 36 struct tm *p = localtime (t); 37 if (p == NULL) 38 return NULL; 39 40 if (tm) 41 *tm = *p; 42 43 return tm; 44 #endif 45 } 46 47 static struct tm * 48 dc_gmtime_r (const time_t *t, struct tm *tm) 49 { 50 #ifdef HAVE_GMTIME_R 51 return gmtime_r (t, tm); 52 #else 53 struct tm *p = gmtime (t); 54 if (p == NULL) 55 return NULL; 56 57 if (tm) 58 *tm = *p; 59 60 return tm; 61 #endif 62 } 63 64 static time_t 65 dc_timegm (struct tm *tm) 66 { 67 #if defined(HAVE_TIMEGM) 68 return timegm (tm); 69 #elif defined (HAVE__MKGMTIME) 70 return _mkgmtime (tm); 71 #else 72 static const unsigned int mdays[] = { 73 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 74 }; 75 76 if (tm == NULL || 77 tm->tm_mon < 0 || tm->tm_mon > 11 || 78 tm->tm_mday < 1 || tm->tm_mday > 31 || 79 tm->tm_hour < 0 || tm->tm_hour > 23 || 80 tm->tm_min < 0 || tm->tm_min > 59 || 81 tm->tm_sec < 0 || tm->tm_sec > 60) 82 return (time_t) -1; 83 84 /* Number of leap days since 1970-01-01. */ 85 int year = tm->tm_year + 1900 - (tm->tm_mon < 2); 86 int leapdays = 87 ((year / 4) - (year / 100) + (year / 400)) - 88 ((1969 / 4) - (1969 / 100) + (1969 / 400)); 89 90 time_t result = 0; 91 result += (tm->tm_year - 70) * 365; 92 result += leapdays; 93 result += mdays[tm->tm_mon]; 94 result += tm->tm_mday - 1; 95 result *= 24; 96 result += tm->tm_hour; 97 result *= 60; 98 result += tm->tm_min; 99 result *= 60; 100 result += tm->tm_sec; 101 return result; 102 #endif 103 } 104 105 dc_ticks_t 106 dc_datetime_now (void) 107 { 108 return time (NULL); 109 } 110 111 dc_datetime_t * 112 dc_datetime_localtime (dc_datetime_t *result, 113 dc_ticks_t ticks) 114 { 115 time_t t = ticks; 116 int offset = 0; 117 118 struct tm tm; 119 if (dc_localtime_r (&t, &tm) == NULL) 120 return NULL; 121 122 #ifdef HAVE_STRUCT_TM_TM_GMTOFF 123 offset = tm.tm_gmtoff; 124 #else 125 struct tm tmp = tm; 126 time_t t_local = dc_timegm (&tmp); 127 if (t_local == (time_t) -1) 128 return NULL; 129 130 offset = t_local - t; 131 #endif 132 133 if (result) { 134 result->year = tm.tm_year + 1900; 135 result->month = tm.tm_mon + 1; 136 result->day = tm.tm_mday; 137 result->hour = tm.tm_hour; 138 result->minute = tm.tm_min; 139 result->second = tm.tm_sec; 140 result->timezone = offset; 141 } 142 143 return result; 144 } 145 146 dc_datetime_t * 147 dc_datetime_gmtime (dc_datetime_t *result, 148 dc_ticks_t ticks) 149 { 150 time_t t = ticks; 151 152 struct tm tm; 153 if (dc_gmtime_r (&t, &tm) == NULL) 154 return NULL; 155 156 if (result) { 157 result->year = tm.tm_year + 1900; 158 result->month = tm.tm_mon + 1; 159 result->day = tm.tm_mday; 160 result->hour = tm.tm_hour; 161 result->minute = tm.tm_min; 162 result->second = tm.tm_sec; 163 result->timezone = 0; 164 } 165 166 return result; 167 } 168 169 dc_ticks_t 170 dc_datetime_mktime (const dc_datetime_t *dt) 171 { 172 if (dt == NULL) 173 return -1; 174 175 struct tm tm; 176 tm.tm_year = dt->year - 1900; 177 tm.tm_mon = dt->month - 1; 178 tm.tm_mday = dt->day; 179 tm.tm_hour = dt->hour; 180 tm.tm_min = dt->minute; 181 tm.tm_sec = dt->second; 182 tm.tm_isdst = 0; 183 184 time_t t = dc_timegm (&tm); 185 if (t == (time_t) -1) 186 return t; 187 188 if (dt->timezone != DC_TIMEZONE_NONE) { 189 t -= dt->timezone; 190 } 191 192 return t; 193 }