Skip to content

Commit bb8606d

Browse files
JazzGlobalyouknowone
authored andcommitted
implement tm_gmtoff and tm_zone
1 parent 0abd8b1 commit bb8606d

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

vm/src/stdlib/time.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ mod decl {
3939
types::PyStructSequence,
4040
};
4141
use chrono::{
42-
DateTime, Datelike, Timelike,
42+
DateTime, Datelike, TimeZone, Timelike,
4343
naive::{NaiveDate, NaiveDateTime, NaiveTime},
4444
};
4545
use std::time::Duration;
@@ -451,6 +451,8 @@ mod decl {
451451
tm_wday: PyObjectRef,
452452
tm_yday: PyObjectRef,
453453
tm_isdst: PyObjectRef,
454+
tm_gmtoff: PyObjectRef,
455+
tm_zone: PyObjectRef,
454456
}
455457

456458
impl std::fmt::Debug for PyStructTime {
@@ -462,6 +464,11 @@ mod decl {
462464
#[pyclass(with(PyStructSequence))]
463465
impl PyStructTime {
464466
fn new(vm: &VirtualMachine, tm: NaiveDateTime, isdst: i32) -> Self {
467+
let local_time = chrono::Local.from_local_datetime(&tm).unwrap();
468+
let offset_seconds =
469+
local_time.offset().local_minus_utc() + if isdst == 1 { 3600 } else { 0 };
470+
let tz_abbr = local_time.format("%Z").to_string();
471+
465472
PyStructTime {
466473
tm_year: vm.ctx.new_int(tm.year()).into(),
467474
tm_mon: vm.ctx.new_int(tm.month()).into(),
@@ -472,6 +479,8 @@ mod decl {
472479
tm_wday: vm.ctx.new_int(tm.weekday().num_days_from_monday()).into(),
473480
tm_yday: vm.ctx.new_int(tm.ordinal()).into(),
474481
tm_isdst: vm.ctx.new_int(isdst).into(),
482+
tm_gmtoff: vm.ctx.new_int(offset_seconds).into(),
483+
tm_zone: vm.ctx.new_str(tz_abbr).into(),
475484
}
476485
}
477486

0 commit comments

Comments
 (0)