extern crate rbpf;
use std::time::{SystemTime, UNIX_EPOCH};
#[allow(unused_variables)]
fn helper_get_time_sec(unused1: u64, unused2: u64, unused3: u64, unused4: u64, unused5: u64) -> u64 {
SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap()
.as_secs()
}
const HELPER_GET_TIME_SEC_IDX: u32 = 0x01;
fn main() {
#[rustfmt::skip]
let prog1 = &[
0xb4, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xb4, 0x01, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x0c, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 ];
let hkey = HELPER_GET_TIME_SEC_IDX as u8;
#[rustfmt::skip]
let prog2 = &[
0xb7, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xb7, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xb7, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xb7, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xb7, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x85, 0x00, 0x00, 0x00, hkey, 0x00, 0x00, 0x00, 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 ];
let mut vm = rbpf::EbpfVmNoData::new(Some(prog1)).unwrap();
assert_eq!(vm.execute_program().unwrap(), 0x3);
vm.set_program(prog2).unwrap();
vm.register_helper(HELPER_GET_TIME_SEC_IDX, helper_get_time_sec)
.unwrap();
let time;
#[cfg(all(not(windows), feature = "std"))]
{
vm.jit_compile().unwrap();
time = unsafe { vm.execute_program_jit().unwrap() };
}
#[cfg(any(windows, not(feature = "std")))]
{
time = vm.execute_program().unwrap();
}
print_date(time);
}
#[rustfmt::skip]
fn print_date(timestamp: u64) {
const SECONDS_PER_MINUTE: u64 = 60;
const SECONDS_PER_HOUR: u64 = 60 * SECONDS_PER_MINUTE;
const SECONDS_PER_DAY: u64 = 24 * SECONDS_PER_HOUR;
const DAYS_IN_MONTH: [u64; 12] = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
fn is_leap_year(year: u64) -> bool {
(year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)
}
fn days_in_year(year: u64) -> u64 {
if is_leap_year(year) { 366 } else { 365 }
}
let seconds = timestamp % SECONDS_PER_MINUTE;
let minutes = (timestamp / SECONDS_PER_MINUTE) % 60;
let hours = (timestamp / SECONDS_PER_HOUR) % 24;
let mut remaining_days = timestamp / SECONDS_PER_DAY;
let mut year = 1970u64;
while remaining_days >= days_in_year(year) {
remaining_days -= days_in_year(year);
year += 1;
}
let mut month = 0usize;
while month < 12 {
let days = if month == 1 && is_leap_year(year) {
29
} else {
DAYS_IN_MONTH[month]
};
if remaining_days < days {
break;
}
remaining_days -= days;
month += 1;
}
let day = remaining_days + 1;
let month = month + 1;
println!("Current date and time (UTC): {year:04}-{month:02}-{day:02} {hours:02}:{minutes:02}:{seconds:02}");
println!("Unix timestamp: {timestamp}");
}