phplang 0.2.2

PHP as a fusevm frontend: lexer, parser, and compiler to fusevm::Chunk on a PhpHost heap — the first compiled standalone PHP runtime
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
//! PHP standard-library `datetime` functions. Part of the `stdlib` chain; see
//! `src/stdlib/mod.rs`. `dispatch` returns `None` for names it does not handle.
//!
//! Date math is delegated to `chrono` (UTC / `NaiveDateTime`). The reference
//! `php` computes `date()` against the process's default timezone; without a tz
//! database (`chrono-tz` is not a dependency) every calculation here runs in UTC,
//! so `date()` and `gmdate()` are equivalent. `date_default_timezone_set/get`
//! still round-trip the configured name, matching PHP's default of "UTC".

use crate::host::with_host;
use crate::stdlib::common::*;
use chrono::{DateTime, Datelike, Duration, NaiveDate, TimeZone, Timelike, Utc};
use fusevm::Value;
use std::cell::RefCell;

thread_local! {
    /// Process default timezone name; PHP defaults to "UTC" when unset.
    static TZ: RefCell<String> = RefCell::new("UTC".to_string());
}

/// Dispatch a `datetime`-category PHP function by lowercased name.
pub fn dispatch(name: &str, args: &[Value]) -> Option<Result<Value, String>> {
    let v = match name {
        "time" => Value::int(Utc::now().timestamp()),
        "mktime" | "gmmktime" => php_mktime(args),
        "date" => php_date(args, false),
        // `T` is the ONE format character the two spellings disagree on: with the
        // default timezone at UTC, `date('T')` is `UTC` and `gmdate('T')` is `GMT`.
        "gmdate" => php_date(args, true),
        "checkdate" => php_checkdate(args),
        "microtime" => php_microtime(args),
        "strtotime" => php_strtotime(args),
        "getdate" => php_getdate(args),
        // Records the configured name and always returns true. LIMITATION: without
        // a `chrono-tz` database, unknown timezone names are NOT rejected (PHP
        // returns false for those) and setting a non-UTC zone does NOT shift
        // subsequent `date()` output (every calculation here runs in UTC). Low
        // priority; documented deviation.
        "date_default_timezone_set" => {
            let tz = str_arg(args, 0);
            TZ.with(|t| *t.borrow_mut() = tz);
            Value::bool(true)
        }
        "date_default_timezone_get" => Value::str(TZ.with(|t| t.borrow().clone())),
        _ => return None,
    };
    Some(Ok(v))
}

/// Full weekday names, Sunday-first (matches `chrono` `num_days_from_sunday`).
const DAYS: [&str; 7] = [
    "Sunday",
    "Monday",
    "Tuesday",
    "Wednesday",
    "Thursday",
    "Friday",
    "Saturday",
];
/// Full month names, 1-based (index 0 unused).
const MONTHS: [&str; 13] = [
    "",
    "January",
    "February",
    "March",
    "April",
    "May",
    "June",
    "July",
    "August",
    "September",
    "October",
    "November",
    "December",
];

/// UTC `DateTime` for a Unix timestamp; the epoch on any out-of-range value.
fn from_ts(ts: i64) -> DateTime<Utc> {
    DateTime::from_timestamp(ts, 0).unwrap_or_else(|| DateTime::from_timestamp(0, 0).unwrap())
}

/// PHP `time()`-style current timestamp, used as the implicit default.
fn now_ts() -> i64 {
    Utc::now().timestamp()
}

/// `date`/`gmdate`: format a timestamp (default: now) with a PHP format string.
fn php_date(args: &[Value], gm: bool) -> Value {
    let fmt = str_arg(args, 0);
    let ts = if args.len() >= 2 {
        int_arg(args, 1)
    } else {
        now_ts()
    };
    Value::str(format_php(&fmt, from_ts(ts), gm))
}

