security/pkix/lib/pkixtime.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/security/pkix/lib/pkixtime.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,70 @@
     1.4 +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* vim: set ts=8 sts=2 et sw=2 tw=80: */
     1.6 +/* This code is made available to you under your choice of the following sets
     1.7 + * of licensing terms:
     1.8 + */
     1.9 +/* This Source Code Form is subject to the terms of the Mozilla Public
    1.10 + * License, v. 2.0. If a copy of the MPL was not distributed with this
    1.11 + * file, You can obtain one at http://mozilla.org/MPL/2.0/.
    1.12 + */
    1.13 +/* Copyright 2014 Mozilla Contributors
    1.14 + *
    1.15 + * Licensed under the Apache License, Version 2.0 (the "License");
    1.16 + * you may not use this file except in compliance with the License.
    1.17 + * You may obtain a copy of the License at
    1.18 + *
    1.19 + *     http://www.apache.org/licenses/LICENSE-2.0
    1.20 + *
    1.21 + * Unless required by applicable law or agreed to in writing, software
    1.22 + * distributed under the License is distributed on an "AS IS" BASIS,
    1.23 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    1.24 + * See the License for the specific language governing permissions and
    1.25 + * limitations under the License.
    1.26 + */
    1.27 +
    1.28 +#include "pkix/Time.h"
    1.29 +#include "pkixutil.h"
    1.30 +#ifdef WIN32
    1.31 +#include "windows.h"
    1.32 +#else
    1.33 +#include "sys/time.h"
    1.34 +#endif
    1.35 +
    1.36 +namespace mozilla { namespace pkix {
    1.37 +
    1.38 +Time
    1.39 +Now()
    1.40 +{
    1.41 +  uint64_t seconds;
    1.42 +
    1.43 +#ifdef WIN32
    1.44 +  // "Contains a 64-bit value representing the number of 100-nanosecond
    1.45 +  // intervals since January 1, 1601 (UTC)."
    1.46 +  //   - http://msdn.microsoft.com/en-us/library/windows/desktop/ms724284(v=vs.85).aspx
    1.47 +  FILETIME ft;
    1.48 +  GetSystemTimeAsFileTime(&ft);
    1.49 +  uint64_t ft64 = (static_cast<uint64_t>(ft.dwHighDateTime) << 32) |
    1.50 +                  ft.dwLowDateTime;
    1.51 +  seconds = (DaysBeforeYear(1601) * Time::ONE_DAY_IN_SECONDS) +
    1.52 +            ft64 / (1000u * 1000u * 1000u / 100u);
    1.53 +#else
    1.54 +  // "The gettimeofday() function shall obtain the current time, expressed as
    1.55 +  // seconds and microseconds since the Epoch."
    1.56 +  //   - http://pubs.opengroup.org/onlinepubs/009695399/functions/gettimeofday.html
    1.57 +  timeval tv;
    1.58 +  (void) gettimeofday(&tv, nullptr);
    1.59 +  seconds = (DaysBeforeYear(1970) * Time::ONE_DAY_IN_SECONDS) + tv.tv_sec;
    1.60 +#endif
    1.61 +
    1.62 +  return TimeFromElapsedSecondsAD(seconds);
    1.63 +}
    1.64 +
    1.65 +Time
    1.66 +TimeFromEpochInSeconds(uint64_t secondsSinceEpoch)
    1.67 +{
    1.68 +  uint64_t seconds = (DaysBeforeYear(1970) * Time::ONE_DAY_IN_SECONDS) +
    1.69 +                     secondsSinceEpoch;
    1.70 +  return TimeFromElapsedSecondsAD(seconds);
    1.71 +}
    1.72 +
    1.73 +} } // namespace mozilla::pkix

mercurial