OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. | 2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 4 * Use of this source code is governed by a BSD-style license |
5 * that can be found in the LICENSE file in the root of the source | 5 * that can be found in the LICENSE file in the root of the source |
6 * tree. An additional intellectual property rights grant can be found | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
(...skipping 186 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
197 if (ts < last_ts_) { | 197 if (ts < last_ts_) { |
198 if (last_ts_ > 0xf0000000 && ts < 0x0fffffff) { | 198 if (last_ts_ > 0xf0000000 && ts < 0x0fffffff) { |
199 ++num_wrap_; | 199 ++num_wrap_; |
200 } | 200 } |
201 } | 201 } |
202 last_ts_ = ts; | 202 last_ts_ = ts; |
203 int64_t unwrapped_ts = ts + (num_wrap_ << 32); | 203 int64_t unwrapped_ts = ts + (num_wrap_ << 32); |
204 return unwrapped_ts; | 204 return unwrapped_ts; |
205 } | 205 } |
206 | 206 |
| 207 int64_t TmToSeconds(const std::tm& tm) { |
| 208 static short int mdays[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; |
| 209 static short int cumul_mdays[12] = {0, 31, 59, 90, 120, 151, |
| 210 181, 212, 243, 273, 304, 334}; |
| 211 int year = tm.tm_year + 1900; |
| 212 int month = tm.tm_mon; |
| 213 int day = tm.tm_mday - 1; // Make 0-based like the rest. |
| 214 int hour = tm.tm_hour; |
| 215 int min = tm.tm_min; |
| 216 int sec = tm.tm_sec; |
| 217 |
| 218 bool expiry_in_leap_year = (year % 4 == 0 && |
| 219 (year % 100 != 0 || year % 400 == 0)); |
| 220 |
| 221 if (year < 1970) |
| 222 return -1; |
| 223 if (month < 0 || month > 11) |
| 224 return -1; |
| 225 if (day < 0 || day >= mdays[month] + (expiry_in_leap_year && month == 2 - 1)) |
| 226 return -1; |
| 227 if (hour < 0 || hour > 23) |
| 228 return -1; |
| 229 if (min < 0 || min > 59) |
| 230 return -1; |
| 231 if (sec < 0 || sec > 59) |
| 232 return -1; |
| 233 |
| 234 day += cumul_mdays[month]; |
| 235 |
| 236 // Add number of leap days between 1970 and the expiration year, inclusive. |
| 237 day += ((year / 4 - 1970 / 4) - (year / 100 - 1970 / 100) + |
| 238 (year / 400 - 1970 / 400)); |
| 239 |
| 240 // We will have added one day too much above if expiration is during a leap |
| 241 // year, and expiration is in January or February. |
| 242 if (expiry_in_leap_year && month <= 2 - 1) // |month| is zero based. |
| 243 day -= 1; |
| 244 |
| 245 // Combine all variables into seconds from 1970-01-01 00:00 (except |month| |
| 246 // which was accumulated into |day| above). |
| 247 return (((static_cast<int64_t> |
| 248 (year - 1970) * 365 + day) * 24 + hour) * 60 + min) * 60 + sec; |
| 249 } |
| 250 |
207 } // namespace rtc | 251 } // namespace rtc |
OLD | NEW |