/// Render `dt` per PHP `date()` format characters. `\` escapes the next char.
///
/// Everything is UTC here (see the module header), which fixes every one of the
/// timezone characters to a constant: offset `+0000`, DST off, and the zone
/// named `UTC` — or `GMT` for `T` under `gmdate`, which is the only place the
/// two spellings differ.
fn format_php(fmt: &str, dt: DateTime<Utc>, gm: bool) -> String {
    let mut out = String::with_capacity(fmt.len() * 2);
    let mut chars = fmt.chars();
    let dow_sun = dt.weekday().num_days_from_sunday() as usize; // 0=Sun..6=Sat
    let dow_iso = dt.weekday().number_from_monday(); // 1=Mon..7=Sun
    let day = dt.day();
    let month = dt.month();
    let hour24 = dt.hour();
    let hour12 = match hour24 % 12 {
        0 => 12,
        h => h,
    };
    while let Some(c) = chars.next() {
        match c {
            '\\' => {
                if let Some(n) = chars.next() {
                    out.push(n);
                }
            }
            'd' => out.push_str(&format!("{day:02}")),
            'j' => out.push_str(&day.to_string()),
            'D' => out.push_str(&DAYS[dow_sun][..3]),
            'l' => out.push_str(DAYS[dow_sun]),
            'N' => out.push_str(&dow_iso.to_string()),
            'w' => out.push_str(&dow_sun.to_string()),
            'S' => out.push_str(ordinal_suffix(day)),
            'z' => out.push_str(&dt.ordinal0().to_string()),
            'W' => out.push_str(&format!("{:02}", dt.iso_week().week())),
            'F' => out.push_str(MONTHS[month as usize]),
            'M' => out.push_str(&MONTHS[month as usize][..3]),
            'm' => out.push_str(&format!("{month:02}")),
            'n' => out.push_str(&month.to_string()),
            't' => out.push_str(&days_in_month(dt.year(), month).to_string()),
            'L' => out.push_str(if is_leap(dt.year()) { "1" } else { "0" }),
            'Y' => out.push_str(&dt.year().to_string()),
            'y' => out.push_str(&format!("{:02}", dt.year().rem_euclid(100))),
            'a' => out.push_str(if hour24 < 12 { "am" } else { "pm" }),
            'A' => out.push_str(if hour24 < 12 { "AM" } else { "PM" }),
            'g' => out.push_str(&hour12.to_string()),
            'G' => out.push_str(&hour24.to_string()),
            'h' => out.push_str(&format!("{hour12:02}")),
            'H' => out.push_str(&format!("{hour24:02}")),
            'i' => out.push_str(&format!("{:02}", dt.minute())),
            's' => out.push_str(&format!("{:02}", dt.second())),
            'U' => out.push_str(&dt.timestamp().to_string()),
            // ISO-8601 week-numbering year (differs from `Y` near Jan 1 / Dec 31).
            'o' => out.push_str(&dt.iso_week().year().to_string()),
            // Microseconds / milliseconds. Timestamps here carry no sub-second
            // component, so these render as zeros, matching PHP integer-second input.
            'u' => out.push_str(&format!("{:06}", dt.timestamp_subsec_micros())),
            'v' => out.push_str(&format!("{:03}", dt.timestamp_subsec_millis())),
            // ISO 8601 date, e.g. "2004-02-12T15:19:21+00:00" (UTC only here).
            'c' => out.push_str(&format!(
                "{:04}-{month:02}-{day:02}T{hour24:02}:{:02}:{:02}+00:00",
                dt.year(),
                dt.minute(),
                dt.second()
            )),
            // RFC 2822 date, e.g. "Thu, 21 Dec 2000 16:01:07 +0000" (UTC only here).
            'r' => out.push_str(&format!(
                "{}, {day:02} {} {} {hour24:02}:{:02}:{:02} +0000",
                &DAYS[dow_sun][..3],
                &MONTHS[month as usize][..3],
                dt.year(),
                dt.minute(),
                dt.second()
            )),
            // ── timezone, all constant in a UTC-only engine ──────────────────
            // The zone identifier and its abbreviation. `T` is the one place
            // `date` and `gmdate` disagree: with the default timezone at UTC the
            // first says `UTC` and the second says `GMT`.
            'e' => out.push_str("UTC"),
            'T' => out.push_str(if gm { "GMT" } else { "UTC" }),
            'I' => out.push('0'),
            'Z' => out.push('0'),
            'O' => out.push_str("+0000"),
            'P' => out.push_str("+00:00"),
            // `p` is `P` with `Z` for a zero offset (PHP 8.0).
            'p' => out.push('Z'),
            // ── expanded years (PHP 8.2) ─────────────────────────────────────
            // `X` always carries a sign; `x` carries one only outside 0000-9999.
            // Both pad to at least four digits, so year -500 is `-0500` under
            // either and year 12345 is `+12345`.
            'X' => out.push_str(&expanded_year(dt.year(), true)),
            'x' => out.push_str(&expanded_year(dt.year(), false)),
            // Swatch Internet Time: the day divided into 1000 beats, counted
            // from midnight in UTC+1, so it is a pure function of the timestamp.
            'B' => {
                let secs = (dt.timestamp() + 3600).rem_euclid(86_400);
                out.push_str(&format!("{:03}", secs * 1000 / 86_400));
            }
            other => out.push(other),
        }
    }
    out
}

