@@ -47,20 +47,26 @@ pub fn pydate_as_date(py_date: &Bound<'_, PyAny>) -> PyResult<Date> {
4747 } )
4848}
4949
50- impl < ' py > IntoPyObject < ' py > for EitherDate < ' py > {
51- type Target = PyDate ;
52- type Output = Bound < ' py , PyDate > ;
53- type Error = PyErr ;
54-
55- fn into_pyobject ( self , py : Python < ' py > ) -> PyResult < Self :: Output > {
50+ impl < ' py > EitherDate < ' py > {
51+ pub fn try_into_py ( & self , py : Python < ' py > , input : & ( impl Input < ' py > + ?Sized ) ) -> ValResult < PyObject > {
5652 match self {
57- Self :: Raw ( date) => PyDate :: new ( py, date. year . into ( ) , date. month , date. day ) ,
58- Self :: Py ( date) => Ok ( date) ,
53+ Self :: Raw ( date) => {
54+ if date. year == 0 {
55+ return Err ( ValError :: new (
56+ ErrorType :: DateParsing {
57+ error : Cow :: Borrowed ( "year 0 is out of range" ) ,
58+ context : None ,
59+ } ,
60+ input,
61+ ) ) ;
62+ } ;
63+ let py_date = PyDate :: new ( py, date. year . into ( ) , date. month , date. day ) ?;
64+ Ok ( py_date. into ( ) )
65+ }
66+ Self :: Py ( py_date) => Ok ( py_date. clone ( ) . into ( ) ) ,
5967 }
6068 }
61- }
6269
63- impl EitherDate < ' _ > {
6470 pub fn as_raw ( & self ) -> PyResult < Date > {
6571 match self {
6672 Self :: Raw ( date) => Ok ( date. clone ( ) ) ,
@@ -278,30 +284,36 @@ pub fn pydatetime_as_datetime(py_dt: &Bound<'_, PyAny>) -> PyResult<DateTime> {
278284 } )
279285}
280286
281- impl < ' py > IntoPyObject < ' py > for EitherDateTime < ' py > {
282- type Target = PyDateTime ;
283- type Output = Bound < ' py , PyDateTime > ;
284- type Error = PyErr ;
285-
286- fn into_pyobject ( self , py : Python < ' py > ) -> PyResult < Self :: Output > {
287+ impl < ' py > EitherDateTime < ' py > {
288+ pub fn try_into_py ( & self , py : Python < ' py > , input : & ( impl Input < ' py > + ?Sized ) ) -> ValResult < PyObject > {
287289 match self {
288- Self :: Raw ( dt) => PyDateTime :: new (
289- py,
290- dt. date . year . into ( ) ,
291- dt. date . month ,
292- dt. date . day ,
293- dt. time . hour ,
294- dt. time . minute ,
295- dt. time . second ,
296- dt. time . microsecond ,
297- time_as_tzinfo ( py, & dt. time ) ?. as_ref ( ) ,
298- ) ,
299- Self :: Py ( dt) => Ok ( dt) ,
290+ Self :: Raw ( dt) => {
291+ if dt. date . year == 0 {
292+ return Err ( ValError :: new (
293+ ErrorType :: DatetimeParsing {
294+ error : Cow :: Borrowed ( "year 0 is out of range" ) ,
295+ context : None ,
296+ } ,
297+ input,
298+ ) ) ;
299+ } ;
300+ let py_dt = PyDateTime :: new (
301+ py,
302+ dt. date . year . into ( ) ,
303+ dt. date . month ,
304+ dt. date . day ,
305+ dt. time . hour ,
306+ dt. time . minute ,
307+ dt. time . second ,
308+ dt. time . microsecond ,
309+ time_as_tzinfo ( py, & dt. time ) ?. as_ref ( ) ,
310+ ) ?;
311+ Ok ( py_dt. into ( ) )
312+ }
313+ Self :: Py ( py_dt) => Ok ( py_dt. clone ( ) . into ( ) ) ,
300314 }
301315 }
302- }
303316
304- impl EitherDateTime < ' _ > {
305317 pub fn as_raw ( & self ) -> PyResult < DateTime > {
306318 match self {
307319 Self :: Raw ( dt) => Ok ( dt. clone ( ) ) ,
0 commit comments