fastshell 0.3.4

Lightweight Bash + Python runtime SDK for mobile AI agents
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
// Copyright (c) 2025 xiefujin <490021684@qq.com>
// Licensed under Apache-2.0, see LICENSE file for full license terms.

use crate::shell::{CommandOutput, Shell};

const DATE_HELP_TEXT: &str = "\
Usage: date [OPTION]... [+FORMAT]
Display the current time in the given FORMAT, or set the system date.

  -u        print or set Coordinated Universal Time (UTC)
  -d STRING display time described by STRING (ISO 8601)
  +FORMAT   output date/time according to FORMAT (e.g. +%Y-%m-%d)
  -h, --help  display this help and exit
";

impl Shell {
    pub fn cmd_date(&self, args: &[&str]) -> CommandOutput {
        if args.contains(&"-h") || args.contains(&"--help") {
            return CommandOutput::success(DATE_HELP_TEXT.to_string());
        }
        let mut use_utc = false;
        let mut date_str: Option<String> = None;
        let mut epoch_override: Option<u64> = None;
        let mut format = None;
        let mut rfc_email = false;
        let mut iso8601 = false;
        let mut iso_suffix = String::new();
        let mut i = 0;

        while i < args.len() {
            match args[i] {
                "-u" | "--utc" | "--universal" => use_utc = true,
                "-d" | "--date" => {
                    if i + 1 < args.len() {
                        date_str = Some(args[i + 1].to_string());
                        i += 1;
                    }
                }
                "-r" | "--reference" => {
                    if i + 1 < args.len() {
                        if let Ok(secs) = args[i + 1].parse::<i64>() {
                            epoch_override = Some(secs.max(0) as u64);
                        }
                        i += 1;
                    }
                }
                "-R" | "--rfc-email" | "--rfc-2822" => rfc_email = true,
                "-I" | "--iso-8601" => {
                    iso8601 = true;
                    iso_suffix = "date".to_string();
                }
                a if a.starts_with("-I") && a.len() > 2 => {
                    iso8601 = true;
                    iso_suffix = a[2..].to_string();
                }
                a if a.starts_with("--iso-8601=") => {
                    iso8601 = true;
                    iso_suffix = a[11..].to_string();
                }
                a if a.starts_with("--date=") => {
                    date_str = Some(a[7..].to_string());
                }
                arg if arg.starts_with('+') => {
                    format = Some(arg[1..].to_string());
                }
                _ => {
                    crate::warn!("date: warning: unsupported option '{}'", args[i]);
                }
            }
            i += 1;
        }

        let secs = if let Some(epoch) = epoch_override {
            epoch
        } else if let Some(ref ds) = date_str {
            if let Some(rest) = ds.strip_prefix('@') {
                // GNU style: `date -d @1700000000`
                match rest.parse::<u64>() {
                    Ok(s) => s,
                    Err(_) => {
                        return CommandOutput::error(format!("date: invalid date '{}'\n", ds), 1)
                    }
                }
            } else {
                match parse_iso8601(ds) {
                    Some(s) => s,
                    None => match parse_relative_time(ds) {
                        Some(s) => s,
                        None => {
                            return CommandOutput::error(
                                format!("date: invalid date '{}'\n", ds),
                                1,
                            )
                        }
                    },
                }
            }
        } else {
            let dur = std::time::SystemTime::now()
                .duration_since(std::time::UNIX_EPOCH)
                .unwrap_or_default();
            let s = dur.as_secs();
            if use_utc {
                s
            } else {
                s
            }
        };

        let output = if rfc_email {
            format!("{}\n", format_rfc2822(secs))
        } else if iso8601 {
            format!("{}\n", format_iso8601(secs, &iso_suffix))
        } else {
            match format {
                Some(ref fmt) => format_date(secs, use_utc, fmt),
                None => format!("{}\n", crate::shell::format_unix_time(secs)),
            }
        };

        CommandOutput::success(output)
    }
}

