runlight 0.3.2

Privacy friendly web analytics that lives inside your Rust app: mount a route, add a script tag, read your stats at /runlight
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
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
//! Dates in a site's timezone. Ranges are computed here as epoch
//! milliseconds so the database only ever compares integers.
//!
//! The TypeScript reads local times through `Intl.DateTimeFormat`, which
//! knows ICU's zone names. jiff reads the system's time zone database, which
//! knows nearly the same ones, and the differences are settled here as the
//! PHP port settles them: a name is matched without regard to case, the few
//! that the database keeps as old fixed rules (CET, EST, MST) go to the zone
//! ICU reads them as, ICU's own extra names (PST, SystemV/EST5EDT) are
//! added, "Factory", which ICU refuses, is refused, and an offset such as
//! `+05:30` is a zone, as ECMA-402 takes one.

use std::collections::HashMap;
use std::sync::{LazyLock, Mutex};

use jiff::tz::{Offset, TimeZone};

use crate::js::{self, date_utc, iso_string};
use crate::re::{js_re, test};

/// The named periods a range may be.
pub const PERIODS: [&str; 10] =
    ["today", "yesterday", "7d", "30d", "90d", "month", "last_month", "year", "12mo", "all"];
/// The intervals a chart may be bucketed by.
pub const INTERVALS: [&str; 4] = ["hour", "day", "week", "month"];

const MAX_BUCKETS: usize = 1000;
/// A month of hours. A longer range is counted by the day instead.
const MAX_HOURS: usize = 744;

/// A span of time in a site's timezone.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Range {
    /// Inclusive.
    pub from: i64,
    /// Exclusive.
    pub to: i64,
    /// The first local date covered, YYYY-MM-DD, inclusive.
    pub from_date: String,
    /// The last local date covered, YYYY-MM-DD, inclusive.
    pub to_date: String,
    /// `hour`, `day`, `week`, or `month`.
    pub interval: &'static str,
}

/// One chart bucket.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Bucket {
    /// Inclusive.
    pub start: i64,
    /// Exclusive.
    pub end: i64,
}

