Skip to content

Commit

Permalink
Jiff iso week date (#189)
Browse files Browse the repository at this point in the history
* added iso weekdate

* iso-weekdate

* move jiff types out into own stub file

* jiff type stuf file
  • Loading branch information
jessekrubin authored Jan 28, 2025
1 parent b3bdfa5 commit 373fa56
Show file tree
Hide file tree
Showing 14 changed files with 1,691 additions and 1,312 deletions.
4 changes: 3 additions & 1 deletion crates/ryo3-jiff/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,21 @@ pub use crate::ry_timezone::RyTimeZone;
pub use crate::ry_zoned::RyZoned;
pub use crate::ry_zoned_difference::RyZonedDifference;

use crate::ry_iso_week_date::RyISOWeekDate;
use pyo3::prelude::*;

pub fn pymod_add(m: &Bound<'_, PyModule>) -> PyResult<()> {
// classes
m.add_class::<RyDate>()?;
m.add_class::<RyDateTime>()?;
m.add_class::<RyISOWeekDate>()?;
m.add_class::<RyOffset>()?;
m.add_class::<RySignedDuration>()?;
m.add_class::<RySpan>()?;
m.add_class::<RyTime>()?;
m.add_class::<RyTimeZone>()?;
m.add_class::<RyTimestamp>()?;
m.add_class::<RyZoned>()?;
m.add_class::<RyOffset>()?;

// difference
m.add_class::<RyDateDifference>()?;
Expand Down
24 changes: 24 additions & 0 deletions crates/ryo3-jiff/src/jiff_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,3 +208,27 @@ impl Display for JiffRoundMode {
write!(f, "{s}")
}
}

// ============================================================================
// WEEKDAY
// ============================================================================
impl JiffWeekday {
fn static_str(self) -> &'static str {
match self.0 {
jiff::civil::Weekday::Monday => "monday",
jiff::civil::Weekday::Tuesday => "tuesday",
jiff::civil::Weekday::Wednesday => "wednesday",
jiff::civil::Weekday::Thursday => "thursday",
jiff::civil::Weekday::Friday => "friday",
jiff::civil::Weekday::Saturday => "saturday",
jiff::civil::Weekday::Sunday => "sunday",
}
}
}

impl Display for JiffWeekday {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let s = self.static_str();
write!(f, "{s}")
}
}
1 change: 1 addition & 0 deletions crates/ryo3-jiff/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ mod ry_span;
mod api;
mod functions;
mod ry_datetime_round;
mod ry_iso_week_date;
mod ry_time;
mod ry_time_difference;
mod ry_timestamp;
Expand Down
41 changes: 16 additions & 25 deletions crates/ryo3-jiff/src/ry_date.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use crate::deprecations::deprecation_warning_intz;
use crate::errors::map_py_value_err;
use crate::ry_date_difference::{DateDifferenceArg, RyDateDifference};
use crate::ry_datetime::RyDateTime;
use crate::ry_iso_week_date::RyISOWeekDate;
use crate::ry_signed_duration::RySignedDuration;
use crate::ry_span::RySpan;
use crate::ry_time::RyTime;
Expand All @@ -17,7 +18,6 @@ use pyo3::{
intern, pyclass, pymethods, Bound, FromPyObject, IntoPyObject, PyAny, PyErr, PyRef, PyRefMut,
PyResult, Python,
};
use ryo3_macros::err_py_not_impl;
use ryo3_std::PyDuration;
use std::fmt::Display;
use std::hash::{DefaultHasher, Hash, Hasher};
Expand Down Expand Up @@ -60,6 +60,7 @@ impl RyDate {
let z = jiff::civil::Date::from(Zoned::now());
Self::from(z)
}

fn at(&self, hour: i8, minute: i8, second: i8, subsec_nanosecond: i32) -> RyDateTime {
RyDateTime::from(self.0.at(hour, minute, second, subsec_nanosecond))
}
Expand Down Expand Up @@ -206,6 +207,7 @@ impl RyDate {
"unsupported operand type(s) for +: 'Date' and 'other'",
))
}

fn saturating_sub<'py>(&self, _py: Python<'py>, other: &Bound<'py, PyAny>) -> PyResult<Self> {
if let Ok(date) = other.downcast::<RySpan>() {
let other = date.extract::<RySpan>()?;
Expand All @@ -224,15 +226,6 @@ impl RyDate {
))
}

// fn __add__(&self, _py: Python<'_>, other: RyDeltaArithmeticSelf) -> PyResult<Self> {
// let t = match other {
// RyDeltaArithmeticSelf::Span(other) => self.0 + other.0,
// RyDeltaArithmeticSelf::SignedDuration(other) => self.0 + other.0,
// RyDeltaArithmeticSelf::Duration(other) => self.0 + other.0,
// };
// Ok(RyDate::from(t))
// }

