blob: 50f1c6a351718da28a3e4fde60f7870e91017536 [file] [log] [blame]
kumarashishg826308d2023-06-23 13:21:22 +00001// Copyright 2014 The PDFium Authors
Philip P. Moltmannd904c1e2018-03-19 09:26:45 -07002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6
7#include "core/fxcrt/cfx_datetime.h"
Haibo Huang49cc9302020-04-27 16:14:24 -07008
9#include "build/build_config.h"
kumarashishg826308d2023-06-23 13:21:22 +000010#include "core/fxcrt/fx_extension.h"
Philip P. Moltmannd904c1e2018-03-19 09:26:45 -070011#include "core/fxcrt/fx_system.h"
kumarashishg826308d2023-06-23 13:21:22 +000012#include "third_party/base/check.h"
13#include "third_party/base/span.h"
Philip P. Moltmannd904c1e2018-03-19 09:26:45 -070014
15namespace {
16
kumarashishg826308d2023-06-23 13:21:22 +000017constexpr uint8_t kDaysPerMonth[12] = {31, 28, 31, 30, 31, 30,
18 31, 31, 30, 31, 30, 31};
19constexpr uint8_t kDaysPerLeapMonth[12] = {31, 29, 31, 30, 31, 30,
20 31, 31, 30, 31, 30, 31};
21constexpr int32_t kDaysBeforeMonth[12] = {0, 31, 59, 90, 120, 151,
22 181, 212, 243, 273, 304, 334};
23constexpr int32_t kDaysBeforeLeapMonth[12] = {0, 31, 60, 91, 121, 152,
24 182, 213, 244, 274, 305, 335};
25constexpr int32_t kDaysPerYear = 365;
26constexpr int32_t kDaysPerLeapYear = 366;
Philip P. Moltmannd904c1e2018-03-19 09:26:45 -070027
28int32_t DaysBeforeMonthInYear(int32_t iYear, uint8_t iMonth) {
kumarashishg826308d2023-06-23 13:21:22 +000029 DCHECK(iYear != 0);
30 pdfium::span<const int32_t> p = FX_IsLeapYear(iYear)
31 ? pdfium::make_span(kDaysBeforeLeapMonth)
32 : pdfium::make_span(kDaysBeforeMonth);
33 // Note: iMonth is one-based.
Philip P. Moltmannd904c1e2018-03-19 09:26:45 -070034 return p[iMonth - 1];
35}
36
37int32_t DaysInYear(int32_t iYear) {
kumarashishg826308d2023-06-23 13:21:22 +000038 DCHECK(iYear != 0);
39 return FX_IsLeapYear(iYear) ? kDaysPerLeapYear : kDaysPerYear;
Philip P. Moltmannd904c1e2018-03-19 09:26:45 -070040}
41
42int64_t DateToDays(int32_t iYear,
43 uint8_t iMonth,
44 uint8_t iDay,
45 bool bIncludeThisDay) {
kumarashishg826308d2023-06-23 13:21:22 +000046 DCHECK(iYear != 0);
47 DCHECK(iMonth >= 1);
48 DCHECK(iMonth <= 12);
49 DCHECK(iDay >= 1);
50 DCHECK(iDay <= FX_DaysInMonth(iYear, iMonth));
Philip P. Moltmannd904c1e2018-03-19 09:26:45 -070051
52 int64_t iDays = DaysBeforeMonthInYear(iYear, iMonth);
53 iDays += iDay;
54 if (!bIncludeThisDay)
55 iDays--;
56
57 if (iYear > 0) {
58 iYear--;
59 } else {
60 iDays -= DaysInYear(iYear);
61 iYear++;
62 }
63 return iDays + static_cast<int64_t>(iYear) * 365 + iYear / 4 - iYear / 100 +
64 iYear / 400;
65}
66
Philip P. Moltmannd904c1e2018-03-19 09:26:45 -070067} // namespace
68
69uint8_t FX_DaysInMonth(int32_t iYear, uint8_t iMonth) {
kumarashishg826308d2023-06-23 13:21:22 +000070 DCHECK(iYear != 0);
71 pdfium::span<const uint8_t> p = FX_IsLeapYear(iYear)
72 ? pdfium::make_span(kDaysPerLeapMonth)
73 : pdfium::make_span(kDaysPerMonth);
74 // Note: iMonth is one-based.
Philip P. Moltmannd904c1e2018-03-19 09:26:45 -070075 return p[iMonth - 1];
76}
77
78bool FX_IsLeapYear(int32_t iYear) {
kumarashishg826308d2023-06-23 13:21:22 +000079 DCHECK(iYear != 0);
Philip P. Moltmannd904c1e2018-03-19 09:26:45 -070080 return ((iYear % 4) == 0 && (iYear % 100) != 0) || (iYear % 400) == 0;
81}
82
83// static
84CFX_DateTime CFX_DateTime::Now() {
kumarashishg826308d2023-06-23 13:21:22 +000085 time_t t = FXSYS_time(nullptr);
86 struct tm* pTime = FXSYS_localtime(&t);
87 return CFX_DateTime(
88 pTime->tm_year + 1900, static_cast<uint8_t>(pTime->tm_mon + 1),
89 static_cast<uint8_t>(pTime->tm_mday),
90 static_cast<uint8_t>(pTime->tm_hour), static_cast<uint8_t>(pTime->tm_min),
91 static_cast<uint8_t>(pTime->tm_sec), 0);
Philip P. Moltmannd904c1e2018-03-19 09:26:45 -070092}
93
94int32_t CFX_DateTime::GetDayOfWeek() const {
95 int32_t v = static_cast<int32_t>(DateToDays(year_, month_, day_, true) % 7);
96 if (v < 0)
97 v += 7;
98 return v;
99}
100
101bool CFX_DateTime::operator==(const CFX_DateTime& other) const {
102 return year_ == other.year_ && month_ == other.month_ && day_ == other.day_ &&
103 hour_ == other.hour_ && minute_ == other.minute_ &&
104 second_ == other.second_ && millisecond_ == other.millisecond_;
105}