/// PHP's `X`/`x` expanded year: at least four digits, signed always for `X` and
/// only outside the four-digit range for `x`.
fn expanded_year(year: i32, always_sign: bool) -> String {
    let sign = if year < 0 {
        "-"
    } else if always_sign || year > 9999 {
        "+"
    } else {
        ""
    };
    format!("{sign}{:04}", year.abs())
}

/// English ordinal suffix for a day-of-month (`1`→"st", `11`→"th", `22`→"nd").
fn ordinal_suffix(day: u32) -> &'static str {
    match (day % 10, day % 100) {
        (1, 11) | (2, 12) | (3, 13) => "th",
        (1, _) => "st",
        (2, _) => "nd",
        (3, _) => "rd",
        _ => "th",
    }
}

/// Proleptic-Gregorian leap-year test.
fn is_leap(year: i32) -> bool {
    (year % 4 == 0 && year % 100 != 0) || year % 400 == 0
}

/// Number of days in a given month.
fn days_in_month(year: i32, month: u32) -> u32 {
    match month {
        1 | 3 | 5 | 7 | 8 | 10 | 12 => 31,
        4 | 6 | 9 | 11 => 30,
        2 if is_leap(year) => 29,
        2 => 28,
        _ => 30,
    }
}

/// `mktime`/`gmmktime`: build a timestamp from
/// `(hour, minute, second, month, day, year)`, each defaulting to the current
/// UTC component. Out-of-range fields normalize as PHP's do (month 13 → next
/// January, day 0 → prior month's last day), via signed arithmetic on a base
/// date-time. Returns `false` when the result falls outside chrono's range —
/// e.g. `mktime(0,0,0,1,999999999999,2020)` yields `false` (a non-crashing
/// deviation from PHP, which would produce a far-future timestamp). Every add is
/// checked so huge field values can never panic.
fn php_mktime(args: &[Value]) -> Value {
    let now = Utc::now();
    let comp = |i: usize, default: i64| -> i64 {
        if args.len() > i {
            int_arg(args, i)
        } else {
            default
        }
    };
    let hour = comp(0, now.hour() as i64);
    let minute = comp(1, now.minute() as i64);
    let second = comp(2, now.second() as i64);
    let month = comp(3, now.month() as i64);
    let day = comp(4, now.day() as i64);
    let year = normalize_year(comp(5, now.year() as i64));

    // Fold the (possibly out-of-range) month into a year/month pair, guarding
    // every step so absurd inputs return false instead of panicking.
    let Some(months_total) = year
        .checked_mul(12)
        .and_then(|x| month.checked_sub(1).and_then(|mm| x.checked_add(mm)))
    else {
        return Value::bool(false);
    };
    let y = months_total.div_euclid(12);
    let m = (months_total.rem_euclid(12) + 1) as u32;
    let Ok(y) = i32::try_from(y) else {
        return Value::bool(false);
    };
    let Some(base) = NaiveDate::from_ymd_opt(y, m, 1).and_then(|d| d.and_hms_opt(0, 0, 0)) else {
        return Value::bool(false);
    };
    // Checked signed arithmetic: `Duration::try_*` reject out-of-range spans and
    // `checked_add`/`checked_add_signed` reject overflow of the running total.
    let (Some(dd), Some(dh), Some(dmin), Some(ds)) = (
        day.checked_sub(1).and_then(Duration::try_days),
        Duration::try_hours(hour),
        Duration::try_minutes(minute),
        Duration::try_seconds(second),
    ) else {
        return Value::bool(false);
    };
    let Some(delta) = dd
        .checked_add(&dh)
        .and_then(|x| x.checked_add(&dmin))
        .and_then(|x| x.checked_add(&ds))
    else {
        return Value::bool(false);
    };
    let Some(dt) = base.checked_add_signed(delta) else {
        return Value::bool(false);
    };
    Value::int(Utc.from_utc_datetime(&dt).timestamp())
}

