Skip to content
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

added tm_gmtoff and tm_zone attributes to PyStructTime #5017

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
added tm_gmtoff and tm_zone attributes to PyStructTime
  • Loading branch information
bobby-palmer committed Jun 19, 2023
commit 6c1001d1a038b5e8e4aff7ccdedf07529beebbbd
12 changes: 11 additions & 1 deletion vm/src/stdlib/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 6,7 @@ pub use time::*;

#[pymodule(name = "time", with(platform))]
mod time {

use crate::{
builtins::{PyStrRef, PyTypeRef},
function::{Either, FuncArgs, OptionalArg},
Expand All @@ -14,7 15,7 @@ mod time {
};
use chrono::{
naive::{NaiveDate, NaiveDateTime, NaiveTime},
Datelike, Timelike,
DateTime, Datelike, Local, Offset, TimeZone, Timelike,
};
use std::time::Duration;

Expand Down Expand Up @@ -325,6 326,8 @@ mod time {
tm_wday: PyObjectRef,
tm_yday: PyObjectRef,
tm_isdst: PyObjectRef,
tm_gmtoff: PyObjectRef,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

okay, now I see what you were talking about in the other pr, I missed this one, sorry!

Yeah, these should be objects here, not options. The distinction between naive/aware is done based on their value (it being None or not), that's what I've understood by quickly looking into this. I'd need some more time to get a better view of this.

tm_zone: PyObjectRef,
}

impl std::fmt::Debug for PyStructTime {
Expand All @@ -336,6 339,8 @@ mod time {
#[pyclass(with(PyStructSequence))]
impl PyStructTime {
fn new(vm: &VirtualMachine, tm: NaiveDateTime, isdst: i32) -> Self {
let offset = Local::now().offset().fix();
let zoned_date: DateTime<Local> = DateTime::from_local(tm, offset);
PyStructTime {
tm_year: vm.ctx.new_int(tm.year()).into(),
tm_mon: vm.ctx.new_int(tm.month()).into(),
Expand All @@ -346,6 351,11 @@ mod time {
tm_wday: vm.ctx.new_int(tm.weekday().num_days_from_monday()).into(),
tm_yday: vm.ctx.new_int(tm.ordinal()).into(),
tm_isdst: vm.ctx.new_int(isdst).into(),
tm_gmtoff: vm.ctx.new_int(offset.local_minus_utc()).into(),
tm_zone: vm
.ctx
.new_str(format!("{}", zoned_date.format("%Z")))
.into(),
}
}

Expand Down
Loading