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! {
static TZ: RefCell<String> = RefCell::new("UTC".to_string());
}
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),
"gmdate" => php_date(args, true),
"checkdate" => php_checkdate(args),
"idate" => php_idate(args),
"microtime" => php_microtime(args),
"strtotime" => php_strtotime(args),
"getdate" => php_getdate(args),
"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))
}
const DAYS: [&str; 7] = [
"Sunday",
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday",
];
const MONTHS: [&str; 13] = [
"",
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December",
];
fn from_ts(ts: i64) -> DateTime<Utc> {
DateTime::from_timestamp(ts, 0).unwrap_or_else(|| DateTime::from_timestamp(0, 0).unwrap())
}
fn now_ts() -> i64 {
Utc::now().timestamp()
}
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(php_format_date(fmt.as_bytes(), ts, !gm))
}
pub fn php_format_date(fmt: &[u8], ts: i64, localtime: bool) -> String {
use crate::timelib as tl;
let mut t = tl::Time::default();
if localtime {
t.tz_info = Some(default_tz());
t.zone_type = tl::ZONETYPE_ID;
tl::unixtime2local(&mut t, ts);
} else {
tl::unixtime2gmt(&mut t, ts);
}
tl::date_format(fmt, &t, localtime)
}
fn is_leap(year: i32) -> bool {
(year % 4 == 0 && year % 100 != 0) || year % 400 == 0
}
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,
}
}
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));
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);
};
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())
}
fn normalize_year(year: i64) -> i64 {
match year {
0..=69 => year + 2000,
70..=100 => year + 1900,
_ => year,
}
}
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)
}
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}"))
}
}
fn php_strtotime(args: &[Value]) -> Value {
let input = str_arg(args, 0);
let base = if args.len() >= 2 && !matches!(args[1], Value::Undef) {
int_arg(args, 1)
} else {
now_ts()
};
match crate::timelib::strtotime_ts(input.as_bytes(), base, &default_tz()) {
Some(ts) => Value::int(ts),
None => Value::bool(false),
}
}
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)),
])
}
pub fn default_tz() -> crate::timelib::TzInfo {
let name = TZ.with(|t| t.borrow().clone());
crate::timelib::tz_lookup(&name)
.unwrap_or_else(|| crate::timelib::tz_lookup("UTC").expect("UTC resolves"))
}
fn php_idate(args: &[Value]) -> Value {
use crate::timelib as tl;
let format = str_arg(args, 0);
if format.len() != 1 {
with_host(|h| h.warn("idate(): idate format is one char"));
return Value::bool(false);
}
let ts = if args.len() >= 2 && !matches!(args[1], Value::Undef) {
int_arg(args, 1)
} else {
now_ts()
};
let tz = default_tz();
let mut t = tl::Time {
tz_info: Some(tz.clone()),
zone_type: tl::ZONETYPE_ID,
..tl::Time::default()
};
tl::unixtime2local(&mut t, ts);
let (isoweek, isoyear) = tl::isoweek_from_date(t.y, t.m, t.d);
let r: i64 = match format.as_bytes()[0] {
b'd' | b'j' => t.d,
b'N' => tl::iso_day_of_week(t.y, t.m, t.d),
b'w' => tl::day_of_week(t.y, t.m, t.d),
b'z' => tl::day_of_year(t.y, t.m, t.d),
b'W' => isoweek,
b'm' | b'n' => t.m,
b't' => tl::days_in_month(t.y, t.m),
b'L' => tl::is_leap(t.y) as i64,
b'y' => t.y % 100,
b'Y' => t.y,
b'o' => isoyear,
b'B' => {
let sse = t.sse;
let mut r = (sse - (sse - ((sse % 86400) + 3600))) * 10;
if r < 0 {
r += 864000;
}
(r / 864) % 1000
}
b'g' | b'h' => {
if t.h % 12 != 0 {
t.h % 12
} else {
12
}
}
b'H' | b'G' => t.h,
b'i' => t.i,
b's' => t.s,
b'I' => 0,
b'Z' => tz.offset,
b'U' => t.sse,
_ => -1,
};
let r = r as i32 as i64;
if r == -1 {
with_host(|h| h.warn("idate(): Unrecognized date format token"));
return Value::bool(false);
}
Value::int(r)
}