cargo_plot/addon/time_tag.rs
1// [EN]: Functions for creating consistent date and time stamps.
2// [PL]: Funkcje do tworzenia spójnych sygnatur daty i czasu.
3
4use chrono::{Datelike, Local, Timelike, Weekday};
5pub use chrono::{NaiveDate, NaiveTime};
6
7/// [EN]: Utility struct for generating consistent time tags.
8/// [PL]: Struktura narzędziowa do generowania spójnych sygnatur czasowych.
9pub struct TimeTag;
10
11impl TimeTag {
12 /// [EN]: Generates a time_tag for the current local time.
13 /// [PL]: Generuje time_tag dla obecnego, lokalnego czasu.
14 #[must_use]
15 pub fn now() -> String {
16 let now = Local::now();
17 Self::format(now.date_naive(), now.time())
18 }
19
20 /// [EN]: Generates a time_tag for a specific provided date and time.
21 /// [PL]: Generuje time_tag dla konkretnej, podanej daty i czasu.
22 #[must_use]
23 pub fn custom(date: NaiveDate, time: NaiveTime) -> String {
24 Self::format(date, time)
25 }
26
27 // [EN]: Private function that performs manual string construction (DRY principle).
28 // [PL]: PRYWATNA funkcja, która wykonuje ręczne budowanie ciągu znaków (zasada DRY).
29 fn format(date: NaiveDate, time: NaiveTime) -> String {
30 let year = date.year();
31 let quarter = ((date.month() - 1) / 3) + 1;
32
33 let weekday = match date.weekday() {
34 Weekday::Mon => "Mon",
35 Weekday::Tue => "Tue",
36 Weekday::Wed => "Wed",
37 Weekday::Thu => "Thu",
38 Weekday::Fri => "Fri",
39 Weekday::Sat => "Sat",
40 Weekday::Sun => "Sun",
41 };
42
43 let month = match date.month() {
44 1 => "Jan",
45 2 => "Feb",
46 3 => "Mar",
47 4 => "Apr",
48 5 => "May",
49 6 => "Jun",
50 7 => "Jul",
51 8 => "Aug",
52 9 => "Sep",
53 10 => "Oct",
54 11 => "Nov",
55 12 => "Dec",
56 _ => unreachable!(),
57 };
58
59 let millis = time.nanosecond() / 1_000_000;
60
61 // [EN]: Format: YYYYQn Dnnn Wnn _ Day DD Mon _ HH MM SS mmm
62 // [PL]: Format: RRRRQn Dnnn Wnn _ Dzień DD Miesiąc _ GG MM SS mmm
63 format!(
64 "{}Q{}D{:03}W{:02}_{}{:02}{}_{:02}{:02}{:02}{:03}",
65 year,
66 quarter,
67 date.ordinal(),
68 date.iso_week().week(),
69 weekday,
70 date.day(),
71 month,
72 time.hour(),
73 time.minute(),
74 time.second(),
75 millis
76 )
77 }
78}