fn format_rfc2822(secs: u64) -> String {
    let days_since_epoch = (secs / 86400) as i32;
    let tod = secs % 86400;
    let (year, month, day) = crate::shell::civil_from_days(days_since_epoch);
    let (hour, minute, second) = (tod / 3600, (tod % 3600) / 60, tod % 60);
    let weekday = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"]
        [((days_since_epoch as i64 + 4) % 7) as usize];
    let month_names = [
        "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
    ];
    format!(
        "{}, {:02} {} {} {:02}:{:02}:{:02} +0000",
        weekday,
        day,
        month_names[(month - 1).max(0) as usize],
        year,
        hour,
        minute,
        second
    )
}

fn format_iso8601(secs: u64, suffix: &str) -> String {
    let days_since_epoch = (secs / 86400) as i32;
    let tod = secs % 86400;
    let (year, month, day) = crate::shell::civil_from_days(days_since_epoch);
    let (hour, minute, second) = (tod / 3600, (tod % 3600) / 60, tod % 60);
    match suffix {
        "seconds" => format!(
            "{:04}-{:02}-{:02}T{:02}:{:02}:{:02}+00:00",
            year, month, day, hour, minute, second
        ),
        "minutes" => format!(
            "{:04}-{:02}-{:02}T{:02}:{:02}+00:00",
            year, month, day, hour, minute
        ),
        "hours" => format!("{:04}-{:02}-{:02}T{:02}:00+00:00", year, month, day, hour),
        "ns" => format!(
            "{:04}-{:02}-{:02}T{:02}:{:02}:{:02}.000000000+00:00",
            year, month, day, hour, minute, second
        ),
        _ => format!("{:04}-{:02}-{:02}", year, month, day),
    }
}

/// Parse a GNU-date-style relative time expression: `yesterday`, `tomorrow`,
/// `-3 days`, `+2 hours`, `5 minutes ago`, etc. Returns epoch seconds.
pub(crate) fn parse_relative_time(s: &str) -> Option<u64> {
    let now = std::time::SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH)
        .ok()?
        .as_secs() as i64;
    let s = s.trim().to_lowercase();

    let delta: Option<i64> = if s == "yesterday" {
        Some(-86400)
    } else if s == "tomorrow" {
        Some(86400)
    } else if s == "now" {
        Some(0)
    } else if let Some(rest) = s.strip_prefix('-') {
        Some(-parse_duration(rest)?)
    } else if let Some(rest) = s.strip_prefix('+') {
        Some(parse_duration(rest)?)
    } else if let Some(rest) = s.strip_suffix(" ago") {
        Some(-parse_duration(rest.trim())?)
    } else if let Some(rest) = s.strip_suffix(" hence") {
        Some(parse_duration(rest.trim())?)
    } else {
        None
    };

    delta.map(|d| (now + d).max(0) as u64)
}

/// Parse a duration like "3 days", "2 hours", "10 minutes" into seconds.
fn parse_duration(s: &str) -> Option<i64> {
    let mut parts = s.trim().split_whitespace();
    let num: i64 = parts.next()?.parse().ok()?;
    let unit = parts.next().unwrap_or("second").trim_end_matches('s');
    match unit {
        "second" | "sec" => Some(num),
        "minute" | "min" => Some(num * 60),
        "hour" | "hr" => Some(num * 3600),
        "day" => Some(num * 86400),
        "week" => Some(num * 7 * 86400),
        "month" => Some(num * 30 * 86400),
        "year" => Some(num * 365 * 86400),
        _ => None,
    }
}

pub(crate) fn parse_iso8601(s: &str) -> Option<u64> {
    let s = s.trim();
    if s.len() < 10 {
        return None;
    }
    let year: i32 = s[0..4].parse().ok()?;
    if s.as_bytes().get(4) != Some(&b'-') {
        return None;
    }
    let month: i32 = s[5..7].parse().ok()?;
    if s.as_bytes().get(7) != Some(&b'-') {
        return None;
    }
    let day: i32 = s[8..10].parse().ok()?;

    let (hour, minute, second) = if s.len() >= 19 && s.as_bytes().get(10) == Some(&b'T') {
        (
            s[11..13].parse::<u64>().ok()?,
            s[14..16].parse::<u64>().ok()?,
            s[17..19].parse::<u64>().ok()?,
        )
    } else {
        (0, 0, 0)
    };

    let days_since_epoch = days_to_epoch(year, month, day);

    Some(days_since_epoch * 86400 + hour * 3600 + minute * 60 + second)
}

