radnelac 0.0.2

Calculations in a variety of different timekeeping systems.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.

use crate::display::private::Case;
use crate::display::private::Content;
use crate::display::private::DisplayItem;
use crate::display::private::DisplayOptions;
use crate::display::private::Item;
use crate::display::private::NumericContent;
use crate::display::private::Sign;
use crate::display::private::TextContent;
pub use crate::display::text::prelude::Language;

const O_LITERAL: DisplayOptions = DisplayOptions {
    numerals: None,
    width: None,
    align: None,
    padding: None,
    case: None,
    sign: Sign::OnlyNegative,
};

const O_YEAR_IN_ERA: DisplayOptions = DisplayOptions {
    numerals: None,
    width: None,
    align: None,
    padding: None,
    case: None,
    sign: Sign::Never,
};

const O_N1: DisplayOptions = DisplayOptions {
    numerals: None,
    width: Some(1),
    align: None,
    padding: None,
    case: Some(Case::Upper),
    sign: Sign::Never,
};

const O_N2: DisplayOptions = DisplayOptions {
    numerals: None,
    width: Some(2),
    align: None,
    padding: Some('0'),
    case: Some(Case::Upper),
    sign: Sign::Never,
};

const O_N3: DisplayOptions = DisplayOptions {
    numerals: None,
    width: Some(3),
    align: None,
    padding: None,
    case: Some(Case::Upper),
    sign: Sign::Never,
};

macro_rules! NumericDateItems {
    ($name: ident, $sep: literal, $prefix: ident, $n_prefix: literal, $stem: ident, $n_stem: literal) => {
        const $name: [Item<'_>; 3] = [
            Item::new(
                Content::Numeric(NumericContent::$prefix),
                DisplayOptions {
                    numerals: None,
                    width: Some($n_prefix),
                    align: None,
                    padding: Some('0'),
                    case: None,
                    sign: Sign::OnlyNegative,
                },
            ),
            Item::new(Content::Literal($sep), O_LITERAL),
            Item::new(
                Content::Numeric(NumericContent::$stem),
                DisplayOptions {
                    numerals: None,
                    width: Some($n_stem),
                    align: None,
                    padding: Some('0'),
                    case: None,
                    sign: Sign::OnlyNegative,
                },
            ),
        ];
    };
    ($name: ident, $sep: literal, $prefix: ident, $n_prefix: literal, $stem: ident, $n_stem: literal, $suffix: ident, $n_suffix: literal) => {
        const $name: [Item<'_>; 5] = [
            Item::new(
                Content::Numeric(NumericContent::$prefix),
                DisplayOptions {
                    numerals: None,
                    width: Some($n_prefix),
                    align: None,
                    padding: Some('0'),
                    case: None,
                    sign: Sign::OnlyNegative,
                },
            ),
            Item::new(Content::Literal($sep), O_LITERAL),
            Item::new(
                Content::Numeric(NumericContent::$stem),
                DisplayOptions {
                    numerals: None,
                    width: Some($n_stem),
                    align: None,
                    padding: Some('0'),
                    case: None,
                    sign: Sign::OnlyNegative,
                },
            ),
            Item::new(Content::Literal($sep), O_LITERAL),
            Item::new(
                Content::Numeric(NumericContent::$suffix),
                DisplayOptions {
                    numerals: None,
                    width: Some($n_suffix),
                    align: None,
                    padding: Some('0'),
                    case: None,
                    sign: Sign::OnlyNegative,
                },
            ),
        ];
    };
}

const I_HHMM_COLON_AMPM: [Item<'_>; 5] = [
    Item::new(
        Content::Numeric(NumericContent::Hour1to12),
        DisplayOptions {
            numerals: None,
            width: Some(2),
            align: None,
            padding: Some('0'),
            case: None,
            sign: Sign::OnlyNegative,
        },
    ),
    Item::new(Content::Literal(":"), O_LITERAL),
    Item::new(
        Content::Numeric(NumericContent::Minute),
        DisplayOptions {
            numerals: None,
            width: Some(2),
            align: None,
            padding: Some('0'),
            case: None,
            sign: Sign::OnlyNegative,
        },
    ),
    Item::new(Content::Literal(" "), O_LITERAL),
    Item::new(Content::Text(TextContent::HalfDayAbbrev), O_LITERAL),
];