/// PHP's two-digit year fixup: 0-69 → 2000-2069, 70-100 → 1970-2000. Four-digit
/// years pass through untouched.
fn normalize_year(year: i64) -> i64 {
    match year {
        0..=69 => year + 2000,
        70..=100 => year + 1900,
        _ => year,
    }
}

/// `checkdate(month, day, year)`: valid Gregorian date, year 1-32767.
fn php_checkdate(args: &[Value]) -> Value {
    let month = int_arg(args, 0);
    let day = int_arg(args, 1);
    let year = int_arg(args, 2);
    let ok = (1..=12).contains(&month)
        && (1..=32767).contains(&year)
        && day >= 1
        && day <= days_in_month(year as i32, month as u32) as i64;
    Value::bool(ok)
}

/// `microtime(as_float=false)`: with a truthy argument, seconds since the epoch
/// as a float; otherwise the classic `"msec sec"` string.
fn php_microtime(args: &[Value]) -> Value {
    let now = Utc::now();
    let secs = now.timestamp();
    let frac = now.timestamp_subsec_micros() as f64 / 1_000_000.0;
    let as_float = with_host(|h| h.is_truthy(&arg(args, 0)));
    if as_float {
        Value::float(secs as f64 + frac)
    } else {
        Value::str(format!("{frac:.8} {secs}"))
    }
}

/// `strtotime(time, baseTimestamp=now)`: parse a subset of PHP's date/time
/// grammar — "now", "today"/"yesterday"/"tomorrow", absolute `YYYY-MM-DD` and
/// `YYYY-MM-DD HH:MM:SS`, `@<unixts>`, and chained relative offsets such as
/// `"+1 week"`, `"-3 days"`, `"2 months 1 day"`. Returns `false` on parse
/// failure, mirroring PHP.
fn php_strtotime(args: &[Value]) -> Value {
    let input = str_arg(args, 0).trim().to_string();
    let base = if args.len() >= 2 {
        int_arg(args, 1)
    } else {
        now_ts()
    };
    match parse_strtotime(&input, base) {
        Some(ts) => Value::int(ts),
        None => Value::bool(false),
    }
}