fn days_to_epoch(y: i32, m: i32, d: i32) -> u64 {
    let (mut y, mut m) = (y as i64, m as i64);
    if m <= 2 {
        y -= 1;
        m += 12;
    }
    let era = if y >= 0 { y / 400 } else { (y - 399) / 400 };
    let yoe = y - era * 400;
    let doy = (153 * (m - 3) + 2) / 5 + d as i64 - 1;
    let doe = yoe * 365 + yoe / 4 - yoe / 100 + doy;
    let epoch: i64 = 719468;
    let days = (era * 146097) as i64 + doe - epoch;
    days as u64
}

fn format_date(secs: u64, use_utc: bool, fmt: &str) -> String {
    let days_since_epoch = (secs / 86400) as i32;
    let time_of_day = secs % 86400;
    let hours = time_of_day / 3600;
    let minutes = (time_of_day % 3600) / 60;
    let seconds = time_of_day % 60;

    let (year, month, day) = crate::shell::civil_from_days(days_since_epoch);

    let hour_12 = if hours == 0 {
        12
    } else if hours > 12 {
        hours - 12
    } else {
        hours
    };
    let am_pm = if hours < 12 { "AM" } else { "PM" };

    let doy = compute_doy(year, month, day);

    let week_number = compute_week_number(year, month, day);

    let weekday_index = (days_since_epoch as i64 + 4) % 7;
    // 0=Sunday in the epoch calculation; adjust to 0=Sunday
    let weekday_names = [
        "Sunday",
        "Monday",
        "Tuesday",
        "Wednesday",
        "Thursday",
        "Friday",
        "Saturday",
    ];
    let month_names = [
        "",
        "January",
        "February",
        "March",
        "April",
        "May",
        "June",
        "July",
        "August",
        "September",
        "October",
        "November",
        "December",
    ];

    let tz_offset = if use_utc {
        "+0000".to_string()
    } else {
        timezone_offset()
    };

    let mut result = fmt.to_string();
    result = result.replace("%F", &format!("{:04}-{:02}-{:02}", year, month, day));
    result = result.replace("%T", &format!("{:02}:{:02}:{:02}", hours, minutes, seconds));
    result = result.replace("%e", &format!("{:2}", day));
    result = result.replace("%Y", &format!("{:04}", year));
    result = result.replace("%m", &format!("{:02}", month));
    result = result.replace("%d", &format!("{:02}", day));
    result = result.replace("%H", &format!("{:02}", hours));
    result = result.replace("%M", &format!("{:02}", minutes));
    result = result.replace("%S", &format!("{:02}", seconds));
    result = result.replace("%s", &format!("{}", secs));
    result = result.replace("%z", &tz_offset);
    result = result.replace("%A", weekday_names[weekday_index as usize]);
    result = result.replace("%B", month_names[month as usize]);
    result = result.replace("%I", &format!("{:02}", hour_12));
    result = result.replace("%p", am_pm);
    result = result.replace("%j", &format!("{:03}", doy));
    result = result.replace("%U", &format!("{:02}", week_number));
    result.push('\n');
    result
}

fn compute_doy(year: i32, month: i32, day: i32) -> u64 {
    let month_days = [0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334];
    let mut doy = month_days[month as usize - 1] + day as u64;
    if month > 2 && is_leap(year) {
        doy += 1;
    }
    doy
}

fn is_leap(year: i32) -> bool {
    (year % 4 == 0 && year % 100 != 0) || year % 400 == 0
}

fn compute_week_number(year: i32, month: i32, day: i32) -> u64 {
    let doy = compute_doy(year, month, day) as i64;
    let jan1_dow = (epoch_days(year, 1, 1) as i64 + 4) % 7;
    let week = (doy + jan1_dow - 1) / 7;
    if week < 0 {
        0
    } else {
        week as u64
    }
}

