TLA Line data Source code
1 : // Copyright (c) 2014-2022 Thomas Fussell
2 : // Copyright (c) 2024-2025 xlnt-community
3 : //
4 : // Permission is hereby granted, free of charge, to any person obtaining a copy
5 : // of this software and associated documentation files (the "Software"), to deal
6 : // in the Software without restriction, including without limitation the rights
7 : // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 : // copies of the Software, and to permit persons to whom the Software is
9 : // furnished to do so, subject to the following conditions:
10 : //
11 : // The above copyright notice and this permission notice shall be included in
12 : // all copies or substantial portions of the Software.
13 : //
14 : // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 : // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 : // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 : // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 : // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 : // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 : // THE SOFTWARE
21 : //
22 : // @license: http://www.opensource.org/licenses/mit-license.php
23 : // @author: see AUTHORS file
24 :
25 : #pragma once
26 :
27 : #include <xlnt/xlnt_config.hpp>
28 : #include <xlnt/utils/calendar.hpp>
29 :
30 : namespace xlnt {
31 :
32 : /// <summary>
33 : /// A date is a specific day specified in terms of a year, month, and day.
34 : /// It can also be initialized as a number of days since a base date using date::from_number.
35 : /// IMPORTANT: The date could be in an empty/invalid state, so you may want to call is_null() before calling any functions!
36 : /// </summary>
37 : struct XLNT_API date
38 : {
39 : /// <summary>
40 : /// Returns the current date according to the system time.
41 : /// If the current date could not be determined, the date will be in an empty state (is_null() will return true).
42 : /// </summary>
43 : static date today();
44 :
45 : /// <summary>
46 : /// Returns a date by adding days_since_base_year to base_date.
47 : /// This includes leap years.
48 : /// </summary>
49 : static date from_number(int days_since_base_year, calendar base_date);
50 :
51 : /// <summary>
52 : /// Constructs a date from a given year, month, and day.
53 : /// </summary>
54 : date(int year_, int month_, int day_);
55 :
56 : /// <summary>
57 : /// Constructs an empty date (a call to is_null() will return true).
58 : /// </summary>
59 UBC 0 : date() = default;
60 :
61 : /// <summary>
62 : /// Returns the number of days between this date and base_date. The date could be in an empty/invalid state, so you may want to call is_null() first!
63 : /// If this function is called when having an empty/invalid state, an xlnt::invalid_attribute exception will be thrown.
64 : /// </summary>
65 : int to_number(calendar base_date) const;
66 :
67 : /// <summary>
68 : /// Calculates and returns the day of the week that this date represents in the range
69 : /// 0 to 6 where 0 represents Sunday.
70 : /// Returns -1 if the weekday could not be determined.
71 : /// </summary>
72 : int weekday() const;
73 :
74 : /// <summary>
75 : /// Returns the year of the date. The date could be in an empty/invalid state, so you may want to call is_null() first!
76 : /// If this function is called when having an empty/invalid state, an xlnt::invalid_attribute exception will be thrown.
77 : /// </summary>
78 : int get_year() const;
79 :
80 : /// <summary>
81 : /// Returns the month of the date. The date could be in an empty/invalid state, so you may want to call is_null() first!
82 : /// If this function is called when having an empty/invalid state, an xlnt::invalid_attribute exception will be thrown.
83 : /// </summary>
84 : int get_month() const;
85 :
86 : /// <summary>
87 : /// Returns the day of the date. The date could be in an empty/invalid state, so you may want to call is_null() first!
88 : /// If this function is called when having an empty/invalid state, an xlnt::invalid_attribute exception will be thrown.
89 : /// </summary>
90 : int get_day() const;
91 :
92 : /// <summary>
93 : /// Returns whether the date is in an empty/invalid state.
94 : /// </summary>
95 CBC 100 : bool is_null() const
96 : {
97 100 : return _is_null;
98 : }
99 :
100 : /// <summary>
101 : /// Returns true if this date is equal to comparand.
102 : /// </summary>
103 : bool operator==(const date &comparand) const;
104 :
105 : /// <summary>
106 : /// Returns true if this date is equal to comparand.
107 : /// </summary>
108 : bool operator!=(const date &comparand) const;
109 :
110 :
111 : /// ----- TODO IMPORTANT: accessing the members directly is DEPRECATED and will be changed in a further release! Please use the getters instead!
112 :
113 : /// <summary>
114 : /// The year
115 : /// </summary>
116 : int year = 0;
117 :
118 : /// <summary>
119 : /// The month
120 : /// </summary>
121 : int month = 0;
122 :
123 : /// <summary>
124 : /// The day
125 : /// </summary>
126 : int day = 0;
127 :
128 :
129 : private:
130 :
131 : /// <summary>
132 : /// Whether the date is in an empty state.
133 : /// </summary>
134 : bool _is_null = true;
135 : };
136 :
137 : } // namespace xlnt
|