/// Names Intl reads as another zone where the database has no name, or keeps
/// an old fixed rule.
const ALIASES: &[(&str, &str)] = &[
    ("cet", "Europe/Brussels"),
    ("eet", "Europe/Athens"),
    ("est", "America/Panama"),
    ("gmt", "UTC"),
    ("gmt+0", "UTC"),
    ("gmt-0", "UTC"),
    ("hst", "Pacific/Honolulu"),
    ("met", "Europe/Brussels"),
    ("mst", "America/Phoenix"),
    ("uct", "UTC"),
    ("wet", "Europe/Lisbon"),
    ("act", "Australia/Darwin"),
    ("aet", "Australia/Sydney"),
    ("agt", "America/Argentina/Buenos_Aires"),
    ("art", "Africa/Cairo"),
    ("ast", "America/Anchorage"),
    ("bet", "America/Sao_Paulo"),
    ("bst", "Asia/Dhaka"),
    ("cat", "Africa/Maputo"),
    ("cnt", "America/St_Johns"),
    ("cst", "America/Chicago"),
    ("ctt", "Asia/Shanghai"),
    ("eat", "Africa/Nairobi"),
    ("ect", "Europe/Paris"),
    ("iet", "America/Indiana/Indianapolis"),
    ("ist", "Asia/Kolkata"),
    ("jst", "Asia/Tokyo"),
    ("mit", "Pacific/Apia"),
    ("net", "Asia/Yerevan"),
    ("nst", "Pacific/Auckland"),
    ("plt", "Asia/Karachi"),
    ("pnt", "America/Phoenix"),
    ("prt", "America/Puerto_Rico"),
    ("pst", "America/Los_Angeles"),
    ("sst", "Pacific/Guadalcanal"),
    ("vst", "Asia/Ho_Chi_Minh"),
    ("systemv/ast4", "Etc/GMT+4"),
    ("systemv/ast4adt", "America/Halifax"),
    ("systemv/est5", "Etc/GMT+5"),
    ("systemv/est5edt", "America/New_York"),
    ("systemv/cst6", "Etc/GMT+6"),
    ("systemv/cst6cdt", "America/Chicago"),
    ("systemv/mst7", "Etc/GMT+7"),
    ("systemv/mst7mdt", "America/Denver"),
    ("systemv/pst8", "Etc/GMT+8"),
    ("systemv/pst8pdt", "America/Los_Angeles"),
    ("systemv/yst9", "Etc/GMT+9"),
    ("systemv/yst9ydt", "America/Anchorage"),
    ("systemv/hst10", "Etc/GMT+10"),
    ("canada/east-saskatchewan", "America/Regina"),
    ("us/pacific-new", "America/Los_Angeles"),
    // Old names the database keeps as links in its backward file. Debian, and
    // Ubuntu from 26.04, ship them apart (tzdata-legacy), so a system may not
    // have them; ICU reads them as the links. From tzdata 2026e.
    ("africa/asmera", "Africa/Asmara"),
    ("america/argentina/comodrivadavia", "America/Argentina/Catamarca"),
    ("america/buenos_aires", "America/Argentina/Buenos_Aires"),
    ("america/catamarca", "America/Argentina/Catamarca"),
    ("america/cordoba", "America/Argentina/Cordoba"),
    ("america/fort_wayne", "America/Indiana/Indianapolis"),
    ("america/godthab", "America/Nuuk"),
    ("america/indianapolis", "America/Indiana/Indianapolis"),
    ("america/jujuy", "America/Argentina/Jujuy"),
    ("america/knox_in", "America/Indiana/Knox"),
    ("america/louisville", "America/Kentucky/Louisville"),
    ("america/mendoza", "America/Argentina/Mendoza"),
    ("america/rosario", "America/Argentina/Cordoba"),
    ("antarctica/south_pole", "Antarctica/McMurdo"),
    ("asia/ashkhabad", "Asia/Ashgabat"),
    ("asia/calcutta", "Asia/Kolkata"),
    ("asia/choibalsan", "Asia/Ulaanbaatar"),
    ("asia/chungking", "Asia/Shanghai"),
    ("asia/dacca", "Asia/Dhaka"),
    ("asia/katmandu", "Asia/Kathmandu"),
    ("asia/macao", "Asia/Macau"),
    ("asia/rangoon", "Asia/Yangon"),
    ("asia/saigon", "Asia/Ho_Chi_Minh"),
    ("asia/thimbu", "Asia/Thimphu"),
    ("asia/ujung_pandang", "Asia/Makassar"),
    ("asia/ulan_bator", "Asia/Ulaanbaatar"),
    ("atlantic/faeroe", "Atlantic/Faroe"),
    ("australia/act", "Australia/Sydney"),
    ("australia/lhi", "Australia/Lord_Howe"),
    ("australia/north", "Australia/Darwin"),
    ("australia/nsw", "Australia/Sydney"),
    ("australia/queensland", "Australia/Brisbane"),
    ("australia/south", "Australia/Adelaide"),
    ("australia/tasmania", "Australia/Hobart"),
    ("australia/victoria", "Australia/Melbourne"),
    ("australia/west", "Australia/Perth"),
    ("brazil/acre", "America/Rio_Branco"),
    ("brazil/denoronha", "America/Noronha"),
    ("brazil/east", "America/Sao_Paulo"),
    ("brazil/west", "America/Manaus"),
    ("canada/atlantic", "America/Halifax"),
    ("canada/central", "America/Winnipeg"),
    ("canada/eastern", "America/Toronto"),
    ("canada/mountain", "America/Edmonton"),
    ("canada/newfoundland", "America/St_Johns"),
    ("canada/pacific", "America/Vancouver"),
    ("canada/saskatchewan", "America/Regina"),
    ("canada/yukon", "America/Whitehorse"),
    ("chile/continental", "America/Santiago"),
    ("chile/easterisland", "Pacific/Easter"),
    ("cst6cdt", "America/Chicago"),
    ("cuba", "America/Havana"),
    ("egypt", "Africa/Cairo"),
    ("eire", "Europe/Dublin"),
    ("est5edt", "America/New_York"),
    ("europe/kiev", "Europe/Kyiv"),
    ("europe/uzhgorod", "Europe/Kyiv"),
    ("europe/zaporozhye", "Europe/Kyiv"),
    ("gb", "Europe/London"),
    ("gb-eire", "Europe/London"),
    ("gmt0", "Etc/GMT"),
    ("greenwich", "Etc/GMT"),
    ("hongkong", "Asia/Hong_Kong"),
    ("iceland", "Atlantic/Reykjavik"),
    ("iran", "Asia/Tehran"),
    ("israel", "Asia/Jerusalem"),
    ("jamaica", "America/Jamaica"),
    ("japan", "Asia/Tokyo"),
    ("kwajalein", "Pacific/Kwajalein"),
    ("libya", "Africa/Tripoli"),
    ("mexico/bajanorte", "America/Tijuana"),
    ("mexico/bajasur", "America/Mazatlan"),
    ("mexico/general", "America/Mexico_City"),
    ("mst7mdt", "America/Denver"),
    ("navajo", "America/Denver"),
    ("nz", "Pacific/Auckland"),
    ("nz-chat", "Pacific/Chatham"),
    ("pacific/enderbury", "Pacific/Kanton"),
    ("pacific/ponape", "Pacific/Pohnpei"),
    ("pacific/truk", "Pacific/Chuuk"),
    ("poland", "Europe/Warsaw"),
    ("portugal", "Europe/Lisbon"),
    ("prc", "Asia/Shanghai"),
    ("pst8pdt", "America/Los_Angeles"),
    ("roc", "Asia/Taipei"),
    ("rok", "Asia/Seoul"),
    ("singapore", "Asia/Singapore"),
    ("turkey", "Europe/Istanbul"),
    ("universal", "Etc/UTC"),
    ("us/alaska", "America/Anchorage"),
    ("us/aleutian", "America/Adak"),
    ("us/arizona", "America/Phoenix"),
    ("us/central", "America/Chicago"),
    ("us/east-indiana", "America/Indiana/Indianapolis"),
    ("us/eastern", "America/New_York"),
    ("us/hawaii", "Pacific/Honolulu"),
    ("us/indiana-starke", "America/Indiana/Knox"),
    ("us/michigan", "America/Detroit"),
    ("us/mountain", "America/Denver"),
    ("us/pacific", "America/Los_Angeles"),
    ("us/samoa", "Pacific/Pago_Pago"),
    ("w-su", "Europe/Moscow"),
    ("zulu", "Etc/UTC"),
];