NumericDateItems!(I_HHMMSS_COLON, ":", Hour0to23, 2, Minute, 2, Second, 2);
NumericDateItems!(I_YYYYMMDD_DASH, "-", Year, 4, Month, 2, DayOfMonth, 2);
NumericDateItems!(I_YYYYYMMDD_DASH, "-", Year, 5, Month, 2, DayOfMonth, 2);
NumericDateItems!(I_YYYYMMDD_SLASH, "/", Year, 4, Month, 2, DayOfMonth, 2);
NumericDateItems!(I_DDMMYYYY_SLASH, "/", DayOfMonth, 2, Month, 2, Year, 4);
NumericDateItems!(I_DDMMYYYY_DOT, ".", DayOfMonth, 2, Month, 2, Year, 4);
NumericDateItems!(I_MMDDYYYY_SLASH, "/", Month, 2, DayOfMonth, 2, Year, 4);
NumericDateItems!(I_YYYYOOO_DASH, "-", Year, 4, DayOfYear, 3);
NumericDateItems!(I_YYYYYOOO_DASH, "-", Year, 5, DayOfYear, 3);

const I_LONG_DATE: [Item<'_>; 9] = [
    Item::new(Content::Text(TextContent::DayOfWeekName), O_LITERAL),
    Item::new(Content::Literal(" "), O_LITERAL),
    Item::new(Content::Text(TextContent::MonthName), O_LITERAL),
    Item::new(Content::Literal(" "), O_LITERAL),
    Item::new(Content::Numeric(NumericContent::DayOfMonth), O_LITERAL),
    Item::new(Content::Literal(", "), O_LITERAL),
    Item::new(Content::Numeric(NumericContent::Year), O_YEAR_IN_ERA),
    Item::new(Content::Literal(" "), O_LITERAL),
    Item::new(Content::Text(TextContent::EraName), O_LITERAL),
];

const I_LONG_DAY_OF_MONTH: [Item<'_>; 9] = [
    Item::new(Content::Text(TextContent::DayOfWeekName), O_LITERAL),
    Item::new(Content::Literal(" "), O_LITERAL),
    Item::new(Content::Text(TextContent::MonthName), O_LITERAL),
    Item::new(Content::Literal(" "), O_LITERAL),
    Item::new(Content::Text(TextContent::DayOfMonthName), O_LITERAL),
    Item::new(Content::Literal(", "), O_LITERAL),
    Item::new(Content::Numeric(NumericContent::Year), O_YEAR_IN_ERA),
    Item::new(Content::Literal(" "), O_LITERAL),
    Item::new(Content::Text(TextContent::EraName), O_LITERAL),
];

const I_LONG_COMPL: [Item<'_>; 5] = [
    Item::new(Content::Text(TextContent::ComplementaryDayName), O_LITERAL),
    Item::new(Content::Literal(", "), O_LITERAL),
    Item::new(Content::Numeric(NumericContent::Year), O_YEAR_IN_ERA),
    Item::new(Content::Literal(" "), O_LITERAL),
    Item::new(Content::Text(TextContent::EraName), O_LITERAL),
];

const I_LONG_DATE_ERA_ABBR: [Item<'_>; 9] = [
    Item::new(Content::Text(TextContent::DayOfWeekName), O_LITERAL),
    Item::new(Content::Literal(" "), O_LITERAL),
    Item::new(Content::Text(TextContent::MonthName), O_LITERAL),
    Item::new(Content::Literal(" "), O_LITERAL),
    Item::new(Content::Numeric(NumericContent::DayOfMonth), O_LITERAL),
    Item::new(Content::Literal(", "), O_LITERAL),
    Item::new(Content::Numeric(NumericContent::Year), O_YEAR_IN_ERA),
    Item::new(Content::Literal(" "), O_LITERAL),
    Item::new(Content::Text(TextContent::EraAbbreviation), O_LITERAL),
];

const I_LONG_DAY_OF_MONTH_ERA_ABBR: [Item<'_>; 9] = [
    Item::new(Content::Text(TextContent::DayOfWeekName), O_LITERAL),
    Item::new(Content::Literal(" "), O_LITERAL),
    Item::new(Content::Text(TextContent::MonthName), O_LITERAL),
    Item::new(Content::Literal(" "), O_LITERAL),
    Item::new(Content::Text(TextContent::DayOfMonthName), O_LITERAL),
    Item::new(Content::Literal(", "), O_LITERAL),
    Item::new(Content::Numeric(NumericContent::Year), O_YEAR_IN_ERA),
    Item::new(Content::Literal(" "), O_LITERAL),
    Item::new(Content::Text(TextContent::EraAbbreviation), O_LITERAL),
];

const I_LONG_COMPL_ERA_ABBR: [Item<'_>; 5] = [
    Item::new(Content::Text(TextContent::ComplementaryDayName), O_LITERAL),
    Item::new(Content::Literal(", "), O_LITERAL),
    Item::new(Content::Numeric(NumericContent::Year), O_YEAR_IN_ERA),
    Item::new(Content::Literal(" "), O_LITERAL),
    Item::new(Content::Text(TextContent::EraAbbreviation), O_LITERAL),
];

