Thanks to visit codestin.com
Credit goes to github.com

Skip to content

time.{tzname,timezone,daylight} #5323

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 14, 2024
Merged
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
64 changes: 60 additions & 4 deletions vm/src/stdlib/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,35 @@

// See also:
// https://docs.python.org/3/library/time.html
pub use time::*;
use crate::{builtins::PyModule, PyRef, VirtualMachine};

pub use decl::time;

pub(crate) fn make_module(vm: &VirtualMachine) -> PyRef<PyModule> {
#[cfg(not(target_env = "msvc"))]
#[cfg(not(target_arch = "wasm32"))]
unsafe {
c_tzset()
};
decl::make_module(vm)
}

#[cfg(not(target_env = "msvc"))]
#[cfg(not(target_arch = "wasm32"))]
extern "C" {
#[link_name = "daylight"]
static c_daylight: std::ffi::c_int;
// pub static dstbias: std::ffi::c_int;
#[link_name = "timezone"]
static c_timezone: std::ffi::c_long;
#[link_name = "tzname"]
static c_tzname: [*const std::ffi::c_char; 2];
#[link_name = "tzset"]
fn c_tzset();
}

#[pymodule(name = "time", with(platform))]
mod time {
mod decl {
use crate::{
builtins::{PyStrRef, PyTypeRef},
function::{Either, FuncArgs, OptionalArg},
Expand Down Expand Up @@ -110,6 +135,37 @@ mod time {
Ok(get_perf_time(vm)?.as_nanos())
}

// #[pyfunction]
// fn tzset() {
// unsafe { super::_tzset() };
// }

#[cfg(not(target_env = "msvc"))]
#[cfg(not(target_arch = "wasm32"))]
#[pyattr]
fn timezone(_vm: &VirtualMachine) -> std::ffi::c_long {
unsafe { super::c_timezone }
}

#[cfg(not(target_env = "msvc"))]
#[cfg(not(target_arch = "wasm32"))]
#[pyattr]
fn daylight(_vm: &VirtualMachine) -> std::ffi::c_int {
unsafe { super::c_daylight }
}

#[cfg(not(target_env = "msvc"))]
#[cfg(not(target_arch = "wasm32"))]
#[pyattr]
fn tzname(vm: &VirtualMachine) -> crate::builtins::PyTupleRef {
use crate::builtins::tuple::IntoPyTuple;

unsafe fn to_str(s: *const std::ffi::c_char) -> String {
std::ffi::CStr::from_ptr(s).to_string_lossy().into_owned()
}
unsafe { (to_str(super::c_tzname[0]), to_str(super::c_tzname[1])) }.into_pytuple(vm)
}

fn pyobj_to_date_time(
value: Either<f64, i64>,
vm: &VirtualMachine,
Expand Down Expand Up @@ -384,7 +440,7 @@ mod time {
#[pymodule(sub)]
mod platform {
#[allow(unused_imports)]
use super::{SEC_TO_NS, US_TO_NS};
use super::decl::{SEC_TO_NS, US_TO_NS};
#[cfg_attr(target_os = "macos", allow(unused_imports))]
use crate::{
builtins::{PyNamespace, PyStrRef},
Expand Down Expand Up @@ -621,7 +677,7 @@ mod platform {
#[cfg(windows)]
#[pymodule]
mod platform {
use super::{time_muldiv, MS_TO_NS, SEC_TO_NS};
use super::decl::{time_muldiv, MS_TO_NS, SEC_TO_NS};
use crate::{
builtins::{PyNamespace, PyStrRef},
stdlib::os::errno_err,
Expand Down