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