• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===-- Collection of constants for time functions --------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #ifndef LLVM_LIBC_SRC_TIME_TIME_CONSTANTS_H
10 #define LLVM_LIBC_SRC_TIME_TIME_CONSTANTS_H
11 
12 #include "hdr/types/time_t.h"
13 #include "src/__support/CPP/array.h"
14 #include "src/__support/CPP/string_view.h"
15 #include <stdint.h>
16 
17 namespace LIBC_NAMESPACE_DECL {
18 namespace time_constants {
19 
20 enum Month : int {
21   JANUARY = 0,
22   FEBRUARY,
23   MARCH,
24   APRIL,
25   MAY,
26   JUNE,
27   JULY,
28   AUGUST,
29   SEPTEMBER,
30   OCTOBER,
31   NOVEMBER,
32   DECEMBER
33 };
34 
35 enum WeekDay : int {
36   SUNDAY = 0,
37   MONDAY,
38   TUESDAY,
39   WEDNESDAY,
40   THURSDAY,
41   FRIDAY,
42   SATURDAY
43 };
44 
45 constexpr int SECONDS_PER_MIN = 60;
46 constexpr int MINUTES_PER_HOUR = 60;
47 constexpr int HOURS_PER_DAY = 24;
48 constexpr int DAYS_PER_WEEK = 7;
49 constexpr int WEEKS_PER_YEAR = 52;
50 constexpr int MONTHS_PER_YEAR = 12;
51 constexpr int MAX_DAYS_PER_MONTH = 31;
52 constexpr int DAYS_PER_NON_LEAP_YEAR = 365;
53 constexpr int DAYS_PER_LEAP_YEAR = 366;
54 
55 constexpr int LAST_DAY_OF_NON_LEAP_YEAR = DAYS_PER_NON_LEAP_YEAR - 1;
56 constexpr int LAST_DAY_OF_LEAP_YEAR = DAYS_PER_LEAP_YEAR - 1;
57 
58 constexpr int SECONDS_PER_HOUR = SECONDS_PER_MIN * MINUTES_PER_HOUR;
59 constexpr int SECONDS_PER_DAY = SECONDS_PER_HOUR * HOURS_PER_DAY;
60 constexpr int NUMBER_OF_SECONDS_IN_LEAP_YEAR =
61     DAYS_PER_LEAP_YEAR * SECONDS_PER_DAY;
62 
63 constexpr int TIME_YEAR_BASE = 1900;
64 constexpr int EPOCH_YEAR = 1970;
65 constexpr int EPOCH_WEEK_DAY = 4;
66 
67 constexpr int ISO_FIRST_DAY_OF_YEAR = 3; // the 4th day of the year, 0-indexed.
68 
69 // For asctime the behavior is undefined if struct tm's tm_wday or tm_mon are
70 // not within the normal ranges as defined in <time.h>, or if struct tm's
71 // tm_year exceeds {INT_MAX}-1990, or if the below asctime_internal algorithm
72 // would attempt to generate more than 26 bytes of output (including the
73 // terminating null).
74 constexpr int ASCTIME_BUFFER_SIZE = 256;
75 constexpr int ASCTIME_MAX_BYTES = 26;
76 
77 /* 2000-03-01 (mod 400 year, immediately after feb29 */
78 constexpr int64_t SECONDS_UNTIL2000_MARCH_FIRST =
79     (946684800LL + SECONDS_PER_DAY * (31 + 29));
80 constexpr int WEEK_DAY_OF2000_MARCH_FIRST = 3;
81 
82 constexpr int DAYS_PER400_YEARS =
83     (DAYS_PER_NON_LEAP_YEAR * 400) + (400 / 4) - 3;
84 constexpr int DAYS_PER100_YEARS =
85     (DAYS_PER_NON_LEAP_YEAR * 100) + (100 / 4) - 1;
86 constexpr int DAYS_PER4_YEARS = (DAYS_PER_NON_LEAP_YEAR * 4) + 1;
87 
88 // The latest time that can be represented in this form is 03:14:07 UTC on
89 // Tuesday, 19 January 2038 (corresponding to 2,147,483,647 seconds since the
90 // start of the epoch). This means that systems using a 32-bit time_t type are
91 // susceptible to the Year 2038 problem.
92 constexpr int END_OF32_BIT_EPOCH_YEAR = 2038;
93 
94 constexpr time_t OUT_OF_RANGE_RETURN_VALUE = -1;
95 
96 constexpr cpp::array<cpp::string_view, DAYS_PER_WEEK> WEEK_DAY_NAMES = {
97     "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
98 
99 constexpr cpp::array<cpp::string_view, DAYS_PER_WEEK> WEEK_DAY_FULL_NAMES = {
100     "Sunday",   "Monday", "Tuesday", "Wednesday",
101     "Thursday", "Friday", "Saturday"};
102 
103 constexpr cpp::array<cpp::string_view, MONTHS_PER_YEAR> MONTH_NAMES = {
104     "Jan", "Feb", "Mar", "Apr", "May", "Jun",
105     "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
106 
107 constexpr cpp::array<cpp::string_view, MONTHS_PER_YEAR> MONTH_FULL_NAMES = {
108     "January", "February", "March",     "April",   "May",      "June",
109     "July",    "August",   "September", "October", "November", "December"};
110 
111 constexpr int NON_LEAP_YEAR_DAYS_IN_MONTH[] = {31, 28, 31, 30, 31, 30,
112                                                31, 31, 30, 31, 30, 31};
113 
114 } // namespace time_constants
115 } // namespace LIBC_NAMESPACE_DECL
116 
117 #endif // LLVM_LIBC_SRC_TIME_TIME_CONSTANTS_H
118