8000 implement tm_gmtoff and tm_zone · RustPython/RustPython@c9efd4b · GitHub
[go: up one dir, main page]

Skip to content

Commit c9efd4b

Browse files
JazzGlobalyouknowone
authored andcommitted
implement tm_gmtoff and tm_zone
1 parent 549cce2 commit c9efd4b

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;
@@ -447,6 +447,8 @@ mod decl {
447447
tm_wday: PyObjectRef,
448448
tm_yday: PyObjectRef,
449449
tm_isdst: PyObjectRef,
450+
tm_gmtoff: PyObjectRef,
451+
tm_zone: PyObjectRef,
450452
}
451453

452454
impl std::fmt::Debug for PyStructTime {
@@ -458,6 +460,11 @@ mod decl {
458460
#[pyclass(with(PyStructSequence))]
459461
impl PyStructTime {
460462
fn new(vm: &VirtualMachine, tm: NaiveDateTime, isdst: i32) -> Self {
463+
let local_time = chrono::Local.from_local_datetime(&tm).unwrap();
464+
let offset_seconds =
465+
local_time.offset().local_minus_utc() + if isdst == 1 { 3600 } else { 0 };
466+
let tz_abbr = local_time.format("%Z").to_string();
467+
461468
PyStructTime {
462469
tm_year: vm.ctx.new_int(tm.year()).into(),
463470
tm_mon: vm.ctx.new_int(tm.month()).into(),
@@ -468,6 +475,8 @@ mod decl {
468475
tm_wday: vm.ctx.new_int(tm.weekday().num_days_from_monday()).into(),
469476
tm_yday: vm.ctx.new_int(tm.ordinal()).into(),
470477
tm_isdst: vm.ctx.new_int(isdst).into(),
478+
tm_gmtoff: vm.ctx.new_int(offset_seconds).into(),
479+
tm_zone: vm.ctx.new_str(tz_abbr).into(),
471480
}
472481
}
473482

0 commit comments

Comments
 (0)
0