static ZONES: LazyLock<Mutex<HashMap<String, Option<TimeZone>>>> = LazyLock::new(|| Mutex::new(HashMap::new()));

/// The zone `Intl.DateTimeFormat` would use for a `timeZone` option, or
/// `None` where it throws a RangeError.
fn zone(timezone: &str) -> Option<TimeZone> {
    let mut zones = ZONES.lock().unwrap_or_else(|e| e.into_inner());
    if let Some(found) = zones.get(timezone) {
        return found.clone();
    }
    let opened = open(timezone);
    zones.insert(timezone.to_string(), opened.clone());
    opened
}

fn open(timezone: &str) -> Option<TimeZone> {
    // An offset, as ECMA-402 takes one: a sign (a minus sign too), two digit hours, and optional minutes.
    let offset = regex::Regex::new(r"^([+\-]|\x{2212})([01][0-9]|2[0-3])(?::?([0-5][0-9]))?$").ok()?;
    if let Some(m) = offset.captures(timezone) {
        let sign = if &m[1] == "+" { 1 } else { -1 };
        let hours: i32 = m[2].parse().ok()?;
        let minutes: i32 = m.get(3).map_or(Some(0), |x| x.as_str().parse().ok())?;
        return Some(TimeZone::fixed(Offset::from_seconds(sign * (hours * 3600 + minutes * 60)).ok()?));
    }
    if !timezone.bytes().all(|b| (0x21..=0x7e).contains(&b)) {
        return None;
    }
    let key = timezone.to_ascii_lowercase();
    // Names the system's database has and ICU does not.
    if matches!(
        key.as_str(),
        "factory" | "localtime" | "posixrules" | "etc/unknown" | "asia/riyadh87" | "mideast/riyadh87"
    ) || key.starts_with("posix/")
        || key.starts_with("right/")
    {
        return None;
    }
    if let Some((_, name)) = ALIASES.iter().find(|(k, _)| *k == key) {
        return TimeZone::get(name).ok();
    }
    TimeZone::get(timezone).ok()
}

