@@ -31,24 +31,26 @@ pub const EPOCH_DAYS_FROM_CE: i32 = 719_163;
31
31
/// converts a `i32` representing a `date32` to [`NaiveDateTime`]
32
32
#[ inline]
33
33
pub fn date32_to_datetime ( v : i32 ) -> NaiveDateTime {
34
- NaiveDateTime :: from_timestamp ( v as i64 * SECONDS_IN_DAY , 0 )
34
+ NaiveDateTime :: from_timestamp_opt ( v as i64 * SECONDS_IN_DAY , 0 )
35
+ . expect ( "invalid or out-of-range datetime" )
35
36
}
36
37
37
38
/// converts a `i32` representing a `date32` to [`NaiveDate`]
38
39
#[ inline]
39
40
pub fn date32_to_date ( days : i32 ) -> NaiveDate {
40
- NaiveDate :: from_num_days_from_ce ( EPOCH_DAYS_FROM_CE + days)
41
+ NaiveDate :: from_num_days_from_ce_opt ( EPOCH_DAYS_FROM_CE + days) . expect ( "out-of-range date" )
41
42
}
42
43
43
44
/// converts a `i64` representing a `date64` to [`NaiveDateTime`]
44
45
#[ inline]
45
46
pub fn date64_to_datetime ( v : i64 ) -> NaiveDateTime {
46
- NaiveDateTime :: from_timestamp (
47
+ NaiveDateTime :: from_timestamp_opt (
47
48
// extract seconds from milliseconds
48
49
v / MILLISECONDS ,
49
50
// discard extracted seconds and convert milliseconds to nanoseconds
50
51
( v % MILLISECONDS * MICROSECONDS ) as u32 ,
51
52
)
53
+ . expect ( "invalid or out-of-range datetime" )
52
54
}
53
55
54
56
/// converts a `i64` representing a `date64` to [`NaiveDate`]
@@ -60,7 +62,7 @@ pub fn date64_to_date(milliseconds: i64) -> NaiveDate {
60
62
/// converts a `i32` representing a `time32(s)` to [`NaiveDateTime`]
61
63
#[ inline]
62
64
pub fn time32s_to_time ( v : i32 ) -> NaiveTime {
63
- NaiveTime :: from_num_seconds_from_midnight ( v as u32 , 0 )
65
+ NaiveTime :: from_num_seconds_from_midnight_opt ( v as u32 , 0 ) . expect ( "invalid time" )
64
66
}
65
67
66
68
/// converts a `i32` representing a `time32(ms)` to [`NaiveTime`]
@@ -71,69 +73,75 @@ pub fn time32ms_to_time(v: i32) -> NaiveTime {
71
73
72
74
let milli_to_nano = 1_000_000 ;
73
75
let nano = ( v - seconds * MILLISECONDS ) * milli_to_nano;
74
- NaiveTime :: from_num_seconds_from_midnight ( seconds as u32 , nano as u32 )
76
+ NaiveTime :: from_num_seconds_from_midnight_opt ( seconds as u32 , nano as u32 )
77
+ . expect ( "invalid time" )
75
78
}
76
79
77
80
/// converts a `i64` representing a `time64(us)` to [`NaiveDateTime`]
78
81
#[ inline]
79
82
pub fn time64us_to_time ( v : i64 ) -> NaiveTime {
80
- NaiveTime :: from_num_seconds_from_midnight (
83
+ NaiveTime :: from_num_seconds_from_midnight_opt (
81
84
// extract seconds from microseconds
82
85
( v / MICROSECONDS ) as u32 ,
83
86
// discard extracted seconds and convert microseconds to
84
87
// nanoseconds
85
88
( v % MICROSECONDS * MILLISECONDS ) as u32 ,
86
89
)
90
+ . expect ( "invalid time" )
87
91
}
88
92
89
93
/// converts a `i64` representing a `time64(ns)` to [`NaiveDateTime`]
90
94
#[ inline]
91
95
pub fn time64ns_to_time ( v : i64 ) -> NaiveTime {
92
- NaiveTime :: from_num_seconds_from_midnight (
96
+ NaiveTime :: from_num_seconds_from_midnight_opt (
93
97
// extract seconds from nanoseconds
94
98
( v / NANOSECONDS ) as u32 ,
95
99
// discard extracted seconds
96
100
( v % NANOSECONDS ) as u32 ,
97
101
)
102
+ . expect ( "invalid time" )
98
103
}
99
104
100
105
/// converts a `i64` representing a `timestamp(s)` to [`NaiveDateTime`]
101
106
#[ inline]
102
107
pub fn timestamp_s_to_datetime ( seconds : i64 ) -> NaiveDateTime {
103
- NaiveDateTime :: from_timestamp ( seconds, 0 )
108
+ NaiveDateTime :: from_timestamp_opt ( seconds, 0 ) . expect ( "invalid or out-of-range datetime" )
104
109
}
105
110
106
111
/// converts a `i64` representing a `timestamp(ms)` to [`NaiveDateTime`]
107
112
#[ inline]
108
113
pub fn timestamp_ms_to_datetime ( v : i64 ) -> NaiveDateTime {
109
- NaiveDateTime :: from_timestamp (
114
+ NaiveDateTime :: from_timestamp_opt (
110
115
// extract seconds from milliseconds
111
116
v / MILLISECONDS ,
112
117
// discard extracted seconds and convert milliseconds to nanoseconds
113
118
( v % MILLISECONDS * MICROSECONDS ) as u32 ,
114
119
)
120
+ . expect ( "invalid or out-of-range datetime" )
115
121
}
116
122
117
123
/// converts a `i64` representing a `timestamp(us)` to [`NaiveDateTime`]
118
124
#[ inline]
119
125
pub fn timestamp_us_to_datetime ( v : i64 ) -> NaiveDateTime {
120
- NaiveDateTime :: from_timestamp (
126
+ NaiveDateTime :: from_timestamp_opt (
121
127
// extract seconds from microseconds
122
128
v / MICROSECONDS ,
123
129
// discard extracted seconds and convert microseconds to nanoseconds
124
130
( v % MICROSECONDS * MILLISECONDS ) as u32 ,
125
131
)
132
+ . expect ( "invalid or out-of-range datetime" )
126
133
}
127
134
128
135
/// converts a `i64` representing a `timestamp(ns)` to [`NaiveDateTime`]
129
136
#[ inline]
130
137
pub fn timestamp_ns_to_datetime ( v : i64 ) -> NaiveDateTime {
131
- NaiveDateTime :: from_timestamp (
138
+ NaiveDateTime :: from_timestamp_opt (
132
139
// extract seconds from nanoseconds
133
140
v / NANOSECONDS ,
134
141
// discard extracted seconds
135
142
( v % NANOSECONDS ) as u32 ,
136
143
)
144
+ . expect ( "invalid or out-of-range datetime" )
137
145
}
138
146
139
147
/// Converts a timestamp in `time_unit` and `timezone` into [`chrono::DateTime`].
@@ -186,7 +194,7 @@ pub fn timeunit_scale(a: TimeUnit, b: TimeUnit) -> f64 {
186
194
/// If the offset is not in any of the allowed forms.
187
195
pub fn parse_offset ( offset : & str ) -> Result < FixedOffset > {
188
196
if offset == "UTC" {
189
- return Ok ( FixedOffset :: east ( 0 ) ) ;
197
+ return Ok ( FixedOffset :: east_opt ( 0 ) . expect ( "FixedOffset::east out of bounds" ) ) ;
190
198
}
191
199
let error = "timezone offset must be of the form [-]00:00" ;
192
200
@@ -206,7 +214,8 @@ pub fn parse_offset(offset: &str) -> Result<FixedOffset> {
206
214
. parse ( )
207
215
. map_err ( |_| Error :: InvalidArgumentError ( error. to_string ( ) ) ) ?;
208
216
209
- Ok ( FixedOffset :: east ( hours * 60 * 60 + minutes * 60 ) )
217
+ Ok ( FixedOffset :: east_opt ( hours * 60 * 60 + minutes * 60 )
218
+ . expect ( "FixedOffset::east out of bounds" ) )
210
219
}
211
220
212
221
/// Parses `value` to `Option<i64>` consistent with the Arrow's definition of timestamp with timezone.
@@ -330,12 +339,15 @@ pub fn utf8_to_naive_timestamp_ns<O: Offset>(
330
339
fn add_month ( year : i32 , month : u32 , months : i32 ) -> chrono:: NaiveDate {
331
340
let new_year = ( year * 12 + ( month - 1 ) as i32 + months) / 12 ;
332
341
let new_month = ( year * 12 + ( month - 1 ) as i32 + months) % 12 + 1 ;
333
- chrono:: NaiveDate :: from_ymd ( new_year, new_month as u32 , 1 )
342
+ chrono:: NaiveDate :: from_ymd_opt ( new_year, new_month as u32 , 1 )
343
+ . expect ( "invalid or out-of-range date" )
334
344
}
335
345
336
346
fn get_days_between_months ( year : i32 , month : u32 , months : i32 ) -> i64 {
337
347
add_month ( year, month, months)
338
- . signed_duration_since ( chrono:: NaiveDate :: from_ymd ( year, month, 1 ) )
348
+ . signed_duration_since (
349
+ chrono:: NaiveDate :: from_ymd_opt ( year, month, 1 ) . expect ( "invalid or out-of-range date" ) ,
350
+ )
339
351
. num_days ( )
340
352
}
341
353
0 commit comments