Wed, 31 Dec 2014 06:55:50 +0100
Added tag UPSTREAM_283F7C6 for changeset ca08bd8f51b2
michael@0 | 1 | /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
michael@0 | 2 | /* vim: set ts=8 sts=2 et sw=2 tw=80: */ |
michael@0 | 3 | /* This code is made available to you under your choice of the following sets |
michael@0 | 4 | * of licensing terms: |
michael@0 | 5 | */ |
michael@0 | 6 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 7 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 8 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. |
michael@0 | 9 | */ |
michael@0 | 10 | /* Copyright 2014 Mozilla Contributors |
michael@0 | 11 | * |
michael@0 | 12 | * Licensed under the Apache License, Version 2.0 (the "License"); |
michael@0 | 13 | * you may not use this file except in compliance with the License. |
michael@0 | 14 | * You may obtain a copy of the License at |
michael@0 | 15 | * |
michael@0 | 16 | * http://www.apache.org/licenses/LICENSE-2.0 |
michael@0 | 17 | * |
michael@0 | 18 | * Unless required by applicable law or agreed to in writing, software |
michael@0 | 19 | * distributed under the License is distributed on an "AS IS" BASIS, |
michael@0 | 20 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
michael@0 | 21 | * See the License for the specific language governing permissions and |
michael@0 | 22 | * limitations under the License. |
michael@0 | 23 | */ |
michael@0 | 24 | |
michael@0 | 25 | #include "pkix/Time.h" |
michael@0 | 26 | #include "pkixutil.h" |
michael@0 | 27 | #ifdef WIN32 |
michael@0 | 28 | #include "windows.h" |
michael@0 | 29 | #else |
michael@0 | 30 | #include "sys/time.h" |
michael@0 | 31 | #endif |
michael@0 | 32 | |
michael@0 | 33 | namespace mozilla { namespace pkix { |
michael@0 | 34 | |
michael@0 | 35 | Time |
michael@0 | 36 | Now() |
michael@0 | 37 | { |
michael@0 | 38 | uint64_t seconds; |
michael@0 | 39 | |
michael@0 | 40 | #ifdef WIN32 |
michael@0 | 41 | // "Contains a 64-bit value representing the number of 100-nanosecond |
michael@0 | 42 | // intervals since January 1, 1601 (UTC)." |
michael@0 | 43 | // - http://msdn.microsoft.com/en-us/library/windows/desktop/ms724284(v=vs.85).aspx |
michael@0 | 44 | FILETIME ft; |
michael@0 | 45 | GetSystemTimeAsFileTime(&ft); |
michael@0 | 46 | uint64_t ft64 = (static_cast<uint64_t>(ft.dwHighDateTime) << 32) | |
michael@0 | 47 | ft.dwLowDateTime; |
michael@0 | 48 | seconds = (DaysBeforeYear(1601) * Time::ONE_DAY_IN_SECONDS) + |
michael@0 | 49 | ft64 / (1000u * 1000u * 1000u / 100u); |
michael@0 | 50 | #else |
michael@0 | 51 | // "The gettimeofday() function shall obtain the current time, expressed as |
michael@0 | 52 | // seconds and microseconds since the Epoch." |
michael@0 | 53 | // - http://pubs.opengroup.org/onlinepubs/009695399/functions/gettimeofday.html |
michael@0 | 54 | timeval tv; |
michael@0 | 55 | (void) gettimeofday(&tv, nullptr); |
michael@0 | 56 | seconds = (DaysBeforeYear(1970) * Time::ONE_DAY_IN_SECONDS) + tv.tv_sec; |
michael@0 | 57 | #endif |
michael@0 | 58 | |
michael@0 | 59 | return TimeFromElapsedSecondsAD(seconds); |
michael@0 | 60 | } |
michael@0 | 61 | |
michael@0 | 62 | Time |
michael@0 | 63 | TimeFromEpochInSeconds(uint64_t secondsSinceEpoch) |
michael@0 | 64 | { |
michael@0 | 65 | uint64_t seconds = (DaysBeforeYear(1970) * Time::ONE_DAY_IN_SECONDS) + |
michael@0 | 66 | secondsSinceEpoch; |
michael@0 | 67 | return TimeFromElapsedSecondsAD(seconds); |
michael@0 | 68 | } |
michael@0 | 69 | |
michael@0 | 70 | } } // namespace mozilla::pkix |