1 // Copyright (c) 2014-2020 Thomas Fussell
2 // Copyright (c) 2010-2015 openpyxl
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, WRISING 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 <cstdint>
28 #include <string>
29 
30 #include <xlnt/xlnt_config.hpp>
31 #include <xlnt/utils/optional.hpp>
32 
33 namespace xlnt {
34 
35 enum class calendar;
36 
37 /// <summary>
38 /// Describes the number formatting applied to text and numbers within a certain cell.
39 /// </summary>
40 class XLNT_API number_format
41 {
42 public:
43     /// <summary>
44     /// Number format "General"
45     /// </summary>
46     static const number_format general();
47 
48     /// <summary>
49     /// Number format "@"
50     /// </summary>
51     static const number_format text();
52 
53     /// <summary>
54     /// Number format "0"
55     /// </summary>
56     static const number_format number();
57 
58     /// <summary>
59     /// Number format "00"
60     /// </summary>
61     static const number_format number_00();
62 
63     /// <summary>
64     /// Number format "#,##0.00"
65     /// </summary>
66     static const number_format number_comma_separated1();
67 
68     /// <summary>
69     /// Number format "0%"
70     /// </summary>
71     static const number_format percentage();
72 
73     /// <summary>
74     /// Number format "0.00%"
75     /// </summary>
76     static const number_format percentage_00();
77 
78     /// <summary>
79     /// Number format "yyyy-mm-dd"
80     /// </summary>
81     static const number_format date_yyyymmdd2();
82 
83     /// <summary>
84     /// Number format "yy-mm-dd"
85     /// </summary>
86     static const number_format date_yymmdd();
87 
88     /// <summary>
89     /// Number format "dd/mm/yy"
90     /// </summary>
91     static const number_format date_ddmmyyyy();
92 
93     /// <summary>
94     /// Number format "d/m/yy"
95     /// </summary>
96     static const number_format date_dmyslash();
97 
98     /// <summary>
99     /// Number format "d-m-yy"
100     /// </summary>
101     static const number_format date_dmyminus();
102 
103     /// <summary>
104     /// Number format "d-m"
105     /// </summary>
106     static const number_format date_dmminus();
107 
108     /// <summary>
109     /// Number format "m-yy"
110     /// </summary>
111     static const number_format date_myminus();
112 
113     /// <summary>
114     /// Number format "mm-dd-yy"
115     /// </summary>
116     static const number_format date_xlsx14();
117 
118     /// <summary>
119     /// Number format "d-mmm-yy"
120     /// </summary>
121     static const number_format date_xlsx15();
122 
123     /// <summary>
124     /// Number format "d-mmm"
125     /// </summary>
126     static const number_format date_xlsx16();
127 
128     /// <summary>
129     /// Number format "mmm-yy"
130     /// </summary>
131     static const number_format date_xlsx17();
132 
133     /// <summary>
134     /// Number format "m/d/yy h:mm"
135     /// </summary>
136     static const number_format date_xlsx22();
137 
138     /// <summary>
139     /// Number format "yyyy-mm-dd h:mm:ss"
140     /// </summary>
141     static const number_format date_datetime();
142 
143     /// <summary>
144     /// Number format "h:mm AM/PM"
145     /// </summary>
146     static const number_format date_time1();
147 
148     /// <summary>
149     /// Number format "h:mm:ss AM/PM"
150     /// </summary>
151     static const number_format date_time2();
152 
153     /// <summary>
154     /// Number format "h:mm"
155     /// </summary>
156     static const number_format date_time3();
157 
158     /// <summary>
159     /// Number format "h:mm:ss"
160     /// </summary>
161     static const number_format date_time4();
162 
163     /// <summary>
164     /// Number format "mm:ss"
165     /// </summary>
166     static const number_format date_time5();
167 
168     /// <summary>
169     /// Number format "h:mm:ss"
170     /// </summary>
171     static const number_format date_time6();
172 
173     /// <summary>
174     /// Returns true if the given format ID corresponds to a known builtin format.
175     /// </summary>
176     static bool is_builtin_format(std::size_t builtin_id);
177 
178     /// <summary>
179     /// Returns the format with the given ID. Thows an invalid_parameter exception
180     /// if builtin_id is not a valid ID.
181     /// </summary>
182     static const number_format &from_builtin_id(std::size_t builtin_id);
183 
184     /// <summary>
185     /// Constructs a default number_format equivalent to "General"
186     /// </summary>
187     number_format();
188 
189     /// <summary>
190     /// Constructs a number format equivalent to that returned from number_format::from_builtin_id(builtin_id).
191     /// </summary>
192     number_format(std::size_t builtin_id);
193 
194     /// <summary>
195     /// Constructs a number format from a code string. If the string matches a builtin ID,
196     /// its ID will also be set to match the builtin ID.
197     /// </summary>
198     number_format(const std::string &code);
199 
200     /// <summary>
201     /// Constructs a number format from a code string and custom ID. Custom ID should generally
202     /// be >= 164.
203     /// </summary>
204     number_format(const std::string &code, std::size_t custom_id);
205 
206     /// <summary>
207     /// Sets the format code of this number format to format_code.
208     /// </summary>
209     void format_string(const std::string &format_code);
210 
211     /// <summary>
212     /// Sets the format code of this number format to format_code and the ID to custom_id.
213     /// </summary>
214     void format_string(const std::string &format_code, std::size_t custom_id);
215 
216     /// <summary>
217     /// Returns the format code this number format uses.
218     /// </summary>
219     std::string format_string() const;
220 
221     /// <summary>
222     /// Returns true if this number format has an ID.
223     /// </summary>
224     bool has_id() const;
225 
226     /// <summary>
227     /// Sets the ID of this number format to id.
228     /// </summary>
229     void id(std::size_t id);
230 
231     /// <summary>
232     /// Returns the ID of this format.
233     /// </summary>
234     std::size_t id() const;
235 
236     /// <summary>
237     /// Returns text formatted according to this number format's format code.
238     /// </summary>
239     std::string format(const std::string &text) const;
240 
241     /// <summary>
242     /// Returns number formatted according to this number format's format code
243     /// with the given base date.
244     /// </summary>
245     std::string format(double number, calendar base_date) const;
246 
247     /// <summary>
248     /// Returns true if this format code returns a number formatted as a date.
249     /// </summary>
250     bool is_date_format() const;
251 
252     /// <summary>
253     /// Returns true if this format is equivalent to other.
254     /// </summary>
255     bool operator==(const number_format &other) const;
256 
257     /// <summary>
258     /// Returns true if this format is not equivalent to other.
259     /// </summary>
260     bool operator!=(const number_format &other) const;
261 
262 private:
263     /// <summary>
264     /// The optional ID
265     /// </summary>
266     optional<std::size_t> id_;
267 
268     /// <summary>
269     /// The format code
270     /// </summary>
271     std::string format_string_;
272 };
273 
274 } // namespace xlnt
275