[Bug] temporal: subtracting months raises ValueError when the result is December two or more years back

Aperta Adatta ai principianti
#8,296 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
70/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
python
Ambito
backend

Direzione di ricerca

Inizia da python/grass/temporal/datetime_math.py e leggi il ramo dei mesi negativi di modify_datetime(); decrement_datetime_by_string() raggiunge la stessa logica. Riproduci i casi di dicembre dell’issue, poi aggiungi test di regressione per questi casi. Il lavoro è completato quando entrambe le funzioni restituiscono le date previste senza sollevare eccezioni e corrispondono a dateutil.relativedelta.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

Describe the bug

modify_datetime() in python/grass/temporal/datetime_math.py raises when subtracting months lands on December two or more calendar years earlier. The negative-months branch splits the count into years and months with int(all_months / 12.001), which truncates toward zero, so it ends up with month 0.

decrement_datetime_by_string() goes through the same code.

This came up in the review of #8091 and is separate from the month-end issue in #8090.

To reproduce

In a GRASS session:

from datetime import datetime
from grass.temporal.datetime_math import decrement_datetime_by_string, modify_datetime

modify_datetime(datetime(2001, 1, 1), months=-13)
# ValueError: month must be in 1..12

decrement_datetime_by_string(datetime(2001, 12, 1), "24 months")
# ValueError: month must be in 1..12

decrement_datetime_by_string(datetime(2001, 5, 1), "29 months")
# ValueError: month must be in 1..12

Smaller steps work, for example 2001-01-01 minus 1 month gives 2000-12-01, and minus 24 months gives 1999-01-01.

Expected behavior

1999-12-01, 1999-12-01 and 1998-12-01, the same results as dateutil.relativedelta.

System description

  • GRASS: current main (9932cef2bd)
  • Python 3.12, Ubuntu 24.04 (Docker)

Additional context

I have a fix ready that computes the new year and month with divmod() on a running month count, plus tests.

Lingua principale
C
Stelle
1.2k
Fork
447
Merge medio
2g 10h
PR unite (30g)
86

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di OSGeo/grass

Tutte le issue di OSGeo/grass

Issue simili

Altre issue su C

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.