time.h 2.64 KB
Newer Older
1
// Copyright (c) 2014 Google Inc. (contributed by Remy Blank <rblank@google.com>)
Kenton Varda's avatar
Kenton Varda committed
2 3
// Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors
// Licensed under the MIT License:
4
//
Kenton Varda's avatar
Kenton Varda committed
5 6 7 8 9 10
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
11
//
Kenton Varda's avatar
Kenton Varda committed
12 13
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
14
//
Kenton Varda's avatar
Kenton Varda committed
15 16 17 18 19 20 21
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
22

23
#pragma once
24

25 26 27 28
#if defined(__GNUC__) && !KJ_HEADER_WARNINGS
#pragma GCC system_header
#endif

29 30 31 32 33 34
#include "units.h"
#include <inttypes.h>

namespace kj {
namespace _ {  // private

35 36 37
class NanosecondLabel;
class TimeLabel;
class DateLabel;
38 39 40

}  // namespace _ (private)

41
using Duration = Quantity<int64_t, _::NanosecondLabel>;
Kenton Varda's avatar
Kenton Varda committed
42
// A time value, in nanoseconds.
43

44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
constexpr Duration NANOSECONDS = unit<Duration>();
constexpr Duration MICROSECONDS = 1000 * NANOSECONDS;
constexpr Duration MILLISECONDS = 1000 * MICROSECONDS;
constexpr Duration SECONDS = 1000 * MILLISECONDS;
constexpr Duration MINUTES = 60 * SECONDS;
constexpr Duration HOURS = 60 * MINUTES;
constexpr Duration DAYS = 24 * HOURS;

using TimePoint = Absolute<Duration, _::TimeLabel>;
// An absolute time measured by some particular instance of `Timer`.  `Time`s from two different
// `Timer`s may be measured from different origins and so are not necessarily compatible.

using Date = Absolute<Duration, _::DateLabel>;
// A point in real-world time, measured relative to the Unix epoch (Jan 1, 1970 00:00:00 UTC).

constexpr Date UNIX_EPOCH = origin<Date>();
// The `Date` representing Jan 1, 1970 00:00:00 UTC.
61

62 63 64
class Clock {
  // Interface to read the current date and time.
public:
65
  virtual Date now() const = 0;
66 67
};

68
const Clock& nullClock();
69 70 71
// A clock which always returns UNIX_EPOCH as the current time. Useful when you don't care about
// time.

72
}  // namespace kj