/// Whether `Intl` takes the name as a time zone.
pub fn is_timezone(value: &str) -> bool {
    zone(value).is_some()
}

/// Year, month, day, hour, minute, and second of an instant in a zone, as
/// Intl formats them. An unknown zone reads as UTC.
fn parts(ts: i64, timezone: &str) -> [i64; 6] {
    let tz = zone(timezone).unwrap_or(TimeZone::UTC);
    let seconds = ts.div_euclid(1000);
    let Ok(at) = jiff::Timestamp::from_second(seconds) else {
        return [1970, 1, 1, 0, 0, 0];
    };
    let dt = tz.to_datetime(at);
    [
        i64::from(dt.year()),
        i64::from(dt.month()),
        i64::from(dt.day()),
        i64::from(dt.hour()),
        i64::from(dt.minute()),
        i64::from(dt.second()),
    ]
}

/// Milliseconds the zone is ahead of UTC at an instant.
fn offset(ts: i64, timezone: &str) -> i64 {
    let [y, mo, d, h, mi, s] = parts(ts, timezone);
    date_utc(y, mo - 1, d, h, mi, s, 0) - (ts - ts % 1000)
}

fn ymd(date: &str) -> (i64, i64, i64) {
    let mut it = date.split('-').map(|p| js::text_number(p) as i64);
    (it.next().unwrap_or(0), it.next().unwrap_or(0), it.next().unwrap_or(0))
}

/// The instant a local date (and hour) begins in a zone.
pub fn start_of(date: &str, timezone: &str, hour: i64) -> i64 {
    let (y, m, d) = ymd(date);
    let guess = date_utc(y, m - 1, d, hour, 0, 0, 0);
    let first = guess - offset(guess, timezone);
    let mut at = guess - offset(first, timezone);
    // Where clocks jump forward at that time (midnight in Santiago, Havana, and the Azores), it never
    // happens, and the sum above lands before it; the day then begins when the clocks land, at most a
    // few quarter hours on.
    for _ in 0..8 {
        let [ly, lm, ld, lh, _, _] = parts(at, timezone);
        if date_utc(ly, lm - 1, ld, lh, 0, 0, 0) >= guess {
            break;
        }
        at += 15 * 60_000;
    }
    // Where clocks fall back across that hour (midnight in Amman in 2019, three hours at Casey station), it
    // happens twice; the day begins at the first.
    if offset(at - 3 * 3_600_000, timezone) == offset(at, timezone) {
        return at;
    }
    for step in 0..12 {
        let back = 3 * 60 - step * 15;
        let [ly, lm, ld, lh, _, _] = parts(at - back * 60_000, timezone);
        if date_utc(ly, lm - 1, ld, lh, 0, 0, 0) == guess {
            return at - back * 60_000;
        }
    }
    at
}

/// The local date of an instant, YYYY-MM-DD.
pub fn local_date(ts: i64, timezone: &str) -> String {
    let [y, m, d, ..] = parts(ts, timezone);
    format!("{y:04}-{m:02}-{d:02}")
}

/// `new Date(Date.UTC(y, m - 1, d + days)).toISOString().slice(0, 10)`.
pub fn add_days(date: &str, days: i64) -> String {
    let (y, m, d) = ymd(date);
    js::head16(&iso_string(date_utc(y, m - 1, d + days, 0, 0, 0, 0)), 10)
}

/// The first of the month `months` from the date's.
pub fn add_months(date: &str, months: i64) -> String {
    let (y, m, _) = ymd(date);
    js::head16(&iso_string(date_utc(y, m - 1 + months, 1, 0, 0, 0, 0)), 10)
}

/// Whether the text is a date from 1900 to 9998 that exists.
pub fn is_date(value: &str) -> bool {
    // Years from 1900 to 9998, so the day after any date is a date too.
    if !test(js_re!(r"^\d{4}-\d{2}-\d{2}$"), value) || !("1900".."9999").contains(&value) {
        return false;
    }
    // A month or day that does not exist (2026-13-01) makes no date at all, rather than a wrong one.
    let (y, m, d) = ymd(value);
    if !(1..=12).contains(&m) || !(1..=31).contains(&d) {
        return false;
    }
    js::head16(&iso_string(date_utc(y, m - 1, d, 0, 0, 0, 0)), 10) == value
}

