summaryrefslogtreecommitdiffstats
path: root/bins/date/src/main.rs
blob: 68e70d35c49b5ed57548c95453b97883a0d27352 (plain) (blame)
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
//! date — show current RTC time via the exports table.

#![no_std]
#![no_main]

#[panic_handler]
fn panic(_info: &core::panic::PanicInfo) -> ! {
    loop { core::hint::spin_loop(); }
}

fn put(exports: &rsapi::Exports, buf: &[u8]) {
    (exports.put_line)(rsapi::Line { ptr: buf.as_ptr(), len: buf.len() });
}

fn dig(b: u8) -> [u8; 2] { [b'0' + b / 10, b'0' + b % 10] }

#[unsafe(no_mangle)]
pub extern "C" fn efi_main(exports: &'static rsapi::Exports, _argc: usize, _argv: *const rsapi::Arg) -> i32 {
    let mut t = rsapi::RtcTime { year: 0, month: 0, day: 0, hour: 0, minute: 0, second: 0, _pad: 0 };
    (exports.rtc_now)(&mut t);
    // "YYYY-MM-DD HH:MM:SS" = 19 bytes
    let mut buf = [0u8; 19];
    let y = t.year;
    buf[0] = b'0' + (y / 1000) as u8; buf[1] = b'0' + (y / 100) as u8 % 10;
    buf[2] = b'0' + (y / 10) as u8 % 10; buf[3] = b'0' + (y % 10) as u8;
    buf[4] = b'-';
    let m = dig(t.month); buf[5] = m[0]; buf[6] = m[1];
    buf[7] = b'-';
    let d = dig(t.day); buf[8] = d[0]; buf[9] = d[1];
    buf[10] = b' ';
    let h = dig(t.hour); buf[11] = h[0]; buf[12] = h[1];
    buf[13] = b':';
    let mi = dig(t.minute); buf[14] = mi[0]; buf[15] = mi[1];
    buf[16] = b':';
    let s = dig(t.second); buf[17] = s[0]; buf[18] = s[1];
    put(exports, &buf);
    0
}