@@ -70,32 +70,41 @@ fn time_monotonic(_vm: &VirtualMachine) -> f64 {
70
70
}
71
71
}
72
72
73
- fn pyobj_to_naive_date_time ( value : Either < f64 , i64 > ) -> NaiveDateTime {
74
- match value {
73
+ fn pyobj_to_naive_date_time (
74
+ value : Either < f64 , i64 > ,
75
+ vm : & VirtualMachine ,
76
+ ) -> PyResult < NaiveDateTime > {
77
+ let timestamp = match value {
75
78
Either :: A ( float) => {
76
79
let secs = float. trunc ( ) as i64 ;
77
80
let nsecs = ( float. fract ( ) * 1e9 ) as u32 ;
78
- NaiveDateTime :: from_timestamp ( secs, nsecs)
81
+ NaiveDateTime :: from_timestamp_opt ( secs, nsecs)
79
82
}
80
- Either :: B ( int) => NaiveDateTime :: from_timestamp ( int, 0 ) ,
81
- }
83
+ Either :: B ( int) => NaiveDateTime :: from_timestamp_opt ( int, 0 ) ,
84
+ } ;
85
+ timestamp. ok_or_else ( || {
86
+ vm. new_overflow_error ( "timestamp out of range for platform time_t" . to_owned ( ) )
87
+ } )
82
88
}
83
89
84
90
/// https://docs.python.org/3/library/time.html?highlight=gmtime#time.gmtime
85
- fn time_gmtime ( secs : OptionalArg < Either < f64 , i64 > > , vm : & VirtualMachine ) -> PyObjectRef {
91
+ fn time_gmtime ( secs : OptionalArg < Either < f64 , i64 > > , vm : & VirtualMachine ) -> PyResult < PyObjectRef > {
86
92
let default = chrono:: offset:: Utc :: now ( ) . naive_utc ( ) ;
87
93
let instant = match secs {
88
- OptionalArg :: Present ( secs) => pyobj_to_naive_date_time ( secs) ,
94
+ OptionalArg :: Present ( secs) => pyobj_to_naive_date_time ( secs, vm ) ? ,
89
95
OptionalArg :: Missing => default,
90
96
} ;
91
- PyStructTime :: new ( vm, instant, 0 ) . into_obj ( vm)
97
+ Ok ( PyStructTime :: new ( vm, instant, 0 ) . into_obj ( vm) )
92
98
}
93
99
94
- fn time_localtime ( secs : OptionalArg < Either < f64 , i64 > > , vm : & VirtualMachine ) -> PyObjectRef {
95
- let instant = optional_or_localtime ( secs) ;
100
+ fn time_localtime (
101
+ secs : OptionalArg < Either < f64 , i64 > > ,
102
+ vm : & VirtualMachine ,
103
+ ) -> PyResult < PyObjectRef > {
104
+ let instant = optional_or_localtime ( secs, vm) ?;
96
105
// TODO: isdst flag must be valid value here
97
106
// https://docs.python.org/3/library/time.html#time.localtime
98
- PyStructTime :: new ( vm, instant, -1 ) . into_obj ( vm)
107
+ Ok ( PyStructTime :: new ( vm, instant, -1 ) . into_obj ( vm) )
99
108
}
100
109
101
110
fn time_mktime ( t : PyStructTime , vm : & VirtualMachine ) -> PyResult {
@@ -105,12 +114,15 @@ fn time_mktime(t: PyStructTime, vm: &VirtualMachine) -> PyResult {
105
114
}
106
115
107
116
/// Construct a localtime from the optional seconds, or get the current local time.
108
- fn optional_or_localtime ( secs : OptionalArg < Either < f64 , i64 > > ) -> NaiveDateTime {
117
+ fn optional_or_localtime (
118
+ secs : OptionalArg < Either < f64 , i64 > > ,
119
+ vm : & VirtualMachine ,
120
+ ) -> PyResult < NaiveDateTime > {
109
121
let default = chrono:: offset:: Local :: now ( ) . naive_local ( ) ;
110
- match secs {
111
- OptionalArg :: Present ( secs) => pyobj_to_naive_date_time ( secs) ,
122
+ Ok ( match secs {
123
+ OptionalArg :: Present ( secs) => pyobj_to_naive_date_time ( secs, vm ) ? ,
112
124
OptionalArg :: Missing => default,
113
- }
125
+ } )
114
126
}
115
127
116
128
const CFMT : & str = "%a %b %e %H:%M:%S %Y" ;
@@ -125,9 +137,9 @@ fn time_asctime(t: OptionalArg<PyStructTime>, vm: &VirtualMachine) -> PyResult {
125
137
Ok ( vm. ctx . new_str ( formatted_time) )
126
138
}
127
139
128
- fn time_ctime ( secs : OptionalArg < Either < f64 , i64 > > ) -> String {
129
- let instant = optional_or_localtime ( secs) ;
130
- instant. format ( & CFMT ) . to_string ( )
140
+ fn time_ctime ( secs : OptionalArg < Either < f64 , i64 > > , vm : & VirtualMachine ) -> PyResult < String > {
141
+ let instant = optional_or_localtime ( secs, vm ) ? ;
142
+ Ok ( instant. format ( & CFMT ) . to_string ( ) )
131
143
}
132
144
133
145
fn time_strftime (
0 commit comments