Repository: pola-rs/polars
File: crates/polars-time/src/windows/duration.rs (Rust)

Code before:
    }

    fn add_impl_month_week_or_day<F, G, J>(
        &self,
        mut t: i64,
        tz: Option<&Tz>,
        nsecs_to_unit: F,
        timestamp_to_datetime: G,
        datetime_to_timestamp: J,
    ) -> PolarsResult<i64>
    where
        F: Fn(i64) -> i64,
        G: Fn(i64) -> NaiveDateTime,
        J: Fn(NaiveDateTime) -> i64,
    {
        let d = self;

        if d.months > 0 {
            t = match tz {
                #[cfg(feature = "timezones")]
                // for UTC, use fastpath below (same as naive)
                Some(tz) if tz != &chrono_tz::UTC => {
                    let original_dt_utc = timestamp_to_datetime(t);
                    let original_dt_local = unlocalize_datetime(original_dt_utc, tz);
                    let result_dt_local = Self::add_month(original_dt_local, d.months, d.negative);
                    datetime_to_timestamp(self.localize_result(
                        original_dt_local,
                        original_dt_utc,
                        result_dt_local,
                        tz,
                    )?)
                },
                _ => datetime_to_timestamp(Self::add_month(
                    timestamp_to_datetime(t),
                    d.months,
                    d.negative,
                )),
            };
        }

        if d.weeks > 0 {
            let t_weeks = nsecs_to_unit(NS_WEEK) * self.weeks;
            t = match tz {
                #[cfg(feature = "timezones")]
                // for UTC, use fastpath below (same as naive)
                Some(tz) if tz != &chrono_tz::UTC => {
                    let original_dt_utc = timestamp_to_datetime(t);
                    let original_dt_local = unlocalize_datetime(original_dt_utc, tz);
                    let mut result_timestamp_local = datetime_to_timestamp(original_dt_local);
                    result_timestamp_local += if d.negative { -t_weeks } else { t_weeks };
                    let result_dt_local = timestamp_to_datetime(result_timestamp_local);

Review comment (suggestion):
it's not quite correct to use `localize_result` unfortunately, as the logic in there only works if `original_dt_local` is itself ambiguous

EDIT: this is now fixed 👍 

Code after:
        mut t: i64,
        tz: Option<&Tz>,
        nsecs_to_unit: F,
        timestamp_to_datetime: G,
        datetime_to_timestamp: J,
    ) -> PolarsResult<i64>
    where
        F: Fn(i64) -> i64,
        G: Fn(i64) -> NaiveDateTime,
        J: Fn(NaiveDateTime) -> i64,
    {
        let d = self;

        if d.months > 0 {
            t = match tz {
                #[cfg(feature = "timezones")]
                // for UTC, use fastpath below (same as naive)
                Some(tz) if tz != &chrono_tz::UTC => {
                    let original_dt_utc = timestamp_to_datetime(t);
                    let original_dt_local = unlocalize_datetime(original_dt_utc, tz);
                    let result_dt_local = Self::add_month(original_dt_local, d.months, d.negative);
                    datetime_to_timestamp(self.localize_result_rfc_5545(
                        original_dt_utc,
                        result_dt_local,
                        tz,
                    )?)
                },
                _ => datetime_to_timestamp(Self::add_month(
                    timestamp_to_datetime(t),
                    d.months,
                    d.negative,
                )),
            };
        }

        if d.weeks > 0 {
            let t_weeks = nsecs_to_unit(NS_WEEK) * self.weeks;
            t = match tz {
                #[cfg(feature = "timezones")]
                // for UTC, use fastpath below (same as naive)
                Some(tz) if tz != &chrono_tz::UTC => {
                    let original_dt_utc = timestamp_to_datetime(t);
                    let original_dt_local = unlocalize_datetime(original_dt_utc, tz);
                    let mut result_timestamp_local = datetime_to_timestamp(original_dt_local);
                    result_timestamp_local += if d.negative { -t_weeks } else { t_weeks };
                    let result_dt_local = timestamp_to_datetime(result_timestamp_local);
                    datetime_to_timestamp(self.localize_result_rfc_5545(
                        original_dt_utc,
                        result_dt_local,
                        tz,
                    )?)