Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 26 additions & 4 deletions crates/vm/src/stdlib/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -251,10 +251,32 @@ mod decl {
host_time::get_tz_info()
}

// #[pyfunction]
// fn tzset() {
// unsafe { super::_tzset() };
// }
#[cfg(all(unix, not(target_arch = "wasm32")))]
#[pyfunction]
fn tzset(vm: &VirtualMachine) -> PyResult<()> {
crate::host_env::time::tz::tzset();
let module = vm.import("time", 0)?;
module.set_attr(
"timezone",
vm.ctx.new_int(crate::host_env::time::tz::timezone()),
vm,
)?;
module.set_attr(
"altzone",
vm.ctx.new_int(crate::host_env::time::tz::altzone()),
vm,
)?;
#[cfg(not(target_os = "freebsd"))]
module.set_attr(
"daylight",
vm.ctx.new_int(crate::host_env::time::tz::daylight()),
vm,
)?;
use crate::builtins::tuple::IntoPyTuple;
let tzname = crate::host_env::time::tz::tzname_strings().into_pytuple(vm);
module.set_attr("tzname", tzname, vm)?;
Ok(())
}

#[cfg(not(target_env = "msvc"))]
#[cfg(not(target_arch = "wasm32"))]
Expand Down
27 changes: 27 additions & 0 deletions extra_tests/snippets/stdlib_time.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,30 @@
pass
else:
assert False, "struct_time accepted a non-dict second argument"

if hasattr(time, "tzset"):
import os

old_tz = os.environ.get("TZ")
try:
os.environ["TZ"] = "EST+05EDT,M3.2.0,M11.1.0"
time.tzset()
assert time.timezone == 18000
assert time.altzone == 14400
if hasattr(time, "daylight"):
assert time.daylight == 1
assert time.tzname == ("EST", "EDT")
assert len(time.tzname) == 2

os.environ["TZ"] = "UTC+0"
time.tzset()
assert time.timezone == 0
if hasattr(time, "daylight"):
assert time.daylight == 0
assert time.tzname == ("UTC", "UTC")
finally:
if old_tz is not None:
os.environ["TZ"] = old_tz
elif "TZ" in os.environ:
del os.environ["TZ"]
time.tzset()
Loading