@@ -37,16 +37,16 @@ pub const EPOCH_DAYS_FROM_CE: i32 = 719_163;
37
37
38
38
/// converts a `i32` representing a `date32` to [`NaiveDateTime`]
39
39
#[ inline]
40
- pub fn date32_to_datetime ( v : i32 ) -> NaiveDateTime {
41
- NaiveDateTime :: from_timestamp ( v as i64 * SECONDS_IN_DAY , 0 )
40
+ pub fn date32_to_datetime ( v : i32 ) -> Option < NaiveDateTime > {
41
+ NaiveDateTime :: from_timestamp_opt ( v as i64 * SECONDS_IN_DAY , 0 )
42
42
}
43
43
44
44
/// converts a `i64` representing a `date64` to [`NaiveDateTime`]
45
45
#[ inline]
46
- pub fn date64_to_datetime ( v : i64 ) -> NaiveDateTime {
46
+ pub fn date64_to_datetime ( v : i64 ) -> Option < NaiveDateTime > {
47
47
let ( sec, milli_sec) = split_second ( v, MILLISECONDS ) ;
48
48
49
- NaiveDateTime :: from_timestamp (
49
+ NaiveDateTime :: from_timestamp_opt (
50
50
// extract seconds from milliseconds
51
51
sec,
52
52
// discard extracted seconds and convert milliseconds to nanoseconds
@@ -56,15 +56,15 @@ pub fn date64_to_datetime(v: i64) -> NaiveDateTime {
56
56
57
57
/// converts a `i32` representing a `time32(s)` to [`NaiveDateTime`]
58
58
#[ inline]
59
- pub fn time32s_to_time ( v : i32 ) -> NaiveTime {
60
- NaiveTime :: from_num_seconds_from_midnight ( v as u32 , 0 )
59
+ pub fn time32s_to_time ( v : i32 ) -> Option < NaiveTime > {
60
+ NaiveTime :: from_num_seconds_from_midnight_opt ( v as u32 , 0 )
61
61
}
62
62
63
63
/// converts a `i32` representing a `time32(ms)` to [`NaiveDateTime`]
64
64
#[ inline]
65
- pub fn time32ms_to_time ( v : i32 ) -> NaiveTime {
65
+ pub fn time32ms_to_time ( v : i32 ) -> Option < NaiveTime > {
66
66
let v = v as i64 ;
67
- NaiveTime :: from_num_seconds_from_midnight (
67
+ NaiveTime :: from_num_seconds_from_midnight_opt (
68
68
// extract seconds from milliseconds
69
69
( v / MILLISECONDS ) as u32 ,
70
70
// discard extracted seconds and convert milliseconds to
@@ -75,8 +75,8 @@ pub fn time32ms_to_time(v: i32) -> NaiveTime {
75
75
76
76
/// converts a `i64` representing a `time64(us)` to [`NaiveDateTime`]
77
77
#[ inline]
78
- pub fn time64us_to_time ( v : i64 ) -> NaiveTime {
79
- NaiveTime :: from_num_seconds_from_midnight (
78
+ pub fn time64us_to_time ( v : i64 ) -> Option < NaiveTime > {
79
+ NaiveTime :: from_num_seconds_from_midnight_opt (
80
80
// extract seconds from microseconds
81
81
( v / MICROSECONDS ) as u32 ,
82
82
// discard extracted seconds and convert microseconds to
@@ -87,8 +87,8 @@ pub fn time64us_to_time(v: i64) -> NaiveTime {
87
87
88
88
/// converts a `i64` representing a `time64(ns)` to [`NaiveDateTime`]
89
89
#[ inline]
90
- pub fn time64ns_to_time ( v : i64 ) -> NaiveTime {
91
- NaiveTime :: from_num_seconds_from_midnight (
90
+ pub fn time64ns_to_time ( v : i64 ) -> Option < NaiveTime > {
91
+ NaiveTime :: from_num_seconds_from_midnight_opt (
92
92
// extract seconds from nanoseconds
93
93
( v / NANOSECONDS ) as u32 ,
94
94
// discard extracted seconds
@@ -98,16 +98,16 @@ pub fn time64ns_to_time(v: i64) -> NaiveTime {
98
98
99
99
/// converts a `i64` representing a `timestamp(s)` to [`NaiveDateTime`]
100
100
#[ inline]
101
- pub fn timestamp_s_to_datetime ( v : i64 ) -> NaiveDateTime {
102
- NaiveDateTime :: from_timestamp ( v, 0 )
101
+ pub fn timestamp_s_to_datetime ( v : i64 ) -> Option < NaiveDateTime > {
102
+ NaiveDateTime :: from_timestamp_opt ( v, 0 )
103
103
}
104
104
105
105
/// converts a `i64` representing a `timestamp(ms)` to [`NaiveDateTime`]
106
106
#[ inline]
107
- pub fn timestamp_ms_to_datetime ( v : i64 ) -> NaiveDateTime {
107
+ pub fn timestamp_ms_to_datetime ( v : i64 ) -> Option < NaiveDateTime > {
108
108
let ( sec, milli_sec) = split_second ( v, MILLISECONDS ) ;
109
109
110
- NaiveDateTime :: from_timestamp (
110
+ NaiveDateTime :: from_timestamp_opt (
111
111
// extract seconds from milliseconds
112
112
sec,
113
113
// discard extracted seconds and convert milliseconds to nanoseconds
@@ -117,10 +117,10 @@ pub fn timestamp_ms_to_datetime(v: i64) -> NaiveDateTime {
117
117
118
118
/// converts a `i64` representing a `timestamp(us)` to [`NaiveDateTime`]
119
119
#[ inline]
120
- pub fn timestamp_us_to_datetime ( v : i64 ) -> NaiveDateTime {
120
+ pub fn timestamp_us_to_datetime ( v : i64 ) -> Option < NaiveDateTime > {
121
121
let ( sec, micro_sec) = split_second ( v, MICROSECONDS ) ;
122
122
123
- NaiveDateTime :: from_timestamp (
123
+ NaiveDateTime :: from_timestamp_opt (
124
124
// extract seconds from microseconds
125
125
sec,
126
126
// discard extracted seconds and convert microseconds to nanoseconds
@@ -130,10 +130,10 @@ pub fn timestamp_us_to_datetime(v: i64) -> NaiveDateTime {
130
130
131
131
/// converts a `i64` representing a `timestamp(ns)` to [`NaiveDateTime`]
132
132
#[ inline]
133
- pub fn timestamp_ns_to_datetime ( v : i64 ) -> NaiveDateTime {
133
+ pub fn timestamp_ns_to_datetime ( v : i64 ) -> Option < NaiveDateTime > {
134
134
let ( sec, nano_sec) = split_second ( v, NANOSECONDS ) ;
135
135
136
- NaiveDateTime :: from_timestamp (
136
+ NaiveDateTime :: from_timestamp_opt (
137
137
// extract seconds from nanoseconds
138
138
sec, // discard extracted seconds
139
139
nano_sec,
@@ -172,14 +172,14 @@ pub fn duration_ns_to_duration(v: i64) -> Duration {
172
172
/// Converts an [`ArrowPrimitiveType`] to [`NaiveDateTime`]
173
173
pub fn as_datetime < T : ArrowPrimitiveType > ( v : i64 ) -> Option < NaiveDateTime > {
174
174
match T :: DATA_TYPE {
175
- DataType :: Date32 => Some ( date32_to_datetime ( v as i32 ) ) ,
176
- DataType :: Date64 => Some ( date64_to_datetime ( v) ) ,
175
+ DataType :: Date32 => date32_to_datetime ( v as i32 ) ,
176
+ DataType :: Date64 => date64_to_datetime ( v) ,
177
177
DataType :: Time32 ( _) | DataType :: Time64 ( _) => None ,
178
178
DataType :: Timestamp ( unit, _) => match unit {
179
- TimeUnit :: Second => Some ( timestamp_s_to_datetime ( v) ) ,
180
- TimeUnit :: Millisecond => Some ( timestamp_ms_to_datetime ( v) ) ,
181
- TimeUnit :: Microsecond => Some ( timestamp_us_to_datetime ( v) ) ,
182
- TimeUnit :: Nanosecond => Some ( timestamp_ns_to_datetime ( v) ) ,
179
+ TimeUnit :: Second => timestamp_s_to_datetime ( v) ,
180
+ TimeUnit :: Millisecond => timestamp_ms_to_datetime ( v) ,
181
+ TimeUnit :: Microsecond => timestamp_us_to_datetime ( v) ,
182
+ TimeUnit :: Nanosecond => timestamp_ns_to_datetime ( v) ,
183
183
} ,
184
184
// interval is not yet fully documented [ARROW-3097]
185
185
DataType :: Interval ( _) => None ,
@@ -199,14 +199,14 @@ pub fn as_time<T: ArrowPrimitiveType>(v: i64) -> Option<NaiveTime> {
199
199
// safe to immediately cast to u32 as `self.value(i)` is positive i32
200
200
let v = v as u32 ;
201
201
match unit {
202
- TimeUnit :: Second => Some ( time32s_to_time ( v as i32 ) ) ,
203
- TimeUnit :: Millisecond => Some ( time32ms_to_time ( v as i32 ) ) ,
202
+ TimeUnit :: Second => time32s_to_time ( v as i32 ) ,
203
+ TimeUnit :: Millisecond => time32ms_to_time ( v as i32 ) ,
204
204
_ => None ,
205
205
}
206
206
}
207
207
DataType :: Time64 ( unit) => match unit {
208
- TimeUnit :: Microsecond => Some ( time64us_to_time ( v) ) ,
209
- TimeUnit :: Nanosecond => Some ( time64ns_to_time ( v) ) ,
208
+ TimeUnit :: Microsecond => time64us_to_time ( v) ,
209
+ TimeUnit :: Nanosecond => time64ns_to_time ( v) ,
210
210
_ => None ,
211
211
} ,
212
212
DataType :: Timestamp ( _, _) => as_datetime :: < T > ( v) . map ( |datetime| datetime. time ( ) ) ,
@@ -241,51 +241,51 @@ mod tests {
241
241
fn negative_input_timestamp_ns_to_datetime ( ) {
242
242
assert_eq ! (
243
243
timestamp_ns_to_datetime( -1 ) ,
244
- NaiveDateTime :: from_timestamp ( -1 , 999_999_999 )
244
+ NaiveDateTime :: from_timestamp_opt ( -1 , 999_999_999 )
245
245
) ;
246
246
247
247
assert_eq ! (
248
248
timestamp_ns_to_datetime( -1_000_000_001 ) ,
249
- NaiveDateTime :: from_timestamp ( -2 , 999_999_999 )
249
+ NaiveDateTime :: from_timestamp_opt ( -2 , 999_999_999 )
250
250
) ;
251
251
}
252
252
253
253
#[ test]
254
254
fn negative_input_timestamp_us_to_datetime ( ) {
255
255
assert_eq ! (
256
256
timestamp_us_to_datetime( -1 ) ,
257
- NaiveDateTime :: from_timestamp ( -1 , 999_999_000 )
257
+ NaiveDateTime :: from_timestamp_opt ( -1 , 999_999_000 )
258
258
) ;
259
259
260
260
assert_eq ! (
261
261
timestamp_us_to_datetime( -1_000_001 ) ,
262
- NaiveDateTime :: from_timestamp ( -2 , 999_999_000 )
262
+ NaiveDateTime :: from_timestamp_opt ( -2 , 999_999_000 )
263
263
) ;
264
264
}
265
265
266
266
#[ test]
267
267
fn negative_input_timestamp_ms_to_datetime ( ) {
268
268
assert_eq ! (
269
269
timestamp_ms_to_datetime( -1 ) ,
270
- NaiveDateTime :: from_timestamp ( -1 , 999_000_000 )
270
+ NaiveDateTime :: from_timestamp_opt ( -1 , 999_000_000 )
271
271
) ;
272
272
273
273
assert_eq ! (
274
274
timestamp_ms_to_datetime( -1_001 ) ,
275
- NaiveDateTime :: from_timestamp ( -2 , 999_000_000 )
275
+ NaiveDateTime :: from_timestamp_opt ( -2 , 999_000_000 )
276
276
) ;
277
277
}
278
278
279
279
#[ test]
280
280
fn negative_input_date64_to_datetime ( ) {
281
281
assert_eq ! (
282
282
date64_to_datetime( -1 ) ,
283
- NaiveDateTime :: from_timestamp ( -1 , 999_000_000 )
283
+ NaiveDateTime :: from_timestamp_opt ( -1 , 999_000_000 )
284
284
) ;
285
285
286
286
assert_eq ! (
287
287
date64_to_datetime( -1_001 ) ,
288
- NaiveDateTime :: from_timestamp ( -2 , 999_000_000 )
288
+ NaiveDateTime :: from_timestamp_opt ( -2 , 999_000_000 )
289
289
) ;
290
290
}
291
291
0 commit comments