fn date_ms(date: &str) -> i64 {
    let (y, m, d) = ymd(date);
    date_utc(y, m - 1, d, 0, 0, 0, 0)
}

fn days_between(from: &str, to: &str) -> i64 {
    js::round_i64((date_ms(to) - date_ms(from)) as f64 / 86_400_000.0)
}

fn default_interval(from_date: &str, to_date: &str) -> &'static str {
    let days = days_between(from_date, to_date);
    if days < 1 {
        "hour"
    } else if days <= 92 {
        "day"
    } else {
        "month"
    }
}

/// What a range is asked for with: a period, or custom dates, and an
/// interval.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct RangeInput {
    /// A named period.
    pub period: Option<String>,
    /// Custom dates.
    pub from: Option<String>,
    /// Custom dates.
    pub to: Option<String>,
    /// The interval.
    pub interval: Option<String>,
}

fn filled(v: &Option<String>) -> Option<&str> {
    v.as_deref().filter(|s| !s.is_empty())
}

/// A named period or custom dates as a range in the site's timezone.
/// `first_date` is the earliest local date with data, used by "all".
pub fn resolve_range(input: &RangeInput, timezone: &str, now: i64, first_date: Option<&str>) -> Option<Range> {
    let today = local_date(now, timezone);
    let (from_date, to_date);
    if filled(&input.from).is_some() || filled(&input.to).is_some() {
        let (from, to) = (filled(&input.from)?, filled(&input.to)?);
        if !is_date(from) || !is_date(to) || from > to {
            return None;
        }
        from_date = from.to_string();
        to_date = to.to_string();
    } else {
        let period = input.period.as_deref().unwrap_or("30d");
        let month_start = format!("{}01", js::head16(&today, 8));
        (from_date, to_date) = match period {
            "today" => (today.clone(), today.clone()),
            "yesterday" => (add_days(&today, -1), add_days(&today, -1)),
            "7d" => (add_days(&today, -6), today.clone()),
            "30d" => (add_days(&today, -29), today.clone()),
            "90d" => (add_days(&today, -89), today.clone()),
            "month" => (month_start, today.clone()),
            "last_month" => (add_months(&today, -1), add_days(&month_start, -1)),
            "year" => (format!("{}-01-01", js::head16(&today, 4)), today.clone()),
            "12mo" => (add_months(&today, -11), today.clone()),
            "all" => (
                match first_date {
                    Some(f) if !f.is_empty() && f < today.as_str() => f.to_string(),
                    _ => today.clone(),
                },
                today.clone(),
            ),
            _ => return None,
        };
    }
    let mut interval = INTERVALS
        .iter()
        .find(|i| input.interval.as_deref() == Some(**i))
        .copied()
        .unwrap_or_else(|| default_interval(&from_date, &to_date));
    let from = start_of(&from_date, timezone, 0);
    let to = start_of(&add_days(&to_date, 1), timezone, 0);
    // An interval too fine for the range moves up to the next one that fits, and the range says which it is.
    // Past 1,000 months there is none.
    while bucket_count(from, to, &from_date, &to_date, interval)
        > if interval == "hour" { MAX_HOURS } else { MAX_BUCKETS } as i64
    {
        interval = match interval {
            "hour" => "day",
            "day" => "week",
            "week" => "month",
            _ => return None,
        };
    }
    Some(Range { from, to, from_date, to_date, interval })
}

/// How many chart buckets a range has at an interval, the first and last counted even when partial.
fn bucket_count(from: i64, to: i64, from_date: &str, to_date: &str, interval: &str) -> i64 {
    match interval {
        "hour" => (to - from + 3_599_999).div_euclid(3_600_000),
        "day" => days_between(from_date, to_date) + 1,
        "week" => {
            let weekday = (weekday_of(from_date) + 6) % 7;
            days_between(&add_days(from_date, -weekday), to_date).div_euclid(7) + 1
        }
        _ => {
            let (fy, fm, _) = ymd(from_date);
            let (ty, tm, _) = ymd(to_date);
            (ty - fy) * 12 + (tm - fm) + 1
        }
    }
}

