1use crate::aes::Aesthetic;
2use crate::data::Value;
3
4use super::Scale;
5
6#[derive(Clone, Copy, Debug)]
8enum DateBreak {
9 Secs(f64),
11 Months(u32),
13}
14
15#[derive(Clone, Debug)]
17pub struct ScaleDateTime {
18 aesthetic: Aesthetic,
19 name: String,
20 min: f64,
21 max: f64,
22 trained: bool,
23 expand: (f64, f64),
24 date_breaks: Option<DateBreak>,
25 date_labels: Option<String>,
26 locale: Option<crate::locale::Locale>,
28}
29
30struct DateParts {
32 year: i64,
33 month: u32,
34 day: u32,
35 hour: u32,
36 minute: u32,
37 second: u32,
38}
39
40fn days_from_civil(y: i64, m: u32, d: u32) -> i64 {
42 let y = if m <= 2 { y - 1 } else { y };
43 let era = (if y >= 0 { y } else { y - 399 }) / 400;
44 let yoe = y - era * 400;
45 let mp = if m > 2 { m - 3 } else { m + 9 } as i64;
46 let doy = (153 * mp + 2) / 5 + d as i64 - 1;
47 let doe = yoe * 365 + yoe / 4 - yoe / 100 + doy;
48 era * 146_097 + doe - 719_468
49}
50
51fn secs_from_civil(y: i64, m: u32, d: u32) -> i64 {
52 days_from_civil(y, m, d) * 86_400
53}
54
55fn civil_from_secs(secs: i64) -> DateParts {
57 let (mut days, rem) = if secs >= 0 {
58 (secs / 86_400, secs % 86_400)
59 } else {
60 let d = (secs - 86_400 + 1) / 86_400;
61 (d, secs - d * 86_400)
62 };
63 let hour = (rem / 3600) as u32;
64 let minute = ((rem % 3600) / 60) as u32;
65 let second = (rem % 60) as u32;
66
67 days += 719_468;
68 let era = if days >= 0 { days } else { days - 146_096 } / 146_097;
69 let doe = (days - era * 146_097) as u32;
70 let yoe = (doe - doe / 1460 + doe / 36524 - doe / 146_096) / 365;
71 let y = yoe as i64 + era * 400;
72 let doy = doe - (365 * yoe + yoe / 4 - yoe / 100);
73 let mp = (5 * doy + 2) / 153;
74 let day = doy - (153 * mp + 2) / 5 + 1;
75 let month = if mp < 10 { mp + 3 } else { mp - 9 };
76 let year = if month <= 2 { y + 1 } else { y };
77 DateParts {
78 year,
79 month,
80 day,
81 hour,
82 minute,
83 second,
84 }
85}
86
87const MONTHS_SHORT: [&str; 12] = [
88 "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
89];
90const MONTHS_LONG: [&str; 12] = [
91 "January",
92 "February",
93 "March",
94 "April",
95 "May",
96 "June",
97 "July",
98 "August",
99 "September",
100 "October",
101 "November",
102 "December",
103];
104const DAYS_SHORT: [&str; 7] = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
105const DAYS_LONG: [&str; 7] = [
106 "Sunday",
107 "Monday",
108 "Tuesday",
109 "Wednesday",
110 "Thursday",
111 "Friday",
112 "Saturday",
113];
114
115#[cfg(test)]
118fn strftime(secs: f64, fmt: &str) -> String {
119 strftime_locale(secs, fmt, None)
120}
121
122pub(crate) fn strftime_locale(
124 secs: f64,
125 fmt: &str,
126 locale: Option<&crate::locale::Locale>,
127) -> String {
128 let p = civil_from_secs(secs as i64);
129 let mi = (p.month.clamp(1, 12) - 1) as usize;
130 let wd = ((secs as i64).div_euclid(86_400) + 4).rem_euclid(7) as usize;
132 let month_short = locale.map_or(MONTHS_SHORT[mi], |l| l.months_short[mi].as_str());
133 let month_long = locale.map_or(MONTHS_LONG[mi], |l| l.months_long[mi].as_str());
134 let day_short = locale.map_or(DAYS_SHORT[wd], |l| l.days_short[wd].as_str());
135 let day_long = locale.map_or(DAYS_LONG[wd], |l| l.days_long[wd].as_str());
136 let mut out = String::new();
137 let mut chars = fmt.chars().peekable();
138 while let Some(c) = chars.next() {
139 if c != '%' {
140 out.push(c);
141 continue;
142 }
143 match chars.next() {
144 Some('Y') => out.push_str(&format!("{:04}", p.year)),
145 Some('y') => out.push_str(&format!("{:02}", p.year.rem_euclid(100))),
146 Some('m') => out.push_str(&format!("{:02}", p.month)),
147 Some('b') => out.push_str(month_short),
148 Some('B') => out.push_str(month_long),
149 Some('a') => out.push_str(day_short),
150 Some('A') => out.push_str(day_long),
151 Some('d') => out.push_str(&format!("{:02}", p.day)),
152 Some('e') => out.push_str(&format!("{:2}", p.day)),
153 Some('H') => out.push_str(&format!("{:02}", p.hour)),
154 Some('M') => out.push_str(&format!("{:02}", p.minute)),
155 Some('S') => out.push_str(&format!("{:02}", p.second)),
156 Some('%') => out.push('%'),
157 Some('-') if matches!(chars.peek(), Some('d' | 'm')) => match chars.next() {
159 Some('d') => out.push_str(&p.day.to_string()),
160 _ => out.push_str(&p.month.to_string()),
161 },
162 Some(other) => {
163 out.push('%');
164 out.push(other);
165 }
166 None => out.push('%'),
167 }
168 }
169 out
170}
171
172fn parse_date_break(spec: &str) -> Option<DateBreak> {
174 let spec = spec.trim().to_lowercase();
175 let mut parts = spec.split_whitespace();
176 let first = parts.next()?;
177 let (n, unit) = match first.parse::<f64>() {
178 Ok(n) => (n, parts.next()?.to_string()),
179 Err(_) => (1.0, first.to_string()),
180 };
181 let unit = unit.trim_end_matches('s');
182 let secs = |s: f64| Some(DateBreak::Secs(n * s));
183 match unit {
184 "sec" | "second" => secs(1.0),
185 "min" | "minute" => secs(60.0),
186 "hour" => secs(3600.0),
187 "day" => secs(86_400.0),
188 "week" => secs(604_800.0),
189 "month" => Some(DateBreak::Months(n.max(1.0) as u32)),
190 "year" => Some(DateBreak::Months((n.max(1.0) as u32) * 12)),
191 _ => None,
192 }
193}
194
195impl ScaleDateTime {
196 pub fn new() -> Self {
197 ScaleDateTime {
198 aesthetic: Aesthetic::X,
199 name: String::new(),
200 min: f64::INFINITY,
201 max: f64::NEG_INFINITY,
202 trained: false,
203 expand: (0.05, 0.0),
204 date_breaks: None,
205 date_labels: None,
206 locale: None,
207 }
208 }
209
210 pub fn for_aesthetic(mut self, aes: Aesthetic) -> Self {
211 self.aesthetic = aes;
212 self
213 }
214
215 pub fn with_name(mut self, name: &str) -> Self {
216 self.name = name.to_string();
217 self
218 }
219
220 pub fn with_date_breaks(mut self, spec: &str) -> Self {
223 self.date_breaks = parse_date_break(spec);
224 self
225 }
226
227 pub fn with_date_labels(mut self, fmt: &str) -> Self {
231 self.date_labels = Some(fmt.to_string());
232 self
233 }
234
235 pub fn with_locale(mut self, locale: crate::locale::Locale) -> Self {
240 self.locale = Some(locale);
241 self
242 }
243
244 fn label(&self, secs: f64, step: f64) -> String {
245 match (&self.date_labels, &self.locale) {
246 (Some(fmt), loc) => strftime_locale(secs, fmt, loc.as_ref()),
247 (None, Some(loc)) => loc.format_datetime(secs as i64),
248 (None, None) => Self::format_datetime(secs, step),
249 }
250 }
251
252 fn month_label(&self, y: i64, m: u32, secs: f64) -> String {
255 match &self.locale {
256 Some(loc) => strftime_locale(secs, &loc.month_format, Some(loc)),
257 None => format!("{y:04}-{m:02}"),
258 }
259 }
260
261 fn expanded_range(&self) -> (f64, f64) {
262 let range = self.max - self.min;
263 let mult = self.expand.0;
264 let add = self.expand.1;
265 (self.min - range * mult - add, self.max + range * mult + add)
266 }
267
268 fn nice_datetime_step(range_secs: f64) -> f64 {
270 const MINUTE: f64 = 60.0;
271 const HOUR: f64 = 3600.0;
272 const DAY: f64 = 86400.0;
273 const WEEK: f64 = 7.0 * DAY;
274 const MONTH: f64 = 30.0 * DAY;
275 const YEAR: f64 = 365.25 * DAY;
276
277 let candidates = [
278 1.0,
279 5.0,
280 10.0,
281 30.0,
282 MINUTE,
283 5.0 * MINUTE,
284 10.0 * MINUTE,
285 30.0 * MINUTE,
286 HOUR,
287 3.0 * HOUR,
288 6.0 * HOUR,
289 12.0 * HOUR,
290 DAY,
291 2.0 * DAY,
292 WEEK,
293 2.0 * WEEK,
294 MONTH,
295 3.0 * MONTH,
296 6.0 * MONTH,
297 YEAR,
298 2.0 * YEAR,
299 5.0 * YEAR,
300 10.0 * YEAR,
301 20.0 * YEAR,
302 50.0 * YEAR,
303 100.0 * YEAR,
304 ];
305
306 let target = range_secs / 5.0;
307 for &c in &candidates {
308 if c >= target {
309 return c;
310 }
311 }
312 let n = (target / (100.0 * YEAR)).ceil();
314 n * 100.0 * YEAR
315 }
316
317 fn format_datetime(secs: f64, _step: f64) -> String {
320 let epoch_secs = secs as i64;
321 crate::data::format_epoch_secs(epoch_secs)
322 }
323}
324
325impl Default for ScaleDateTime {
326 fn default() -> Self {
327 Self::new()
328 }
329}
330
331impl Scale for ScaleDateTime {
332 fn set_default_locale(&mut self, locale: &crate::locale::Locale) {
333 if self.locale.is_none() {
334 self.locale = Some(locale.clone());
335 }
336 }
337
338 fn locale(&self) -> Option<&crate::locale::Locale> {
339 self.locale.as_ref()
340 }
341
342 fn aesthetic(&self) -> Aesthetic {
343 self.aesthetic.clone()
344 }
345
346 fn train(&mut self, values: &[Value]) {
347 for v in values {
348 if let Some(f) = v.as_f64() {
349 if f.is_finite() {
350 if f < self.min {
351 self.min = f;
352 }
353 if f > self.max {
354 self.max = f;
355 }
356 }
357 }
358 }
359 self.trained = true;
360 }
361
362 fn map(&self, value: &Value) -> f64 {
363 let f = match value.as_f64() {
364 Some(f) => f,
365 None => return 0.0,
366 };
367 if f == f64::INFINITY {
369 return 1.0;
370 }
371 if f == f64::NEG_INFINITY {
372 return 0.0;
373 }
374 if !self.trained || !self.min.is_finite() || !self.max.is_finite() {
377 return 0.5;
378 }
379 let (emin, emax) = self.expanded_range();
380 let range = emax - emin;
381 if range.abs() < f64::EPSILON {
382 0.5
383 } else {
384 (f - emin) / range
385 }
386 }
387
388 fn expanded_domain(&self) -> Option<(f64, f64)> {
389 if !self.trained || !self.min.is_finite() || !self.max.is_finite() {
390 return None;
391 }
392 let (a, b) = self.expanded_range();
393 if !(a.is_finite() && b.is_finite()) {
394 return None;
395 }
396 if (b - a).abs() < f64::EPSILON {
397 return Some((a - 0.5, b + 0.5));
399 }
400 Some((a, b))
401 }
402
403 fn breaks(&self) -> Vec<(f64, String)> {
404 if !self.trained || self.min > self.max {
405 return vec![];
406 }
407
408 let range = self.max - self.min;
409 if super::util::is_degenerate_range(self.min, self.max) {
410 return vec![(0.5, self.label(self.min, 1.0))];
411 }
412
413 let (emin, emax) = self.expanded_range();
414 const MAX_ABS_SECS: f64 = 1e12;
417 if !(emin.abs() <= MAX_ABS_SECS && emax.abs() <= MAX_ABS_SECS) {
418 return vec![];
419 }
420
421 if let Some(DateBreak::Months(n)) = self.date_breaks {
423 let n = n.max(1);
424 let start = civil_from_secs(emin.ceil() as i64);
425 let (mut y, mut m) = (start.year, start.month);
426 if (secs_from_civil(y, m, 1) as f64) < emin {
428 m += 1;
429 if m > 12 {
430 m = 1;
431 y += 1;
432 }
433 }
434 let mut breaks = Vec::new();
435 let mut guard = 0;
436 loop {
437 let secs = secs_from_civil(y, m, 1) as f64;
438 if secs > emax + 1.0 || guard > 10_000 {
439 break;
440 }
441 breaks.push((self.map(&Value::Float(secs)), self.label(secs, 0.0)));
442 m += n;
443 while m > 12 {
444 m -= 12;
445 y += 1;
446 }
447 guard += 1;
448 }
449 return breaks;
450 }
451
452 let explicit_secs = matches!(self.date_breaks, Some(DateBreak::Secs(s)) if s > 0.0);
453 let step = match self.date_breaks {
454 Some(DateBreak::Secs(s)) if s > 0.0 => s,
455 _ => Self::nice_datetime_step(range),
456 };
457
458 const DAY: f64 = 86_400.0;
462 const MONTH: f64 = 30.0 * DAY;
463 const YEAR: f64 = 365.25 * DAY;
464 if !explicit_secs && step >= 0.9 * YEAR {
465 let n = ((step / YEAR).round() as i64).max(1);
466 let mut y = civil_from_secs(emin.ceil() as i64).year.div_euclid(n) * n;
467 while (secs_from_civil(y, 1, 1) as f64) < emin {
468 y += n;
469 }
470 let mut breaks = Vec::new();
471 while (secs_from_civil(y, 1, 1) as f64) <= emax + 1.0 {
472 let secs = secs_from_civil(y, 1, 1) as f64;
473 breaks.push((self.map(&Value::Float(secs)), format!("{y}")));
474 y += n;
475 }
476 return breaks;
477 }
478 if !explicit_secs && step >= 0.9 * MONTH {
479 let n = ((step / MONTH).round() as i64).max(1);
480 let start = civil_from_secs(emin.ceil() as i64);
481 let mut tm = start.year * 12 + (start.month as i64 - 1); while (secs_from_civil(tm.div_euclid(12), tm.rem_euclid(12) as u32 + 1, 1) as f64)
483 < emin
484 {
485 tm += 1;
486 }
487 let mut breaks = Vec::new();
488 let mut guard = 0;
489 loop {
490 let (y, m) = (tm.div_euclid(12), tm.rem_euclid(12) as u32 + 1);
491 let secs = secs_from_civil(y, m, 1) as f64;
492 if secs > emax + 1.0 || guard > 10_000 {
493 break;
494 }
495 breaks.push((self.map(&Value::Float(secs)), self.month_label(y, m, secs)));
496 tm += n;
497 guard += 1;
498 }
499 return breaks;
500 }
501
502 let start = (emin / step).ceil() * step;
503 super::util::stepped_breaks(start, emax, step)
504 .into_iter()
505 .map(|v| (self.map(&Value::Float(v)), self.label(v, step)))
506 .collect()
507 }
508
509 fn name(&self) -> &str {
510 &self.name
511 }
512
513 fn set_name(&mut self, name: &str) {
514 self.name = name.to_string();
515 }
516
517 fn set_limits(&mut self, min: f64, max: f64) {
518 self.min = min;
519 self.max = max;
520 self.trained = true;
521 }
522
523 fn clone_box(&self) -> Box<dyn Scale> {
524 Box::new(self.clone())
525 }
526
527 fn reset_training(&mut self) {
528 self.min = f64::INFINITY;
529 self.max = f64::NEG_INFINITY;
530 self.trained = false;
531 }
532}
533
534#[cfg(test)]
535mod tests {
536 use super::*;
537
538 #[test]
539 fn civil_roundtrip() {
540 let p = civil_from_secs(1_615_811_445);
542 assert_eq!((p.year, p.month, p.day), (2021, 3, 15));
543 assert_eq!((p.hour, p.minute, p.second), (12, 30, 45));
544 assert_eq!(secs_from_civil(2021, 3, 15), 1_615_766_400); }
546
547 #[test]
548 fn multiyear_default_breaks_are_clean_years() {
549 let mut s = ScaleDateTime::new();
552 s.train(&[
553 Value::DateTime(secs_from_civil(2011, 2, 1)),
554 Value::DateTime(secs_from_civil(2016, 4, 1)),
555 ]);
556 let labels: Vec<String> = s.breaks().into_iter().map(|(_, l)| l).collect();
557 assert!(!labels.is_empty());
558 for l in &labels {
559 assert!(
560 l.len() == 4 && l.chars().all(|c| c.is_ascii_digit()),
561 "expected a bare year label, got {l:?}"
562 );
563 }
564 assert!(labels.contains(&"2014".to_string()));
565 }
566
567 #[test]
568 fn strftime_subset() {
569 let s = 1_615_766_400.0; assert_eq!(strftime(s, "%Y-%m-%d"), "2021-03-15");
571 assert_eq!(strftime(s, "%b %Y"), "Mar 2021");
572 assert_eq!(strftime(s, "%B"), "March");
573 assert_eq!(strftime(s, "100%%"), "100%");
574 }
575
576 #[test]
577 fn strftime_weekdays_and_unpadded() {
578 let s = 1_615_766_400.0; assert_eq!(strftime(s, "%a %A"), "Mon Monday");
580 assert_eq!(strftime(s - 86_400.0 * 10.0, "%-d.%-m. %d"), "5.3. 05");
581 assert_eq!(strftime(s, "%-x"), "%-x");
582 assert_eq!(strftime(-86_400.0, "%a"), "Wed"); }
584
585 #[test]
586 fn locale_month_and_day_labels() {
587 let de = crate::locale::Locale::de_de();
588 let mut s = ScaleDateTime::new().with_locale(de.clone());
590 s.set_limits(
591 secs_from_civil(2026, 7, 1) as f64,
592 secs_from_civil(2027, 2, 1) as f64,
593 );
594 let labels: Vec<String> = s.breaks().into_iter().map(|(_, l)| l).collect();
595 assert!(labels.contains(&"Okt. 2026".to_string()), "{labels:?}");
596 assert!(labels.contains(&"Jan. 2027".to_string()), "{labels:?}");
597 let mut plain = ScaleDateTime::new();
599 plain.set_limits(
600 secs_from_civil(2026, 7, 1) as f64,
601 secs_from_civil(2027, 2, 1) as f64,
602 );
603 let labels: Vec<String> = plain.breaks().into_iter().map(|(_, l)| l).collect();
604 assert!(labels.contains(&"2026-10".to_string()), "{labels:?}");
605 let mut f = ScaleDateTime::new()
607 .with_date_labels("%B")
608 .with_date_breaks("1 month");
609 f.set_default_locale(&de);
610 f.set_limits(
611 secs_from_civil(2026, 2, 20) as f64,
612 secs_from_civil(2026, 4, 2) as f64,
613 );
614 let labels: Vec<String> = f.breaks().into_iter().map(|(_, l)| l).collect();
615 assert_eq!(labels, ["März", "April"]);
616 let mut d = ScaleDateTime::new().with_locale(de);
618 d.set_limits(
619 secs_from_civil(2026, 10, 12) as f64,
620 secs_from_civil(2026, 10, 16) as f64,
621 );
622 let labels: Vec<String> = d.breaks().into_iter().map(|(_, l)| l).collect();
623 assert!(labels.contains(&"15. Okt. 2026".to_string()), "{labels:?}");
624 let mut own = ScaleDateTime::new().with_locale(crate::locale::Locale::fr_fr());
626 own.set_default_locale(&crate::locale::Locale::de_de());
627 assert_eq!(own.locale().unwrap().tag, "fr-FR");
628 }
629
630 #[test]
631 fn parse_specs() {
632 assert!(matches!(
633 parse_date_break("1 month"),
634 Some(DateBreak::Months(1))
635 ));
636 assert!(matches!(
637 parse_date_break("3 months"),
638 Some(DateBreak::Months(3))
639 ));
640 assert!(matches!(
641 parse_date_break("1 year"),
642 Some(DateBreak::Months(12))
643 ));
644 assert!(
645 matches!(parse_date_break("2 weeks"), Some(DateBreak::Secs(s)) if s == 1_209_600.0)
646 );
647 assert!(matches!(
648 parse_date_break("month"),
649 Some(DateBreak::Months(1))
650 ));
651 assert!(parse_date_break("fortnight").is_none());
652 }
653
654 #[test]
655 fn monthly_breaks_land_on_first_of_month() {
656 let mut s = ScaleDateTime::new()
657 .with_date_breaks("1 month")
658 .with_date_labels("%Y-%m-%d");
659 s.set_limits(
661 secs_from_civil(2021, 1, 10) as f64,
662 secs_from_civil(2021, 4, 20) as f64,
663 );
664 let labels: Vec<String> = s.breaks().into_iter().map(|(_, l)| l).collect();
665 assert!(labels.iter().all(|l| l.ends_with("-01")), "{labels:?}");
666 assert!(labels.contains(&"2021-02-01".to_string()));
667 }
668}