/// Core of `strtotime`; `None` signals an unparseable string.
fn parse_strtotime(input: &str, base: i64) -> Option<i64> {
    // PHP 8: an empty string is a parse failure, not the base timestamp.
    if input.is_empty() {
        return None;
    }
    let low = input.to_ascii_lowercase();
    match low.as_str() {
        "now" => return Some(base),
        "today" | "midnight" => return Some(start_of_day(base)),
        "yesterday" => return Some(start_of_day(base) - 86_400),
        "tomorrow" => return Some(start_of_day(base) + 86_400),
        _ => {}
    }
    // "@<timestamp>" — explicit Unix seconds.
    if let Some(rest) = input.strip_prefix('@') {
        return rest.trim().parse::<i64>().ok();
    }
    // A trailing timezone is taken off first and applied to the result. The
    // absolute formats below are exact, so `"1970-01-02 UTC"` did not match any
    // of them and the whole call answered `false`.
    let (input, offset) = match split_timezone(input) {
        Some(split) => split,
        None => (input, 0),
    };
    // Absolute "YYYY-MM-DD HH:MM:SS" then "YYYY-MM-DD".
    if let Ok(dt) = chrono::NaiveDateTime::parse_from_str(input, "%Y-%m-%d %H:%M:%S") {
        return Some(Utc.from_utc_datetime(&dt).timestamp() - offset);
    }
    if let Some(dt) = NaiveDate::parse_from_str(input, "%Y-%m-%d")
        .ok()
        .and_then(|d| d.and_hms_opt(0, 0, 0))
    {
        return Some(Utc.from_utc_datetime(&dt).timestamp() - offset);
    }
    parse_relative(&low, base)
}

/// Split a trailing timezone off an absolute date string: the remainder, and
/// the zone's offset in seconds EAST of UTC.
///
/// Only the zones this module can honour EXACTLY are accepted — `UTC`, `GMT`,
/// `UT`, `Z`, and a numeric `+HH:MM` / `+HHMM` / `+HH` offset. A named zone from
/// the tz database (`America/New_York`, `EST`) is left alone and the parse
/// fails as before: without `chrono-tz` there is no offset to apply, and
/// answering with UTC's would be a wrong timestamp rather than a refusal.
fn split_timezone(s: &str) -> Option<(&str, i64)> {
    // A bare trailing `Z` is written with no separator (`…T12:00:00Z`).
    if let Some(head) = s.strip_suffix(['Z', 'z']) {
        if !head.is_empty() {
            return Some((head.trim_end(), 0));
        }
    }
    // Split on WHITESPACE only. Splitting on `T` as well — the ISO 8601 date
    // separator — cut `"1970-01-02 UTC"` inside the zone name itself.
    let (head, tail) = s.rsplit_once(char::is_whitespace)?;
    let head = head.trim_end();
    if head.is_empty() {
        return None;
    }
    if tail.eq_ignore_ascii_case("utc")
        || tail.eq_ignore_ascii_case("gmt")
        || tail.eq_ignore_ascii_case("ut")
        || tail.eq_ignore_ascii_case("z")
    {
        return Some((head, 0));
    }
    // `+HH:MM`, `+HHMM`, `+HH` — and the same with `-`.
    let sign = match tail.as_bytes().first()? {
        b'+' => 1,
        b'-' => -1,
        _ => return None,
    };
    let digits: String = tail[1..].chars().filter(|c| *c != ':').collect();
    if !digits.bytes().all(|b| b.is_ascii_digit()) {
        return None;
    }
    let (h, m) = match digits.len() {
        2 => (digits.parse::<i64>().ok()?, 0),
        4 => (digits[..2].parse().ok()?, digits[2..].parse().ok()?),
        _ => return None,
    };
    (h < 24 && m < 60).then_some((head, sign * (h * 3600 + m * 60)))
}

/// Midnight (UTC) of the day containing `ts`.
fn start_of_day(ts: i64) -> i64 {
    let d = from_ts(ts).date_naive().and_hms_opt(0, 0, 0).unwrap();
    Utc.from_utc_datetime(&d).timestamp()
}