fn epoch_days(y: i32, m: i32, d: i32) -> u64 {
    days_to_epoch(y, m, d)
}

fn timezone_offset() -> String {
    let now = std::time::SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH)
        .unwrap()
        .as_secs() as libc::time_t;
    let mut tm: libc::tm = unsafe { std::mem::zeroed() };
    unsafe { libc::localtime_r(&now, &mut tm) };
    let offset_secs = tm.tm_gmtoff;
    let sign = if offset_secs < 0 { '-' } else { '+' };
    let abs_secs = offset_secs.abs();
    let hours = abs_secs / 3600;
    let minutes = (abs_secs % 3600) / 60;
    format!("{}{:02}{:02}", sign, hours, minutes)
}

#[cfg(test)]
mod tests {
    use super::Shell;
    use std::fs;
    use std::sync::atomic::{AtomicUsize, Ordering};

    static TEST_COUNTER: AtomicUsize = AtomicUsize::new(0);

    fn mk_shell() -> Shell {
        let n = TEST_COUNTER.fetch_add(1, Ordering::SeqCst);
        let dir =
            std::env::temp_dir().join(format!("fastshell_date_test_{}_{}", std::process::id(), n));
        let _ = fs::remove_dir_all(&dir);
        let vfs = crate::vfs::Vfs::new(dir).unwrap();
        Shell::new(vfs)
    }

    #[test]
    fn test_date_help() {
        let mut s = mk_shell();
        let out = s.execute("date", &["-h"], None);
        assert_eq!(out.exit_code, 0);
        assert!(!out.stdout.is_empty());
    }

    #[test]
    fn test_date_help_long() {
        let mut s = mk_shell();
        let out = s.execute("date", &["--help"], None);
        assert_eq!(out.exit_code, 0);
        assert!(!out.stdout.is_empty());
    }

    #[test]
    fn test_date_full_date_format() {
        let mut s = mk_shell();
        let out = s.execute("date", &["+%F"], None);
        // %F = %Y-%m-%d
        let re = regex::Regex::new(r"^\d{4}-\d{2}-\d{2}$").unwrap();
        assert!(
            re.is_match(out.stdout.trim()),
            "%F should be YYYY-MM-DD, got: {}",
            out.stdout
        );
    }

    #[test]
    fn test_date_full_time_format() {
        let mut s = mk_shell();
        let out = s.execute("date", &["+%T"], None);
        // %T = %H:%M:%S
        let re = regex::Regex::new(r"^\d{2}:\d{2}:\d{2}$").unwrap();
        assert!(
            re.is_match(out.stdout.trim()),
            "%T should be HH:MM:SS, got: {}",
            out.stdout
        );
    }

    #[test]
    fn test_parse_duration_days() {
        assert_eq!(super::parse_duration("3 days"), Some(3 * 86400));
        assert_eq!(super::parse_duration("2 hours"), Some(2 * 3600));
        assert_eq!(super::parse_duration("10 minutes"), Some(10 * 60));
        assert_eq!(super::parse_duration("1 week"), Some(7 * 86400));
    }

    #[test]
    fn test_parse_relative_time_yesterday() {
        let now = std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .unwrap()
            .as_secs();
        let yesterday = super::parse_relative_time("yesterday").unwrap();
        assert!(
            (yesterday as i64 - (now as i64 - 86400)).abs() < 5,
            "yesterday should be ~now-86400"
        );
    }

    #[test]
    fn test_parse_relative_time_ago() {
        let now = std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .unwrap()
            .as_secs();
        let v = super::parse_relative_time("5 minutes ago").unwrap();
        assert!(
            (v as i64 - (now as i64 - 300)).abs() < 5,
            "5 minutes ago should be ~now-300"
        );
    }

    #[test]
    fn test_date_d_yesterday() {
        let mut s = mk_shell();
        let out = s.execute("date", &["-d", "yesterday", "+%s"], None);
        let now = std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .unwrap()
            .as_secs();
        let val: i64 = out.stdout.trim().parse().unwrap();
        assert!(
            (val - (now as i64 - 86400)).abs() < 5,
            "date -d yesterday +%s should be ~now-86400"
        );
    }
}