const I_YEAR_WEEK_DAY: [Item<'_>; 5] = [
    Item::new(Content::Numeric(NumericContent::Year), O_LITERAL),
    Item::new(Content::Literal("-W"), O_LITERAL),
    Item::new(Content::Numeric(NumericContent::WeekOfYear), O_N2),
    Item::new(Content::Literal("-"), O_LITERAL),
    Item::new(Content::Numeric(NumericContent::DayOfWeek), O_N1),
];

const I_YEAR_MDD: [Item<'_>; 4] = [
    Item::new(Content::Numeric(NumericContent::Year), O_LITERAL),
    Item::new(Content::Literal("-"), O_LITERAL),
    Item::new(Content::Text(TextContent::MonthName), O_N1),
    Item::new(Content::Numeric(NumericContent::DayOfMonth), O_N2),
];

const I_YEAR_COMPL: [Item<'_>; 3] = [
    Item::new(Content::Numeric(NumericContent::Year), O_LITERAL),
    Item::new(Content::Literal("-"), O_LITERAL),
    Item::new(Content::Text(TextContent::ComplementaryDayName), O_N3),
];

const I_COMPL_ONLY: [Item<'_>; 1] = [Item::new(
    Content::Text(TextContent::ComplementaryDayName),
    O_LITERAL,
)];

const I_WEEKDAY_NAME_ONLY: [Item<'_>; 1] = [Item::new(
    Content::Text(TextContent::DayOfWeekName),
    O_LITERAL,
)];

const I_EPOCH_SECONDS_ONLY: [Item<'_>; 1] = [Item::new(
    Content::Numeric(NumericContent::SecondsSinceEpoch),
    O_LITERAL,
)];

const I_EPOCH_DAYS_ONLY: [Item<'_>; 1] = [Item::new(
    Content::Numeric(NumericContent::DaysSinceEpoch),
    O_LITERAL,
)];