/// Apply one or more `"[+-]N unit"` offsets (optionally with a trailing "ago")
/// to `base`. Every token must match or the whole parse fails.
fn parse_relative(low: &str, base: i64) -> Option<i64> {
    let re = regex::Regex::new(
        r"([+-]?\d+)\s*(sec(?:ond)?|min(?:ute)?|hour|day|week|month|year)s?(\s+ago)?",
    )
    .ok()?;
    let mut dt = from_ts(base);
    let mut matched_end = 0usize;
    let mut any = false;
    for cap in re.captures_iter(low) {
        let whole = cap.get(0).unwrap();
        // Reject stray characters between tokens (keeps parsing strict).
        let gap = low[matched_end..whole.start()].trim();
        if !gap.is_empty() {
            return None;
        }
        matched_end = whole.end();
        any = true;
        let mut n: i64 = cap[1].parse().ok()?;
        if cap.get(3).is_some() {
            n = -n; // "N unit ago"
        }
        dt = apply_unit(dt, &cap[2], n)?;
    }
    // Require at least one token and no unconsumed trailing text.
    if !any || !low[matched_end..].trim().is_empty() {
        return None;
    }
    Some(dt.timestamp())
}

/// Shift `dt` by `n` of the named unit; month/year use calendar arithmetic.
/// Every add is checked, so huge `n` yields `None` (→ `false`) instead of a panic.
fn apply_unit(dt: DateTime<Utc>, unit: &str, n: i64) -> Option<DateTime<Utc>> {
    let dur = match unit {
        "sec" | "second" => Duration::try_seconds(n)?,
        "min" | "minute" => Duration::try_minutes(n)?,
        "hour" => Duration::try_hours(n)?,
        "day" => Duration::try_days(n)?,
        "week" => Duration::try_weeks(n)?,
        "month" => return add_months_overflow(dt, n),
        "year" => return add_months_overflow(dt, n.checked_mul(12)?),
        _ => return None,
    };
    dt.checked_add_signed(dur)
}

/// Add `months` (may be negative) to `dt`, matching PHP's **overflowing** month
/// arithmetic rather than chrono's clamping `checked_add_months`. PHP keeps the
/// day-of-month and lets it spill into the following month when the target month
/// is shorter: `2011-01-31 +1 month` → `2011-03-03`, not `2011-02-28`. Achieved
/// by landing on the first of the target month and adding `(day-1)` days.
/// Returns `None` on year/day overflow (→ `false`).
fn add_months_overflow(dt: DateTime<Utc>, months: i64) -> Option<DateTime<Utc>> {
    let total = (dt.year() as i64)
        .checked_mul(12)?
        .checked_add(dt.month0() as i64)?
        .checked_add(months)?;
    let ny = i32::try_from(total.div_euclid(12)).ok()?;
    let nm = (total.rem_euclid(12) + 1) as u32;
    let day_off = Duration::try_days(dt.day() as i64 - 1)?;
    let first = NaiveDate::from_ymd_opt(ny, nm, 1)?.and_time(dt.time());
    let shifted = first.checked_add_signed(day_off)?;
    Some(Utc.from_utc_datetime(&shifted))
}

/// `getdate(timestamp=now)`: the associative/indexed array of date components.
fn php_getdate(args: &[Value]) -> Value {
    let ts = if args.is_empty() {
        now_ts()
    } else {
        int_arg(args, 0)
    };
    let dt = from_ts(ts);
    let dow_sun = dt.weekday().num_days_from_sunday() as usize;
    make_map(vec![
        (Value::str("seconds"), Value::int(dt.second() as i64)),
        (Value::str("minutes"), Value::int(dt.minute() as i64)),
        (Value::str("hours"), Value::int(dt.hour() as i64)),
        (Value::str("mday"), Value::int(dt.day() as i64)),
        (Value::str("wday"), Value::int(dow_sun as i64)),
        (Value::str("mon"), Value::int(dt.month() as i64)),
        (Value::str("year"), Value::int(dt.year() as i64)),
        (Value::str("yday"), Value::int(dt.ordinal0() as i64)),
        (Value::str("weekday"), Value::str(DAYS[dow_sun])),
        (Value::str("month"), Value::str(MONTHS[dt.month() as usize])),
        (Value::int(0), Value::int(ts)),
    ])
}