summaryrefslogtreecommitdiff
path: root/absl/time
diff options
context:
space:
mode:
authorGravatar Abseil Team <absl-team@google.com>2018-10-10 12:31:37 -0700
committerGravatar CJ Johnson <johnsoncj@google.com>2018-10-10 15:35:19 -0400
commitf340f773edab951656b19b6f1a77c964a78ec4c2 (patch)
treec42bf7faf49fb2355661c9f39c40513bc1ff2697 /absl/time
parent445998d7ac4e5d3c50411d377e3b50e960d2d6c2 (diff)
Export of internal Abseil changes.
-- 906c47420646d510edd2479d5542c56f5fa31b65 by CJ Johnson <johnsoncj@google.com>: Import of CCTZ from GitHub. PiperOrigin-RevId: 216573923 -- 74560d4afd2b605909e677c6fc3076049fb3010a by Eric Fiselier <ericwf@google.com>: Avoid -Wformat-pedantic in benchmark. PiperOrigin-RevId: 216523769 -- 9bcc9da8b03e6d1ea43ee78931256c5541cb9686 by Eric Fiselier <ericwf@google.com>: Delete unused CityHash functions. PiperOrigin-RevId: 216464492 -- a42563b394c89fbb4c55cb5a6a5edbf96d271eea by Abseil Team <absl-team@google.com>: Introduce new Abseil interfaces for converting between civil times and absolute times.s Deprecates absl::ConvertDateTime() and absl::FromDateTime(). PiperOrigin-RevId: 216424948 -- 088e11235124267517d7f137854fa5554679c24f by Eric Fiselier <ericwf@google.com>: Remove unneeded break statements in test. PiperOrigin-RevId: 216403321 GitOrigin-RevId: 906c47420646d510edd2479d5542c56f5fa31b65 Change-Id: Idb44420be623e369c66f5a9c92bdc9ab46d3ec92
Diffstat (limited to 'absl/time')
-rw-r--r--absl/time/BUILD.bazel5
-rw-r--r--absl/time/CMakeLists.txt4
-rw-r--r--absl/time/civil_time.cc88
-rw-r--r--absl/time/civil_time.h487
-rw-r--r--absl/time/civil_time_benchmark.cc57
-rw-r--r--absl/time/civil_time_test.cc1073
-rw-r--r--absl/time/format_benchmark.cc7
-rw-r--r--absl/time/format_test.cc37
-rw-r--r--absl/time/internal/cctz/src/time_zone_posix.h40
-rw-r--r--absl/time/internal/test_util.cc6
-rw-r--r--absl/time/internal/test_util.h24
-rw-r--r--absl/time/time.cc299
-rw-r--r--absl/time/time.h614
-rw-r--r--absl/time/time_benchmark.cc38
-rw-r--r--absl/time/time_norm_test.cc306
-rw-r--r--absl/time/time_test.cc330
16 files changed, 2524 insertions, 891 deletions
diff --git a/absl/time/BUILD.bazel b/absl/time/BUILD.bazel
index c7c16d43..969ddd2e 100644
--- a/absl/time/BUILD.bazel
+++ b/absl/time/BUILD.bazel
@@ -27,6 +27,7 @@ licenses(["notice"]) # Apache 2.0
cc_library(
name = "time",
srcs = [
+ "civil_time.cc",
"clock.cc",
"duration.cc",
"format.cc",
@@ -35,6 +36,7 @@ cc_library(
"time.cc",
],
hdrs = [
+ "civil_time.h",
"clock.h",
"time.h",
],
@@ -72,10 +74,10 @@ cc_library(
cc_test(
name = "time_test",
srcs = [
+ "civil_time_test.cc",
"clock_test.cc",
"duration_test.cc",
"format_test.cc",
- "time_norm_test.cc",
"time_test.cc",
"time_zone_test.cc",
],
@@ -94,6 +96,7 @@ cc_test(
cc_test(
name = "time_benchmark",
srcs = [
+ "civil_time_benchmark.cc",
"clock_benchmark.cc",
"duration_benchmark.cc",
"format_benchmark.cc",
diff --git a/absl/time/CMakeLists.txt b/absl/time/CMakeLists.txt
index 06272364..53216cda 100644
--- a/absl/time/CMakeLists.txt
+++ b/absl/time/CMakeLists.txt
@@ -15,6 +15,7 @@
#
list(APPEND TIME_PUBLIC_HEADERS
+ "civil_time.h"
"clock.h"
"time.h"
)
@@ -29,6 +30,7 @@ list(APPEND TIME_INTERNAL_HEADERS
)
list(APPEND TIME_SRC
+ "civil_time.cc"
"time.cc"
"clock.cc"
"duration.cc"
@@ -74,11 +76,11 @@ absl_library(
# test time_test
list(APPEND TIME_TEST_SRC
+ "civil_time_test.cc"
"time_test.cc"
"clock_test.cc"
"duration_test.cc"
"format_test.cc"
- "time_norm_test.cc"
"time_test.cc"
"time_zone_test.cc"
"internal/test_util.cc"
diff --git a/absl/time/civil_time.cc b/absl/time/civil_time.cc
new file mode 100644
index 00000000..56541799
--- /dev/null
+++ b/absl/time/civil_time.cc
@@ -0,0 +1,88 @@
+// Copyright 2018 The Abseil Authors.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#include "absl/time/civil_time.h"
+
+#include <cstdlib>
+#include <string>
+
+#include "absl/strings/str_cat.h"
+#include "absl/time/time.h"
+
+namespace absl {
+
+namespace {
+
+// Since a civil time has a larger year range than absl::Time (64-bit years vs
+// 64-bit seconds, respectively) we normalize years to roughly +/- 400 years
+// around the year 2400, which will produce an equivalent year in a range that
+// absl::Time can handle.
+inline civil_year_t NormalizeYear(civil_year_t year) {
+ return 2400 + year % 400;
+}
+
+// Formats the given CivilSecond according to the given format.
+std::string FormatYearAnd(string_view fmt, CivilSecond cs) {
+ const CivilSecond ncs(NormalizeYear(cs.year()), cs.month(), cs.day(),
+ cs.hour(), cs.minute(), cs.second());
+ const TimeZone utc = UTCTimeZone();
+ // TODO(absl-team): Avoid conversion of fmt std::string.
+ return StrCat(cs.year(), FormatTime(std::string(fmt), FromCivil(ncs, utc), utc));
+}
+
+} // namespace
+
+std::string FormatCivilTime(CivilSecond c) {
+ return FormatYearAnd("-%m-%dT%H:%M:%S", c);
+}
+std::string FormatCivilTime(CivilMinute c) {
+ return FormatYearAnd("-%m-%dT%H:%M", c);
+}
+std::string FormatCivilTime(CivilHour c) {
+ return FormatYearAnd("-%m-%dT%H", c);
+}
+std::string FormatCivilTime(CivilDay c) {
+ return FormatYearAnd("-%m-%d", c);
+}
+std::string FormatCivilTime(CivilMonth c) {
+ return FormatYearAnd("-%m", c);
+}
+std::string FormatCivilTime(CivilYear c) {
+ return FormatYearAnd("", c);
+}
+
+namespace time_internal {
+
+std::ostream& operator<<(std::ostream& os, CivilYear y) {
+ return os << FormatCivilTime(y);
+}
+std::ostream& operator<<(std::ostream& os, CivilMonth m) {
+ return os << FormatCivilTime(m);
+}
+std::ostream& operator<<(std::ostream& os, CivilDay d) {
+ return os << FormatCivilTime(d);
+}
+std::ostream& operator<<(std::ostream& os, CivilHour h) {
+ return os << FormatCivilTime(h);
+}
+std::ostream& operator<<(std::ostream& os, CivilMinute m) {
+ return os << FormatCivilTime(m);
+}
+std::ostream& operator<<(std::ostream& os, CivilSecond s) {
+ return os << FormatCivilTime(s);
+}
+
+} // namespace time_internal
+
+} // namespace absl
diff --git a/absl/time/civil_time.h b/absl/time/civil_time.h
new file mode 100644
index 00000000..a1f55305
--- /dev/null
+++ b/absl/time/civil_time.h
@@ -0,0 +1,487 @@
+// Copyright 2018 The Abseil Authors.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// -----------------------------------------------------------------------------
+// File: civil_time.h
+// -----------------------------------------------------------------------------
+//
+// This header file defines abstractions for computing with "civil time".
+// The term "civil time" refers to the legally recognized human-scale time
+// that is represented by the six fields `YYYY-MM-DD hh:mm:ss`. A "date"
+// is perhaps the most common example of a civil time (represented here as
+// an `absl::CivilDay`).
+//
+// Modern-day civil time follows the Gregorian Calendar and is a
+// time-zone-independent concept: a civil time of "2015-06-01 12:00:00", for
+// example, is not tied to a time zone. Put another way, a civil time does not
+// map to a unique point in time; a civil time must be mapped to an absolute
+// time *through* a time zone.
+//
+// Because a civil time is what most people think of as "time," it is common to
+// map absolute times to civil times to present to users.
+//
+// Time zones define the relationship between absolute and civil times. Given an
+// absolute or civil time and a time zone, you can compute the other time:
+//
+// Civil Time = F(Absolute Time, Time Zone)
+// Absolute Time = G(Civil Time, Time Zone)
+//
+// The Abseil time library allows you to construct such civil times from
+// absolute times; consult time.h for such functionality.
+//
+// This library provides six classes for constructing civil-time objects, and
+// provides several helper functions for rounding, iterating, and performing
+// arithmetic on civil-time objects, while avoiding complications like
+// daylight-saving time (DST):
+//
+// * `absl::CivilSecond`
+// * `absl::CivilMinute`
+// * `absl::CivilHour`
+// * `absl::CivilDay`
+// * `absl::CivilMonth`
+// * `absl::CivilYear`
+//
+// Example:
+//
+// // Construct a civil-time object for a specific day
+// const absl::CivilDay cd(1969, 07, 20);
+//
+// // Construct a civil-time object for a specific second
+// const absl::CivilSecond cd(2018, 8, 1, 12, 0, 1);
+//
+// Note: In C++14 and later, this library is usable in a constexpr context.
+//
+// Example:
+//
+// // Valid in C++14
+// constexpr absl::CivilDay cd(1969, 07, 20);
+//
+
+#ifndef ABSL_TIME_CIVIL_TIME_H_
+#define ABSL_TIME_CIVIL_TIME_H_
+
+#include <string>
+
+#include "absl/base/port.h" // Needed for string vs std::string
+#include "absl/strings/string_view.h"
+#include "absl/time/internal/cctz/include/cctz/civil_time.h"
+
+namespace absl {
+
+namespace time_internal {
+struct second_tag : cctz::detail::second_tag {};
+struct minute_tag : second_tag, cctz::detail::minute_tag {};
+struct hour_tag : minute_tag, cctz::detail::hour_tag {};
+struct day_tag : hour_tag, cctz::detail::day_tag {};
+struct month_tag : day_tag, cctz::detail::month_tag {};
+struct year_tag : month_tag, cctz::detail::year_tag {};
+} // namespace time_internal
+
+// -----------------------------------------------------------------------------
+// CivilSecond, CivilMinute, CivilHour, CivilDay, CivilMonth, CivilYear
+// -----------------------------------------------------------------------------
+//
+// Each of these civil-time types is a simple value type with the same
+// interface for construction and the same six accessors for each of the civil
+// time fields (year, month, day, hour, minute, and second, aka YMDHMS). These
+// classes differ only in their alignment, which is indicated by the type name
+// and specifies the field on which arithmetic operates.
+//
+// CONSTRUCTION
+//
+// Each of the civil-time types can be constructed in two ways: by directly
+// passing to the constructor up to six integers representing the YMDHMS fields,
+// or by copying the YMDHMS fields from a differently aligned civil-time type.
+// Omitted fields are assigned their minimum valid value. Hours, minutes, and
+// seconds will be set to 0, month and day will be set to 1. Since there is no
+// minimum year, the default is 1970.
+//
+// Examples:
+//
+// absl::CivilDay default_value; // 1970-01-01 00:00:00
+//
+// absl::CivilDay a(2015, 2, 3); // 2015-02-03 00:00:00
+// absl::CivilDay b(2015, 2, 3, 4, 5, 6); // 2015-02-03 00:00:00
+// absl::CivilDay c(2015); // 2015-01-01 00:00:00
+//
+// absl::CivilSecond ss(2015, 2, 3, 4, 5, 6); // 2015-02-03 04:05:06
+// absl::CivilMinute mm(ss); // 2015-02-03 04:05:00
+// absl::CivilHour hh(mm); // 2015-02-03 04:00:00
+// absl::CivilDay d(hh); // 2015-02-03 00:00:00
+// absl::CivilMonth m(d); // 2015-02-01 00:00:00
+// absl::CivilYear y(m); // 2015-01-01 00:00:00
+//
+// m = absl::CivilMonth(y); // 2015-01-01 00:00:00
+// d = absl::CivilDay(m); // 2015-01-01 00:00:00
+// hh = absl::CivilHour(d); // 2015-01-01 00:00:00
+// mm = absl::CivilMinute(hh); // 2015-01-01 00:00:00
+// ss = absl::CivilSecond(mm); // 2015-01-01 00:00:00
+//
+// Each civil-time class is aligned to the civil-time field indicated in the
+// class's name after normalization. Alignment is performed by setting all the
+// inferior fields to their minimum valid value (as described above). The
+// following are examples of how each of the six types would align the fields
+// representing November 22, 2015 at 12:34:56 in the afternoon. (Note: the
+// string format used here is not important; it's just a shorthand way of
+// showing the six YMDHMS fields.)
+//
+// absl::CivilSecond : 2015-11-22 12:34:56
+// absl::CivilMinute : 2015-11-22 12:34:00
+// absl::CivilHour : 2015-11-22 12:00:00
+// absl::CivilDay : 2015-11-22 00:00:00
+// absl::CivilMonth : 2015-11-01 00:00:00
+// absl::CivilYear : 2015-01-01 00:00:00
+//
+// Each civil-time type performs arithmetic on the field to which it is
+// aligned. This means that adding 1 to an absl::CivilDay increments the day
+// field (normalizing as necessary), and subtracting 7 from an absl::CivilMonth
+// operates on the month field (normalizing as necessary). All arithmetic
+// produces a valid civil time. Difference requires two similarly aligned
+// civil-time objects and returns the scalar answer in units of the objects'
+// alignment. For example, the difference between two absl::CivilHour objects
+// will give an answer in units of civil hours.
+//
+// ALIGNMENT CONVERSION
+//
+// The alignment of a civil-time object cannot change, but the object may be
+// used to construct a new object with a different alignment. This is referred
+// to as "realigning". When realigning to a type with the same or more
+// precision (e.g., absl::CivilDay -> absl::CivilSecond), the conversion may be
+// performed implicitly since no information is lost. However, if information
+// could be discarded (e.g., CivilSecond -> CivilDay), the conversion must
+// be explicit at the call site.
+//
+// Examples:
+//
+// void UseDay(absl::CivilDay day);
+//
+// absl::CivilSecond cs;
+// UseDay(cs); // Won't compile because data may be discarded
+// UseDay(absl::CivilDay(cs)); // OK: explicit conversion
+//
+// absl::CivilDay cd;
+// UseDay(cd); // OK: no conversion needed
+//
+// absl::CivilMonth cm;
+// UseDay(cm); // OK: implicit conversion to absl::CivilDay
+//
+// NORMALIZATION
+//
+// Normalization takes invalid values and adjusts them to produce valid values.
+// Within the civil-time library, integer arguments passed to the Civil*
+// constructors may be out-of-range, in which case they are normalized by
+// carrying overflow into a field of courser granularity to produce valid
+// civil-time objects. This normalization enables natural arithmetic on
+// constructor arguments without worrying about the field's range.
+//
+// Examples:
+//
+// // Out-of-range; normalized to 2016-11-01
+// absl::CivilDay d(2016, 10, 32);
+// // Out-of-range, negative: normalized to 2016-10-30T23
+// absl::CivilHour h1(2016, 10, 31, -1);
+// // Normalization is cumulative: normalized to 2016-10-30T23
+// absl::CivilHour h2(2016, 10, 32, -25);
+//
+// Note: If normalization is undesired, you can signal an error by comparing
+// the constructor arguments to the normalized values returned by the YMDHMS
+// properties.
+//
+// COMPARISON
+//
+// Comparison between civil-time objects considers all six YMDHMS fields,
+// regardless of the type's alignment. Comparison between differently aligned
+// civil-time types is allowed.
+//
+// Examples:
+//
+// absl::CivilDay feb_3(2015, 2, 3); // 2015-02-03 00:00:00
+// absl::CivilDay mar_4(2015, 3, 4); // 2015-03-04 00:00:00
+// // feb_3 < mar_4
+// // absl::CivilYear(feb_3) == absl::CivilYear(mar_4)
+//
+// absl::CivilSecond feb_3_noon(2015, 2, 3, 12, 0, 0); // 2015-02-03 12:00:00
+// // feb_3 < feb_3_noon
+// // feb_3 == absl::CivilDay(feb_3_noon)
+//
+// // Iterates all the days of February 2015.
+// for (absl::CivilDay d(2015, 2, 1); d < absl::CivilMonth(2015, 3); ++d) {
+// // ...
+// }
+//
+// ARITHMETIC
+//
+// Civil-time types support natural arithmetic operators such as addition,
+// subtraction, and difference. Arithmetic operates on the civil-time field
+// indicated in the type's name. Difference operators require arguments with
+// the same alignment and return the answer in units of the alignment.
+//
+// Example:
+//
+// absl::CivilDay a(2015, 2, 3);
+// ++a; // 2015-02-04 00:00:00
+// --a; // 2015-02-03 00:00:00
+// absl::CivilDay b = a + 1; // 2015-02-04 00:00:00
+// absl::CivilDay c = 1 + b; // 2015-02-05 00:00:00
+// int n = c - a; // n = 2 (civil days)
+// int m = c - absl::CivilMonth(c); // Won't compile: different types.
+//
+// ACCESSORS
+//
+// Each civil-time type has accessors for all six of the civil-time fields:
+// year, month, day, hour, minute, and second.
+//
+// civil_year_t year()
+// int month()
+// int day()
+// int hour()
+// int minute()
+// int second()
+//
+// Recall that fields inferior to the type's aligment will be set to their
+// minimum valid value.
+//
+// Example:
+//
+// absl::CivilDay d(2015, 6, 28);
+// // d.year() == 2015
+// // d.month() == 6
+// // d.day() == 28
+// // d.hour() == 0
+// // d.minute() == 0
+// // d.second() == 0
+//
+// CASE STUDY: Adding a month to January 31.
+//
+// One of the classic questions that arises when considering a civil time
+// library (or a date library or a date/time library) is this:
+// "What is the result of adding a month to January 31?"
+// This is an interesting question because it is unclear what is meant by a
+// "month", and several different answers are possible, depending on context:
+//
+// 1. March 3 (or 2 if a leap year), if "add a month" means to add a month to
+// the current month, and adjust the date to overflow the extra days into
+// March. In this case the result of "February 31" would be normalized as
+// within the civil-time library.
+// 2. February 28 (or 29 if a leap year), if "add a month" means to add a
+// month, and adjust the date while holding the resulting month constant.
+// In this case, the result of "February 31" would be truncated to the last
+// day in February.
+// 3. An error. The caller may get some error, an exception, an invalid date
+// object, or perhaps return `false`. This may make sense because there is
+// no single unambiguously correct answer to the question.
+//
+// Practically speaking, any answer that is not what the programmer intended
+// is the wrong answer.
+//
+// The Abseil time library avoids this problem by making it impossible to
+// ask ambiguous questions. All civil-time objects are aligned to a particular
+// civil-field boundary (such as aligned to a year, month, day, hour, minute,
+// or second), and arithmetic operates on the field to which the object is
+// aligned. This means that in order to "add a month" the object must first be
+// aligned to a month boundary, which is equivalent to the first day of that
+// month.
+//
+// Of course, there are ways to compute an answer the question at hand using
+// this Abseil time library, but they require the programmer to be explicit
+// about the answer they expect. To illustrate, let's see how to compute all
+// three of the above possible answers to the question of "Jan 31 plus 1
+// month":
+//
+// Example:
+//
+// const absl::CivilDay d(2015, 1, 31);
+//
+// // Answer 1:
+// // Add 1 to the month field in the constructor, and rely on normalization.
+// const auto normalized = absl::CivilDay(d.year(), d.month() + 1, d.day());
+// // normalized == 2015-03-03 (aka Feb 31)
+//
+// // Answer 2:
+// // Add 1 to month field, capping to the end of next month.
+// const auto next_month = absl::CivilMonth(d) + 1;
+// const auto last_day_of_next_month = absl::CivilDay(next_month + 1) - 1;
+// const auto capped = std::min(normalized, last_day_of_next_month);
+// // capped == 2015-02-28
+//
+// // Answer 3:
+// // Signal an error if the normalized answer is not in next month.
+// if (absl::CivilMonth(normalized) != next_month) {
+// // error, month overflow
+// }
+//
+using CivilSecond =
+ time_internal::cctz::detail::civil_time<time_internal::second_tag>;
+using CivilMinute =
+ time_internal::cctz::detail::civil_time<time_internal::minute_tag>;
+using CivilHour =
+ time_internal::cctz::detail::civil_time<time_internal::hour_tag>;
+using CivilDay =
+ time_internal::cctz::detail::civil_time<time_internal::day_tag>;
+using CivilMonth =
+ time_internal::cctz::detail::civil_time<time_internal::month_tag>;
+using CivilYear =
+ time_internal::cctz::detail::civil_time<time_internal::year_tag>;
+
+// civil_year_t
+//
+// Type alias of a civil-time year value. This type is guaranteed to (at least)
+// support any year value supported by `time_t`.
+//
+// Example:
+//
+// absl::CivilSecond cs = ...;
+// absl::civil_year_t y = cs.year();
+// cs = absl::CivilSecond(y, 1, 1, 0, 0 0); // CivilSecond(CivilYear(cs))
+//
+using civil_year_t = time_internal::cctz::year_t;
+
+// civil_diff_t
+//
+// Type alias of the difference between two civil-time values.
+// This type is used to indicate arguments that are not
+// normalized (such as parameters to the civil-time constructors), the results
+// of civil-time subtraction, or the operand to civil-time addition.
+//
+// Example:
+//
+// absl::civil_diff_t n_sec = cs1 - cs2; // cs1 == cs2 + n_sec;
+//
+using civil_diff_t = time_internal::cctz::diff_t;
+
+// Weekday::monday, Weekday::tuesday, Weekday::wednesday, Weekday::thursday,
+// Weekday::friday, Weekday::saturday, Weekday::sunday
+//
+// The Weekday enum class represents the civil-time concept of a "weekday" with
+// members for all days of the week.
+//
+// absl::Weekday wd = absl::Weekday::thursday;
+//
+using Weekday = time_internal::cctz::weekday;
+
+// GetWeekday()
+//
+// Returns the absl::Weekday for the given absl::CivilDay.
+//
+// Example:
+//
+// absl::CivilDay a(2015, 8, 13);
+// absl::Weekday wd = absl::GetWeekday(a); // wd == absl::Weekday::thursday
+//
+inline Weekday GetWeekday(CivilDay cd) {
+ return time_internal::cctz::get_weekday(cd);
+}
+
+// NextWeekday()
+// PrevWeekday()
+//
+// Returns the absl::CivilDay that strictly follows or precedes a given
+// absl::CivilDay, and that falls on the given absl::Weekday.
+//
+// Example, given the following month:
+//
+// August 2015
+// Su Mo Tu We Th Fr Sa
+// 1
+// 2 3 4 5 6 7 8
+// 9 10 11 12 13 14 15
+// 16 17 18 19 20 21 22
+// 23 24 25 26 27 28 29
+// 30 31
+//
+// absl::CivilDay a(2015, 8, 13);
+// // absl::GetWeekday(a) == absl::Weekday::thursday
+// absl::CivilDay b = absl::NextWeekday(a, absl::Weekday::thursday);
+// // b = 2015-08-20
+// absl::CivilDay c = absl::PrevWeekday(a, absl::Weekday::thursday);
+// // c = 2015-08-06
+//
+// absl::CivilDay d = ...
+// // Gets the following Thursday if d is not already Thursday
+// absl::CivilDay thurs1 = absl::PrevWeekday(d, absl::Weekday::thursday) + 7;
+// // Gets the previous Thursday if d is not already Thursday
+// absl::CivilDay thurs2 = absl::NextWeekday(d, absl::Weekday::thursday) - 7;
+//
+inline CivilDay NextWeekday(CivilDay cd, Weekday wd) {
+ return CivilDay(time_internal::cctz::next_weekday(cd, wd));
+}
+inline CivilDay PrevWeekday(CivilDay cd, Weekday wd) {
+ return CivilDay(time_internal::cctz::prev_weekday(cd, wd));
+}
+
+// GetYearDay()
+//
+// Returns the day-of-year for the given absl::CivilDay.
+//
+// Example:
+//
+// absl::CivilDay a(2015, 1, 1);
+// int yd_jan_1 = absl::GetYearDay(a); // yd_jan_1 = 1
+// absl::CivilDay b(2015, 12, 31);
+// int yd_dec_31 = absl::GetYearDay(b); // yd_dec_31 = 365
+//
+inline int GetYearDay(CivilDay cd) {
+ return time_internal::cctz::get_yearday(cd);
+}
+
+// FormatCivilTime()
+//
+// Formats the given civil-time value into a string value of the following
+// format:
+//
+// Type | Format
+// ---------------------------------
+// CivilSecond | YYYY-MM-DDTHH:MM:SS
+// CivilMinute | YYYY-MM-DDTHH:MM
+// CivilHour | YYYY-MM-DDTHH
+// CivilDay | YYYY-MM-DD
+// CivilMonth | YYYY-MM
+// CivilYear | YYYY
+//
+// Example:
+//
+// absl::CivilDay d = absl::CivilDay(1969, 7, 20);
+// string day_string = absl::FormatCivilTime(d); // "1969-07-20"
+//
+std::string FormatCivilTime(CivilSecond c);
+std::string FormatCivilTime(CivilMinute c);
+std::string FormatCivilTime(CivilHour c);
+std::string FormatCivilTime(CivilDay c);
+std::string FormatCivilTime(CivilMonth c);
+std::string FormatCivilTime(CivilYear c);
+
+namespace time_internal { // For functions found via ADL on civil-time tags.
+
+// Streaming Operators
+//
+// Each civil-time type may be sent to an output stream using operator<<().
+// The result matches the string produced by `FormatCivilTime()`.
+//
+// Example:
+//
+// absl::CivilDay d = absl::CivilDay("1969-07-20");
+// std::cout << "Date is: " << d << "\n";
+//
+std::ostream& operator<<(std::ostream& os, CivilYear y);
+std::ostream& operator<<(std::ostream& os, CivilMonth m);
+std::ostream& operator<<(std::ostream& os, CivilDay d);
+std::ostream& operator<<(std::ostream& os, CivilHour h);
+std::ostream& operator<<(std::ostream& os, CivilMinute m);
+std::ostream& operator<<(std::ostream& os, CivilSecond s);
+
+} // namespace time_internal
+
+} // namespace absl
+
+#endif // ABSL_TIME_CIVIL_TIME_H_
diff --git a/absl/time/civil_time_benchmark.cc b/absl/time/civil_time_benchmark.cc
new file mode 100644
index 00000000..567c2a33
--- /dev/null
+++ b/absl/time/civil_time_benchmark.cc
@@ -0,0 +1,57 @@
+// Copyright 2018 The Abseil Authors.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#include "absl/time/civil_time.h"
+
+#include "benchmark/benchmark.h"
+
+namespace {
+
+// Benchmark Time(ns) CPU(ns) Iterations
+// -------------------------------------------------------------------------
+// BM_Difference_Days 20 20 34542508
+// BM_Step_Days 15 15 48098146
+// BM_Format 688 687 1019803
+// BM_Parse 921 920 762788
+// BM_RoundTripFormatParse 1766 1764 396092
+
+void BM_Difference_Days(benchmark::State& state) {
+ const absl::CivilDay c(2014, 8, 22);
+ const absl::CivilDay epoch(1970, 1, 1);
+ while (state.KeepRunning()) {
+ const absl::civil_diff_t n = c - epoch;
+ benchmark::DoNotOptimize(n);
+ }
+}
+BENCHMARK(BM_Difference_Days);
+
+void BM_Step_Days(benchmark::State& state) {
+ const absl::CivilDay kStart(2014, 8, 22);
+ absl::CivilDay c = kStart;
+ while (state.KeepRunning()) {
+ benchmark::DoNotOptimize(++c);
+ }
+}
+BENCHMARK(BM_Step_Days);
+
+void BM_Format(benchmark::State& state) {
+ const absl::CivilSecond c(2014, 1, 2, 3, 4, 5);
+ while (state.KeepRunning()) {
+ const std::string s = absl::FormatCivilTime(c);
+ benchmark::DoNotOptimize(s);
+ }
+}
+BENCHMARK(BM_Format);
+
+} // namespace
diff --git a/absl/time/civil_time_test.cc b/absl/time/civil_time_test.cc
new file mode 100644
index 00000000..dc83d7a9
--- /dev/null
+++ b/absl/time/civil_time_test.cc
@@ -0,0 +1,1073 @@
+// Copyright 2018 The Abseil Authors.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#include "absl/time/civil_time.h"
+
+#include <limits>
+#include <sstream>
+#include <type_traits>
+
+#include "absl/base/macros.h"
+#include "gtest/gtest.h"
+
+namespace {
+
+TEST(CivilTime, DefaultConstruction) {
+ absl::CivilSecond ss;
+ EXPECT_EQ("1970-01-01T00:00:00", absl::FormatCivilTime(ss));
+
+ absl::CivilMinute mm;
+ EXPECT_EQ("1970-01-01T00:00", absl::FormatCivilTime(mm));
+
+ absl::CivilHour hh;
+ EXPECT_EQ("1970-01-01T00", absl::FormatCivilTime(hh));
+
+ absl::CivilDay d;
+ EXPECT_EQ("1970-01-01", absl::FormatCivilTime(d));
+
+ absl::CivilMonth m;
+ EXPECT_EQ("1970-01", absl::FormatCivilTime(m));
+
+ absl::CivilYear y;
+ EXPECT_EQ("1970", absl::FormatCivilTime(y));
+}
+
+TEST(CivilTime, StructMember) {
+ struct S {
+ absl::CivilDay day;
+ };
+ S s = {};
+ EXPECT_EQ(absl::CivilDay{}, s.day);
+}
+
+TEST(CivilTime, FieldsConstruction) {
+ EXPECT_EQ("2015-01-02T03:04:05",
+ absl::FormatCivilTime(absl::CivilSecond(2015, 1, 2, 3, 4, 5)));
+ EXPECT_EQ("2015-01-02T03:04:00",
+ absl::FormatCivilTime(absl::CivilSecond(2015, 1, 2, 3, 4)));
+ EXPECT_EQ("2015-01-02T03:00:00",
+ absl::FormatCivilTime(absl::CivilSecond(2015, 1, 2, 3)));
+ EXPECT_EQ("2015-01-02T00:00:00",
+ absl::FormatCivilTime(absl::CivilSecond(2015, 1, 2)));
+ EXPECT_EQ("2015-01-01T00:00:00",
+ absl::FormatCivilTime(absl::CivilSecond(2015, 1)));
+ EXPECT_EQ("2015-01-01T00:00:00",
+ absl::FormatCivilTime(absl::CivilSecond(2015)));
+
+ EXPECT_EQ("2015-01-02T03:04",
+ absl::FormatCivilTime(absl::CivilMinute(2015, 1, 2, 3, 4, 5)));
+ EXPECT_EQ("2015-01-02T03:04",
+ absl::FormatCivilTime(absl::CivilMinute(2015, 1, 2, 3, 4)));
+ EXPECT_EQ("2015-01-02T03:00",
+ absl::FormatCivilTime(absl::CivilMinute(2015, 1, 2, 3)));
+ EXPECT_EQ("2015-01-02T00:00",
+ absl::FormatCivilTime(absl::CivilMinute(2015, 1, 2)));
+ EXPECT_EQ("2015-01-01T00:00",
+ absl::FormatCivilTime(absl::CivilMinute(2015, 1)));
+ EXPECT_EQ("2015-01-01T00:00",
+ absl::FormatCivilTime(absl::CivilMinute(2015)));
+
+ EXPECT_EQ("2015-01-02T03",
+ absl::FormatCivilTime(absl::CivilHour(2015, 1, 2, 3, 4, 5)));
+ EXPECT_EQ("2015-01-02T03",
+ absl::FormatCivilTime(absl::CivilHour(2015, 1, 2, 3, 4)));
+ EXPECT_EQ("2015-01-02T03",
+ absl::FormatCivilTime(absl::CivilHour(2015, 1, 2, 3)));
+ EXPECT_EQ("2015-01-02T00",
+ absl::FormatCivilTime(absl::CivilHour(2015, 1, 2)));
+ EXPECT_EQ("2015-01-01T00",
+ absl::FormatCivilTime(absl::CivilHour(2015, 1)));
+ EXPECT_EQ("2015-01-01T00",
+ absl::FormatCivilTime(absl::CivilHour(2015)));
+
+ EXPECT_EQ("2015-01-02",
+ absl::FormatCivilTime(absl::CivilDay(2015, 1, 2, 3, 4, 5)));
+ EXPECT_EQ("2015-01-02",
+ absl::FormatCivilTime(absl::CivilDay(2015, 1, 2, 3, 4)));
+ EXPECT_EQ("2015-01-02",
+ absl::FormatCivilTime(absl::CivilDay(2015, 1, 2, 3)));
+ EXPECT_EQ("2015-01-02",
+ absl::FormatCivilTime(absl::CivilDay(2015, 1, 2)));
+ EXPECT_EQ("2015-01-01",
+ absl::FormatCivilTime(absl::CivilDay(2015, 1)));
+ EXPECT_EQ("2015-01-01",
+ absl::FormatCivilTime(absl::CivilDay(2015)));
+
+ EXPECT_EQ("2015-01",
+ absl::FormatCivilTime(absl::CivilMonth(2015, 1, 2, 3, 4, 5)));
+ EXPECT_EQ("2015-01",
+ absl::FormatCivilTime(absl::CivilMonth(2015, 1, 2, 3, 4)));
+ EXPECT_EQ("2015-01",
+ absl::FormatCivilTime(absl::CivilMonth(2015, 1, 2, 3)));
+ EXPECT_EQ("2015-01",
+ absl::FormatCivilTime(absl::CivilMonth(2015, 1, 2)));
+ EXPECT_EQ("2015-01",
+ absl::FormatCivilTime(absl::CivilMonth(2015, 1)));
+ EXPECT_EQ("2015-01",
+ absl::FormatCivilTime(absl::CivilMonth(2015)));
+
+ EXPECT_EQ("2015",
+ absl::FormatCivilTime(absl::CivilYear(2015, 1, 2, 3, 4, 5)));
+ EXPECT_EQ("2015",
+ absl::FormatCivilTime(absl::CivilYear(2015, 1, 2, 3, 4)));
+ EXPECT_EQ("2015",
+ absl::FormatCivilTime(absl::CivilYear(2015, 1, 2, 3)));
+ EXPECT_EQ("2015",
+ absl::FormatCivilTime(absl::CivilYear(2015, 1, 2)));
+ EXPECT_EQ("2015",
+ absl::FormatCivilTime(absl::CivilYear(2015, 1)));
+ EXPECT_EQ("2015",
+ absl::FormatCivilTime(absl::CivilYear(2015)));
+}
+
+TEST(CivilTime, FieldsConstructionLimits) {
+ const int kIntMax = std::numeric_limits<int>::max();
+ EXPECT_EQ("2038-01-19T03:14:07",
+ absl::FormatCivilTime(absl::CivilSecond(
+ 1970, 1, 1, 0, 0, kIntMax)));
+ EXPECT_EQ("6121-02-11T05:21:07",
+ absl::FormatCivilTime(absl::CivilSecond(
+ 1970, 1, 1, 0, kIntMax, kIntMax)));
+ EXPECT_EQ("251104-11-20T12:21:07",
+ absl::FormatCivilTime(absl::CivilSecond(
+ 1970, 1, 1, kIntMax, kIntMax, kIntMax)));
+ EXPECT_EQ("6130715-05-30T12:21:07",
+ absl::FormatCivilTime(absl::CivilSecond(
+ 1970, 1, kIntMax, kIntMax, kIntMax, kIntMax)));
+ EXPECT_EQ("185087685-11-26T12:21:07",
+ absl::FormatCivilTime(absl::CivilSecond(
+ 1970, kIntMax, kIntMax, kIntMax, kIntMax, kIntMax)));
+
+ const int kIntMin = std::numeric_limits<int>::min();
+ EXPECT_EQ("1901-12-13T20:45:52",
+ absl::FormatCivilTime(absl::CivilSecond(
+ 1970, 1, 1, 0, 0, kIntMin)));
+ EXPECT_EQ("-2182-11-20T18:37:52",
+ absl::FormatCivilTime(absl::CivilSecond(
+ 1970, 1, 1, 0, kIntMin, kIntMin)));
+ EXPECT_EQ("-247165-02-11T10:37:52",
+ absl::FormatCivilTime(absl::CivilSecond(
+ 1970, 1, 1, kIntMin, kIntMin, kIntMin)));
+ EXPECT_EQ("-6126776-08-01T10:37:52",
+ absl::FormatCivilTime(absl::CivilSecond(
+ 1970, 1, kIntMin, kIntMin, kIntMin, kIntMin)));
+ EXPECT_EQ("-185083747-10-31T10:37:52",
+ absl::FormatCivilTime(absl::CivilSecond(
+ 1970, kIntMin, kIntMin, kIntMin, kIntMin, kIntMin)));
+}
+
+TEST(CivilTime, RangeLimits) {
+ const absl::civil_year_t kYearMax =
+ std::numeric_limits<absl::civil_year_t>::max();
+ EXPECT_EQ(absl::CivilYear(kYearMax),
+ absl::CivilYear::max());
+ EXPECT_EQ(absl::CivilMonth(kYearMax, 12),
+ absl::CivilMonth::max());
+ EXPECT_EQ(absl::CivilDay(kYearMax, 12, 31),
+ absl::CivilDay::max());
+ EXPECT_EQ(absl::CivilHour(kYearMax, 12, 31, 23),
+ absl::CivilHour::max());
+ EXPECT_EQ(absl::CivilMinute(kYearMax, 12, 31, 23, 59),
+ absl::CivilMinute::max());
+ EXPECT_EQ(absl::CivilSecond(kYearMax, 12, 31, 23, 59, 59),
+ absl::CivilSecond::max());
+
+ const absl::civil_year_t kYearMin =
+ std::numeric_limits<absl::civil_year_t>::min();
+ EXPECT_EQ(absl::CivilYear(kYearMin),
+ absl::CivilYear::min());
+ EXPECT_EQ(absl::CivilMonth(kYearMin, 1),
+ absl::CivilMonth::min());
+ EXPECT_EQ(absl::CivilDay(kYearMin, 1, 1),
+ absl::CivilDay::min());
+ EXPECT_EQ(absl::CivilHour(kYearMin, 1, 1, 0),
+ absl::CivilHour::min());
+ EXPECT_EQ(absl::CivilMinute(kYearMin, 1, 1, 0, 0),
+ absl::CivilMinute::min());
+ EXPECT_EQ(absl::CivilSecond(kYearMin, 1, 1, 0, 0, 0),
+ absl::CivilSecond::min());
+}
+
+TEST(CivilTime, ImplicitCrossAlignment) {
+ absl::CivilYear year(2015);
+ absl::CivilMonth month = year;
+ absl::CivilDay day = month;
+ absl::CivilHour hour = day;
+ absl::CivilMinute minute = hour;
+ absl::CivilSecond second = minute;
+
+ second = year;
+ EXPECT_EQ(second, year);
+ second = month;
+ EXPECT_EQ(second, month);
+ second = day;
+ EXPECT_EQ(second, day);
+ second = hour;
+ EXPECT_EQ(second, hour);
+ second = minute;
+ EXPECT_EQ(second, minute);
+
+ minute = year;
+ EXPECT_EQ(minute, year);
+ minute = month;
+ EXPECT_EQ(minute, month);
+ minute = day;
+ EXPECT_EQ(minute, day);
+ minute = hour;
+ EXPECT_EQ(minute, hour);
+
+ hour = year;
+ EXPECT_EQ(hour, year);
+ hour = month;
+ EXPECT_EQ(hour, month);
+ hour = day;
+ EXPECT_EQ(hour, day);
+
+ day = year;
+ EXPECT_EQ(day, year);
+ day = month;
+ EXPECT_EQ(day, month);
+
+ month = year;
+ EXPECT_EQ(month, year);
+
+ // Ensures unsafe conversions are not allowed.
+ EXPECT_FALSE(
+ (std::is_convertible<absl::CivilSecond, absl::CivilMinute>::value));
+ EXPECT_FALSE(
+ (std::is_convertible<absl::CivilSecond, absl::CivilHour>::value));
+ EXPECT_FALSE(
+ (std::is_convertible<absl::CivilSecond, absl::CivilDay>::value));
+ EXPECT_FALSE(
+ (std::is_convertible<absl::CivilSecond, absl::CivilMonth>::value));
+ EXPECT_FALSE(
+ (std::is_convertible<absl::CivilSecond, absl::CivilYear>::value));
+
+ EXPECT_FALSE(
+ (std::is_convertible<absl::CivilMinute, absl::CivilHour>::value));
+ EXPECT_FALSE(
+ (std::is_convertible<absl::CivilMinute, absl::CivilDay>::value));
+ EXPECT_FALSE(
+ (std::is_convertible<absl::CivilMinute, absl::CivilMonth>::value));
+ EXPECT_FALSE(
+ (std::is_convertible<absl::CivilMinute, absl::CivilYear>::value));
+
+ EXPECT_FALSE(
+ (std::is_convertible<absl::CivilHour, absl::CivilDay>::value));
+ EXPECT_FALSE(
+ (std::is_convertible<absl::CivilHour, absl::CivilMonth>::value));
+ EXPECT_FALSE(
+ (std::is_convertible<absl::CivilHour, absl::CivilYear>::value));
+
+ EXPECT_FALSE(
+ (std::is_convertible<absl::CivilDay, absl::CivilMonth>::value));
+ EXPECT_FALSE(
+ (std::is_convertible<absl::CivilDay, absl::CivilYear>::value));
+
+ EXPECT_FALSE(
+ (std::is_convertible<absl::CivilMonth, absl::CivilYear>::value));
+}
+
+TEST(CivilTime, ExplicitCrossAlignment) {
+ //
+ // Assign from smaller units -> larger units
+ //
+
+ absl::CivilSecond second(2015, 1, 2, 3, 4, 5);
+ EXPECT_EQ("2015-01-02T03:04:05", absl::FormatCivilTime(second));
+
+ absl::CivilMinute minute(second);
+ EXPECT_EQ("2015-01-02T03:04", absl::FormatCivilTime(minute));
+
+ absl::CivilHour hour(minute);
+ EXPECT_EQ("2015-01-02T03", absl::FormatCivilTime(hour));
+
+ absl::CivilDay day(hour);
+ EXPECT_EQ("2015-01-02", absl::FormatCivilTime(day));
+
+ absl::CivilMonth month(day);
+ EXPECT_EQ("2015-01", absl::FormatCivilTime(month));
+
+ absl::CivilYear year(month);
+ EXPECT_EQ("2015", absl::FormatCivilTime(year));
+
+ //
+ // Now assign from larger units -> smaller units
+ //
+
+ month = absl::CivilMonth(year);
+ EXPECT_EQ("2015-01", absl::FormatCivilTime(month));
+
+ day = absl::CivilDay(month);
+ EXPECT_EQ("2015-01-01", absl::FormatCivilTime(day));
+
+ hour = absl::CivilHour(day);
+ EXPECT_EQ("2015-01-01T00", absl::FormatCivilTime(hour));
+
+ minute = absl::CivilMinute(hour);
+ EXPECT_EQ("2015-01-01T00:00", absl::FormatCivilTime(minute));
+
+ second = absl::CivilSecond(minute);
+ EXPECT_EQ("2015-01-01T00:00:00", absl::FormatCivilTime(second));
+}
+
+// Metafunction to test whether difference is allowed between two types.
+template <typename T1, typename T2>
+struct HasDiff {
+ template <typename U1, typename U2>
+ static std::false_type test(...);
+ template <typename U1, typename U2>
+ static std::true_type test(decltype(std::declval<U1>() - std::declval<U2>()));
+ static constexpr bool value = decltype(test<T1, T2>(0))::value;
+};
+
+TEST(CivilTime, DisallowCrossAlignedDifference) {
+ // Difference is allowed between types with the same alignment.
+ static_assert(HasDiff<absl::CivilSecond, absl::CivilSecond>::value, "");
+ static_assert(HasDiff<absl::CivilMinute, absl::CivilMinute>::value, "");
+ static_assert(HasDiff<absl::CivilHour, absl::CivilHour>::value, "");
+ static_assert(HasDiff<absl::CivilDay, absl::CivilDay>::value, "");
+ static_assert(HasDiff<absl::CivilMonth, absl::CivilMonth>::value, "");
+ static_assert(HasDiff<absl::CivilYear, absl::CivilYear>::value, "");
+
+ // Difference is disallowed between types with different alignments.
+ static_assert(!HasDiff<absl::CivilSecond, absl::CivilMinute>::value, "");
+ static_assert(!HasDiff<absl::CivilSecond, absl::CivilHour>::value, "");
+ static_assert(!HasDiff<absl::CivilSecond, absl::CivilDay>::value, "");
+ static_assert(!HasDiff<absl::CivilSecond, absl::CivilMonth>::value, "");
+ static_assert(!HasDiff<absl::CivilSecond, absl::CivilYear>::value, "");
+
+ static_assert(!HasDiff<absl::CivilMinute, absl::CivilHour>::value, "");
+ static_assert(!HasDiff<absl::CivilMinute, absl::CivilDay>::value, "");
+ static_assert(!HasDiff<absl::CivilMinute, absl::CivilMonth>::value, "");
+ static_assert(!HasDiff<absl::CivilMinute, absl::CivilYear>::value, "");
+
+ static_assert(!HasDiff<absl::CivilHour, absl::CivilDay>::value, "");
+ static_assert(!HasDiff<absl::CivilHour, absl::CivilMonth>::value, "");
+ static_assert(!HasDiff<absl::CivilHour, absl::CivilYear>::value, "");
+
+ static_assert(!HasDiff<absl::CivilDay, absl::CivilMonth>::value, "");
+ static_assert(!HasDiff<absl::CivilDay, absl::CivilYear>::value, "");
+
+ static_assert(!HasDiff<absl::CivilMonth, absl::CivilYear>::value, "");
+}
+
+TEST(CivilTime, ValueSemantics) {
+ const absl::CivilHour a(2015, 1, 2, 3);
+ const absl::CivilHour b = a;
+ const absl::CivilHour c(b);
+ absl::CivilHour d;
+ d = c;
+ EXPECT_EQ("2015-01-02T03", absl::FormatCivilTime(d));
+}
+
+TEST(CivilTime, Relational) {
+ // Tests that the alignment unit is ignored in comparison.
+ const absl::CivilYear year(2014);
+ const absl::CivilMonth month(year);
+ EXPECT_EQ(year, month);
+
+#define TEST_RELATIONAL(OLDER, YOUNGER) \
+ do { \
+ EXPECT_FALSE(OLDER < OLDER); \
+ EXPECT_FALSE(OLDER > OLDER); \
+ EXPECT_TRUE(OLDER >= OLDER); \
+ EXPECT_TRUE(OLDER <= OLDER); \
+ EXPECT_FALSE(YOUNGER < YOUNGER); \
+ EXPECT_FALSE(YOUNGER > YOUNGER); \
+ EXPECT_TRUE(YOUNGER >= YOUNGER); \
+ EXPECT_TRUE(YOUNGER <= YOUNGER); \
+ EXPECT_EQ(OLDER, OLDER); \
+ EXPECT_NE(OLDER, YOUNGER); \
+ EXPECT_LT(OLDER, YOUNGER); \
+ EXPECT_LE(OLDER, YOUNGER); \
+ EXPECT_GT(YOUNGER, OLDER); \
+ EXPECT_GE(YOUNGER, OLDER); \
+ } while (0)
+
+ // Alignment is ignored in comparison (verified above), so CivilSecond is
+ // used to test comparison in all field positions.
+ TEST_RELATIONAL(absl::CivilSecond(2014, 1, 1, 0, 0, 0),
+ absl::CivilSecond(2015, 1, 1, 0, 0, 0));
+ TEST_RELATIONAL(absl::CivilSecond(2014, 1, 1, 0, 0, 0),
+ absl::CivilSecond(2014, 2, 1, 0, 0, 0));
+ TEST_RELATIONAL(absl::CivilSecond(2014, 1, 1, 0, 0, 0),
+ absl::CivilSecond(2014, 1, 2, 0, 0, 0));
+ TEST_RELATIONAL(absl::CivilSecond(2014, 1, 1, 0, 0, 0),
+ absl::CivilSecond(2014, 1, 1, 1, 0, 0));
+ TEST_RELATIONAL(absl::CivilSecond(2014, 1, 1, 1, 0, 0),
+ absl::CivilSecond(2014, 1, 1, 1, 1, 0));
+ TEST_RELATIONAL(absl::CivilSecond(2014, 1, 1, 1, 1, 0),
+ absl::CivilSecond(2014, 1, 1, 1, 1, 1));
+
+ // Tests the relational operators of two different civil-time types.
+ TEST_RELATIONAL(absl::CivilDay(2014, 1, 1),
+ absl::CivilMinute(2014, 1, 1, 1, 1));
+ TEST_RELATIONAL(absl::CivilDay(2014, 1, 1),
+ absl::CivilMonth(2014, 2));
+
+#undef TEST_RELATIONAL
+}
+
+TEST(CivilTime, Arithmetic) {
+ absl::CivilSecond second(2015, 1, 2, 3, 4, 5);
+ EXPECT_EQ("2015-01-02T03:04:06", absl::FormatCivilTime(second += 1));
+ EXPECT_EQ("2015-01-02T03:04:07", absl::FormatCivilTime(second + 1));
+ EXPECT_EQ("2015-01-02T03:04:08", absl::FormatCivilTime(2 + second));
+ EXPECT_EQ("2015-01-02T03:04:05", absl::FormatCivilTime(second - 1));
+ EXPECT_EQ("2015-01-02T03:04:05", absl::FormatCivilTime(second -= 1));
+ EXPECT_EQ("2015-01-02T03:04:05", absl::FormatCivilTime(second++));
+ EXPECT_EQ("2015-01-02T03:04:07", absl::FormatCivilTime(++second));
+ EXPECT_EQ("2015-01-02T03:04:07", absl::FormatCivilTime(second--));
+ EXPECT_EQ("2015-01-02T03:04:05", absl::FormatCivilTime(--second));
+
+ absl::CivilMinute minute(2015, 1, 2, 3, 4);
+ EXPECT_EQ("2015-01-02T03:05", absl::FormatCivilTime(minute += 1));
+ EXPECT_EQ("2015-01-02T03:06", absl::FormatCivilTime(minute + 1));
+ EXPECT_EQ("2015-01-02T03:07", absl::FormatCivilTime(2 + minute));
+ EXPECT_EQ("2015-01-02T03:04", absl::FormatCivilTime(minute - 1));
+ EXPECT_EQ("2015-01-02T03:04", absl::FormatCivilTime(minute -= 1));
+ EXPECT_EQ("2015-01-02T03:04", absl::FormatCivilTime(minute++));
+ EXPECT_EQ("2015-01-02T03:06", absl::FormatCivilTime(++minute));
+ EXPECT_EQ("2015-01-02T03:06", absl::FormatCivilTime(minute--));
+ EXPECT_EQ("2015-01-02T03:04", absl::FormatCivilTime(--minute));
+
+ absl::CivilHour hour(2015, 1, 2, 3);
+ EXPECT_EQ("2015-01-02T04", absl::FormatCivilTime(hour += 1));
+ EXPECT_EQ("2015-01-02T05", absl::FormatCivilTime(hour + 1));
+ EXPECT_EQ("2015-01-02T06", absl::FormatCivilTime(2 + hour));
+ EXPECT_EQ("2015-01-02T03", absl::FormatCivilTime(hour - 1));
+ EXPECT_EQ("2015-01-02T03", absl::FormatCivilTime(hour -= 1));
+ EXPECT_EQ("2015-01-02T03", absl::FormatCivilTime(hour++));
+ EXPECT_EQ("2015-01-02T05", absl::FormatCivilTime(++hour));
+ EXPECT_EQ("2015-01-02T05", absl::FormatCivilTime(hour--));
+ EXPECT_EQ("2015-01-02T03", absl::FormatCivilTime(--hour));
+
+ absl::CivilDay day(2015, 1, 2);
+ EXPECT_EQ("2015-01-03", absl::FormatCivilTime(day += 1));
+ EXPECT_EQ("2015-01-04", absl::FormatCivilTime(day + 1));
+ EXPECT_EQ("2015-01-05", absl::FormatCivilTime(2 + day));
+ EXPECT_EQ("2015-01-02", absl::FormatCivilTime(day - 1));
+ EXPECT_EQ("2015-01-02", absl::FormatCivilTime(day -= 1));
+ EXPECT_EQ("2015-01-02", absl::FormatCivilTime(day++));
+ EXPECT_EQ("2015-01-04", absl::FormatCivilTime(++day));
+ EXPECT_EQ("2015-01-04", absl::FormatCivilTime(day--));
+ EXPECT_EQ("2015-01-02", absl::FormatCivilTime(--day));
+
+ absl::CivilMonth month(2015, 1);
+ EXPECT_EQ("2015-02", absl::FormatCivilTime(month += 1));
+ EXPECT_EQ("2015-03", absl::FormatCivilTime(month + 1));
+ EXPECT_EQ("2015-04", absl::FormatCivilTime(2 + month));
+ EXPECT_EQ("2015-01", absl::FormatCivilTime(month - 1));
+ EXPECT_EQ("2015-01", absl::FormatCivilTime(month -= 1));
+ EXPECT_EQ("2015-01", absl::FormatCivilTime(month++));
+ EXPECT_EQ("2015-03", absl::FormatCivilTime(++month));
+ EXPECT_EQ("2015-03", absl::FormatCivilTime(month--));
+ EXPECT_EQ("2015-01", absl::FormatCivilTime(--month));
+
+ absl::CivilYear year(2015);
+ EXPECT_EQ("2016", absl::FormatCivilTime(year += 1));
+ EXPECT_EQ("2017", absl::FormatCivilTime(year + 1));
+ EXPECT_EQ("2018", absl::FormatCivilTime(2 + year));
+ EXPECT_EQ("2015", absl::FormatCivilTime(year - 1));
+ EXPECT_EQ("2015", absl::FormatCivilTime(year -= 1));
+ EXPECT_EQ("2015", absl::FormatCivilTime(year++));
+ EXPECT_EQ("2017", absl::FormatCivilTime(++year));
+ EXPECT_EQ("2017", absl::FormatCivilTime(year--));
+ EXPECT_EQ("2015", absl::FormatCivilTime(--year));
+}
+
+TEST(CivilTime, ArithmeticLimits) {
+ const int kIntMax = std::numeric_limits<int>::max();
+ const int kIntMin = std::numeric_limits<int>::min();
+
+ absl::CivilSecond second(1970, 1, 1, 0, 0, 0);
+ second += kIntMax;
+ EXPECT_EQ("2038-01-19T03:14:07", absl::FormatCivilTime(second));
+ second -= kIntMax;
+ EXPECT_EQ("1970-01-01T00:00:00", absl::FormatCivilTime(second));
+ second += kIntMin;
+ EXPECT_EQ("1901-12-13T20:45:52", absl::FormatCivilTime(second));
+ second -= kIntMin;
+ EXPECT_EQ("1970-01-01T00:00:00", absl::FormatCivilTime(second));
+
+ absl::CivilMinute minute(1970, 1, 1, 0, 0);
+ minute += kIntMax;
+ EXPECT_EQ("6053-01-23T02:07", absl::FormatCivilTime(minute));
+ minute -= kIntMax;
+ EXPECT_EQ("1970-01-01T00:00", absl::FormatCivilTime(minute));
+ minute += kIntMin;
+ EXPECT_EQ("-2114-12-08T21:52", absl::FormatCivilTime(minute));
+ minute -= kIntMin;
+ EXPECT_EQ("1970-01-01T00:00", absl::FormatCivilTime(minute));
+
+ absl::CivilHour hour(1970, 1, 1, 0);
+ hour += kIntMax;
+ EXPECT_EQ("246953-10-09T07", absl::FormatCivilTime(hour));
+ hour -= kIntMax;
+ EXPECT_EQ("1970-01-01T00", absl::FormatCivilTime(hour));
+ hour += kIntMin;
+ EXPECT_EQ("-243014-03-24T16", absl::FormatCivilTime(hour));
+ hour -= kIntMin;
+ EXPECT_EQ("1970-01-01T00", absl::FormatCivilTime(hour));
+
+ absl::CivilDay day(1970, 1, 1);
+ day += kIntMax;
+ EXPECT_EQ("5881580-07-11", absl::FormatCivilTime(day));
+ day -= kIntMax;
+ EXPECT_EQ("1970-01-01", absl::FormatCivilTime(day));
+ day += kIntMin;
+ EXPECT_EQ("-5877641-06-23", absl::FormatCivilTime(day));
+ day -= kIntMin;
+ EXPECT_EQ("1970-01-01", absl::FormatCivilTime(day));
+
+ absl::CivilMonth month(1970, 1);
+ month += kIntMax;
+ EXPECT_EQ("178958940-08", absl::FormatCivilTime(month));
+ month -= kIntMax;
+ EXPECT_EQ("1970-01", absl::FormatCivilTime(month));
+ month += kIntMin;
+ EXPECT_EQ("-178955001-05", absl::FormatCivilTime(month));
+ month -= kIntMin;
+ EXPECT_EQ("1970-01", absl::FormatCivilTime(month));
+
+ absl::CivilYear year(0);
+ year += kIntMax;
+ EXPECT_EQ("2147483647", absl::FormatCivilTime(year));
+ year -= kIntMax;
+ EXPECT_EQ("0", absl::FormatCivilTime(year));
+ year += kIntMin;
+ EXPECT_EQ("-2147483648", absl::FormatCivilTime(year));
+ year -= kIntMin;
+ EXPECT_EQ("0", absl::FormatCivilTime(year));
+}
+
+TEST(CivilTime, Difference) {
+ absl::CivilSecond second(2015, 1, 2, 3, 4, 5);
+ EXPECT_EQ(0, second - second);
+ EXPECT_EQ(10, (second + 10) - second);
+ EXPECT_EQ(-10, (second - 10) - second);
+
+ absl::CivilMinute minute(2015, 1, 2, 3, 4);
+ EXPECT_EQ(0, minute - minute);
+ EXPECT_EQ(10, (minute + 10) - minute);
+ EXPECT_EQ(-10, (minute - 10) - minute);
+
+ absl::CivilHour hour(2015, 1, 2, 3);
+ EXPECT_EQ(0, hour - hour);
+ EXPECT_EQ(10, (hour + 10) - hour);
+ EXPECT_EQ(-10, (hour - 10) - hour);
+
+ absl::CivilDay day(2015, 1, 2);
+ EXPECT_EQ(0, day - day);
+ EXPECT_EQ(10, (day + 10) - day);
+ EXPECT_EQ(-10, (day - 10) - day);
+
+ absl::CivilMonth month(2015, 1);
+ EXPECT_EQ(0, month - month);
+ EXPECT_EQ(10, (month + 10) - month);
+ EXPECT_EQ(-10, (month - 10) - month);
+
+ absl::CivilYear year(2015);
+ EXPECT_EQ(0, year - year);
+ EXPECT_EQ(10, (year + 10) - year);
+ EXPECT_EQ(-10, (year - 10) - year);
+}
+
+TEST(CivilTime, DifferenceLimits) {
+ const absl::civil_diff_t kDiffMax =
+ std::numeric_limits<absl::civil_diff_t>::max();
+ const absl::civil_diff_t kDiffMin =
+ std::numeric_limits<absl::civil_diff_t>::min();
+
+ // Check day arithmetic at the end of the year range.
+ const absl::CivilDay max_day(kDiffMax, 12, 31);
+ EXPECT_EQ(1, max_day - (max_day - 1));
+ EXPECT_EQ(-1, (max_day - 1) - max_day);
+
+ // Check day arithmetic at the start of the year range.
+ const absl::CivilDay min_day(kDiffMin, 1, 1);
+ EXPECT_EQ(1, (min_day + 1) - min_day);
+ EXPECT_EQ(-1, min_day - (min_day + 1));
+
+ // Check the limits of the return value.
+ const absl::CivilDay d1(1970, 1, 1);
+ const absl::CivilDay d2(25252734927768524, 7, 27);
+ EXPECT_EQ(kDiffMax, d2 - d1);
+ EXPECT_EQ(kDiffMin, d1 - (d2 + 1));
+}
+
+TEST(CivilTime, Properties) {
+ absl::CivilSecond ss(2015, 2, 3, 4, 5, 6);
+ EXPECT_EQ(2015, ss.year());
+ EXPECT_EQ(2, ss.month());
+ EXPECT_EQ(3, ss.day());
+ EXPECT_EQ(4, ss.hour());
+ EXPECT_EQ(5, ss.minute());
+ EXPECT_EQ(6, ss.second());
+
+ absl::CivilMinute mm(2015, 2, 3, 4, 5, 6);
+ EXPECT_EQ(2015, mm.year());
+ EXPECT_EQ(2, mm.month());
+ EXPECT_EQ(3, mm.day());
+ EXPECT_EQ(4, mm.hour());
+ EXPECT_EQ(5, mm.minute());
+ EXPECT_EQ(0, mm.second());
+
+ absl::CivilHour hh(2015, 2, 3, 4, 5, 6);
+ EXPECT_EQ(2015, hh.year());
+ EXPECT_EQ(2, hh.month());
+ EXPECT_EQ(3, hh.day());
+ EXPECT_EQ(4, hh.hour());
+ EXPECT_EQ(0, hh.minute());
+ EXPECT_EQ(0, hh.second());
+
+ absl::CivilDay d(2015, 2, 3, 4, 5, 6);
+ EXPECT_EQ(2015, d.year());
+ EXPECT_EQ(2, d.month());
+ EXPECT_EQ(3, d.day());
+ EXPECT_EQ(0, d.hour());
+ EXPECT_EQ(0, d.minute());
+ EXPECT_EQ(0, d.second());
+
+ absl::CivilMonth m(2015, 2, 3, 4, 5, 6);
+ EXPECT_EQ(2015, m.year());
+ EXPECT_EQ(2, m.month());
+ EXPECT_EQ(1, m.day());
+ EXPECT_EQ(0, m.hour());
+ EXPECT_EQ(0, m.minute());
+ EXPECT_EQ(0, m.second());
+
+ absl::CivilYear y(2015, 2, 3, 4, 5, 6);
+ EXPECT_EQ(2015, y.year());
+ EXPECT_EQ(1, y.month());
+ EXPECT_EQ(1, y.day());
+ EXPECT_EQ(0, y.hour());
+ EXPECT_EQ(0, y.minute());
+ EXPECT_EQ(0, y.second());
+}
+
+TEST(CivilTime, Format) {
+ absl::CivilSecond ss;
+ EXPECT_EQ("1970-01-01T00:00:00", absl::FormatCivilTime(ss));
+
+ absl::CivilMinute mm;
+ EXPECT_EQ("1970-01-01T00:00", absl::FormatCivilTime(mm));
+
+ absl::CivilHour hh;
+ EXPECT_EQ("1970-01-01T00", absl::FormatCivilTime(hh));
+
+ absl::CivilDay d;
+ EXPECT_EQ("1970-01-01", absl::FormatCivilTime(d));
+
+ absl::CivilMonth m;
+ EXPECT_EQ("1970-01", absl::FormatCivilTime(m));
+
+ absl::CivilYear y;
+ EXPECT_EQ("1970", absl::FormatCivilTime(y));
+}
+
+TEST(CivilTime, FormatAndParseLenient) {
+ absl::CivilSecond ss;
+ EXPECT_EQ("1970-01-01T00:00:00", absl::FormatCivilTime(ss));
+
+ absl::CivilMinute mm;
+ EXPECT_EQ("1970-01-01T00:00", absl::FormatCivilTime(mm));
+
+ absl::CivilHour hh;
+ EXPECT_EQ("1970-01-01T00", absl::FormatCivilTime(hh));
+
+ absl::CivilDay d;
+ EXPECT_EQ("1970-01-01", absl::FormatCivilTime(d));
+
+ absl::CivilMonth m;
+ EXPECT_EQ("1970-01", absl::FormatCivilTime(m));
+
+ absl::CivilYear y;
+ EXPECT_EQ("1970", absl::FormatCivilTime(y));
+}
+
+TEST(CivilTime, OutputStream) {
+ absl::CivilSecond cs(2016, 2, 3, 4, 5, 6);
+ {
+ std::stringstream ss;
+ ss << std::left << std::setfill('.');
+ ss << std::setw(3) << 'X';
+ ss << std::setw(21) << absl::CivilYear(cs);
+ ss << std::setw(3) << 'X';
+ EXPECT_EQ("X..2016.................X..", ss.str());
+ }
+ {
+ std::stringstream ss;
+ ss << std::left << std::setfill('.');
+ ss << std::setw(3) << 'X';
+ ss << std::setw(21) << absl::CivilMonth(cs);
+ ss << std::setw(3) << 'X';
+ EXPECT_EQ("X..2016-02..............X..", ss.str());
+ }
+ {
+ std::stringstream ss;
+ ss << std::left << std::setfill('.');
+ ss << std::setw(3) << 'X';
+ ss << std::setw(21) << absl::CivilDay(cs);
+ ss << std::setw(3) << 'X';
+ EXPECT_EQ("X..2016-02-03...........X..", ss.str());
+ }
+ {
+ std::stringstream ss;
+ ss << std::left << std::setfill('.');
+ ss << std::setw(3) << 'X';
+ ss << std::setw(21) << absl::CivilHour(cs);
+ ss << std::setw(3) << 'X';
+ EXPECT_EQ("X..2016-02-03T04........X..", ss.str());
+ }
+ {
+ std::stringstream ss;
+ ss << std::left << std::setfill('.');
+ ss << std::setw(3) << 'X';
+ ss << std::setw(21) << absl::CivilMinute(cs);
+ ss << std::setw(3) << 'X';
+ EXPECT_EQ("X..2016-02-03T04:05.....X..", ss.str());
+ }
+ {
+ std::stringstream ss;
+ ss << std::left << std::setfill('.');
+ ss << std::setw(3) << 'X';
+ ss << std::setw(21) << absl::CivilSecond(cs);
+ ss << std::setw(3) << 'X';
+ EXPECT_EQ("X..2016-02-03T04:05:06..X..", ss.str());
+ }
+ {
+ std::stringstream ss;
+ ss << std::left << std::setfill('.');
+ ss << std::setw(3) << 'X';
+ ss << std::setw(21) << absl::Weekday::wednesday;
+ ss << std::setw(3) << 'X';
+ EXPECT_EQ("X..Wednesday............X..", ss.str());
+ }
+}
+
+TEST(CivilTime, Weekday) {
+ absl::CivilDay d(1970, 1, 1);
+ EXPECT_EQ(absl::Weekday::thursday, absl::GetWeekday(d)) << d;
+
+ // We used to get this wrong for years < -30.
+ d = absl::CivilDay(-31, 12, 24);
+ EXPECT_EQ(absl::Weekday::wednesday, absl::GetWeekday(d)) << d;
+}
+
+TEST(CivilTime, NextPrevWeekday) {
+ // Jan 1, 1970 was a Thursday.
+ const absl::CivilDay thursday(1970, 1, 1);
+
+ // Thursday -> Thursday
+ absl::CivilDay d = absl::NextWeekday(thursday, absl::Weekday::thursday);
+ EXPECT_EQ(7, d - thursday) << d;
+ EXPECT_EQ(d - 14, absl::PrevWeekday(thursday, absl::Weekday::thursday));
+
+ // Thursday -> Friday
+ d = absl::NextWeekday(thursday, absl::Weekday::friday);
+ EXPECT_EQ(1, d - thursday) << d;
+ EXPECT_EQ(d - 7, absl::PrevWeekday(thursday, absl::Weekday::friday));
+
+ // Thursday -> Saturday
+ d = absl::NextWeekday(thursday, absl::Weekday::saturday);
+ EXPECT_EQ(2, d - thursday) << d;
+ EXPECT_EQ(d - 7, absl::PrevWeekday(thursday, absl::Weekday::saturday));
+
+ // Thursday -> Sunday
+ d = absl::NextWeekday(thursday, absl::Weekday::sunday);
+ EXPECT_EQ(3, d - thursday) << d;
+ EXPECT_EQ(d - 7, absl::PrevWeekday(thursday, absl::Weekday::sunday));
+
+ // Thursday -> Monday
+ d = absl::NextWeekday(thursday, absl::Weekday::monday);
+ EXPECT_EQ(4, d - thursday) << d;
+ EXPECT_EQ(d - 7, absl::PrevWeekday(thursday, absl::Weekday::monday));
+
+ // Thursday -> Tuesday
+ d = absl::NextWeekday(thursday, absl::Weekday::tuesday);
+ EXPECT_EQ(5, d - thursday) << d;
+ EXPECT_EQ(d - 7, absl::PrevWeekday(thursday, absl::Weekday::tuesday));
+
+ // Thursday -> Wednesday
+ d = absl::NextWeekday(thursday, absl::Weekday::wednesday);
+ EXPECT_EQ(6, d - thursday) << d;
+ EXPECT_EQ(d - 7, absl::PrevWeekday(thursday, absl::Weekday::wednesday));
+}
+
+// NOTE: Run this with --copt=-ftrapv to detect overflow problems.
+TEST(CivilTime, DifferenceWithHugeYear) {
+ absl::CivilDay d1(9223372036854775807, 1, 1);
+ absl::CivilDay d2(9223372036854775807, 12, 31);
+ EXPECT_EQ(364, d2 - d1);
+
+ d1 = absl::CivilDay(-9223372036854775807 - 1, 1, 1);
+ d2 = absl::CivilDay(-9223372036854775807 - 1, 12, 31);
+ EXPECT_EQ(365, d2 - d1);
+
+ // Check the limits of the return value at the end of the year range.
+ d1 = absl::CivilDay(9223372036854775807, 1, 1);
+ d2 = absl::CivilDay(9198119301927009252, 6, 6);
+ EXPECT_EQ(9223372036854775807, d1 - d2);
+ d2 = d2 - 1;
+ EXPECT_EQ(-9223372036854775807 - 1, d2 - d1);
+
+ // Check the limits of the return value at the start of the year range.
+ d1 = absl::CivilDay(-9223372036854775807 - 1, 1, 1);
+ d2 = absl::CivilDay(-9198119301927009254, 7, 28);
+ EXPECT_EQ(9223372036854775807, d2 - d1);
+ d2 = d2 + 1;
+ EXPECT_EQ(-9223372036854775807 - 1, d1 - d2);
+
+ // Check the limits of the return value from either side of year 0.
+ d1 = absl::CivilDay(-12626367463883278, 9, 3);
+ d2 = absl::CivilDay(12626367463883277, 3, 28);
+ EXPECT_EQ(9223372036854775807, d2 - d1);
+ d2 = d2 + 1;
+ EXPECT_EQ(-9223372036854775807 - 1, d1 - d2);
+}
+
+// NOTE: Run this with --copt=-ftrapv to detect overflow problems.
+TEST(CivilTime, DifferenceNoIntermediateOverflow) {
+ // The difference up to the minute field would be below the minimum
+ // int64_t, but the 52 extra seconds brings us back to the minimum.
+ absl::CivilSecond s1(-292277022657, 1, 27, 8, 29 - 1, 52);
+ absl::CivilSecond s2(1970, 1, 1, 0, 0 - 1, 0);
+ EXPECT_EQ(-9223372036854775807 - 1, s1 - s2);
+
+ // The difference up to the minute field would be above the maximum
+ // int64_t, but the -53 extra seconds brings us back to the maximum.
+ s1 = absl::CivilSecond(292277026596, 12, 4, 15, 30, 7 - 7);
+ s2 = absl::CivilSecond(1970, 1, 1, 0, 0, 0 - 7);
+ EXPECT_EQ(9223372036854775807, s1 - s2);
+}
+
+TEST(CivilTime, NormalizeSimpleOverflow) {
+ absl::CivilSecond cs;
+ cs = absl::CivilSecond(2013, 11, 15, 16, 32, 59 + 1);
+ EXPECT_EQ("2013-11-15T16:33:00", absl::FormatCivilTime(cs));
+ cs = absl::CivilSecond(2013, 11, 15, 16, 59 + 1, 14);
+ EXPECT_EQ("2013-11-15T17:00:14", absl::FormatCivilTime(cs));
+ cs = absl::CivilSecond(2013, 11, 15, 23 + 1, 32, 14);
+ EXPECT_EQ("2013-11-16T00:32:14", absl::FormatCivilTime(cs));
+ cs = absl::CivilSecond(2013, 11, 30 + 1, 16, 32, 14);
+ EXPECT_EQ("2013-12-01T16:32:14", absl::FormatCivilTime(cs));
+ cs = absl::CivilSecond(2013, 12 + 1, 15, 16, 32, 14);
+ EXPECT_EQ("2014-01-15T16:32:14", absl::FormatCivilTime(cs));
+}
+
+TEST(CivilTime, NormalizeSimpleUnderflow) {
+ absl::CivilSecond cs;
+ cs = absl::CivilSecond(2013, 11, 15, 16, 32, 0 - 1);
+ EXPECT_EQ("2013-11-15T16:31:59", absl::FormatCivilTime(cs));
+ cs = absl::CivilSecond(2013, 11, 15, 16, 0 - 1, 14);
+ EXPECT_EQ("2013-11-15T15:59:14", absl::FormatCivilTime(cs));
+ cs = absl::CivilSecond(2013, 11, 15, 0 - 1, 32, 14);
+ EXPECT_EQ("2013-11-14T23:32:14", absl::FormatCivilTime(cs));
+ cs = absl::CivilSecond(2013, 11, 1 - 1, 16, 32, 14);
+ EXPECT_EQ("2013-10-31T16:32:14", absl::FormatCivilTime(cs));
+ cs = absl::CivilSecond(2013, 1 - 1, 15, 16, 32, 14);
+ EXPECT_EQ("2012-12-15T16:32:14", absl::FormatCivilTime(cs));
+}
+
+TEST(CivilTime, NormalizeMultipleOverflow) {
+ absl::CivilSecond cs(2013, 12, 31, 23, 59, 59 + 1);
+ EXPECT_EQ("2014-01-01T00:00:00", absl::FormatCivilTime(cs));
+}
+
+TEST(CivilTime, NormalizeMultipleUnderflow) {
+ absl::CivilSecond cs(2014, 1, 1, 0, 0, 0 - 1);
+ EXPECT_EQ("2013-12-31T23:59:59", absl::FormatCivilTime(cs));
+}
+
+TEST(CivilTime, NormalizeOverflowLimits) {
+ absl::CivilSecond cs;
+
+ const int kintmax = std::numeric_limits<int>::max();
+ cs = absl::CivilSecond(0, kintmax, kintmax, kintmax, kintmax, kintmax);
+ EXPECT_EQ("185085715-11-27T12:21:07", absl::FormatCivilTime(cs));
+
+ const int kintmin = std::numeric_limits<int>::min();
+ cs = absl::CivilSecond(0, kintmin, kintmin, kintmin, kintmin, kintmin);
+ EXPECT_EQ("-185085717-10-31T10:37:52", absl::FormatCivilTime(cs));
+}
+
+TEST(CivilTime, NormalizeComplexOverflow) {
+ absl::CivilSecond cs;
+ cs = absl::CivilSecond(2013, 11, 15, 16, 32, 14 + 123456789);
+ EXPECT_EQ("2017-10-14T14:05:23", absl::FormatCivilTime(cs));
+ cs = absl::CivilSecond(2013, 11, 15, 16, 32 + 1234567, 14);
+ EXPECT_EQ("2016-03-22T00:39:14", absl::FormatCivilTime(cs));
+ cs = absl::CivilSecond(2013, 11, 15, 16 + 123456, 32, 14);
+ EXPECT_EQ("2027-12-16T16:32:14", absl::FormatCivilTime(cs));
+ cs = absl::CivilSecond(2013, 11, 15 + 1234, 16, 32, 14);
+ EXPECT_EQ("2017-04-02T16:32:14", absl::FormatCivilTime(cs));
+ cs = absl::CivilSecond(2013, 11 + 123, 15, 16, 32, 14);
+ EXPECT_EQ("2024-02-15T16:32:14", absl::FormatCivilTime(cs));
+}
+
+TEST(CivilTime, NormalizeComplexUnderflow) {
+ absl::CivilSecond cs;
+ cs = absl::CivilSecond(1999, 3, 0, 0, 0, 0); // year 400
+ EXPECT_EQ("1999-02-28T00:00:00", absl::FormatCivilTime(cs));
+ cs = absl::CivilSecond(2013, 11, 15, 16, 32, 14 - 123456789);
+ EXPECT_EQ("2009-12-17T18:59:05", absl::FormatCivilTime(cs));
+ cs = absl::CivilSecond(2013, 11, 15, 16, 32 - 1234567, 14);
+ EXPECT_EQ("2011-07-12T08:25:14", absl::FormatCivilTime(cs));
+ cs = absl::CivilSecond(2013, 11, 15, 16 - 123456, 32, 14);
+ EXPECT_EQ("1999-10-16T16:32:14", absl::FormatCivilTime(cs));
+ cs = absl::CivilSecond(2013, 11, 15 - 1234, 16, 32, 14);
+ EXPECT_EQ("2010-06-30T16:32:14", absl::FormatCivilTime(cs));
+ cs = absl::CivilSecond(2013, 11 - 123, 15, 16, 32, 14);
+ EXPECT_EQ("2003-08-15T16:32:14", absl::FormatCivilTime(cs));
+}
+
+TEST(CivilTime, NormalizeMishmash) {
+ absl::CivilSecond cs;
+ cs = absl::CivilSecond(2013, 11 - 123, 15 + 1234, 16 - 123456, 32 + 1234567,
+ 14 - 123456789);
+ EXPECT_EQ("1991-05-09T03:06:05", absl::FormatCivilTime(cs));
+ cs = absl::CivilSecond(2013, 11 + 123, 15 - 1234, 16 + 123456, 32 - 1234567,
+ 14 + 123456789);
+ EXPECT_EQ("2036-05-24T05:58:23", absl::FormatCivilTime(cs));
+
+ cs = absl::CivilSecond(2013, 11, -146097 + 1, 16, 32, 14);
+ EXPECT_EQ("1613-11-01T16:32:14", absl::FormatCivilTime(cs));
+ cs = absl::CivilSecond(2013, 11 + 400 * 12, -146097 + 1, 16, 32, 14);
+ EXPECT_EQ("2013-11-01T16:32:14", absl::FormatCivilTime(cs));
+}
+
+// Convert all the days from 1970-1-1 to 1970-1-146097 (aka 2369-12-31)
+// and check that they normalize to the expected time. 146097 days span
+// the 400-year Gregorian cycle used during normalization.
+TEST(CivilTime, NormalizeAllTheDays) {
+ absl::CivilDay expected(1970, 1, 1);
+ for (int day = 1; day <= 146097; ++day) {
+ absl::CivilSecond cs(1970, 1, day, 0, 0, 0);
+ EXPECT_EQ(expected, cs);
+ ++expected;
+ }
+}
+
+TEST(CivilTime, NormalizeWithHugeYear) {
+ absl::CivilMonth c(9223372036854775807, 1);
+ EXPECT_EQ("9223372036854775807-01", absl::FormatCivilTime(c));
+ c = c - 1; // Causes normalization
+ EXPECT_EQ("9223372036854775806-12", absl::FormatCivilTime(c));
+
+ c = absl::CivilMonth(-9223372036854775807 - 1, 1);
+ EXPECT_EQ("-9223372036854775808-01", absl::FormatCivilTime(c));
+ c = c + 12; // Causes normalization
+ EXPECT_EQ("-9223372036854775807-01", absl::FormatCivilTime(c));
+}
+
+TEST(CivilTime, LeapYears) {
+ const absl::CivilSecond s1(2013, 2, 28 + 1, 0, 0, 0);
+ EXPECT_EQ("2013-03-01T00:00:00", absl::FormatCivilTime(s1));
+
+ const absl::CivilSecond s2(2012, 2, 28 + 1, 0, 0, 0);
+ EXPECT_EQ("2012-02-29T00:00:00", absl::FormatCivilTime(s2));
+
+ const absl::CivilSecond s3(1900, 2, 28 + 1, 0, 0, 0);
+ EXPECT_EQ("1900-03-01T00:00:00", absl::FormatCivilTime(s3));
+
+ const struct {
+ int year;
+ int days;
+ struct {
+ int month;
+ int day;
+ } leap_day; // The date of the day after Feb 28.
+ } kLeapYearTable[]{
+ {1900, 365, {3, 1}},
+ {1999, 365, {3, 1}},
+ {2000, 366, {2, 29}}, // leap year
+ {2001, 365, {3, 1}},
+ {2002, 365, {3, 1}},
+ {2003, 365, {3, 1}},
+ {2004, 366, {2, 29}}, // leap year
+ {2005, 365, {3, 1}},
+ {2006, 365, {3, 1}},
+ {2007, 365, {3, 1}},
+ {2008, 366, {2, 29}}, // leap year
+ {2009, 365, {3, 1}},
+ {2100, 365, {3, 1}},
+ };
+
+ for (int i = 0; i < ABSL_ARRAYSIZE(kLeapYearTable); ++i) {
+ const int y = kLeapYearTable[i].year;
+ const int m = kLeapYearTable[i].leap_day.month;
+ const int d = kLeapYearTable[i].leap_day.day;
+ const int n = kLeapYearTable[i].days;
+
+ // Tests incrementing through the leap day.
+ const absl::CivilDay feb28(y, 2, 28);
+ const absl::CivilDay next_day = feb28 + 1;
+ EXPECT_EQ(m, next_day.month());
+ EXPECT_EQ(d, next_day.day());
+
+ // Tests difference in days of leap years.
+ const absl::CivilYear year(feb28);
+ const absl::CivilYear next_year = year + 1;
+ EXPECT_EQ(n, absl::CivilDay(next_year) - absl::CivilDay(year));
+ }
+}
+
+TEST(CivilTime, FirstThursdayInMonth) {
+ const absl::CivilDay nov1(2014, 11, 1);
+ const absl::CivilDay thursday =
+ absl::PrevWeekday(nov1, absl::Weekday::thursday) + 7;
+ EXPECT_EQ("2014-11-06", absl::FormatCivilTime(thursday));
+
+ // Bonus: Date of Thanksgiving in the United States
+ // Rule: Fourth Thursday of November
+ const absl::CivilDay thanksgiving = thursday + 7 * 3;
+ EXPECT_EQ("2014-11-27", absl::FormatCivilTime(thanksgiving));
+}
+
+TEST(CivilTime, DocumentationExample) {
+ absl::CivilSecond second(2015, 6, 28, 1, 2, 3); // 2015-06-28 01:02:03
+ absl::CivilMinute minute(second); // 2015-06-28 01:02:00
+ absl::CivilDay day(minute); // 2015-06-28 00:00:00
+
+ second -= 1; // 2015-06-28 01:02:02
+ --second; // 2015-06-28 01:02:01
+ EXPECT_EQ(minute, second - 1); // Comparison between types
+ EXPECT_LT(minute, second);
+
+ // int diff = second - minute; // ERROR: Mixed types, won't compile
+
+ absl::CivilDay june_1(2015, 6, 1); // Pass fields to c'tor.
+ int diff = day - june_1; // Num days between 'day' and June 1
+ EXPECT_EQ(27, diff);
+
+ // Fields smaller than alignment are floored to their minimum value.
+ absl::CivilDay day_floor(2015, 1, 2, 9, 9, 9);
+ EXPECT_EQ(0, day_floor.hour()); // 09:09:09 is floored
+ EXPECT_EQ(absl::CivilDay(2015, 1, 2), day_floor);
+
+ // Unspecified fields default to their minium value
+ absl::CivilDay day_default(2015); // Defaults to Jan 1
+ EXPECT_EQ(absl::CivilDay(2015, 1, 1), day_default);
+
+ // Iterates all the days of June.
+ absl::CivilMonth june(day); // CivilDay -> CivilMonth
+ absl::CivilMonth july = june + 1;
+ for (absl::CivilDay day = june_1; day < july; ++day) {
+ // ...
+ }
+}
+
+} // namespace
diff --git a/absl/time/format_benchmark.cc b/absl/time/format_benchmark.cc
index ee53d71c..766f1b39 100644
--- a/absl/time/format_benchmark.cc
+++ b/absl/time/format_benchmark.cc
@@ -38,7 +38,8 @@ void BM_Format_FormatTime(benchmark::State& state) {
const absl::TimeZone lax =
absl::time_internal::LoadTimeZone("America/Los_Angeles");
const absl::Time t =
- absl::FromDateTime(1977, 6, 28, 9, 8, 7, lax) + absl::Nanoseconds(1);
+ absl::FromCivil(absl::CivilSecond(1977, 6, 28, 9, 8, 7), lax) +
+ absl::Nanoseconds(1);
while (state.KeepRunning()) {
benchmark::DoNotOptimize(absl::FormatTime(fmt, t, lax).length());
}
@@ -50,8 +51,8 @@ void BM_Format_ParseTime(benchmark::State& state) {
state.SetLabel(fmt);
const absl::TimeZone lax =
absl::time_internal::LoadTimeZone("America/Los_Angeles");
- absl::Time t =
- absl::FromDateTime(1977, 6, 28, 9, 8, 7, lax) + absl::Nanoseconds(1);
+ absl::Time t = absl::FromCivil(absl::CivilSecond(1977, 6, 28, 9, 8, 7), lax) +
+ absl::Nanoseconds(1);
const std::string when = absl::FormatTime(fmt, t, lax);
std::string err;
while (state.KeepRunning()) {
diff --git a/absl/time/format_test.cc b/absl/time/format_test.cc
index 7c84c33f..40f4c246 100644
--- a/absl/time/format_test.cc
+++ b/absl/time/format_test.cc
@@ -118,7 +118,7 @@ TEST(FormatTime, RFC1123FormatPadsYear) { // locale specific
absl::TimeZone tz = absl::UTCTimeZone();
// A year of 77 should be padded to 0077.
- absl::Time t = absl::FromDateTime(77, 6, 28, 9, 8, 7, tz);
+ absl::Time t = absl::FromCivil(absl::CivilSecond(77, 6, 28, 9, 8, 7), tz);
EXPECT_EQ("Mon, 28 Jun 0077 09:08:07 +0000",
absl::FormatTime(absl::RFC1123_full, t, tz));
EXPECT_EQ("28 Jun 0077 09:08:07 +0000",
@@ -154,9 +154,9 @@ TEST(ParseTime, Basics) {
EXPECT_TRUE(absl::ParseTime("%Y-%m-%d %H:%M:%S %z",
"2013-06-28 19:08:09 -0800", &t, &err))
<< err;
- absl::Time::Breakdown bd = t.In(absl::FixedTimeZone(-8 * 60 * 60));
- ABSL_INTERNAL_EXPECT_TIME(bd, 2013, 6, 28, 19, 8, 9, -8 * 60 * 60, false);
- EXPECT_EQ(absl::ZeroDuration(), bd.subsecond);
+ const auto ci = absl::FixedTimeZone(-8 * 60 * 60).At(t);
+ EXPECT_EQ(absl::CivilSecond(2013, 6, 28, 19, 8, 9), ci.cs);
+ EXPECT_EQ(absl::ZeroDuration(), ci.subsecond);
}
TEST(ParseTime, NullErrorString) {
@@ -177,17 +177,17 @@ TEST(ParseTime, WithTimeZone) {
EXPECT_TRUE(
absl::ParseTime("%Y-%m-%d %H:%M:%S", "2013-06-28 19:08:09", tz, &t, &e))
<< e;
- absl::Time::Breakdown bd = t.In(tz);
- ABSL_INTERNAL_EXPECT_TIME(bd, 2013, 6, 28, 19, 8, 9, -7 * 60 * 60, true);
- EXPECT_EQ(absl::ZeroDuration(), bd.subsecond);
+ auto ci = tz.At(t);
+ EXPECT_EQ(absl::CivilSecond(2013, 6, 28, 19, 8, 9), ci.cs);
+ EXPECT_EQ(absl::ZeroDuration(), ci.subsecond);
// But the timezone is ignored when a UTC offset is present.
EXPECT_TRUE(absl::ParseTime("%Y-%m-%d %H:%M:%S %z",
"2013-06-28 19:08:09 +0800", tz, &t, &e))
<< e;
- bd = t.In(absl::FixedTimeZone(8 * 60 * 60));
- ABSL_INTERNAL_EXPECT_TIME(bd, 2013, 6, 28, 19, 8, 9, 8 * 60 * 60, false);
- EXPECT_EQ(absl::ZeroDuration(), bd.subsecond);
+ ci = absl::FixedTimeZone(8 * 60 * 60).At(t);
+ EXPECT_EQ(absl::CivilSecond(2013, 6, 28, 19, 8, 9), ci.cs);
+ EXPECT_EQ(absl::ZeroDuration(), ci.subsecond);
}
TEST(ParseTime, ErrorCases) {
@@ -332,15 +332,15 @@ TEST(ParseTime, InfiniteTime) {
EXPECT_TRUE(absl::ParseTime("infinite-future %H:%M", "infinite-future 03:04",
&t, &err));
EXPECT_NE(absl::InfiniteFuture(), t);
- EXPECT_EQ(3, t.In(tz).hour);
- EXPECT_EQ(4, t.In(tz).minute);
+ EXPECT_EQ(3, tz.At(t).cs.hour());
+ EXPECT_EQ(4, tz.At(t).cs.minute());
// "infinite-past" as literal std::string
EXPECT_TRUE(
absl::ParseTime("infinite-past %H:%M", "infinite-past 03:04", &t, &err));
EXPECT_NE(absl::InfinitePast(), t);
- EXPECT_EQ(3, t.In(tz).hour);
- EXPECT_EQ(4, t.In(tz).minute);
+ EXPECT_EQ(3, tz.At(t).cs.hour());
+ EXPECT_EQ(4, tz.At(t).cs.minute());
// The input doesn't match the format.
EXPECT_FALSE(absl::ParseTime("infinite-future %H:%M", "03:04", &t, &err));
@@ -365,16 +365,17 @@ TEST(ParseTime, FailsOnUnrepresentableTime) {
//
TEST(FormatParse, RoundTrip) {
- const absl::TimeZone gst =
+ const absl::TimeZone lax =
absl::time_internal::LoadTimeZone("America/Los_Angeles");
- const absl::Time in = absl::FromDateTime(1977, 6, 28, 9, 8, 7, gst);
+ const absl::Time in =
+ absl::FromCivil(absl::CivilSecond(1977, 6, 28, 9, 8, 7), lax);
const absl::Duration subseconds = absl::Nanoseconds(654321);
std::string err;
// RFC3339, which renders subseconds.
{
absl::Time out;
- const std::string s = absl::FormatTime(absl::RFC3339_full, in + subseconds, gst);
+ const std::string s = absl::FormatTime(absl::RFC3339_full, in + subseconds, lax);
EXPECT_TRUE(absl::ParseTime(absl::RFC3339_full, s, &out, &err))
<< s << ": " << err;
EXPECT_EQ(in + subseconds, out); // RFC3339_full includes %Ez
@@ -383,7 +384,7 @@ TEST(FormatParse, RoundTrip) {
// RFC1123, which only does whole seconds.
{
absl::Time out;
- const std::string s = absl::FormatTime(absl::RFC1123_full, in, gst);
+ const std::string s = absl::FormatTime(absl::RFC1123_full, in, lax);
EXPECT_TRUE(absl::ParseTime(absl::RFC1123_full, s, &out, &err))
<< s << ": " << err;
EXPECT_EQ(in, out); // RFC1123_full includes %z
diff --git a/absl/time/internal/cctz/src/time_zone_posix.h b/absl/time/internal/cctz/src/time_zone_posix.h
index 9ccd4a8b..ef2a8c16 100644
--- a/absl/time/internal/cctz/src/time_zone_posix.h
+++ b/absl/time/internal/cctz/src/time_zone_posix.h
@@ -68,25 +68,35 @@ namespace cctz {
// it would take us to another day, and perhaps week, or even month.
struct PosixTransition {
enum DateFormat { J, N, M };
- struct {
+
+ struct Date {
+ struct NonLeapDay {
+ std::int_fast16_t day; // day of non-leap year [1:365]
+ };
+ struct Day {
+ std::int_fast16_t day; // day of year [0:365]
+ };
+ struct MonthWeekWeekday {
+ std::int_fast8_t month; // month of year [1:12]
+ std::int_fast8_t week; // week of month [1:5] (5==last)
+ std::int_fast8_t weekday; // 0==Sun, ..., 6=Sat
+ };
+
DateFormat fmt;
+
union {
- struct {
- std::int_fast16_t day; // day of non-leap year [1:365]
- } j;
- struct {
- std::int_fast16_t day; // day of year [0:365]
- } n;
- struct {
- std::int_fast8_t month; // month of year [1:12]
- std::int_fast8_t week; // week of month [1:5] (5==last)
- std::int_fast8_t weekday; // 0==Sun, ..., 6=Sat
- } m;
+ NonLeapDay j;
+ Day n;
+ MonthWeekWeekday m;
};
- } date;
- struct {
+ };
+
+ struct Time {
std::int_fast32_t offset; // seconds before/after 00:00:00
- } time;
+ };
+
+ Date date;
+ Time time;
};
// The entirety of a POSIX-string specified time-zone rule. The standard
diff --git a/absl/time/internal/test_util.cc b/absl/time/internal/test_util.cc
index bbbef7da..4483f2a9 100644
--- a/absl/time/internal/test_util.cc
+++ b/absl/time/internal/test_util.cc
@@ -26,12 +26,6 @@ namespace cctz = absl::time_internal::cctz;
namespace absl {
namespace time_internal {
-#if GTEST_USES_SIMPLE_RE
-extern const char kZoneAbbrRE[] = ".*"; // just punt
-#else
-extern const char kZoneAbbrRE[] = "[A-Za-z]{3,4}|[-+][0-9]{2}([0-9]{2})?";
-#endif
-
TimeZone LoadTimeZone(const std::string& name) {
TimeZone tz;
ABSL_RAW_CHECK(LoadTimeZone(name, &tz), name.c_str());
diff --git a/absl/time/internal/test_util.h b/absl/time/internal/test_util.h
index 8fd5fb9f..d9940293 100644
--- a/absl/time/internal/test_util.h
+++ b/absl/time/internal/test_util.h
@@ -17,35 +17,11 @@
#include <string>
-#include "gmock/gmock.h"
-#include "gtest/gtest.h"
#include "absl/time/time.h"
-// This helper is a macro so that failed expectations show up with the
-// correct line numbers.
-//
-// This is for internal testing of the Base Time library itself. This is not
-// part of a public API.
-#define ABSL_INTERNAL_EXPECT_TIME(bd, y, m, d, h, min, s, off, isdst) \
- do { \
- EXPECT_EQ(y, bd.year); \
- EXPECT_EQ(m, bd.month); \
- EXPECT_EQ(d, bd.day); \
- EXPECT_EQ(h, bd.hour); \
- EXPECT_EQ(min, bd.minute); \
- EXPECT_EQ(s, bd.second); \
- EXPECT_EQ(off, bd.offset); \
- EXPECT_EQ(isdst, bd.is_dst); \
- EXPECT_THAT(bd.zone_abbr, \
- testing::MatchesRegex(absl::time_internal::kZoneAbbrRE)); \
- } while (0)
-
namespace absl {
namespace time_internal {
-// A regular expression that matches all zone abbreviations (%Z).
-extern const char kZoneAbbrRE[];
-
// Loads the named timezone, but dies on any failure.
absl::TimeZone LoadTimeZone(const std::string& name);
diff --git a/absl/time/time.cc b/absl/time/time.cc
index 71fd8ee6..0703856f 100644
--- a/absl/time/time.cc
+++ b/absl/time/time.cc
@@ -22,13 +22,14 @@
// NOTE: To keep type verbosity to a minimum, the following variable naming
// conventions are used throughout this file.
//
-// cz: A cctz::time_zone
// tz: An absl::TimeZone
+// ci: An absl::TimeZone::CivilInfo
+// ti: An absl::TimeZone::TimeInfo
+// cd: An absl::CivilDay or a cctz::civil_day
+// cs: An absl::CivilSecond or a cctz::civil_second
+// bd: An absl::Time::Breakdown
// cl: A cctz::time_zone::civil_lookup
// al: A cctz::time_zone::absolute_lookup
-// cd: A cctz::civil_day
-// cs: A cctz::civil_second
-// bd: An absl::Time::Breakdown
#include "absl/time/time.h"
@@ -75,7 +76,7 @@ inline absl::Time::Breakdown InfiniteFutureBreakdown() {
return bd;
}
-inline Time::Breakdown InfinitePastBreakdown() {
+inline absl::Time::Breakdown InfinitePastBreakdown() {
Time::Breakdown bd;
bd.year = std::numeric_limits<int64_t>::min();
bd.month = 1;
@@ -92,6 +93,26 @@ inline Time::Breakdown InfinitePastBreakdown() {
return bd;
}
+inline absl::TimeZone::CivilInfo InfiniteFutureCivilInfo() {
+ TimeZone::CivilInfo ci;
+ ci.cs = CivilSecond::max();
+ ci.subsecond = InfiniteDuration();
+ ci.offset = 0;
+ ci.is_dst = false;
+ ci.zone_abbr = "-00";
+ return ci;
+}
+
+inline absl::TimeZone::CivilInfo InfinitePastCivilInfo() {
+ TimeZone::CivilInfo ci;
+ ci.cs = CivilSecond::min();
+ ci.subsecond = -InfiniteDuration();
+ ci.offset = 0;
+ ci.is_dst = false;
+ ci.zone_abbr = "-00";
+ return ci;
+}
+
inline absl::TimeConversion InfiniteFutureTimeConversion() {
absl::TimeConversion tc;
tc.pre = tc.trans = tc.post = absl::InfiniteFuture();
@@ -134,19 +155,6 @@ Time MakeTimeWithOverflow(const cctz::time_point<cctz::seconds>& sec,
return time_internal::FromUnixDuration(time_internal::MakeDuration(hi));
}
-inline absl::TimeConversion::Kind MapKind(
- const cctz::time_zone::civil_lookup::civil_kind& kind) {
- switch (kind) {
- case cctz::time_zone::civil_lookup::UNIQUE:
- return absl::TimeConversion::UNIQUE;
- case cctz::time_zone::civil_lookup::SKIPPED:
- return absl::TimeConversion::SKIPPED;
- case cctz::time_zone::civil_lookup::REPEATED:
- return absl::TimeConversion::REPEATED;
- }
- return absl::TimeConversion::UNIQUE;
-}
-
// Returns Mon=1..Sun=7.
inline int MapWeekday(const cctz::weekday& wd) {
switch (wd) {
@@ -170,9 +178,13 @@ inline int MapWeekday(const cctz::weekday& wd) {
} // namespace
+//
+// Time
+//
+
absl::Time::Breakdown Time::In(absl::TimeZone tz) const {
- if (*this == absl::InfiniteFuture()) return absl::InfiniteFutureBreakdown();
- if (*this == absl::InfinitePast()) return absl::InfinitePastBreakdown();
+ if (*this == absl::InfiniteFuture()) return InfiniteFutureBreakdown();
+ if (*this == absl::InfinitePast()) return InfinitePastBreakdown();
const auto tp = unix_epoch() + cctz::seconds(time_internal::GetRepHi(rep_));
const auto al = cctz::time_zone(tz).lookup(tp);
@@ -187,92 +199,18 @@ absl::Time::Breakdown Time::In(absl::TimeZone tz) const {
bd.minute = cs.minute();
bd.second = cs.second();
bd.subsecond = time_internal::MakeDuration(0, time_internal::GetRepLo(rep_));
- bd.weekday = MapWeekday(get_weekday(cd));
- bd.yearday = get_yearday(cd);
+ bd.weekday = MapWeekday(cctz::get_weekday(cd));
+ bd.yearday = cctz::get_yearday(cd);
bd.offset = al.offset;
bd.is_dst = al.is_dst;
bd.zone_abbr = al.abbr;
return bd;
}
-absl::Time FromTM(const struct tm& tm, absl::TimeZone tz) {
- const auto cz = cctz::time_zone(tz);
- const auto cs =
- cctz::civil_second(tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
- tm.tm_hour, tm.tm_min, tm.tm_sec);
- const auto cl = cz.lookup(cs);
- const auto tp = tm.tm_isdst == 0 ? cl.post : cl.pre;
- return MakeTimeWithOverflow(tp, cs, cz);
-}
-
-struct tm ToTM(absl::Time t, absl::TimeZone tz) {
- const absl::Time::Breakdown bd = t.In(tz);
- struct tm tm;
- std::memset(&tm, 0, sizeof(tm));
- tm.tm_sec = bd.second;
- tm.tm_min = bd.minute;
- tm.tm_hour = bd.hour;
- tm.tm_mday = bd.day;
- tm.tm_mon = bd.month - 1;
-
- // Saturates tm.tm_year in cases of over/underflow, accounting for the fact
- // that tm.tm_year is years since 1900.
- if (bd.year < std::numeric_limits<int>::min() + 1900) {
- tm.tm_year = std::numeric_limits<int>::min();
- } else if (bd.year > std::numeric_limits<int>::max()) {
- tm.tm_year = std::numeric_limits<int>::max() - 1900;
- } else {
- tm.tm_year = static_cast<int>(bd.year - 1900);
- }
-
- tm.tm_wday = bd.weekday % 7;
- tm.tm_yday = bd.yearday - 1;
- tm.tm_isdst = bd.is_dst ? 1 : 0;
-
- return tm;
-}
-
//
-// Factory functions.
+// Conversions from/to other time types.
//
-absl::TimeConversion ConvertDateTime(int64_t year, int mon, int day, int hour,
- int min, int sec, TimeZone tz) {
- // Avoids years that are too extreme for civil_second to normalize.
- if (year > 300000000000) return InfiniteFutureTimeConversion();
- if (year < -300000000000) return InfinitePastTimeConversion();
- const auto cz = cctz::time_zone(tz);
- const auto cs = cctz::civil_second(year, mon, day, hour, min, sec);
- absl::TimeConversion tc;
- tc.normalized = year != cs.year() || mon != cs.month() || day != cs.day() ||
- hour != cs.hour() || min != cs.minute() || sec != cs.second();
- const auto cl = cz.lookup(cs);
- // Converts the civil_lookup struct to a TimeConversion.
- tc.pre = MakeTimeWithOverflow(cl.pre, cs, cz, &tc.normalized);
- tc.trans = MakeTimeWithOverflow(cl.trans, cs, cz, &tc.normalized);
- tc.post = MakeTimeWithOverflow(cl.post, cs, cz, &tc.normalized);
- tc.kind = MapKind(cl.kind);
- return tc;
-}
-
-absl::Time FromDateTime(int64_t year, int mon, int day, int hour, int min,
- int sec, TimeZone tz) {
- if (year > 300000000000) return InfiniteFuture();
- if (year < -300000000000) return InfinitePast();
- const auto cz = cctz::time_zone(tz);
- const auto cs = cctz::civil_second(year, mon, day, hour, min, sec);
- const auto cl = cz.lookup(cs);
- return MakeTimeWithOverflow(cl.pre, cs, cz);
-}
-
-absl::Time TimeFromTimespec(timespec ts) {
- return time_internal::FromUnixDuration(absl::DurationFromTimespec(ts));
-}
-
-absl::Time TimeFromTimeval(timeval tv) {
- return time_internal::FromUnixDuration(absl::DurationFromTimeval(tv));
-}
-
absl::Time FromUDate(double udate) {
return time_internal::FromUnixDuration(absl::Milliseconds(udate));
}
@@ -281,10 +219,6 @@ absl::Time FromUniversal(int64_t universal) {
return absl::UniversalEpoch() + 100 * absl::Nanoseconds(universal);
}
-//
-// Conversion to other time types.
-//
-
int64_t ToUnixNanos(Time t) {
if (time_internal::GetRepHi(time_internal::ToUnixDuration(t)) >= 0 &&
time_internal::GetRepHi(time_internal::ToUnixDuration(t)) >> 33 == 0) {
@@ -321,6 +255,23 @@ int64_t ToUnixSeconds(Time t) {
time_t ToTimeT(Time t) { return absl::ToTimespec(t).tv_sec; }
+double ToUDate(Time t) {
+ return absl::FDivDuration(time_internal::ToUnixDuration(t),
+ absl::Milliseconds(1));
+}
+
+int64_t ToUniversal(absl::Time t) {
+ return absl::FloorToUnit(t - absl::UniversalEpoch(), absl::Nanoseconds(100));
+}
+
+absl::Time TimeFromTimespec(timespec ts) {
+ return time_internal::FromUnixDuration(absl::DurationFromTimespec(ts));
+}
+
+absl::Time TimeFromTimeval(timeval tv) {
+ return time_internal::FromUnixDuration(absl::DurationFromTimeval(tv));
+}
+
timespec ToTimespec(Time t) {
timespec ts;
absl::Duration d = time_internal::ToUnixDuration(t);
@@ -359,15 +310,6 @@ timeval ToTimeval(Time t) {
return tv;
}
-double ToUDate(Time t) {
- return absl::FDivDuration(time_internal::ToUnixDuration(t),
- absl::Milliseconds(1));
-}
-
-int64_t ToUniversal(absl::Time t) {
- return absl::FloorToUnit(t - absl::UniversalEpoch(), absl::Nanoseconds(100));
-}
-
Time FromChrono(const std::chrono::system_clock::time_point& tp) {
return time_internal::FromUnixDuration(time_internal::FromChrono(
tp - std::chrono::system_clock::from_time_t(0)));
@@ -381,4 +323,141 @@ std::chrono::system_clock::time_point ToChronoTime(absl::Time t) {
time_internal::ToChronoDuration<D>(d);
}
+//
+// TimeZone
+//
+
+absl::TimeZone::CivilInfo TimeZone::At(Time t) const {
+ if (t == absl::InfiniteFuture()) return InfiniteFutureCivilInfo();
+ if (t == absl::InfinitePast()) return InfinitePastCivilInfo();
+
+ const auto ud = time_internal::ToUnixDuration(t);
+ const auto tp = unix_epoch() + cctz::seconds(time_internal::GetRepHi(ud));
+ const auto al = cz_.lookup(tp);
+
+ TimeZone::CivilInfo ci;
+ ci.cs = CivilSecond(al.cs);
+ ci.subsecond = time_internal::MakeDuration(0, time_internal::GetRepLo(ud));
+ ci.offset = al.offset;
+ ci.is_dst = al.is_dst;
+ ci.zone_abbr = al.abbr;
+ return ci;
+}
+
+absl::TimeZone::TimeInfo TimeZone::At(CivilSecond ct) const {
+ const cctz::civil_second cs(ct);
+ const auto cl = cz_.lookup(cs);
+
+ TimeZone::TimeInfo ti;
+ switch (cl.kind) {
+ case cctz::time_zone::civil_lookup::UNIQUE:
+ ti.kind = TimeZone::TimeInfo::UNIQUE;
+ break;
+ case cctz::time_zone::civil_lookup::SKIPPED:
+ ti.kind = TimeZone::TimeInfo::SKIPPED;
+ break;
+ case cctz::time_zone::civil_lookup::REPEATED:
+ ti.kind = TimeZone::TimeInfo::REPEATED;
+ break;
+ }
+ ti.pre = MakeTimeWithOverflow(cl.pre, cs, cz_);
+ ti.trans = MakeTimeWithOverflow(cl.trans, cs, cz_);
+ ti.post = MakeTimeWithOverflow(cl.post, cs, cz_);
+ return ti;
+}
+
+//
+// Conversions involving time zones.
+//
+
+absl::TimeConversion ConvertDateTime(int64_t year, int mon, int day, int hour,
+ int min, int sec, TimeZone tz) {
+ // Avoids years that are too extreme for CivilSecond to normalize.
+ if (year > 300000000000) return InfiniteFutureTimeConversion();
+ if (year < -300000000000) return InfinitePastTimeConversion();
+
+ const CivilSecond cs(year, mon, day, hour, min, sec);
+ const auto ti = tz.At(cs);
+
+ TimeConversion tc;
+ tc.pre = ti.pre;
+ tc.trans = ti.trans;
+ tc.post = ti.post;
+ switch (ti.kind) {
+ case TimeZone::TimeInfo::UNIQUE:
+ tc.kind = TimeConversion::UNIQUE;
+ break;
+ case TimeZone::TimeInfo::SKIPPED:
+ tc.kind = TimeConversion::SKIPPED;
+ break;
+ case TimeZone::TimeInfo::REPEATED:
+ tc.kind = TimeConversion::REPEATED;
+ break;
+ }
+ tc.normalized = false;
+ if (year != cs.year() || mon != cs.month() || day != cs.day() ||
+ hour != cs.hour() || min != cs.minute() || sec != cs.second()) {
+ tc.normalized = true;
+ }
+ return tc;
+}
+
+absl::Time FromTM(const struct tm& tm, absl::TimeZone tz) {
+ const CivilSecond cs(tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
+ tm.tm_hour, tm.tm_min, tm.tm_sec);
+ const auto ti = tz.At(cs);
+ return tm.tm_isdst == 0 ? ti.post : ti.pre;
+}
+
+struct tm ToTM(absl::Time t, absl::TimeZone tz) {
+ struct tm tm = {};
+
+ const auto ci = tz.At(t);
+ const auto& cs = ci.cs;
+ tm.tm_sec = cs.second();
+ tm.tm_min = cs.minute();
+ tm.tm_hour = cs.hour();
+ tm.tm_mday = cs.day();
+ tm.tm_mon = cs.month() - 1;
+
+ // Saturates tm.tm_year in cases of over/underflow, accounting for the fact
+ // that tm.tm_year is years since 1900.
+ if (cs.year() < std::numeric_limits<int>::min() + 1900) {
+ tm.tm_year = std::numeric_limits<int>::min();
+ } else if (cs.year() > std::numeric_limits<int>::max()) {
+ tm.tm_year = std::numeric_limits<int>::max() - 1900;
+ } else {
+ tm.tm_year = static_cast<int>(cs.year() - 1900);
+ }
+
+ const CivilDay cd(cs);
+ switch (GetWeekday(cd)) {
+ case Weekday::sunday:
+ tm.tm_wday = 0;
+ break;
+ case Weekday::monday:
+ tm.tm_wday = 1;
+ break;
+ case Weekday::tuesday:
+ tm.tm_wday = 2;
+ break;
+ case Weekday::wednesday:
+ tm.tm_wday = 3;
+ break;
+ case Weekday::thursday:
+ tm.tm_wday = 4;
+ break;
+ case Weekday::friday:
+ tm.tm_wday = 5;
+ break;
+ case Weekday::saturday:
+ tm.tm_wday = 6;
+ break;
+ }
+ tm.tm_yday = GetYearDay(cd) - 1;
+ tm.tm_isdst = ci.is_dst ? 1 : 0;
+
+ return tm;
+}
+
} // namespace absl
diff --git a/absl/time/time.h b/absl/time/time.h
index 50bf971d..2858da29 100644
--- a/absl/time/time.h
+++ b/absl/time/time.h
@@ -25,18 +25,29 @@
// * `absl::TimeZone` defines geopolitical time zone regions (as collected
// within the IANA Time Zone database (https://www.iana.org/time-zones)).
//
+// Note: Absolute times are distinct from civil times, which refer to the
+// human-scale time commonly represented by `YYYY-MM-DD hh:mm:ss`. The mapping
+// between absolute and civil times can be specified by use of time zones
+// (`absl::TimeZone` within this API). That is:
+//
+// Civil Time = F(Absolute Time, Time Zone)
+// Absolute Time = G(Civil Time, Time Zone)
+//
+// See civil_time.h for abstractions related to constructing and manipulating
+// civil time.
//
// Example:
//
// absl::TimeZone nyc;
-//
// // LoadTimeZone() may fail so it's always better to check for success.
// if (!absl::LoadTimeZone("America/New_York", &nyc)) {
// // handle error case
// }
//
// // My flight leaves NYC on Jan 2, 2017 at 03:04:05
-// absl::Time takeoff = absl::FromDateTime(2017, 1, 2, 3, 4, 5, nyc);
+// absl::CivilSecond cs(2017, 1, 2, 3, 4, 5);
+// absl::Time takeoff = absl::FromCivil(cs, nyc);
+//
// absl::Duration flight_duration = absl::Hours(21) + absl::Minutes(35);
// absl::Time landing = takeoff + flight_duration;
//
@@ -48,6 +59,7 @@
// "My flight will land in Sydney on %Y-%m-%d at %H:%M:%S",
// landing, syd);
//
+
#ifndef ABSL_TIME_TIME_H_
#define ABSL_TIME_TIME_H_
@@ -66,6 +78,7 @@
#include "absl/base/port.h" // Needed for string vs std::string
#include "absl/strings/string_view.h"
+#include "absl/time/civil_time.h"
#include "absl/time/internal/cctz/include/cctz/time_zone.h"
namespace absl {
@@ -348,11 +361,11 @@ constexpr Duration InfiniteDuration();
// Factory functions for constructing `Duration` values from an integral number
// of the unit indicated by the factory function's name.
//
-// Note: no "Days()" factory function exists because "a day" is ambiguous. Civil
-// days are not always 24 hours long, and a 24-hour duration often does not
-// correspond with a civil day. If a 24-hour duration is needed, use
-// `absl::Hours(24)`.
-//
+// Note: no "Days()" factory function exists because "a day" is ambiguous.
+// Civil days are not always 24 hours long, and a 24-hour duration often does
+// not correspond with a civil day. If a 24-hour duration is needed, use
+// `absl::Hours(24)`. (If you actually want a civil day, use absl::CivilDay
+// from civil_time.h.)
//
// Example:
//
@@ -371,6 +384,7 @@ constexpr Duration Hours(int64_t n);
// factories, which should be preferred.
//
// Example:
+//
// auto a = absl::Seconds(1.5); // OK
// auto b = absl::Milliseconds(1500); // BETTER
template <typename T, time_internal::EnableIfFloat<T> = 0>
@@ -546,7 +560,7 @@ std::string UnparseFlag(Duration d);
//
// `absl::Time` uses a resolution that is high enough to avoid loss in
// precision, and a range that is wide enough to avoid overflow, when
-// converting between tick counts in most Google time scales (i.e., precision
+// converting between tick counts in most Google time scales (i.e., resolution
// of at least one nanosecond, and range +/-100 billion years). Conversions
// between the time scales are performed by truncating (towards negative
// infinity) to the nearest representable point.
@@ -556,7 +570,6 @@ std::string UnparseFlag(Duration d);
// absl::Time t1 = ...;
// absl::Time t2 = t1 + absl::Minutes(2);
// absl::Duration d = t2 - t1; // == absl::Minutes(2)
-// absl::Time::Breakdown bd = t1.In(absl::LocalTimeZone());
//
class Time {
public:
@@ -590,7 +603,10 @@ class Time {
// intended to represent an instant in time. So, rather than passing
// a `Time::Breakdown` to a function, pass an `absl::Time` and an
// `absl::TimeZone`.
- struct Breakdown {
+ //
+ // Deprecated. Use `absl::TimeZone::CivilInfo`.
+ struct
+ Breakdown {
int64_t year; // year (e.g., 2013)
int month; // month of year [1:12]
int day; // day of month [1:31]
@@ -614,6 +630,8 @@ class Time {
// Time::In()
//
// Returns the breakdown of this instant in the given TimeZone.
+ //
+ // Deprecated. Use `absl::TimeZone::At(Time)`.
Breakdown In(TimeZone tz) const;
template <typename H>
@@ -679,126 +697,6 @@ constexpr Time InfinitePast() {
time_internal::MakeDuration(std::numeric_limits<int64_t>::min(), ~0U));
}
-// TimeConversion
-//
-// An `absl::TimeConversion` represents the conversion of year, month, day,
-// hour, minute, and second values (i.e., a civil time), in a particular
-// `absl::TimeZone`, to a time instant (an absolute time), as returned by
-// `absl::ConvertDateTime()`. (Subseconds must be handled separately.)
-//
-// It is possible, though, for a caller to try to convert values that
-// do not represent an actual or unique instant in time (due to a shift
-// in UTC offset in the `absl::TimeZone`, which results in a discontinuity in
-// the civil-time components). For example, a daylight-saving-time
-// transition skips or repeats civil times---in the United States, March
-// 13, 2011 02:15 never occurred, while November 6, 2011 01:15 occurred
-// twice---so requests for such times are not well-defined.
-//
-// To account for these possibilities, `absl::TimeConversion` is richer
-// than just a single `absl::Time`. When the civil time is skipped or
-// repeated, `absl::ConvertDateTime()` returns times calculated using the
-// pre-transition and post-transition UTC offsets, plus the transition
-// time itself.
-//
-// Examples:
-//
-// absl::TimeZone lax;
-// if (!absl::LoadTimeZone("America/Los_Angeles", &lax)) {
-// // handle error case
-// }
-//
-// // A unique civil time
-// absl::TimeConversion jan01 =
-// absl::ConvertDateTime(2011, 1, 1, 0, 0, 0, lax);
-// // jan01.kind == TimeConversion::UNIQUE
-// // jan01.pre is 2011/01/01 00:00:00 -0800
-// // jan01.trans is 2011/01/01 00:00:00 -0800
-// // jan01.post is 2011/01/01 00:00:00 -0800
-//
-// // A Spring DST transition, when there is a gap in civil time
-// absl::TimeConversion mar13 =
-// absl::ConvertDateTime(2011, 3, 13, 2, 15, 0, lax);
-// // mar13.kind == TimeConversion::SKIPPED
-// // mar13.pre is 2011/03/13 03:15:00 -0700
-// // mar13.trans is 2011/03/13 03:00:00 -0700
-// // mar13.post is 2011/03/13 01:15:00 -0800
-//
-// // A Fall DST transition, when civil times are repeated
-// absl::TimeConversion nov06 =
-// absl::ConvertDateTime(2011, 11, 6, 1, 15, 0, lax);
-// // nov06.kind == TimeConversion::REPEATED
-// // nov06.pre is 2011/11/06 01:15:00 -0700
-// // nov06.trans is 2011/11/06 01:00:00 -0800
-// // nov06.post is 2011/11/06 01:15:00 -0800
-//
-// The input month, day, hour, minute, and second values can also be
-// outside of their valid ranges, in which case they will be "normalized"
-// during the conversion.
-//
-// Example:
-//
-// // "October 32" normalizes to "November 1".
-// absl::TimeZone tz = absl::LocalTimeZone();
-// absl::TimeConversion tc =
-// absl::ConvertDateTime(2013, 10, 32, 8, 30, 0, tz);
-// // tc.kind == TimeConversion::UNIQUE && tc.normalized == true
-// // tc.pre.In(tz).month == 11 && tc.pre.In(tz).day == 1
-struct TimeConversion {
- Time pre; // time calculated using the pre-transition offset
- Time trans; // when the civil-time discontinuity occurred
- Time post; // time calculated using the post-transition offset
-
- enum Kind {
- UNIQUE, // the civil time was singular (pre == trans == post)
- SKIPPED, // the civil time did not exist
- REPEATED, // the civil time was ambiguous
- };
- Kind kind;
-
- bool normalized; // input values were outside their valid ranges
-};
-
-// ConvertDateTime()
-//
-// The full generality of a civil time to absl::Time conversion.
-TimeConversion ConvertDateTime(int64_t year, int mon, int day, int hour,
- int min, int sec, TimeZone tz);
-
-// FromDateTime()
-//
-// A convenience wrapper for `absl::ConvertDateTime()` that simply returns the
-// "pre" `absl::Time`. That is, the unique result, or the instant that
-// is correct using the pre-transition offset (as if the transition
-// never happened). This is typically the answer that humans expected when
-// faced with non-unique times, such as near daylight-saving time transitions.
-//
-// Example:
-//
-// absl::TimeZone seattle;
-// if (!absl::LoadTimeZone("America/Los_Angeles", &seattle)) {
-// // handle error case
-// }
-// absl::Time t = absl::FromDateTime(2017, 9, 26, 9, 30, 0, seattle);
-Time FromDateTime(int64_t year, int mon, int day, int hour, int min, int sec,
- TimeZone tz);
-
-// FromTM()
-//
-// Converts the `tm_year`, `tm_mon`, `tm_mday`, `tm_hour`, `tm_min`, and
-// `tm_sec` fields to an `absl::Time` using the given time zone. See ctime(3)
-// for a description of the expected values of the tm fields. IFF the indicated
-// time instant is not unique (see `absl::ConvertDateTime()` above), the
-// `tm_isdst` field is consulted to select the desired instant (`tm_isdst` > 0
-// means DST, `tm_isdst` == 0 means no DST, `tm_isdst` < 0 means use the default
-// like `absl::FromDateTime()`).
-Time FromTM(const struct tm& tm, TimeZone tz);
-
-// ToTM()
-//
-// Converts the given `absl::Time` to a struct tm using the given time zone.
-// See ctime(3) for a description of the values of the tm fields.
-struct tm ToTM(Time t, TimeZone tz);
-
// FromUnixNanos()
// FromUnixMicros()
// FromUnixMillis()
@@ -883,6 +781,340 @@ Time FromChrono(const std::chrono::system_clock::time_point& tp);
// // tp == std::chrono::system_clock::from_time_t(123);
std::chrono::system_clock::time_point ToChronoTime(Time);
+// Support for flag values of type Time. Time flags must be specified in a
+// format that matches absl::RFC3339_full. For example:
+//
+// --start_time=2016-01-02T03:04:05.678+08:00
+//
+// Note: A UTC offset (or 'Z' indicating a zero-offset from UTC) is required.
+//
+// Additionally, if you'd like to specify a time as a count of
+// seconds/milliseconds/etc from the Unix epoch, use an absl::Duration flag
+// and add that duration to absl::UnixEpoch() to get an absl::Time.
+bool ParseFlag(const std::string& text, Time* t, std::string* error);
+std::string UnparseFlag(Time t);
+
+// TimeZone
+//
+// The `absl::TimeZone` is an opaque, small, value-type class representing a
+// geo-political region within which particular rules are used for converting
+// between absolute and civil times (see https://git.io/v59Ly). `absl::TimeZone`
+// values are named using the TZ identifiers from the IANA Time Zone Database,
+// such as "America/Los_Angeles" or "Australia/Sydney". `absl::TimeZone` values
+// are created from factory functions such as `absl::LoadTimeZone()`. Note:
+// strings like "PST" and "EDT" are not valid TZ identifiers. Prefer to pass by
+// value rather than const reference.
+//
+// For more on the fundamental concepts of time zones, absolute times, and civil
+// times, see https://github.com/google/cctz#fundamental-concepts
+//
+// Examples:
+//
+// absl::TimeZone utc = absl::UTCTimeZone();
+// absl::TimeZone pst = absl::FixedTimeZone(-8 * 60 * 60);
+// absl::TimeZone loc = absl::LocalTimeZone();
+// absl::TimeZone lax;
+// if (!absl::LoadTimeZone("America/Los_Angeles", &lax)) {
+// // handle error case
+// }
+//
+// See also:
+// - https://github.com/google/cctz
+// - http://www.iana.org/time-zones
+// - http://en.wikipedia.org/wiki/Zoneinfo
+class TimeZone {
+ public:
+ explicit TimeZone(time_internal::cctz::time_zone tz) : cz_(tz) {}
+ TimeZone() = default; // UTC, but prefer UTCTimeZone() to be explicit.
+ TimeZone(const TimeZone&) = default;
+ TimeZone& operator=(const TimeZone&) = default;
+
+ explicit operator time_internal::cctz::time_zone() const { return cz_; }
+
+ std::string name() const { return cz_.name(); }
+
+ // TimeZone::CivilInfo
+ //
+ // Information about the civil time corresponding to an absolute time.
+ // This struct is not intended to represent an instant in time. So, rather
+ // than passing a `TimeZone::CivilInfo` to a function, pass an `absl::Time`
+ // and an `absl::TimeZone`.
+ struct CivilInfo {
+ CivilSecond cs;
+ Duration subsecond;
+
+ // Note: The following fields exist for backward compatibility
+ // with older APIs. Accessing these fields directly is a sign of
+ // imprudent logic in the calling code. Modern time-related code
+ // should only access this data indirectly by way of FormatTime().
+ // These fields are undefined for InfiniteFuture() and InfinitePast().
+ int offset; // seconds east of UTC
+ bool is_dst; // is offset non-standard?
+ const char* zone_abbr; // time-zone abbreviation (e.g., "PST")
+ };
+
+ // TimeZone::At(Time)
+ //
+ // Returns the civil time for this TimeZone at a certain `absl::Time`.
+ // If the input time is infinite, the output civil second will be set to
+ // CivilSecond::max() or min(), and the subsecond will be infinite.
+ //
+ // Example:
+ //
+ // const auto epoch = lax.At(absl::UnixEpoch());
+ // // epoch.cs == 1969-12-31 16:00:00
+ // // epoch.subsecond == absl::ZeroDuration()
+ // // epoch.offset == -28800
+ // // epoch.is_dst == false
+ // // epoch.abbr == "PST"
+ CivilInfo At(Time t) const;
+
+ // TimeZone::TimeInfo
+ //
+ // Information about the absolute times corresponding to a civil time.
+ // (Subseconds must be handled separately.)
+ //
+ // It is possible for a caller to pass a civil-time value that does
+ // not represent an actual or unique instant in time (due to a shift
+ // in UTC offset in the TimeZone, which results in a discontinuity in
+ // the civil-time components). For example, a daylight-saving-time
+ // transition skips or repeats civil times---in the United States,
+ // March 13, 2011 02:15 never occurred, while November 6, 2011 01:15
+ // occurred twice---so requests for such times are not well-defined.
+ // To account for these possibilities, `absl::TimeZone::TimeInfo` is
+ // richer than just a single `absl::Time`.
+ struct TimeInfo {
+ enum CivilKind {
+ UNIQUE, // the civil time was singular (pre == trans == post)
+ SKIPPED, // the civil time did not exist (pre => trans > post)
+ REPEATED, // the civil time was ambiguous (pre < trans <= post)
+ } kind;
+ Time pre; // time calculated using the pre-transition offset
+ Time trans; // when the civil-time discontinuity occurred
+ Time post; // time calculated using the post-transition offset
+ };
+
+ // TimeZone::At(CivilSecond)
+ //
+ // Returns an `absl::TimeInfo` containing the absolute time(s) for this
+ // TimeZone at an `absl::CivilSecond`. When the civil time is skipped or
+ // repeated, returns times calculated using the pre-transition and post-
+ // transition UTC offsets, plus the transition time itself.
+ //
+ // Examples:
+ //
+ // // A unique civil time
+ // const auto jan01 = lax.At(absl::CivilSecond(2011, 1, 1, 0, 0, 0));
+ // // jan01.kind == TimeZone::TimeInfo::UNIQUE
+ // // jan01.pre is 2011-01-01 00:00:00 -0800
+ // // jan01.trans is 2011-01-01 00:00:00 -0800
+ // // jan01.post is 2011-01-01 00:00:00 -0800
+ //
+ // // A Spring DST transition, when there is a gap in civil time
+ // const auto mar13 = lax.At(absl::CivilSecond(2011, 3, 13, 2, 15, 0));
+ // // mar13.kind == TimeZone::TimeInfo::SKIPPED
+ // // mar13.pre is 2011-03-13 03:15:00 -0700
+ // // mar13.trans is 2011-03-13 03:00:00 -0700
+ // // mar13.post is 2011-03-13 01:15:00 -0800
+ //
+ // // A Fall DST transition, when civil times are repeated
+ // const auto nov06 = lax.At(absl::CivilSecond(2011, 11, 6, 1, 15, 0));
+ // // nov06.kind == TimeZone::TimeInfo::REPEATED
+ // // nov06.pre is 2011-11-06 01:15:00 -0700
+ // // nov06.trans is 2011-11-06 01:00:00 -0800
+ // // nov06.post is 2011-11-06 01:15:00 -0800
+ TimeInfo At(CivilSecond ct) const;
+
+ template <typename H>
+ friend H AbslHashValue(H h, TimeZone tz) {
+ return H::combine(std::move(h), tz.cz_);
+ }
+
+ private:
+ friend bool operator==(TimeZone a, TimeZone b) { return a.cz_ == b.cz_; }
+ friend bool operator!=(TimeZone a, TimeZone b) { return a.cz_ != b.cz_; }
+ friend std::ostream& operator<<(std::ostream& os, TimeZone tz) {
+ return os << tz.name();
+ }
+
+ time_internal::cctz::time_zone cz_;
+};
+
+// LoadTimeZone()
+//
+// Loads the named zone. May perform I/O on the initial load of the named
+// zone. If the name is invalid, or some other kind of error occurs, returns
+// `false` and `*tz` is set to the UTC time zone.
+inline bool LoadTimeZone(const std::string& name, TimeZone* tz) {
+ if (name == "localtime") {
+ *tz = TimeZone(time_internal::cctz::local_time_zone());
+ return true;
+ }
+ time_internal::cctz::time_zone cz;
+ const bool b = time_internal::cctz::load_time_zone(name, &cz);
+ *tz = TimeZone(cz);
+ return b;
+}
+
+// FixedTimeZone()
+//
+// Returns a TimeZone that is a fixed offset (seconds east) from UTC.
+// Note: If the absolute value of the offset is greater than 24 hours
+// you'll get UTC (i.e., no offset) instead.
+inline TimeZone FixedTimeZone(int seconds) {
+ return TimeZone(
+ time_internal::cctz::fixed_time_zone(std::chrono::seconds(seconds)));
+}
+
+// UTCTimeZone()
+//
+// Convenience method returning the UTC time zone.
+inline TimeZone UTCTimeZone() {
+ return TimeZone(time_internal::cctz::utc_time_zone());
+}
+
+// LocalTimeZone()
+//
+// Convenience method returning the local time zone, or UTC if there is
+// no configured local zone. Warning: Be wary of using LocalTimeZone(),
+// and particularly so in a server process, as the zone configured for the
+// local machine should be irrelevant. Prefer an explicit zone name.
+inline TimeZone LocalTimeZone() {
+ return TimeZone(time_internal::cctz::local_time_zone());
+}
+
+// ToCivilSecond()
+// ToCivilMinute()
+// ToCivilHour()
+// ToCivilDay()
+// ToCivilMonth()
+// ToCivilYear()
+//
+// Helpers for TimeZone::At(Time) to return particularly aligned civil times.
+//
+// Example:
+//
+// absl::Time t = ...;
+// absl::TimeZone tz = ...;
+// const auto cd = absl::ToCivilDay(t, tz);
+inline CivilSecond ToCivilSecond(Time t, TimeZone tz) {
+ return tz.At(t).cs; // already a CivilSecond
+}
+inline CivilMinute ToCivilMinute(Time t, TimeZone tz) {
+ return CivilMinute(tz.At(t).cs);
+}
+inline CivilHour ToCivilHour(Time t, TimeZone tz) {
+ return CivilHour(tz.At(t).cs);
+}
+inline CivilDay ToCivilDay(Time t, TimeZone tz) {
+ return CivilDay(tz.At(t).cs);
+}
+inline CivilMonth ToCivilMonth(Time t, TimeZone tz) {
+ return CivilMonth(tz.At(t).cs);
+}
+inline CivilYear ToCivilYear(Time t, TimeZone tz) {
+ return CivilYear(tz.At(t).cs);
+}
+
+// FromCivil()
+//
+// Helper for TimeZone::At(CivilSecond) that provides "order-preserving
+// semantics." If the civil time maps to a unique time, that time is
+// returned. If the civil time is repeated in the given time zone, the
+// time using the pre-transition offset is returned. Otherwise, the
+// civil time is skipped in the given time zone, and the transition time
+// is returned. This means that for any two civil times, ct1 and ct2,
+// (ct1 < ct2) => (FromCivil(ct1) <= FromCivil(ct2)), the equal case
+// being when two non-existent civil times map to the same transition time.
+//
+// Note: Accepts civil times of any alignment.
+inline Time FromCivil(CivilSecond ct, TimeZone tz) {
+ const auto ti = tz.At(ct);
+ if (ti.kind == TimeZone::TimeInfo::SKIPPED) return ti.trans;
+ return ti.pre;
+}
+
+// TimeConversion
+//
+// An `absl::TimeConversion` represents the conversion of year, month, day,
+// hour, minute, and second values (i.e., a civil time), in a particular
+// `absl::TimeZone`, to a time instant (an absolute time), as returned by
+// `absl::ConvertDateTime()`. Lecacy version of `absl::TimeZone::TimeInfo`.
+//
+// Deprecated. Use `absl::TimeZone::TimeInfo`.
+struct
+ TimeConversion {
+ Time pre; // time calculated using the pre-transition offset
+ Time trans; // when the civil-time discontinuity occurred
+ Time post; // time calculated using the post-transition offset
+
+ enum Kind {
+ UNIQUE, // the civil time was singular (pre == trans == post)
+ SKIPPED, // the civil time did not exist
+ REPEATED, // the civil time was ambiguous
+ };
+ Kind kind;
+
+ bool normalized; // input values were outside their valid ranges
+};
+
+// ConvertDateTime()
+//
+// Legacy version of `absl::TimeZone::At(absl::CivilSecond)` that takes
+// the civil time as six, separate values (YMDHMS).
+//
+// The input month, day, hour, minute, and second values can be outside
+// of their valid ranges, in which case they will be "normalized" during
+// the conversion.
+//
+// Example:
+//
+// // "October 32" normalizes to "November 1".
+// absl::TimeConversion tc =
+// absl::ConvertDateTime(2013, 10, 32, 8, 30, 0, lax);
+// // tc.kind == TimeConversion::UNIQUE && tc.normalized == true
+// // absl::ToCivilDay(tc.pre, tz).month() == 11
+// // absl::ToCivilDay(tc.pre, tz).day() == 1
+//
+// Deprecated. Use `absl::TimeZone::At(CivilSecond)`.
+TimeConversion ConvertDateTime(int64_t year, int mon, int day, int hour,
+ int min, int sec, TimeZone tz);
+
+// FromDateTime()
+//
+// A convenience wrapper for `absl::ConvertDateTime()` that simply returns
+// the "pre" `absl::Time`. That is, the unique result, or the instant that
+// is correct using the pre-transition offset (as if the transition never
+// happened).
+//
+// Example:
+//
+// absl::Time t = absl::FromDateTime(2017, 9, 26, 9, 30, 0, lax);
+// // t = 2017-09-26 09:30:00 -0700
+//
+// Deprecated. Use `absl::TimeZone::At(CivilSecond).pre`.
+inline Time FromDateTime(int64_t year, int mon, int day, int hour,
+ int min, int sec, TimeZone tz) {
+ return ConvertDateTime(year, mon, day, hour, min, sec, tz).pre;
+}
+
+// FromTM()
+//
+// Converts the `tm_year`, `tm_mon`, `tm_mday`, `tm_hour`, `tm_min`, and
+// `tm_sec` fields to an `absl::Time` using the given time zone. See ctime(3)
+// for a description of the expected values of the tm fields. If the indicated
+// time instant is not unique (see `absl::TimeZone::At(absl::CivilSecond)`
+// above), the `tm_isdst` field is consulted to select the desired instant
+// (`tm_isdst` > 0 means DST, `tm_isdst` == 0 means no DST, `tm_isdst` < 0
+// means use the post-transition offset).
+Time FromTM(const struct tm& tm, TimeZone tz);
+
+// ToTM()
+//
+// Converts the given `absl::Time` to a struct tm using the given time zone.
+// See ctime(3) for a description of the values of the tm fields.
+struct tm ToTM(Time t, TimeZone tz);
+
// RFC3339_full
// RFC3339_sec
//
@@ -929,12 +1161,8 @@ extern const char RFC1123_no_wday[]; // %d %b %E4Y %H:%M:%S %z
//
// Example:
//
-// absl::TimeZone lax;
-// if (!absl::LoadTimeZone("America/Los_Angeles", &lax)) {
-// // handle error case
-// }
-// absl::Time t = absl::FromDateTime(2013, 1, 2, 3, 4, 5, lax);
-//
+// absl::CivilSecond cs(2013, 1, 2, 3, 4, 5);
+// absl::Time t = absl::FromCivil(cs, lax);
// string f = absl::FormatTime("%H:%M:%S", t, lax); // "03:04:05"
// f = absl::FormatTime("%H:%M:%E3S", t, lax); // "03:04:05.000"
//
@@ -985,7 +1213,7 @@ inline std::ostream& operator<<(std::ostream& os, Time t) {
// in the conversion.
//
// Date and time fields that are out-of-range will be treated as errors
-// rather than normalizing them like `absl::FromDateTime()` does. For example,
+// rather than normalizing them like `absl::CivilSecond` does. For example,
// it is an error to parse the date "Oct 32, 2013" because 32 is out of range.
//
// A leap second of ":60" is normalized to ":00" of the following minute
@@ -1012,121 +1240,11 @@ bool ParseTime(const std::string& format, const std::string& input, Time* time,
// given TimeZone. This means that the input, by itself, does not identify a
// unique instant. Being time-zone dependent, it also admits the possibility
// of ambiguity or non-existence, in which case the "pre" time (as defined
-// for ConvertDateTime()) is returned. For these reasons we recommend that
+// by TimeZone::TimeInfo) is returned. For these reasons we recommend that
// all date/time strings include a UTC offset so they're context independent.
bool ParseTime(const std::string& format, const std::string& input, TimeZone tz,
Time* time, std::string* err);
-// Support for flag values of type Time. Time flags must be specified in a
-// format that matches absl::RFC3339_full. For example:
-//
-// --start_time=2016-01-02T03:04:05.678+08:00
-//
-// Note: A UTC offset (or 'Z' indicating a zero-offset from UTC) is required.
-//
-// Additionally, if you'd like to specify a time as a count of
-// seconds/milliseconds/etc from the Unix epoch, use an absl::Duration flag
-// and add that duration to absl::UnixEpoch() to get an absl::Time.
-bool ParseFlag(const std::string& text, Time* t, std::string* error);
-std::string UnparseFlag(Time t);
-
-// TimeZone
-//
-// The `absl::TimeZone` is an opaque, small, value-type class representing a
-// geo-political region within which particular rules are used for converting
-// between absolute and civil times (see https://git.io/v59Ly). `absl::TimeZone`
-// values are named using the TZ identifiers from the IANA Time Zone Database,
-// such as "America/Los_Angeles" or "Australia/Sydney". `absl::TimeZone` values
-// are created from factory functions such as `absl::LoadTimeZone()`. Note:
-// strings like "PST" and "EDT" are not valid TZ identifiers. Prefer to pass by
-// value rather than const reference.
-//
-// For more on the fundamental concepts of time zones, absolute times, and civil
-// times, see https://github.com/google/cctz#fundamental-concepts
-//
-// Examples:
-//
-// absl::TimeZone utc = absl::UTCTimeZone();
-// absl::TimeZone pst = absl::FixedTimeZone(-8 * 60 * 60);
-// absl::TimeZone loc = absl::LocalTimeZone();
-// absl::TimeZone lax;
-// if (!absl::LoadTimeZone("America/Los_Angeles", &lax)) {
-// // handle error case
-// }
-//
-// See also:
-// - https://github.com/google/cctz
-// - http://www.iana.org/time-zones
-// - http://en.wikipedia.org/wiki/Zoneinfo
-class TimeZone {
- public:
- explicit TimeZone(time_internal::cctz::time_zone tz) : cz_(tz) {}
- TimeZone() = default; // UTC, but prefer UTCTimeZone() to be explicit.
- TimeZone(const TimeZone&) = default;
- TimeZone& operator=(const TimeZone&) = default;
-
- explicit operator time_internal::cctz::time_zone() const { return cz_; }
-
- std::string name() const { return cz_.name(); }
-
- template <typename H>
- friend H AbslHashValue(H h, TimeZone tz) {
- return H::combine(std::move(h), tz.cz_);
- }
-
- private:
- friend bool operator==(TimeZone a, TimeZone b) { return a.cz_ == b.cz_; }
- friend bool operator!=(TimeZone a, TimeZone b) { return a.cz_ != b.cz_; }
- friend std::ostream& operator<<(std::ostream& os, TimeZone tz) {
- return os << tz.name();
- }
-
- time_internal::cctz::time_zone cz_;
-};
-
-// LoadTimeZone()
-//
-// Loads the named zone. May perform I/O on the initial load of the named
-// zone. If the name is invalid, or some other kind of error occurs, returns
-// `false` and `*tz` is set to the UTC time zone.
-inline bool LoadTimeZone(const std::string& name, TimeZone* tz) {
- if (name == "localtime") {
- *tz = TimeZone(time_internal::cctz::local_time_zone());
- return true;
- }
- time_internal::cctz::time_zone cz;
- const bool b = time_internal::cctz::load_time_zone(name, &cz);
- *tz = TimeZone(cz);
- return b;
-}
-
-// FixedTimeZone()
-//
-// Returns a TimeZone that is a fixed offset (seconds east) from UTC.
-// Note: If the absolute value of the offset is greater than 24 hours
-// you'll get UTC (i.e., no offset) instead.
-inline TimeZone FixedTimeZone(int seconds) {
- return TimeZone(
- time_internal::cctz::fixed_time_zone(std::chrono::seconds(seconds)));
-}
-
-// UTCTimeZone()
-//
-// Convenience method returning the UTC time zone.
-inline TimeZone UTCTimeZone() {
- return TimeZone(time_internal::cctz::utc_time_zone());
-}
-
-// LocalTimeZone()
-//
-// Convenience method returning the local time zone, or UTC if there is
-// no configured local zone. Warning: Be wary of using LocalTimeZone(),
-// and particularly so in a server process, as the zone configured for the
-// local machine should be irrelevant. Prefer an explicit zone name.
-inline TimeZone LocalTimeZone() {
- return TimeZone(time_internal::cctz::local_time_zone());
-}
-
// ============================================================================
// Implementation Details Follow
// ============================================================================
diff --git a/absl/time/time_benchmark.cc b/absl/time/time_benchmark.cc
index e1009946..9bbed6f8 100644
--- a/absl/time/time_benchmark.cc
+++ b/absl/time/time_benchmark.cc
@@ -169,32 +169,32 @@ void BM_Time_ToUnixSeconds(benchmark::State& state) {
BENCHMARK(BM_Time_ToUnixSeconds);
//
-// FromDateTime
+// FromCivil
//
-// In each "FromDateTime" benchmark we switch between two YMDhms
-// values separated by at least one transition in order to defeat any
-// internal caching of previous results (e.g., see time_local_hint_).
+// In each "FromCivil" benchmark we switch between two YMDhms values
+// separated by at least one transition in order to defeat any internal
+// caching of previous results (e.g., see time_local_hint_).
//
// The "UTC" variants use UTC instead of the Google/local time zone.
// The "Day0" variants require normalization of the day of month.
//
-void BM_Time_FromDateTime_Absl(benchmark::State& state) {
+void BM_Time_FromCivil_Absl(benchmark::State& state) {
const absl::TimeZone tz =
absl::time_internal::LoadTimeZone("America/Los_Angeles");
int i = 0;
while (state.KeepRunning()) {
if ((i & 1) == 0) {
- absl::FromDateTime(2014, 12, 18, 20, 16, 18, tz);
+ absl::FromCivil(absl::CivilSecond(2014, 12, 18, 20, 16, 18), tz);
} else {
- absl::FromDateTime(2013, 11, 15, 18, 30, 27, tz);
+ absl::FromCivil(absl::CivilSecond(2013, 11, 15, 18, 30, 27), tz);
}
++i;
}
}
-BENCHMARK(BM_Time_FromDateTime_Absl);
+BENCHMARK(BM_Time_FromCivil_Absl);
-void BM_Time_FromDateTime_Libc(benchmark::State& state) {
+void BM_Time_FromCivil_Libc(benchmark::State& state) {
// No timezone support, so just use localtime.
int i = 0;
while (state.KeepRunning()) {
@@ -219,32 +219,32 @@ void BM_Time_FromDateTime_Libc(benchmark::State& state) {
++i;
}
}
-BENCHMARK(BM_Time_FromDateTime_Libc);
+BENCHMARK(BM_Time_FromCivil_Libc);
-void BM_Time_FromDateTimeUTC_Absl(benchmark::State& state) {
+void BM_Time_FromCivilUTC_Absl(benchmark::State& state) {
const absl::TimeZone tz = absl::UTCTimeZone();
while (state.KeepRunning()) {
- FromDateTime(2014, 12, 18, 20, 16, 18, tz);
+ absl::FromCivil(absl::CivilSecond(2014, 12, 18, 20, 16, 18), tz);
}
}
-BENCHMARK(BM_Time_FromDateTimeUTC_Absl);
+BENCHMARK(BM_Time_FromCivilUTC_Absl);
-void BM_Time_FromDateTimeDay0_Absl(benchmark::State& state) {
+void BM_Time_FromCivilDay0_Absl(benchmark::State& state) {
const absl::TimeZone tz =
absl::time_internal::LoadTimeZone("America/Los_Angeles");
int i = 0;
while (state.KeepRunning()) {
if ((i & 1) == 0) {
- absl::FromDateTime(2014, 12, 0, 20, 16, 18, tz);
+ absl::FromCivil(absl::CivilSecond(2014, 12, 0, 20, 16, 18), tz);
} else {
- absl::FromDateTime(2013, 11, 0, 18, 30, 27, tz);
+ absl::FromCivil(absl::CivilSecond(2013, 11, 0, 18, 30, 27), tz);
}
++i;
}
}
-BENCHMARK(BM_Time_FromDateTimeDay0_Absl);
+BENCHMARK(BM_Time_FromCivilDay0_Absl);
-void BM_Time_FromDateTimeDay0_Libc(benchmark::State& state) {
+void BM_Time_FromCivilDay0_Libc(benchmark::State& state) {
// No timezone support, so just use localtime.
int i = 0;
while (state.KeepRunning()) {
@@ -269,7 +269,7 @@ void BM_Time_FromDateTimeDay0_Libc(benchmark::State& state) {
++i;
}
}
-BENCHMARK(BM_Time_FromDateTimeDay0_Libc);
+BENCHMARK(BM_Time_FromCivilDay0_Libc);
//
// To/FromTimespec
diff --git a/absl/time/time_norm_test.cc b/absl/time/time_norm_test.cc
deleted file mode 100644
index 4436242e..00000000
--- a/absl/time/time_norm_test.cc
+++ /dev/null
@@ -1,306 +0,0 @@
-// Copyright 2017 The Abseil Authors.
-//
-// Licensed under the Apache License, Version 2.0 (the "License");
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-//
-// http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-
-// This file contains tests for FromDateTime() normalization, which is
-// time-zone independent so we just use UTC throughout.
-
-#include <cstdint>
-#include <limits>
-
-#include "gmock/gmock.h"
-#include "gtest/gtest.h"
-#include "absl/time/internal/test_util.h"
-#include "absl/time/time.h"
-
-namespace {
-
-TEST(TimeNormCase, SimpleOverflow) {
- const absl::TimeZone utc = absl::UTCTimeZone();
-
- absl::TimeConversion tc =
- absl::ConvertDateTime(2013, 11, 15, 16, 32, 59 + 1, utc);
- EXPECT_TRUE(tc.normalized);
- EXPECT_EQ(absl::TimeConversion::UNIQUE, tc.kind);
- absl::Time::Breakdown bd = tc.pre.In(utc);
- ABSL_INTERNAL_EXPECT_TIME(bd, 2013, 11, 15, 16, 33, 0, 0, false);
-
- tc = absl::ConvertDateTime(2013, 11, 15, 16, 59 + 1, 14, utc);
- EXPECT_TRUE(tc.normalized);
- EXPECT_EQ(absl::TimeConversion::UNIQUE, tc.kind);
- bd = tc.pre.In(utc);
- ABSL_INTERNAL_EXPECT_TIME(bd, 2013, 11, 15, 17, 0, 14, 0, false);
-
- tc = absl::ConvertDateTime(2013, 11, 15, 23 + 1, 32, 14, utc);
- EXPECT_TRUE(tc.normalized);
- EXPECT_EQ(absl::TimeConversion::UNIQUE, tc.kind);
- bd = tc.pre.In(utc);
- ABSL_INTERNAL_EXPECT_TIME(bd, 2013, 11, 16, 0, 32, 14, 0, false);
-
- tc = absl::ConvertDateTime(2013, 11, 30 + 1, 16, 32, 14, utc);
- EXPECT_TRUE(tc.normalized);
- EXPECT_EQ(absl::TimeConversion::UNIQUE, tc.kind);
- bd = tc.pre.In(utc);
- ABSL_INTERNAL_EXPECT_TIME(bd, 2013, 12, 1, 16, 32, 14, 0, false);
-
- tc = absl::ConvertDateTime(2013, 12 + 1, 15, 16, 32, 14, utc);
- EXPECT_TRUE(tc.normalized);
- EXPECT_EQ(absl::TimeConversion::UNIQUE, tc.kind);
- bd = tc.pre.In(utc);
- ABSL_INTERNAL_EXPECT_TIME(bd, 2014, 1, 15, 16, 32, 14, 0, false);
-}
-
-TEST(TimeNormCase, SimpleUnderflow) {
- const absl::TimeZone utc = absl::UTCTimeZone();
-
- absl::TimeConversion tc = ConvertDateTime(2013, 11, 15, 16, 32, 0 - 1, utc);
- EXPECT_TRUE(tc.normalized);
- EXPECT_EQ(absl::TimeConversion::UNIQUE, tc.kind);
- absl::Time::Breakdown bd = tc.pre.In(utc);
- ABSL_INTERNAL_EXPECT_TIME(bd, 2013, 11, 15, 16, 31, 59, 0, false);
-
- tc = ConvertDateTime(2013, 11, 15, 16, 0 - 1, 14, utc);
- EXPECT_TRUE(tc.normalized);
- EXPECT_EQ(absl::TimeConversion::UNIQUE, tc.kind);
- bd = tc.pre.In(utc);
- ABSL_INTERNAL_EXPECT_TIME(bd, 2013, 11, 15, 15, 59, 14, 0, false);
-
- tc = ConvertDateTime(2013, 11, 15, 0 - 1, 32, 14, utc);
- EXPECT_TRUE(tc.normalized);
- EXPECT_EQ(absl::TimeConversion::UNIQUE, tc.kind);
- bd = tc.pre.In(utc);
- ABSL_INTERNAL_EXPECT_TIME(bd, 2013, 11, 14, 23, 32, 14, 0, false);
-
- tc = ConvertDateTime(2013, 11, 1 - 1, 16, 32, 14, utc);
- EXPECT_TRUE(tc.normalized);
- EXPECT_EQ(absl::TimeConversion::UNIQUE, tc.kind);
- bd = tc.pre.In(utc);
- ABSL_INTERNAL_EXPECT_TIME(bd, 2013, 10, 31, 16, 32, 14, 0, false);
-
- tc = ConvertDateTime(2013, 1 - 1, 15, 16, 32, 14, utc);
- EXPECT_TRUE(tc.normalized);
- EXPECT_EQ(absl::TimeConversion::UNIQUE, tc.kind);
- bd = tc.pre.In(utc);
- ABSL_INTERNAL_EXPECT_TIME(bd, 2012, 12, 15, 16, 32, 14, 0, false);
-}
-
-TEST(TimeNormCase, MultipleOverflow) {
- const absl::TimeZone utc = absl::UTCTimeZone();
- absl::TimeConversion tc = ConvertDateTime(2013, 12, 31, 23, 59, 59 + 1, utc);
- EXPECT_TRUE(tc.normalized);
- EXPECT_EQ(absl::TimeConversion::UNIQUE, tc.kind);
- absl::Time::Breakdown bd = tc.pre.In(utc);
- ABSL_INTERNAL_EXPECT_TIME(bd, 2014, 1, 1, 0, 0, 0, 0, false);
-}
-
-TEST(TimeNormCase, MultipleUnderflow) {
- const absl::TimeZone utc = absl::UTCTimeZone();
- absl::TimeConversion tc = absl::ConvertDateTime(2014, 1, 1, 0, 0, 0 - 1, utc);
- EXPECT_TRUE(tc.normalized);
- EXPECT_EQ(absl::TimeConversion::UNIQUE, tc.kind);
- absl::Time::Breakdown bd = tc.pre.In(utc);
- ABSL_INTERNAL_EXPECT_TIME(bd, 2013, 12, 31, 23, 59, 59, 0, false);
-}
-
-TEST(TimeNormCase, OverflowLimits) {
- const absl::TimeZone utc = absl::UTCTimeZone();
- absl::TimeConversion tc;
- absl::Time::Breakdown bd;
-
- const int kintmax = std::numeric_limits<int>::max();
- tc = absl::ConvertDateTime(0, kintmax, kintmax, kintmax, kintmax, kintmax,
- utc);
- EXPECT_TRUE(tc.normalized);
- EXPECT_EQ(absl::TimeConversion::UNIQUE, tc.kind);
- bd = tc.pre.In(utc);
- ABSL_INTERNAL_EXPECT_TIME(bd, 185085715, 11, 27, 12, 21, 7, 0, false);
-
- const int kintmin = std::numeric_limits<int>::min();
- tc = absl::ConvertDateTime(0, kintmin, kintmin, kintmin, kintmin, kintmin,
- utc);
- EXPECT_TRUE(tc.normalized);
- EXPECT_EQ(absl::TimeConversion::UNIQUE, tc.kind);
- bd = tc.pre.In(utc);
- ABSL_INTERNAL_EXPECT_TIME(bd, -185085717, 10, 31, 10, 37, 52, 0, false);
-
- const int64_t max_year = std::numeric_limits<int64_t>::max();
- tc = absl::ConvertDateTime(max_year, 12, 31, 23, 59, 59, utc);
- EXPECT_TRUE(tc.normalized);
- EXPECT_EQ(absl::TimeConversion::UNIQUE, tc.kind);
- EXPECT_EQ(absl::InfiniteFuture(), tc.pre);
-
- const int64_t min_year = std::numeric_limits<int64_t>::min();
- tc = absl::ConvertDateTime(min_year, 1, 1, 0, 0, 0, utc);
- EXPECT_TRUE(tc.normalized);
- EXPECT_EQ(absl::TimeConversion::UNIQUE, tc.kind);
- EXPECT_EQ(absl::InfinitePast(), tc.pre);
-}
-
-TEST(TimeNormCase, ComplexOverflow) {
- const absl::TimeZone utc = absl::UTCTimeZone();
-
- absl::TimeConversion tc =
- ConvertDateTime(2013, 11, 15, 16, 32, 14 + 123456789, utc);
- EXPECT_TRUE(tc.normalized);
- EXPECT_EQ(absl::TimeConversion::UNIQUE, tc.kind);
- absl::Time::Breakdown bd = tc.pre.In(utc);
- ABSL_INTERNAL_EXPECT_TIME(bd, 2017, 10, 14, 14, 5, 23, 0, false);
-
- tc = absl::ConvertDateTime(2013, 11, 15, 16, 32 + 1234567, 14, utc);
- EXPECT_TRUE(tc.normalized);
- EXPECT_EQ(absl::TimeConversion::UNIQUE, tc.kind);
- bd = tc.pre.In(utc);
- ABSL_INTERNAL_EXPECT_TIME(bd, 2016, 3, 22, 0, 39, 14, 0, false);
-
- tc = absl::ConvertDateTime(2013, 11, 15, 16 + 123456, 32, 14, utc);
- EXPECT_TRUE(tc.normalized);
- EXPECT_EQ(absl::TimeConversion::UNIQUE, tc.kind);
- bd = tc.pre.In(utc);
- ABSL_INTERNAL_EXPECT_TIME(bd, 2027, 12, 16, 16, 32, 14, 0, false);
-
- tc = absl::ConvertDateTime(2013, 11, 15 + 1234, 16, 32, 14, utc);
- EXPECT_TRUE(tc.normalized);
- EXPECT_EQ(absl::TimeConversion::UNIQUE, tc.kind);
- bd = tc.pre.In(utc);
- ABSL_INTERNAL_EXPECT_TIME(bd, 2017, 4, 2, 16, 32, 14, 0, false);
-
- tc = absl::ConvertDateTime(2013, 11 + 123, 15, 16, 32, 14, utc);
- EXPECT_TRUE(tc.normalized);
- EXPECT_EQ(absl::TimeConversion::UNIQUE, tc.kind);
- bd = tc.pre.In(utc);
- ABSL_INTERNAL_EXPECT_TIME(bd, 2024, 2, 15, 16, 32, 14, 0, false);
-}
-
-TEST(TimeNormCase, ComplexUnderflow) {
- const absl::TimeZone utc = absl::UTCTimeZone();
-
- absl::TimeConversion tc =
- absl::ConvertDateTime(1999, 3, 0, 0, 0, 0, utc); // year 400
- EXPECT_TRUE(tc.normalized);
- EXPECT_EQ(absl::TimeConversion::UNIQUE, tc.kind);
- absl::Time::Breakdown bd = tc.pre.In(utc);
- ABSL_INTERNAL_EXPECT_TIME(bd, 1999, 2, 28, 0, 0, 0, 0, false);
-
- tc = absl::ConvertDateTime(2013, 11, 15, 16, 32, 14 - 123456789, utc);
- EXPECT_TRUE(tc.normalized);
- EXPECT_EQ(absl::TimeConversion::UNIQUE, tc.kind);
- bd = tc.pre.In(utc);
- ABSL_INTERNAL_EXPECT_TIME(bd, 2009, 12, 17, 18, 59, 5, 0, false);
-
- tc = absl::ConvertDateTime(2013, 11, 15, 16, 32 - 1234567, 14, utc);
- EXPECT_TRUE(tc.normalized);
- EXPECT_EQ(absl::TimeConversion::UNIQUE, tc.kind);
- bd = tc.pre.In(utc);
- ABSL_INTERNAL_EXPECT_TIME(bd, 2011, 7, 12, 8, 25, 14, 0, false);
-
- tc = absl::ConvertDateTime(2013, 11, 15, 16 - 123456, 32, 14, utc);
- EXPECT_TRUE(tc.normalized);
- EXPECT_EQ(absl::TimeConversion::UNIQUE, tc.kind);
- bd = tc.pre.In(utc);
- ABSL_INTERNAL_EXPECT_TIME(bd, 1999, 10, 16, 16, 32, 14, 0, false);
-
- tc = absl::ConvertDateTime(2013, 11, 15 - 1234, 16, 32, 14, utc);
- EXPECT_TRUE(tc.normalized);
- EXPECT_EQ(absl::TimeConversion::UNIQUE, tc.kind);
- bd = tc.pre.In(utc);
- ABSL_INTERNAL_EXPECT_TIME(bd, 2010, 6, 30, 16, 32, 14, 0, false);
-
- tc = absl::ConvertDateTime(2013, 11 - 123, 15, 16, 32, 14, utc);
- EXPECT_TRUE(tc.normalized);
- EXPECT_EQ(absl::TimeConversion::UNIQUE, tc.kind);
- bd = tc.pre.In(utc);
- ABSL_INTERNAL_EXPECT_TIME(bd, 2003, 8, 15, 16, 32, 14, 0, false);
-}
-
-TEST(TimeNormCase, Mishmash) {
- const absl::TimeZone utc = absl::UTCTimeZone();
-
- absl::TimeConversion tc =
- absl::ConvertDateTime(2013, 11 - 123, 15 + 1234, 16 - 123456,
- 32 + 1234567, 14 - 123456789, utc);
- EXPECT_TRUE(tc.normalized);
- EXPECT_EQ(absl::TimeConversion::UNIQUE, tc.kind);
- absl::Time::Breakdown bd = tc.pre.In(utc);
- ABSL_INTERNAL_EXPECT_TIME(bd, 1991, 5, 9, 3, 6, 5, 0, false);
-
- tc = absl::ConvertDateTime(2013, 11 + 123, 15 - 1234, 16 + 123456,
- 32 - 1234567, 14 + 123456789, utc);
- EXPECT_TRUE(tc.normalized);
- EXPECT_EQ(absl::TimeConversion::UNIQUE, tc.kind);
- bd = tc.pre.In(utc);
- ABSL_INTERNAL_EXPECT_TIME(bd, 2036, 5, 24, 5, 58, 23, 0, false);
-
- // Here is a normalization case we got wrong for a while. Because the
- // day is converted to "1" within a 400-year (146097-day) period, we
- // didn't need to roll the month and so we didn't mark it as normalized.
- tc = absl::ConvertDateTime(2013, 11, -146097 + 1, 16, 32, 14, utc);
- EXPECT_TRUE(tc.normalized);
- EXPECT_EQ(absl::TimeConversion::UNIQUE, tc.kind);
- bd = tc.pre.In(utc);
- ABSL_INTERNAL_EXPECT_TIME(bd, 1613, 11, 1, 16, 32, 14, 0, false);
-
- // Even though the month overflow compensates for the day underflow,
- // this should still be marked as normalized.
- tc = absl::ConvertDateTime(2013, 11 + 400 * 12, -146097 + 1, 16, 32, 14, utc);
- EXPECT_TRUE(tc.normalized);
- EXPECT_EQ(absl::TimeConversion::UNIQUE, tc.kind);
- bd = tc.pre.In(utc);
- ABSL_INTERNAL_EXPECT_TIME(bd, 2013, 11, 1, 16, 32, 14, 0, false);
-}
-
-TEST(TimeNormCase, LeapYears) {
- const absl::TimeZone utc = absl::UTCTimeZone();
-
- absl::TimeConversion tc =
- absl::ConvertDateTime(2013, 2, 28 + 1, 0, 0, 0, utc);
- EXPECT_TRUE(tc.normalized);
- EXPECT_EQ(absl::TimeConversion::UNIQUE, tc.kind);
- absl::Time::Breakdown bd = tc.pre.In(utc);
- ABSL_INTERNAL_EXPECT_TIME(bd, 2013, 3, 1, 0, 0, 0, 0, false);
-
- tc = absl::ConvertDateTime(2012, 2, 28 + 1, 0, 0, 0, utc);
- EXPECT_FALSE(tc.normalized);
- EXPECT_EQ(absl::TimeConversion::UNIQUE, tc.kind);
- bd = tc.pre.In(utc);
- ABSL_INTERNAL_EXPECT_TIME(bd, 2012, 2, 29, 0, 0, 0, 0, false);
-
- tc = absl::ConvertDateTime(2000, 2, 28 + 1, 0, 0, 0, utc);
- EXPECT_FALSE(tc.normalized);
- EXPECT_EQ(absl::TimeConversion::UNIQUE, tc.kind);
- bd = tc.pre.In(utc);
- ABSL_INTERNAL_EXPECT_TIME(bd, 2000, 2, 29, 0, 0, 0, 0, false);
-
- tc = absl::ConvertDateTime(1900, 2, 28 + 1, 0, 0, 0, utc);
- EXPECT_TRUE(tc.normalized);
- EXPECT_EQ(absl::TimeConversion::UNIQUE, tc.kind);
- bd = tc.pre.In(utc);
- ABSL_INTERNAL_EXPECT_TIME(bd, 1900, 3, 1, 0, 0, 0, 0, false);
-}
-
-// Convert all the days from 1970-1-1 to 1970-1-146097 (aka 2369-12-31)
-// and check that they normalize to the expected time. 146097 days span
-// the 400-year Gregorian cycle used during normalization.
-TEST(TimeNormCase, AllTheDays) {
- const absl::TimeZone utc = absl::UTCTimeZone();
- absl::Time exp_time = absl::UnixEpoch();
-
- for (int day = 1; day <= 146097; ++day) {
- absl::TimeConversion tc = absl::ConvertDateTime(1970, 1, day, 0, 0, 0, utc);
- EXPECT_EQ(day > 31, tc.normalized);
- EXPECT_EQ(absl::TimeConversion::UNIQUE, tc.kind);
- EXPECT_EQ(exp_time, tc.pre);
- exp_time += absl::Hours(24);
- }
-}
-
-} // namespace
diff --git a/absl/time/time_test.cc b/absl/time/time_test.cc
index 4f8f58a6..feca4587 100644
--- a/absl/time/time_test.cc
+++ b/absl/time/time_test.cc
@@ -28,6 +28,27 @@
namespace {
+#if GTEST_USES_SIMPLE_RE
+const char kZoneAbbrRE[] = ".*"; // just punt
+#else
+const char kZoneAbbrRE[] = "[A-Za-z]{3,4}|[-+][0-9]{2}([0-9]{2})?";
+#endif
+
+// This helper is a macro so that failed expectations show up with the
+// correct line numbers.
+#define EXPECT_CIVIL_INFO(ci, y, m, d, h, min, s, off, isdst) \
+ do { \
+ EXPECT_EQ(y, ci.cs.year()); \
+ EXPECT_EQ(m, ci.cs.month()); \
+ EXPECT_EQ(d, ci.cs.day()); \
+ EXPECT_EQ(h, ci.cs.hour()); \
+ EXPECT_EQ(min, ci.cs.minute()); \
+ EXPECT_EQ(s, ci.cs.second()); \
+ EXPECT_EQ(off, ci.offset); \
+ EXPECT_EQ(isdst, ci.is_dst); \
+ EXPECT_THAT(ci.zone_abbr, testing::MatchesRegex(kZoneAbbrRE)); \
+ } while (0)
+
// A gMock matcher to match timespec values. Use this matcher like:
// timespec ts1, ts2;
// EXPECT_THAT(ts1, TimespecMatcher(ts2));
@@ -84,10 +105,10 @@ TEST(Time, ValueSemantics) {
}
TEST(Time, UnixEpoch) {
- absl::Time::Breakdown bd = absl::UnixEpoch().In(absl::UTCTimeZone());
- ABSL_INTERNAL_EXPECT_TIME(bd, 1970, 1, 1, 0, 0, 0, 0, false);
- EXPECT_EQ(absl::ZeroDuration(), bd.subsecond);
- EXPECT_EQ(4, bd.weekday); // Thursday
+ const auto ci = absl::UTCTimeZone().At(absl::UnixEpoch());
+ EXPECT_EQ(absl::CivilSecond(1970, 1, 1, 0, 0, 0), ci.cs);
+ EXPECT_EQ(absl::ZeroDuration(), ci.subsecond);
+ EXPECT_EQ(absl::Weekday::thursday, absl::GetWeekday(absl::CivilDay(ci.cs)));
}
TEST(Time, Breakdown) {
@@ -95,26 +116,26 @@ TEST(Time, Breakdown) {
absl::Time t = absl::UnixEpoch();
// The Unix epoch as seen in NYC.
- absl::Time::Breakdown bd = t.In(tz);
- ABSL_INTERNAL_EXPECT_TIME(bd, 1969, 12, 31, 19, 0, 0, -18000, false);
- EXPECT_EQ(absl::ZeroDuration(), bd.subsecond);
- EXPECT_EQ(3, bd.weekday); // Wednesday
+ auto ci = tz.At(t);
+ EXPECT_CIVIL_INFO(ci, 1969, 12, 31, 19, 0, 0, -18000, false);
+ EXPECT_EQ(absl::ZeroDuration(), ci.subsecond);
+ EXPECT_EQ(absl::Weekday::wednesday, absl::GetWeekday(absl::CivilDay(ci.cs)));
// Just before the epoch.
t -= absl::Nanoseconds(1);
- bd = t.In(tz);
- ABSL_INTERNAL_EXPECT_TIME(bd, 1969, 12, 31, 18, 59, 59, -18000, false);
- EXPECT_EQ(absl::Nanoseconds(999999999), bd.subsecond);
- EXPECT_EQ(3, bd.weekday); // Wednesday
+ ci = tz.At(t);
+ EXPECT_CIVIL_INFO(ci, 1969, 12, 31, 18, 59, 59, -18000, false);
+ EXPECT_EQ(absl::Nanoseconds(999999999), ci.subsecond);
+ EXPECT_EQ(absl::Weekday::wednesday, absl::GetWeekday(absl::CivilDay(ci.cs)));
// Some time later.
t += absl::Hours(24) * 2735;
t += absl::Hours(18) + absl::Minutes(30) + absl::Seconds(15) +
absl::Nanoseconds(9);
- bd = t.In(tz);
- ABSL_INTERNAL_EXPECT_TIME(bd, 1977, 6, 28, 14, 30, 15, -14400, true);
- EXPECT_EQ(8, bd.subsecond / absl::Nanoseconds(1));
- EXPECT_EQ(2, bd.weekday); // Tuesday
+ ci = tz.At(t);
+ EXPECT_CIVIL_INFO(ci, 1977, 6, 28, 14, 30, 15, -14400, true);
+ EXPECT_EQ(8, ci.subsecond / absl::Nanoseconds(1));
+ EXPECT_EQ(absl::Weekday::tuesday, absl::GetWeekday(absl::CivilDay(ci.cs)));
}
TEST(Time, AdditiveOperators) {
@@ -550,67 +571,63 @@ TEST(Time, ToChronoTime) {
absl::ToChronoTime(absl::UnixEpoch() - tick));
}
-TEST(Time, ConvertDateTime) {
- const absl::TimeZone utc = absl::UTCTimeZone();
- const absl::TimeZone goog =
- absl::time_internal::LoadTimeZone("America/Los_Angeles");
+TEST(Time, TimeZoneAt) {
const absl::TimeZone nyc =
absl::time_internal::LoadTimeZone("America/New_York");
const std::string fmt = "%a, %e %b %Y %H:%M:%S %z (%Z)";
- // A simple case of normalization.
- absl::TimeConversion oct32 = ConvertDateTime(2013, 10, 32, 8, 30, 0, goog);
- EXPECT_TRUE(oct32.normalized);
- EXPECT_EQ(absl::TimeConversion::UNIQUE, oct32.kind);
- absl::TimeConversion nov01 = ConvertDateTime(2013, 11, 1, 8, 30, 0, goog);
- EXPECT_FALSE(nov01.normalized);
- EXPECT_EQ(absl::TimeConversion::UNIQUE, nov01.kind);
- EXPECT_EQ(oct32.pre, nov01.pre);
- EXPECT_EQ("Fri, 1 Nov 2013 08:30:00 -0700 (PDT)",
- absl::FormatTime(fmt, nov01.pre, goog));
+ // A non-transition where the civil time is unique.
+ absl::CivilSecond nov01(2013, 11, 1, 8, 30, 0);
+ const auto nov01_ci = nyc.At(nov01);
+ EXPECT_EQ(absl::TimeZone::TimeInfo::UNIQUE, nov01_ci.kind);
+ EXPECT_EQ("Fri, 1 Nov 2013 08:30:00 -0400 (EDT)",
+ absl::FormatTime(fmt, nov01_ci.pre, nyc));
+ EXPECT_EQ(nov01_ci.pre, nov01_ci.trans);
+ EXPECT_EQ(nov01_ci.pre, nov01_ci.post);
+ EXPECT_EQ(nov01_ci.pre, absl::FromCivil(nov01, nyc));
// A Spring DST transition, when there is a gap in civil time
// and we prefer the later of the possible interpretations of a
// non-existent time.
- absl::TimeConversion mar13 = ConvertDateTime(2011, 3, 13, 2, 15, 0, nyc);
- EXPECT_FALSE(mar13.normalized);
- EXPECT_EQ(absl::TimeConversion::SKIPPED, mar13.kind);
+ absl::CivilSecond mar13(2011, 3, 13, 2, 15, 0);
+ const auto mar_ci = nyc.At(mar13);
+ EXPECT_EQ(absl::TimeZone::TimeInfo::SKIPPED, mar_ci.kind);
EXPECT_EQ("Sun, 13 Mar 2011 03:15:00 -0400 (EDT)",
- absl::FormatTime(fmt, mar13.pre, nyc));
+ absl::FormatTime(fmt, mar_ci.pre, nyc));
EXPECT_EQ("Sun, 13 Mar 2011 03:00:00 -0400 (EDT)",
- absl::FormatTime(fmt, mar13.trans, nyc));
+ absl::FormatTime(fmt, mar_ci.trans, nyc));
EXPECT_EQ("Sun, 13 Mar 2011 01:15:00 -0500 (EST)",
- absl::FormatTime(fmt, mar13.post, nyc));
- EXPECT_EQ(mar13.pre, absl::FromDateTime(2011, 3, 13, 2, 15, 0, nyc));
+ absl::FormatTime(fmt, mar_ci.post, nyc));
+ EXPECT_EQ(mar_ci.trans, absl::FromCivil(mar13, nyc));
// A Fall DST transition, when civil times are repeated and
// we prefer the earlier of the possible interpretations of an
// ambiguous time.
- absl::TimeConversion nov06 = ConvertDateTime(2011, 11, 6, 1, 15, 0, nyc);
- EXPECT_FALSE(nov06.normalized);
- EXPECT_EQ(absl::TimeConversion::REPEATED, nov06.kind);
+ absl::CivilSecond nov06(2011, 11, 6, 1, 15, 0);
+ const auto nov06_ci = nyc.At(nov06);
+ EXPECT_EQ(absl::TimeZone::TimeInfo::REPEATED, nov06_ci.kind);
EXPECT_EQ("Sun, 6 Nov 2011 01:15:00 -0400 (EDT)",
- absl::FormatTime(fmt, nov06.pre, nyc));
+ absl::FormatTime(fmt, nov06_ci.pre, nyc));
EXPECT_EQ("Sun, 6 Nov 2011 01:00:00 -0500 (EST)",
- absl::FormatTime(fmt, nov06.trans, nyc));
+ absl::FormatTime(fmt, nov06_ci.trans, nyc));
EXPECT_EQ("Sun, 6 Nov 2011 01:15:00 -0500 (EST)",
- absl::FormatTime(fmt, nov06.post, nyc));
- EXPECT_EQ(nov06.pre, absl::FromDateTime(2011, 11, 6, 1, 15, 0, nyc));
+ absl::FormatTime(fmt, nov06_ci.post, nyc));
+ EXPECT_EQ(nov06_ci.pre, absl::FromCivil(nov06, nyc));
// Check that (time_t) -1 is handled correctly.
- absl::TimeConversion minus1 = ConvertDateTime(1969, 12, 31, 18, 59, 59, nyc);
- EXPECT_FALSE(minus1.normalized);
- EXPECT_EQ(absl::TimeConversion::UNIQUE, minus1.kind);
- EXPECT_EQ(-1, absl::ToTimeT(minus1.pre));
+ absl::CivilSecond minus1(1969, 12, 31, 18, 59, 59);
+ const auto minus1_cl = nyc.At(minus1);
+ EXPECT_EQ(absl::TimeZone::TimeInfo::UNIQUE, minus1_cl.kind);
+ EXPECT_EQ(-1, absl::ToTimeT(minus1_cl.pre));
EXPECT_EQ("Wed, 31 Dec 1969 18:59:59 -0500 (EST)",
- absl::FormatTime(fmt, minus1.pre, nyc));
+ absl::FormatTime(fmt, minus1_cl.pre, nyc));
EXPECT_EQ("Wed, 31 Dec 1969 23:59:59 +0000 (UTC)",
- absl::FormatTime(fmt, minus1.pre, utc));
+ absl::FormatTime(fmt, minus1_cl.pre, absl::UTCTimeZone()));
}
-// FromDateTime(year, mon, day, hour, min, sec, UTCTimeZone()) has
-// a specialized fastpath implementation which we exercise here.
-TEST(Time, FromDateTimeUTC) {
+// FromCivil(CivilSecond(year, mon, day, hour, min, sec), UTCTimeZone())
+// has a specialized fastpath implementation, which we exercise here.
+TEST(Time, FromCivilUTC) {
const absl::TimeZone utc = absl::UTCTimeZone();
const std::string fmt = "%a, %e %b %Y %H:%M:%S %z (%Z)";
const int kMax = std::numeric_limits<int>::max();
@@ -618,65 +635,36 @@ TEST(Time, FromDateTimeUTC) {
absl::Time t;
// 292091940881 is the last positive year to use the fastpath.
- t = absl::FromDateTime(292091940881, kMax, kMax, kMax, kMax, kMax, utc);
+ t = absl::FromCivil(
+ absl::CivilSecond(292091940881, kMax, kMax, kMax, kMax, kMax), utc);
EXPECT_EQ("Fri, 25 Nov 292277026596 12:21:07 +0000 (UTC)",
absl::FormatTime(fmt, t, utc));
- t = absl::FromDateTime(292091940882, kMax, kMax, kMax, kMax, kMax, utc);
- EXPECT_EQ("infinite-future", absl::FormatTime(fmt, t, utc)); // no overflow
- t = absl::FromDateTime(
- std::numeric_limits<int64_t>::max(), kMax, kMax, kMax, kMax, kMax, utc);
+ t = absl::FromCivil(
+ absl::CivilSecond(292091940882, kMax, kMax, kMax, kMax, kMax), utc);
EXPECT_EQ("infinite-future", absl::FormatTime(fmt, t, utc)); // no overflow
// -292091936940 is the last negative year to use the fastpath.
- t = absl::FromDateTime(-292091936940, kMin, kMin, kMin, kMin, kMin, utc);
+ t = absl::FromCivil(
+ absl::CivilSecond(-292091936940, kMin, kMin, kMin, kMin, kMin), utc);
EXPECT_EQ("Fri, 1 Nov -292277022657 10:37:52 +0000 (UTC)",
absl::FormatTime(fmt, t, utc));
- t = absl::FromDateTime(-292091936941, kMin, kMin, kMin, kMin, kMin, utc);
+ t = absl::FromCivil(
+ absl::CivilSecond(-292091936941, kMin, kMin, kMin, kMin, kMin), utc);
EXPECT_EQ("infinite-past", absl::FormatTime(fmt, t, utc)); // no underflow
- t = absl::FromDateTime(
- std::numeric_limits<int64_t>::min(), kMin, kMin, kMin, kMin, kMin, utc);
- EXPECT_EQ("infinite-past", absl::FormatTime(fmt, t, utc)); // no overflow
// Check that we're counting leap years correctly.
- t = absl::FromDateTime(1900, 2, 28, 23, 59, 59, utc);
+ t = absl::FromCivil(absl::CivilSecond(1900, 2, 28, 23, 59, 59), utc);
EXPECT_EQ("Wed, 28 Feb 1900 23:59:59 +0000 (UTC)",
absl::FormatTime(fmt, t, utc));
- t = absl::FromDateTime(1900, 3, 1, 0, 0, 0, utc);
+ t = absl::FromCivil(absl::CivilSecond(1900, 3, 1, 0, 0, 0), utc);
EXPECT_EQ("Thu, 1 Mar 1900 00:00:00 +0000 (UTC)",
absl::FormatTime(fmt, t, utc));
- t = absl::FromDateTime(2000, 2, 29, 23, 59, 59, utc);
+ t = absl::FromCivil(absl::CivilSecond(2000, 2, 29, 23, 59, 59), utc);
EXPECT_EQ("Tue, 29 Feb 2000 23:59:59 +0000 (UTC)",
absl::FormatTime(fmt, t, utc));
- t = absl::FromDateTime(2000, 3, 1, 0, 0, 0, utc);
+ t = absl::FromCivil(absl::CivilSecond(2000, 3, 1, 0, 0, 0), utc);
EXPECT_EQ("Wed, 1 Mar 2000 00:00:00 +0000 (UTC)",
absl::FormatTime(fmt, t, utc));
-
- // Check normalization.
- const std::string ymdhms = "%Y-%m-%d %H:%M:%S";
- t = absl::FromDateTime(2015, 1, 1, 0, 0, 60, utc);
- EXPECT_EQ("2015-01-01 00:01:00", absl::FormatTime(ymdhms, t, utc));
- t = absl::FromDateTime(2015, 1, 1, 0, 60, 0, utc);
- EXPECT_EQ("2015-01-01 01:00:00", absl::FormatTime(ymdhms, t, utc));
- t = absl::FromDateTime(2015, 1, 1, 24, 0, 0, utc);
- EXPECT_EQ("2015-01-02 00:00:00", absl::FormatTime(ymdhms, t, utc));
- t = absl::FromDateTime(2015, 1, 32, 0, 0, 0, utc);
- EXPECT_EQ("2015-02-01 00:00:00", absl::FormatTime(ymdhms, t, utc));
- t = absl::FromDateTime(2015, 13, 1, 0, 0, 0, utc);
- EXPECT_EQ("2016-01-01 00:00:00", absl::FormatTime(ymdhms, t, utc));
- t = absl::FromDateTime(2015, 13, 32, 60, 60, 60, utc);
- EXPECT_EQ("2016-02-03 13:01:00", absl::FormatTime(ymdhms, t, utc));
- t = absl::FromDateTime(2015, 1, 1, 0, 0, -1, utc);
- EXPECT_EQ("2014-12-31 23:59:59", absl::FormatTime(ymdhms, t, utc));
- t = absl::FromDateTime(2015, 1, 1, 0, -1, 0, utc);
- EXPECT_EQ("2014-12-31 23:59:00", absl::FormatTime(ymdhms, t, utc));
- t = absl::FromDateTime(2015, 1, 1, -1, 0, 0, utc);
- EXPECT_EQ("2014-12-31 23:00:00", absl::FormatTime(ymdhms, t, utc));
- t = absl::FromDateTime(2015, 1, -1, 0, 0, 0, utc);
- EXPECT_EQ("2014-12-30 00:00:00", absl::FormatTime(ymdhms, t, utc));
- t = absl::FromDateTime(2015, -1, 1, 0, 0, 0, utc);
- EXPECT_EQ("2014-11-01 00:00:00", absl::FormatTime(ymdhms, t, utc));
- t = absl::FromDateTime(2015, -1, -1, -1, -1, -1, utc);
- EXPECT_EQ("2014-10-29 22:58:59", absl::FormatTime(ymdhms, t, utc));
}
TEST(Time, ToTM) {
@@ -684,8 +672,10 @@ TEST(Time, ToTM) {
// Compares the results of ToTM() to gmtime_r() for lots of times over the
// course of a few days.
- const absl::Time start = absl::FromDateTime(2014, 1, 2, 3, 4, 5, utc);
- const absl::Time end = absl::FromDateTime(2014, 1, 5, 3, 4, 5, utc);
+ const absl::Time start =
+ absl::FromCivil(absl::CivilSecond(2014, 1, 2, 3, 4, 5), utc);
+ const absl::Time end =
+ absl::FromCivil(absl::CivilSecond(2014, 1, 5, 3, 4, 5), utc);
for (absl::Time t = start; t < end; t += absl::Seconds(30)) {
const struct tm tm_bt = ToTM(t, utc);
const time_t tt = absl::ToTimeT(t);
@@ -711,12 +701,12 @@ TEST(Time, ToTM) {
// Checks that the tm_isdst field is correct when in standard time.
const absl::TimeZone nyc =
absl::time_internal::LoadTimeZone("America/New_York");
- absl::Time t = absl::FromDateTime(2014, 3, 1, 0, 0, 0, nyc);
+ absl::Time t = absl::FromCivil(absl::CivilSecond(2014, 3, 1, 0, 0, 0), nyc);
struct tm tm = ToTM(t, nyc);
EXPECT_FALSE(tm.tm_isdst);
// Checks that the tm_isdst field is correct when in daylight time.
- t = absl::FromDateTime(2014, 4, 1, 0, 0, 0, nyc);
+ t = absl::FromCivil(absl::CivilSecond(2014, 4, 1, 0, 0, 0), nyc);
tm = ToTM(t, nyc);
EXPECT_TRUE(tm.tm_isdst);
@@ -808,8 +798,8 @@ TEST(Time, TMRoundTrip) {
absl::time_internal::LoadTimeZone("America/New_York");
// Test round-tripping across a skipped transition
- absl::Time start = absl::FromDateTime(2014, 3, 9, 0, 0, 0, nyc);
- absl::Time end = absl::FromDateTime(2014, 3, 9, 4, 0, 0, nyc);
+ absl::Time start = absl::FromCivil(absl::CivilHour(2014, 3, 9, 0), nyc);
+ absl::Time end = absl::FromCivil(absl::CivilHour(2014, 3, 9, 4), nyc);
for (absl::Time t = start; t < end; t += absl::Minutes(1)) {
struct tm tm = ToTM(t, nyc);
absl::Time rt = FromTM(tm, nyc);
@@ -817,8 +807,8 @@ TEST(Time, TMRoundTrip) {
}
// Test round-tripping across an ambiguous transition
- start = absl::FromDateTime(2014, 11, 2, 0, 0, 0, nyc);
- end = absl::FromDateTime(2014, 11, 2, 4, 0, 0, nyc);
+ start = absl::FromCivil(absl::CivilHour(2014, 11, 2, 0), nyc);
+ end = absl::FromCivil(absl::CivilHour(2014, 11, 2, 4), nyc);
for (absl::Time t = start; t < end; t += absl::Minutes(1)) {
struct tm tm = ToTM(t, nyc);
absl::Time rt = FromTM(tm, nyc);
@@ -826,8 +816,8 @@ TEST(Time, TMRoundTrip) {
}
// Test round-tripping of unique instants crossing a day boundary
- start = absl::FromDateTime(2014, 6, 27, 22, 0, 0, nyc);
- end = absl::FromDateTime(2014, 6, 28, 4, 0, 0, nyc);
+ start = absl::FromCivil(absl::CivilHour(2014, 6, 27, 22), nyc);
+ end = absl::FromCivil(absl::CivilHour(2014, 6, 28, 4), nyc);
for (absl::Time t = start; t < end; t += absl::Minutes(1)) {
struct tm tm = ToTM(t, nyc);
absl::Time rt = FromTM(tm, nyc);
@@ -980,27 +970,27 @@ TEST(Time, ConversionSaturation) {
EXPECT_EQ(min_timespec_sec, ts.tv_sec);
EXPECT_EQ(0, ts.tv_nsec);
- // Checks how Time::In() saturates on infinities.
- absl::Time::Breakdown bd = absl::InfiniteFuture().In(utc);
- ABSL_INTERNAL_EXPECT_TIME(bd, std::numeric_limits<int64_t>::max(), 12, 31, 23,
+ // Checks how TimeZone::At() saturates on infinities.
+ auto ci = utc.At(absl::InfiniteFuture());
+ EXPECT_CIVIL_INFO(ci, std::numeric_limits<int64_t>::max(), 12, 31, 23,
59, 59, 0, false);
- EXPECT_EQ(absl::InfiniteDuration(), bd.subsecond);
- EXPECT_EQ(4, bd.weekday); // Thursday
- EXPECT_EQ(365, bd.yearday);
- EXPECT_STREQ("-00", bd.zone_abbr); // artifact of absl::Time::In()
- bd = absl::InfinitePast().In(utc);
- ABSL_INTERNAL_EXPECT_TIME(bd, std::numeric_limits<int64_t>::min(), 1, 1, 0, 0,
+ EXPECT_EQ(absl::InfiniteDuration(), ci.subsecond);
+ EXPECT_EQ(absl::Weekday::thursday, absl::GetWeekday(absl::CivilDay(ci.cs)));
+ EXPECT_EQ(365, absl::GetYearDay(absl::CivilDay(ci.cs)));
+ EXPECT_STREQ("-00", ci.zone_abbr); // artifact of TimeZone::At()
+ ci = utc.At(absl::InfinitePast());
+ EXPECT_CIVIL_INFO(ci, std::numeric_limits<int64_t>::min(), 1, 1, 0, 0,
0, 0, false);
- EXPECT_EQ(-absl::InfiniteDuration(), bd.subsecond);
- EXPECT_EQ(7, bd.weekday); // Sunday
- EXPECT_EQ(1, bd.yearday);
- EXPECT_STREQ("-00", bd.zone_abbr); // artifact of absl::Time::In()
+ EXPECT_EQ(-absl::InfiniteDuration(), ci.subsecond);
+ EXPECT_EQ(absl::Weekday::sunday, absl::GetWeekday(absl::CivilDay(ci.cs)));
+ EXPECT_EQ(1, absl::GetYearDay(absl::CivilDay(ci.cs)));
+ EXPECT_STREQ("-00", ci.zone_abbr); // artifact of TimeZone::At()
// Approach the maximal Time value from below.
- t = absl::FromDateTime(292277026596, 12, 4, 15, 30, 6, utc);
+ t = absl::FromCivil(absl::CivilSecond(292277026596, 12, 4, 15, 30, 6), utc);
EXPECT_EQ("292277026596-12-04T15:30:06+00:00",
absl::FormatTime(absl::RFC3339_full, t, utc));
- t = absl::FromDateTime(292277026596, 12, 4, 15, 30, 7, utc);
+ t = absl::FromCivil(absl::CivilSecond(292277026596, 12, 4, 15, 30, 7), utc);
EXPECT_EQ("292277026596-12-04T15:30:07+00:00",
absl::FormatTime(absl::RFC3339_full, t, utc));
EXPECT_EQ(
@@ -1008,21 +998,21 @@ TEST(Time, ConversionSaturation) {
// Checks that we can also get the maximal Time value for a far-east zone.
const absl::TimeZone plus14 = absl::FixedTimeZone(14 * 60 * 60);
- t = absl::FromDateTime(292277026596, 12, 5, 5, 30, 7, plus14);
+ t = absl::FromCivil(absl::CivilSecond(292277026596, 12, 5, 5, 30, 7), plus14);
EXPECT_EQ("292277026596-12-05T05:30:07+14:00",
absl::FormatTime(absl::RFC3339_full, t, plus14));
EXPECT_EQ(
absl::UnixEpoch() + absl::Seconds(std::numeric_limits<int64_t>::max()), t);
// One second later should push us to infinity.
- t = absl::FromDateTime(292277026596, 12, 4, 15, 30, 8, utc);
+ t = absl::FromCivil(absl::CivilSecond(292277026596, 12, 4, 15, 30, 8), utc);
EXPECT_EQ("infinite-future", absl::FormatTime(absl::RFC3339_full, t, utc));
// Approach the minimal Time value from above.
- t = absl::FromDateTime(-292277022657, 1, 27, 8, 29, 53, utc);
+ t = absl::FromCivil(absl::CivilSecond(-292277022657, 1, 27, 8, 29, 53), utc);
EXPECT_EQ("-292277022657-01-27T08:29:53+00:00",
absl::FormatTime(absl::RFC3339_full, t, utc));
- t = absl::FromDateTime(-292277022657, 1, 27, 8, 29, 52, utc);
+ t = absl::FromCivil(absl::CivilSecond(-292277022657, 1, 27, 8, 29, 52), utc);
EXPECT_EQ("-292277022657-01-27T08:29:52+00:00",
absl::FormatTime(absl::RFC3339_full, t, utc));
EXPECT_EQ(
@@ -1030,14 +1020,15 @@ TEST(Time, ConversionSaturation) {
// Checks that we can also get the minimal Time value for a far-west zone.
const absl::TimeZone minus12 = absl::FixedTimeZone(-12 * 60 * 60);
- t = absl::FromDateTime(-292277022657, 1, 26, 20, 29, 52, minus12);
+ t = absl::FromCivil(absl::CivilSecond(-292277022657, 1, 26, 20, 29, 52),
+ minus12);
EXPECT_EQ("-292277022657-01-26T20:29:52-12:00",
absl::FormatTime(absl::RFC3339_full, t, minus12));
EXPECT_EQ(
absl::UnixEpoch() + absl::Seconds(std::numeric_limits<int64_t>::min()), t);
// One second before should push us to -infinity.
- t = absl::FromDateTime(-292277022657, 1, 27, 8, 29, 51, utc);
+ t = absl::FromCivil(absl::CivilSecond(-292277022657, 1, 27, 8, 29, 51), utc);
EXPECT_EQ("infinite-past", absl::FormatTime(absl::RFC3339_full, t, utc));
}
@@ -1051,38 +1042,97 @@ TEST(Time, ExtendedConversionSaturation) {
absl::time_internal::LoadTimeZone("America/New_York");
const absl::Time max =
absl::FromUnixSeconds(std::numeric_limits<int64_t>::max());
- absl::Time::Breakdown bd;
+ absl::TimeZone::CivilInfo ci;
absl::Time t;
// The maximal time converted in each zone.
- bd = max.In(syd);
- ABSL_INTERNAL_EXPECT_TIME(bd, 292277026596, 12, 5, 2, 30, 7, 39600, true);
- t = absl::FromDateTime(292277026596, 12, 5, 2, 30, 7, syd);
+ ci = syd.At(max);
+ EXPECT_CIVIL_INFO(ci, 292277026596, 12, 5, 2, 30, 7, 39600, true);
+ t = absl::FromCivil(absl::CivilSecond(292277026596, 12, 5, 2, 30, 7), syd);
EXPECT_EQ(max, t);
- bd = max.In(nyc);
- ABSL_INTERNAL_EXPECT_TIME(bd, 292277026596, 12, 4, 10, 30, 7, -18000, false);
- t = absl::FromDateTime(292277026596, 12, 4, 10, 30, 7, nyc);
+ ci = nyc.At(max);
+ EXPECT_CIVIL_INFO(ci, 292277026596, 12, 4, 10, 30, 7, -18000, false);
+ t = absl::FromCivil(absl::CivilSecond(292277026596, 12, 4, 10, 30, 7), nyc);
EXPECT_EQ(max, t);
// One second later should push us to infinity.
- t = absl::FromDateTime(292277026596, 12, 5, 2, 30, 8, syd);
+ t = absl::FromCivil(absl::CivilSecond(292277026596, 12, 5, 2, 30, 8), syd);
EXPECT_EQ(absl::InfiniteFuture(), t);
- t = absl::FromDateTime(292277026596, 12, 4, 10, 30, 8, nyc);
+ t = absl::FromCivil(absl::CivilSecond(292277026596, 12, 4, 10, 30, 8), nyc);
EXPECT_EQ(absl::InfiniteFuture(), t);
// And we should stick there.
- t = absl::FromDateTime(292277026596, 12, 5, 2, 30, 9, syd);
+ t = absl::FromCivil(absl::CivilSecond(292277026596, 12, 5, 2, 30, 9), syd);
EXPECT_EQ(absl::InfiniteFuture(), t);
- t = absl::FromDateTime(292277026596, 12, 4, 10, 30, 9, nyc);
+ t = absl::FromCivil(absl::CivilSecond(292277026596, 12, 4, 10, 30, 9), nyc);
EXPECT_EQ(absl::InfiniteFuture(), t);
// All the way up to a saturated date/time, without overflow.
- t = absl::FromDateTime(
- std::numeric_limits<int64_t>::max(), 12, 31, 23, 59, 59, syd);
+ t = absl::FromCivil(absl::CivilSecond::max(), syd);
EXPECT_EQ(absl::InfiniteFuture(), t);
- t = absl::FromDateTime(
- std::numeric_limits<int64_t>::max(), 12, 31, 23, 59, 59, nyc);
+ t = absl::FromCivil(absl::CivilSecond::max(), nyc);
EXPECT_EQ(absl::InfiniteFuture(), t);
}
+TEST(Time, FromCivilAlignment) {
+ const absl::TimeZone utc = absl::UTCTimeZone();
+ const absl::CivilSecond cs(2015, 2, 3, 4, 5, 6);
+ absl::Time t = absl::FromCivil(cs, utc);
+ EXPECT_EQ("2015-02-03T04:05:06+00:00", absl::FormatTime(t, utc));
+ t = absl::FromCivil(absl::CivilMinute(cs), utc);
+ EXPECT_EQ("2015-02-03T04:05:00+00:00", absl::FormatTime(t, utc));
+ t = absl::FromCivil(absl::CivilHour(cs), utc);
+ EXPECT_EQ("2015-02-03T04:00:00+00:00", absl::FormatTime(t, utc));
+ t = absl::FromCivil(absl::CivilDay(cs), utc);
+ EXPECT_EQ("2015-02-03T00:00:00+00:00", absl::FormatTime(t, utc));
+ t = absl::FromCivil(absl::CivilMonth(cs), utc);
+ EXPECT_EQ("2015-02-01T00:00:00+00:00", absl::FormatTime(t, utc));
+ t = absl::FromCivil(absl::CivilYear(cs), utc);
+ EXPECT_EQ("2015-01-01T00:00:00+00:00", absl::FormatTime(t, utc));
+}
+
+TEST(Time, LegacyDateTime) {
+ const absl::TimeZone utc = absl::UTCTimeZone();
+ const std::string ymdhms = "%Y-%m-%d %H:%M:%S";
+ const int kMax = std::numeric_limits<int>::max();
+ const int kMin = std::numeric_limits<int>::min();
+ absl::Time t;
+
+ t = absl::FromDateTime(std::numeric_limits<absl::civil_year_t>::max(),
+ kMax, kMax, kMax, kMax, kMax, utc);
+ EXPECT_EQ("infinite-future",
+ absl::FormatTime(ymdhms, t, utc)); // no overflow
+ t = absl::FromDateTime(std::numeric_limits<absl::civil_year_t>::min(),
+ kMin, kMin, kMin, kMin, kMin, utc);
+ EXPECT_EQ("infinite-past",
+ absl::FormatTime(ymdhms, t, utc)); // no overflow
+
+ // Check normalization.
+ EXPECT_TRUE(absl::ConvertDateTime(2013, 10, 32, 8, 30, 0, utc).normalized);
+ t = absl::FromDateTime(2015, 1, 1, 0, 0, 60, utc);
+ EXPECT_EQ("2015-01-01 00:01:00", absl::FormatTime(ymdhms, t, utc));
+ t = absl::FromDateTime(2015, 1, 1, 0, 60, 0, utc);
+ EXPECT_EQ("2015-01-01 01:00:00", absl::FormatTime(ymdhms, t, utc));
+ t = absl::FromDateTime(2015, 1, 1, 24, 0, 0, utc);
+ EXPECT_EQ("2015-01-02 00:00:00", absl::FormatTime(ymdhms, t, utc));
+ t = absl::FromDateTime(2015, 1, 32, 0, 0, 0, utc);
+ EXPECT_EQ("2015-02-01 00:00:00", absl::FormatTime(ymdhms, t, utc));
+ t = absl::FromDateTime(2015, 13, 1, 0, 0, 0, utc);
+ EXPECT_EQ("2016-01-01 00:00:00", absl::FormatTime(ymdhms, t, utc));
+ t = absl::FromDateTime(2015, 13, 32, 60, 60, 60, utc);
+ EXPECT_EQ("2016-02-03 13:01:00", absl::FormatTime(ymdhms, t, utc));
+ t = absl::FromDateTime(2015, 1, 1, 0, 0, -1, utc);
+ EXPECT_EQ("2014-12-31 23:59:59", absl::FormatTime(ymdhms, t, utc));
+ t = absl::FromDateTime(2015, 1, 1, 0, -1, 0, utc);
+ EXPECT_EQ("2014-12-31 23:59:00", absl::FormatTime(ymdhms, t, utc));
+ t = absl::FromDateTime(2015, 1, 1, -1, 0, 0, utc);
+ EXPECT_EQ("2014-12-31 23:00:00", absl::FormatTime(ymdhms, t, utc));
+ t = absl::FromDateTime(2015, 1, -1, 0, 0, 0, utc);
+ EXPECT_EQ("2014-12-30 00:00:00", absl::FormatTime(ymdhms, t, utc));
+ t = absl::FromDateTime(2015, -1, 1, 0, 0, 0, utc);
+ EXPECT_EQ("2014-11-01 00:00:00", absl::FormatTime(ymdhms, t, utc));
+ t = absl::FromDateTime(2015, -1, -1, -1, -1, -1, utc);
+ EXPECT_EQ("2014-10-29 22:58:59", absl::FormatTime(ymdhms, t, utc));
+}
+
} // namespace