/// Represents a date format
/// ## Crate Features
///
/// This is only available if `display` is enabled.
#[derive(Debug, PartialEq, Clone, Copy)]
pub struct PresetFormat<'a>(&'a [Item<'a>]);

/// HH:MM AM/PM time format
/// ## Crate Features
///
/// This is only available if `display` is enabled.
pub const HHMM_COLON_AMPM: PresetFormat<'static> = PresetFormat::<'static>(&I_HHMM_COLON_AMPM);
/// HH:MM:SS time format
/// ## Crate Features
///
/// This is only available if `display` is enabled.
pub const HHMMSS_COLON: PresetFormat<'static> = PresetFormat::<'static>(&I_HHMMSS_COLON);
/// YYYY-MM-DD numeric date format
/// ## Crate Features
///
/// This is only available if `display` is enabled.
pub const YYYYMMDD_DASH: PresetFormat<'static> = PresetFormat::<'static>(&I_YYYYMMDD_DASH);
/// YYYYY-MM-DD numeric date format
///
/// This is intended for the Holocene Calendar.
///
/// ## Crate Features
///
/// This is only available if `display` is enabled.
pub const YYYYYMMDD_DASH: PresetFormat<'static> = PresetFormat::<'static>(&I_YYYYYMMDD_DASH);
/// YYYY/MM/DD numeric date format
/// ## Crate Features
///
/// This is only available if `display` is enabled.
pub const YYYYMMDD_SLASH: PresetFormat<'static> = PresetFormat::<'static>(&I_YYYYMMDD_SLASH);
/// DD/MM/YYYY numeric date format
/// ## Crate Features
///
/// This is only available if `display` is enabled.
pub const DDMMYYYY_SLASH: PresetFormat<'static> = PresetFormat::<'static>(&I_DDMMYYYY_SLASH);
/// DD.MM.YYYY numeric date format
/// ## Crate Features
///
/// This is only available if `display` is enabled.
pub const DDMMYYYY_DOT: PresetFormat<'static> = PresetFormat::<'static>(&I_DDMMYYYY_DOT);
/// MM/DD/YYYY numeric date format
/// ## Crate Features
///
/// This is only available if `display` is enabled.
pub const MMDDYYYY_SLASH: PresetFormat<'static> = PresetFormat::<'static>(&I_MMDDYYYY_SLASH);
/// YYYY-OOO numeric date format, where OOO is day of year
///
/// ## Crate Features
///
/// This is only available if `display` is enabled.
pub const YYYYOOO_DASH: PresetFormat<'static> = PresetFormat::<'static>(&I_YYYYOOO_DASH);
/// YYYY-OOO numeric date format, where OOO is day of year
///
/// ## Crate Features
///
/// This is only available if `display` is enabled.
pub const YYYYYOOO_DASH: PresetFormat<'static> = PresetFormat::<'static>(&I_YYYYYOOO_DASH);
/// Calendar-specific long date format
/// ## Crate Features
///
/// This is only available if `display` is enabled.
pub const LONG_DATE: PresetFormat<'static> = PresetFormat::<'static>(&I_LONG_DATE);
/// Calendar-specific long date format with day of month name
/// ## Crate Features
///
/// This is only available if `display` is enabled.
pub const LONG_DAY_OF_MONTH: PresetFormat<'static> = PresetFormat::<'static>(&I_LONG_DAY_OF_MONTH);
/// Calendar-specific long complementary day format
///
/// This is intended for calendars with complementary days.
/// ## Crate Features
///
/// This is only available if `display` is enabled.
pub const LONG_COMPL: PresetFormat<'static> = PresetFormat::<'static>(&I_LONG_COMPL);
/// Calendar-specific long date format, with abbreviated era
/// ## Crate Features
///
/// This is only available if `display` is enabled.
pub const LONG_DATE_ERA_ABBR: PresetFormat<'static> =
    PresetFormat::<'static>(&I_LONG_DATE_ERA_ABBR);
/// Calendar-specific long date format with day of month name and abbreviated era
/// ## Crate Features
///
/// This is only available if `display` is enabled.
pub const LONG_DAY_OF_MONTH_ERA_ABBR: PresetFormat<'static> =
    PresetFormat::<'static>(&I_LONG_DAY_OF_MONTH_ERA_ABBR);
/// Calendar-specific long complementary day format, with abbreviated era
///
/// This is intended for calendars with complementary days.
/// ## Crate Features
///
/// This is only available if `display` is enabled.
pub const LONG_COMPL_ERA_ABBR: PresetFormat<'static> =
    PresetFormat::<'static>(&I_LONG_COMPL_ERA_ABBR);
/// YYYY-Www-DD alphanumeric date format
///
/// This is inteded for the ISO calendar.
/// ## Crate Features
///
/// This is only available if `display` is enabled.
pub const YEAR_WEEK_DAY: PresetFormat<'static> = PresetFormat::<'static>(&I_YEAR_WEEK_DAY);
/// Y-mDD alphanumeric date format, where Y has variable length, m is a single character
///
/// This is intended for the Tranquility calendar
/// ## Crate Features
///
/// This is only available if `display` is enabled.
pub const YEAR_MDD: PresetFormat<'static> = PresetFormat::<'static>(&I_YEAR_MDD);
/// Y-CCC alphanumeric date format, where Y has variable length, CCC is the complementary day.
///
/// This is intended for calendars with complementary days.
/// ## Crate Features
///
/// This is only available if `display` is enabled.
pub const YEAR_COMPL: PresetFormat<'static> = PresetFormat::<'static>(&I_YEAR_COMPL);
/// Format which is the full name of the complementary day
///
/// This is intended for calendars with complementary days.
/// ## Crate Features
///
/// This is only available if `display` is enabled.
pub const COMPL_ONLY: PresetFormat<'static> = PresetFormat::<'static>(&I_COMPL_ONLY);
/// Format which is the full name of the weekday
///
/// ## Crate Features
///
/// This is only available if `display` is enabled.
pub const WEEKDAY_NAME_ONLY: PresetFormat<'static> = PresetFormat::<'static>(&I_WEEKDAY_NAME_ONLY);
/// Format which is the seconds since an epoch only
///
/// The epoch is specific to the timekeeping system.
///
/// ## Crate Features
///
/// This is only available if `display` is enabled.
pub const EPOCH_SECONDS_ONLY: PresetFormat<'static> =
    PresetFormat::<'static>(&I_EPOCH_SECONDS_ONLY);
/// Format which is the days since an epoch only
///
/// The epoch is specific to the timekeeping system.
///
/// ## Crate Features
///
/// This is only available if `display` is enabled.
pub const EPOCH_DAYS_ONLY: PresetFormat<'static> = PresetFormat::<'static>(&I_EPOCH_DAYS_ONLY);

/// Format a date in a preset format
/// ## Crate Features
///
/// This is only available if `display` is enabled.
pub trait PresetDisplay: DisplayItem {
    /// Checks if language is supported
    fn supported_display_lang(lang: Language) -> bool {
        Self::supported_lang(lang)
    }

    /// Format a date in any `PresetFormat`
    fn preset_str(&self, lang: Language, preset: PresetFormat) -> String {
        let mut result = String::new();
        for item in preset.0 {
            result.push_str(&self.fmt_item(lang, *item))
        }
        result
    }

    /// Format a date in a calendar-specific long format
    fn long_date(&self) -> String {
        self.preset_str(Language::EN, LONG_DATE)
    }

    /// Format a date in a calendar-specific short format
    fn short_date(&self) -> String {
        self.preset_str(Language::EN, YYYYMMDD_DASH)
    }
}