• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..03-May-2022-

cmake/H06-Feb-2018-4437

examples/H03-May-2022-272130

include/cctz/H06-Feb-2018-1,281593

src/H06-Feb-2018-8,2876,378

testdata/H06-Feb-2018-697688

.gitignoreH A D06-Feb-201894 54

BUILDH A D06-Feb-20183.7 KiB183162

CONTRIBUTING.mdH A D06-Feb-20181.4 KiB3023

MakefileH A D06-Feb-20183 KiB10654

README.mdH A D06-Feb-20189.5 KiB227183

WORKSPACEH A D06-Feb-20181,000 4541

README.md

1This is not an official Google product.
2
3# Overview
4
5CCTZ contains two libraries that cooperate with `<chrono>` to give C++
6programmers all the necessary tools for computing with dates, times, and time
7zones in a simple and correct manner. The libraries in CCTZ are:
8
9*   **The Civil-Time Library** &mdash; This is a header-only library that
10    supports computing with human-scale time, such as dates (which are
11    represented by the `cctz::civil_day` class). This library is declared in
12    [`include/cctz/civil_time.h`](https://github.com/google/cctz/blob/master/include/cctz/civil_time.h).
13*   **The Time-Zone Library** &mdash; This library uses the IANA time zone
14    database that is installed on the system to convert between *absolute time*
15    and *civil time*. This library is declared in
16    [`include/cctz/time_zone.h`](https://github.com/google/cctz/blob/master/include/cctz/time_zone.h).
17
18These libraries are currently known to work on **Linux**, **Mac OS X**, and
19**Android**.
20
21They will also work on **Windows** if you install the zoneinfo files. We are
22interested, though, in an implementation of the cctz::TimeZoneIf interface that
23calls the Windows time APIs instead. Please contact us if you're interested in
24contributing.
25
26# Getting Started
27
28CCTZ is best built and tested using the [Bazel](http://bazel.io) build system
29and the [Google Test](https://github.com/google/googletest) framework. (There is
30also a simple [`Makefile`](https://github.com/google/cctz/blob/master/Makefile)
31and a
32[`CMakeLists.txt`](https://github.com/google/cctz/blob/master/CMakeLists.txt)
33that should work if you're unable to use Bazel.)
34
351.  Download/install
36    [Bazel](https://docs.bazel.build/versions/master/install.html)
372.  Get the cctz source: `git clone https://github.com/google/cctz.git` then `cd
38    cctz`
393.  Build cctz and run the tests: `bazel test :all`
40
41With CMake:
42
431.  Make sure you have CMake >= 2.8.12 installed.
442.  Get the cctz source: `git clone https://github.com/google/cctz.git` then `cd
45    cctz`.
463.  Build cctz so that is can be used by shared libraries and run the tests (use
47    `-DBUILD_TESTING=OFF` to skip the tests):
48
49        mkdir mybuild
50        cd mybuild
51        cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_POSITION_INDEPENDENT_CODE=ON ..
52        cmake --build . --config Release
53        ctest
54
554.  Use in your CMake-based project with:
56
57    ```cmake
58    add_subdirectory(cctz)
59    add_executable(mytarget file.cc)
60    target_link_libraries(mytarget cctz::cctz)
61    ```
62
63Note: When using CCTZ in your own project, you might find it easiest to compile
64the sources using your existing build system.
65
66Next Steps:
67
681.  See the documentation for the libraries in CCTZ:
69    *   Civil Time:
70        [`include/cctz/civil_time.h`](https://github.com/google/cctz/blob/master/include/cctz/civil_time.h)
71    *   Time Zone:
72        [`include/cctz/time_zone.h`](https://github.com/google/cctz/blob/master/include/cctz/time_zone.h)
732.  Look at the examples in https://github.com/google/cctz/tree/master/examples
743.  Join our mailing list to ask questions and keep informed of changes:
75    *   https://groups.google.com/forum/#!forum/cctz
76
77# Fundamental Concepts
78
79*[The concepts presented here describe general truths about the problem domain
80and are library and language agnostic. An understanding of these concepts helps
81the programmer correctly reason about even the most-complicated time-programming
82challenges and produce the simplest possible solutions.]*
83
84There are two main ways to think about time in a computer program: as *absolute
85time*, and as *civil time*. Both have their uses and it is important to
86understand when each is appropriate. Absolute and civil times may be converted
87back and forth using a *time zone* &mdash; this is the only way to correctly
88convert between them. Let us now look more deeply at the three main concepts of
89time programming: Absolute Time, Civil Time, and Time Zone.
90
91*Absolute time* uniquely and universally represents a specific instant in time.
92It has no notion of calendars, or dates, or times of day. Instead, it is a
93measure of the passage of real time, typically as a simple count of ticks since
94some epoch. Absolute times are independent of all time zones and do not suffer
95from human-imposed complexities such as daylight-saving time (DST). Many C++
96types exist to represent absolute times, classically `time_t` and more recently
97`std::chrono::time_point`.
98
99*Civil time* is the legally recognized representation of time for ordinary
100affairs (cf. http://www.merriam-webster.com/dictionary/civil). It is a
101human-scale representation of time that consists of the six fields &mdash; year,
102month, day, hour, minute, and second (sometimes shortened to "YMDHMS") &mdash;
103and it follows the rules of the Proleptic Gregorian Calendar, with 24-hour days
104divided into 60-minute hours and 60-second minutes. Like absolute times, civil
105times are also independent of all time zones and their related complexities
106(e.g., DST). While `std::tm` contains the six civil-time fields (YMDHMS), plus a
107few more, it does not have behavior to enforce the rules of civil time.
108
109*Time zones* are geo-political regions within which human-defined rules are
110shared to convert between the absolute-time and civil-time domains. A time
111zone's rules include things like the region's offset from the UTC time standard,
112daylight-saving adjustments, and short abbreviation strings. Time zones often
113have a history of disparate rules that apply only for certain periods, because
114the rules may change at the whim of a region's local government. For this
115reason, time-zone rules are usually compiled into data snapshots that are used
116at runtime to perform conversions between absolute and civil times. There is
117currently no C++ standard library supporting arbitrary time zones.
118
119In order for programmers to reason about and program applications that correctly
120deal with these concepts, they must have a library that correctly implements the
121above concepts. CCTZ adds to the existing C++11 `<chrono>` library to fully
122implement the above concepts.
123
124*   Absolute time &mdash; This is implemented by the existing C++11
125    [`<chrono>`](http://en.cppreference.com/w/cpp/chrono) library without
126    modification. For example, an absolute point in time is represented by a
127    `std::chrono::time_point`.
128*   Civil time &mdash; This is implemented by the
129    [`include/cctz/civil_time.h`](https://github.com/google/cctz/blob/master/include/cctz/civil_time.h)
130    library that is provided as part of CCTZ. For example, a "date" is
131    represented by a `cctz::civil_day`.
132*   Time zone &mdash; This is implemented by the
133    [`include/cctz/time_zone.h`](https://github.com/google/cctz/blob/master/include/cctz/time_zone.h)
134    library that is provided as part of CCTZ. For example, a time zone is
135    represented by an instance of the class `cctz::time_zone`.
136
137# Examples
138
139## Hello February 2016
140
141This "hello world" example uses a for-loop to iterate the days from the first of
142February until the month of March. Each day is streamed to output, and if the
143day happens to be the 29th, we also output the day of the week.
144
145```
146#include <iostream>
147#include "cctz/civil_time.h"
148
149int main() {
150  for (cctz::civil_day d(2016, 2, 1); d < cctz::civil_month(2016, 3); ++d) {
151    std::cout << "Hello " << d;
152    if (d.day() == 29) {
153      std::cout << " <- leap day is a " << cctz::get_weekday(d);
154    }
155    std::cout << "\n";
156  }
157}
158```
159
160The output of the above program is
161
162```
163Hello 2016-02-01
164Hello 2016-02-02
165Hello 2016-02-03
166[...]
167Hello 2016-02-27
168Hello 2016-02-28
169Hello 2016-02-29 <- leap day is a Monday
170```
171
172## One giant leap
173
174This example shows how to use all three libraries (`<chrono>`, civil time, and
175time zone) together. In this example, we know that viewers in New York watched
176Neil Armstrong first walk on the moon on July 20, 1969 at 10:56 PM. But we'd
177like to see what time it was for our friend watching in Sydney, Australia.
178
179```
180#include <iostream>
181#include "cctz/civil_time.h"
182#include "cctz/time_zone.h"
183
184int main() {
185  cctz::time_zone nyc;
186  cctz::load_time_zone("America/New_York", &nyc);
187
188  // Converts the input civil time in NYC to an absolute time.
189  const auto moon_walk =
190    cctz::convert(cctz::civil_second(1969, 7, 20, 22, 56, 0), nyc);
191
192  std::cout << "Moon walk in NYC: "
193            << cctz::format("%Y-%m-%d %H:%M:%S %Ez\n", moon_walk, nyc);
194
195  cctz::time_zone syd;
196  if (!cctz::load_time_zone("Australia/Sydney", &syd)) return -1;
197  std::cout << "Moon walk in SYD: "
198            << cctz::format("%Y-%m-%d %H:%M:%S %Ez\n", moon_walk, syd);
199}
200```
201
202The output of the above program is
203
204```
205Moon walk in NYC: 1969-07-20 22:56:00 -04:00
206Moon walk in SYD: 1969-07-21 12:56:00 +10:00
207```
208
209This example shows that the absolute time (the `std::chrono::time_point`) of the
210first walk on the moon is the same no matter the time zone of the viewer (the
211same time point is used in both calls to `format()`). The only difference is the
212time zone in which the `moon_walk` time point is rendered. And in this case we
213can see that our friend in Sydney was probably eating lunch while watching that
214historic event.
215
216# References
217
218*   CCTZ [FAQ](https://github.com/google/cctz/wiki/FAQ)
219*   See also the [Time Programming Fundamentals](https://youtu.be/2rnIHsqABfM)
220    talk from CppCon 2015 ([slides available here](http://goo.gl/ofof4N)). This
221    talk mostly describes the older CCTZ v1 API, but the *concepts* are the
222    same.
223*   ISO C++ proposal to standardize the Civil-Time Library:
224    https://github.com/devjgm/papers/blob/master/d0215r1.md
225*   ISO C++ proposal to standardize the Time-Zone Library:
226    https://github.com/devjgm/papers/blob/master/d0216r1.md
227