fn __iadd__(&mut self, _py: Python<'_>, other: RyDeltaArithmeticSelf) -> PyResult<()> {
let t = match other {
RyDeltaArithmeticSelf::Span(other) => self.0 + other.0,
Expand Down Expand Up @@ -292,38 +285,32 @@ impl RyDate {
fn day_of_year(&self) -> i16 {
self.0.day_of_year()
}

fn day_of_year_no_leap(&self) -> Option<i16> {
self.0.day_of_year_no_leap()
}

fn days_in_month(&self) -> i8 {
self.0.days_in_month()
}

fn days_in_year(&self) -> i16 {
self.0.days_in_year()
}

fn duration_since(&self, other: &Self) -> RySignedDuration {
RySignedDuration::from(self.0.duration_since(other.0))
}
fn duration_until(&self, other: &Self) -> RySignedDuration {
RySignedDuration::from(self.0.duration_until(other.0))
}
// #[cfg(Py_LIMITED_API)]
// fn era_year<'py>(&self, py: Python<'py>) -> PyResult<Bound<'py, PyAny>> {
// let era_year = JiffEraYear(self.0.era_year());
// era_year.into_pyobject(py)
// }
//
// #[cfg(not(Py_LIMITED_API))]
// fn era_year<'py>(&self, py: Python<'py>) -> PyResult<Bound<'py,PyTuple>> {
// let era_year = JiffEraYear(self.0.era_year());
// era_year.into_pyobject(py)
// }

fn era_year<'py>(&self, py: Python<'py>) -> PyResult<Bound<'py, PyAny>> {
let era_year = JiffEraYear(self.0.era_year());
let obj = era_year.into_pyobject(py)?;
Ok(obj.into_any())
}

fn first_of_month(&self) -> RyDate {
Self::from(self.0.first_of_month())
}
Expand All @@ -346,6 +333,7 @@ impl RyDate {
fn yesterday(&self) -> PyResult<Self> {
self.0.yesterday().map(From::from).map_err(map_py_value_err)
}

fn strftime(&self, format: &str) -> PyResult<String> {
Ok(self.0.strftime(format).to_string())
}
Expand Down Expand Up @@ -421,8 +409,11 @@ impl RyDate {
}

#[classmethod]
fn from_iso_week_date(_cls: &Bound<'_, PyType>, _iso_week_date: &str) -> PyResult<()> {
err_py_not_impl!()
fn from_iso_week_date(
_cls: &Bound<'_, PyType>,
iso_week_date: &RyISOWeekDate,
) -> PyResult<Self> {
Ok(Self::from(iso_week_date.0.date()))
}

fn nth_weekday(&self, nth: i32, weekday: JiffWeekday) -> PyResult<Self> {
Expand All @@ -439,8 +430,8 @@ impl RyDate {
.map_err(map_py_value_err)
}

fn iso_week_date(&self) -> PyResult<()> {
err_py_not_impl!()
fn iso_week_date(&self) -> RyISOWeekDate {
self.0.iso_week_date().into()
}
}

Expand Down
15 changes: 15 additions & 0 deletions crates/ryo3-jiff/src/ry_datetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use crate::deprecations::deprecation_warning_intz;
use crate::errors::map_py_value_err;
use crate::jiff_types::JiffDateTime;
use crate::ry_datetime_difference::{DateTimeDifferenceArg, RyDateTimeDifference};
use crate::ry_iso_week_date::RyISOWeekDate;
use crate::ry_signed_duration::RySignedDuration;
use crate::ry_span::RySpan;
use crate::ry_time::RyTime;
Expand Down Expand Up @@ -164,6 +165,7 @@ impl RyDateTime {
self.subsec_nanosecond()
)
}

fn __hash__(&self) -> u64 {
let mut hasher = DefaultHasher::new();
self.0.hash(&mut hasher);
Expand Down Expand Up @@ -206,6 +208,7 @@ impl RyDateTime {
self.0 = t;
Ok(())
}

fn __sub__<'py>(
&self,
py: Python<'py>,
Expand Down Expand Up @@ -297,6 +300,10 @@ impl RyDateTime {
self.in_tz(tz)
}

fn iso_week_date(&self) -> RyISOWeekDate {
RyISOWeekDate::from(self.0.iso_week_date())
}

fn to_zoned(&self, tz: RyTimeZone) -> PyResult<RyZoned> {
self.0
.to_zoned(tz.0)
Expand Down Expand Up @@ -351,26 +358,33 @@ impl RyDateTime {
fn day_of_year(&self) -> i16 {
self.0.day_of_year()
}

fn day_of_year_no_leap(&self) -> Option<i16> {
self.0.day_of_year_no_leap()
}

fn days_in_month(&self) -> i8 {
self.0.days_in_month()
}

fn days_in_year(&self) -> i16 {
self.0.days_in_year()
}

fn duration_since(&self, dt: &Self) -> RySignedDuration {
self.0.duration_since(dt.0).into()
}

fn duration_until(&self, dt: &Self) -> RySignedDuration {
self.0.duration_until(dt.0).into()
}

/// Returns the end of the day DateTime
fn end_of_day(&self) -> RyDateTime {
RyDateTime::from(self.0.end_of_day())
}

/// Return the era year as a tuple (era, year)
fn era_year<'py>(&self, py: Python<'py>) -> PyResult<Bound<'py, PyAny>> {
let era_year = JiffEraYear(self.0.era_year());
let obj = era_year.into_pyobject(py)?;
Expand Down Expand Up @@ -495,6 +509,7 @@ impl RyDateTime {
}
}
}

impl Display for RyDateTime {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.0.fmt(f)
Expand Down
Loading

0 comments on commit 373fa56

Please sign in to comment.