michael@0: /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* vim: set ts=8 sts=2 et sw=2 tw=80: */ michael@0: /* This code is made available to you under your choice of the following sets michael@0: * of licensing terms: michael@0: */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. michael@0: */ michael@0: /* Copyright 2014 Mozilla Contributors michael@0: * michael@0: * Licensed under the Apache License, Version 2.0 (the "License"); michael@0: * you may not use this file except in compliance with the License. michael@0: * You may obtain a copy of the License at michael@0: * michael@0: * http://www.apache.org/licenses/LICENSE-2.0 michael@0: * michael@0: * Unless required by applicable law or agreed to in writing, software michael@0: * distributed under the License is distributed on an "AS IS" BASIS, michael@0: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. michael@0: * See the License for the specific language governing permissions and michael@0: * limitations under the License. michael@0: */ michael@0: michael@0: #include "pkix/Time.h" michael@0: #include "pkixutil.h" michael@0: #ifdef WIN32 michael@0: #include "windows.h" michael@0: #else michael@0: #include "sys/time.h" michael@0: #endif michael@0: michael@0: namespace mozilla { namespace pkix { michael@0: michael@0: Time michael@0: Now() michael@0: { michael@0: uint64_t seconds; michael@0: michael@0: #ifdef WIN32 michael@0: // "Contains a 64-bit value representing the number of 100-nanosecond michael@0: // intervals since January 1, 1601 (UTC)." michael@0: // - http://msdn.microsoft.com/en-us/library/windows/desktop/ms724284(v=vs.85).aspx michael@0: FILETIME ft; michael@0: GetSystemTimeAsFileTime(&ft); michael@0: uint64_t ft64 = (static_cast(ft.dwHighDateTime) << 32) | michael@0: ft.dwLowDateTime; michael@0: seconds = (DaysBeforeYear(1601) * Time::ONE_DAY_IN_SECONDS) + michael@0: ft64 / (1000u * 1000u * 1000u / 100u); michael@0: #else michael@0: // "The gettimeofday() function shall obtain the current time, expressed as michael@0: // seconds and microseconds since the Epoch." michael@0: // - http://pubs.opengroup.org/onlinepubs/009695399/functions/gettimeofday.html michael@0: timeval tv; michael@0: (void) gettimeofday(&tv, nullptr); michael@0: seconds = (DaysBeforeYear(1970) * Time::ONE_DAY_IN_SECONDS) + tv.tv_sec; michael@0: #endif michael@0: michael@0: return TimeFromElapsedSecondsAD(seconds); michael@0: } michael@0: michael@0: Time michael@0: TimeFromEpochInSeconds(uint64_t secondsSinceEpoch) michael@0: { michael@0: uint64_t seconds = (DaysBeforeYear(1970) * Time::ONE_DAY_IN_SECONDS) + michael@0: secondsSinceEpoch; michael@0: return TimeFromElapsedSecondsAD(seconds); michael@0: } michael@0: michael@0: } } // namespace mozilla::pkix