fn add_years(date: &str, years: i64) -> String {
    let (y, m, d) = ymd(date);
    let mut shifted = date_utc(y + years, m - 1, d, 0, 0, 0, 0);
    // Feb 29 in a year without one becomes Feb 28, not Mar 1.
    let (sy, sm, _) = js::civil_from_days(shifted.div_euclid(86_400_000));
    if sm != m {
        shifted = date_utc(sy, sm - 1, 0, 0, 0, 0, 0);
    }
    js::head16(&iso_string(shifted), 10)
}

/// The range a period is compared with: the same number of days just
/// before it, the same dates a year earlier, or custom dates. `None` for
/// "off" or bad custom dates.
pub fn compare_range(
    range: &Range,
    mode: &str,
    timezone: &str,
    custom_from: Option<&str>,
    custom_to: Option<&str>,
) -> Option<Range> {
    let (from_date, to_date);
    match mode {
        "off" => return None,
        "year" => {
            from_date = add_years(&range.from_date, -1);
            to_date = add_years(&range.to_date, -1);
        }
        "custom" => {
            let from = custom_from.filter(|s| !s.is_empty())?;
            let to = custom_to.filter(|s| !s.is_empty())?;
            if !is_date(from) || !is_date(to) || from > to {
                return None;
            }
            from_date = from.to_string();
            to_date = to.to_string();
        }
        _ => {
            let days = days_between(&range.from_date, &range.to_date) + 1;
            from_date = add_days(&range.from_date, -days);
            to_date = add_days(&range.from_date, -1);
        }
    }
    Some(Range {
        from: start_of(&from_date, timezone, 0),
        to: start_of(&add_days(&to_date, 1), timezone, 0),
        from_date,
        to_date,
        interval: range.interval,
    })
}

fn weekday_of(date: &str) -> i64 {
    // getUTCDay: 0 is Sunday; 1970-01-01 was a Thursday.
    (date_ms(date).div_euclid(86_400_000) + 4).rem_euclid(7)
}

/// Chart buckets covering a range, each starting on a local boundary and ending at the next, so only the
/// first and last can be partial. `resolve_range` picks an interval that fits; a range given here with more
/// buckets than that, such as a longer comparison, is cut off at a boundary, never merged into its last bucket.
pub fn buckets(range: &Range, timezone: &str) -> Vec<Bucket> {
    let mut starts: Vec<i64> = Vec::new();
    let most = if range.interval == "hour" { MAX_HOURS } else { MAX_BUCKETS };
    // One start past the limit, so the last bucket kept still ends where the next would begin.
    if range.interval == "hour" {
        let mut t = range.from;
        while t < range.to && starts.len() <= MAX_HOURS {
            starts.push(t);
            t += 3_600_000;
        }
    } else {
        let mut date = range.from_date.clone();
        if range.interval == "week" {
            let weekday = (weekday_of(&date) + 6) % 7;
            date = add_days(&date, -weekday);
        } else if range.interval == "month" {
            date = format!("{}01", js::head16(&date, 8));
        }
        while date <= range.to_date && starts.len() <= MAX_BUCKETS {
            starts.push(start_of(&date, timezone, 0));
            date = match range.interval {
                "day" => add_days(&date, 1),
                "week" => add_days(&date, 7),
                _ => add_months(&date, 1),
            };
        }
    }
    (0..starts.len().min(most))
        .map(|i| Bucket {
            start: starts[i].max(range.from),
            end: starts.get(i + 1).copied().unwrap_or(range.to).min(range.to),
        })
        .collect()
}

/// The local weekday (Monday is 0) and hour of an instant.
pub fn local_weekday_hour(ts: i64, timezone: &str) -> (i64, i64) {
    let [y, m, d, h, ..] = parts(ts, timezone);
    let days = date_utc(y, m - 1, d, 0, 0, 0, 0).div_euclid(86_400_000);
    // getUTCDay counts from Sunday, and 1970-01-01 was a Thursday.
    (((days + 4).rem_euclid(7) + 6) % 7, h)
}