Skip to content

Commit 6a01122

Browse files
authored
chore: update deprecated chrono functions (PLC-lang#1208)
1 parent 66ce86a commit 6a01122

6 files changed

+116
-69
lines changed

libs/stdlib/src/extra_functions.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,7 @@ mod test {
386386
let datetime = chrono::NaiveDate::from_ymd_opt(1982, 12, 15)
387387
.and_then(|date| date.and_hms_nano_opt(0, 0, 0, 0))
388388
.expect("Cannot create date time from given parameters");
389-
let timestamp = datetime.timestamp_nanos_opt().unwrap();
389+
let timestamp = datetime.and_utc().timestamp_nanos_opt().unwrap();
390390

391391
let mut dest = [0_u8; 81];
392392
let dest_ptr = dest.as_mut_ptr();
@@ -403,7 +403,7 @@ mod test {
403403
let datetime = chrono::NaiveDate::from_ymd_opt(1982, 12, 15)
404404
.and_then(|date| date.and_hms_nano_opt(10, 10, 2, 123456789))
405405
.expect("Cannot create date time from given parameters");
406-
let timestamp = datetime.timestamp_nanos_opt().unwrap();
406+
let timestamp = datetime.and_utc().timestamp_nanos_opt().unwrap();
407407

408408
let mut dest = [0_u8; 81];
409409
let dest_ptr = dest.as_mut_ptr();
@@ -420,7 +420,7 @@ mod test {
420420
let datetime = chrono::NaiveDate::from_ymd_opt(1982, 12, 15)
421421
.and_then(|date| date.and_hms_nano_opt(10, 10, 2, 123456789))
422422
.expect("Cannot create date time from given parameters");
423-
let timestamp = datetime.timestamp_nanos_opt().unwrap();
423+
let timestamp = datetime.and_utc().timestamp_nanos_opt().unwrap();
424424

425425
let mut dest = [0_u8; 81];
426426
let dest_ptr = dest.as_mut_ptr();
@@ -437,7 +437,7 @@ mod test {
437437
let datetime = chrono::NaiveDate::from_ymd_opt(2023, 1, 23)
438438
.and_then(|date| date.and_hms_nano_opt(10, 10, 0, 123456789))
439439
.expect("Cannot create date time from given parameters");
440-
let timestamp = datetime.timestamp_nanos_opt().unwrap();
440+
let timestamp = datetime.and_utc().timestamp_nanos_opt().unwrap();
441441

442442
let mut dest = [0_u8; 81];
443443
let dest_ptr = dest.as_mut_ptr();

libs/stdlib/tests/date_time_conversion_tests.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ fn ldt_to_dt_conversion() {
5656
.unwrap()
5757
.and_hms_opt(22, 33, 14)
5858
.unwrap()
59+
.and_utc()
5960
.timestamp_nanos_opt()
6061
.unwrap()
6162
);
@@ -76,6 +77,7 @@ fn ldt_to_date_conversion() {
7677
.unwrap()
7778
.and_hms_opt(0, 0, 0)
7879
.unwrap()
80+
.and_utc()
7981
.timestamp_nanos_opt()
8082
.unwrap()
8183
);
@@ -96,6 +98,7 @@ fn ldt_to_ltod_conversion() {
9698
.unwrap()
9799
.and_hms_nano_opt(15, 36, 30, 123456000)
98100
.unwrap()
101+
.and_utc()
99102
.timestamp_nanos_opt()
100103
.unwrap()
101104
);
@@ -116,6 +119,7 @@ fn ldt_to_tod_conversion() {
116119
.unwrap()
117120
.and_hms_milli_opt(20, 15, 11, 543)
118121
.unwrap()
122+
.and_utc()
119123
.timestamp_nanos_opt()
120124
.unwrap()
121125
);
@@ -136,6 +140,7 @@ fn dt_to_ldt_conversion() {
136140
.unwrap()
137141
.and_hms_opt(22, 33, 14)
138142
.unwrap()
143+
.and_utc()
139144
.timestamp_nanos_opt()
140145
.unwrap()
141146
);
@@ -156,6 +161,7 @@ fn dt_to_date_conversion() {
156161
.unwrap()
157162
.and_hms_opt(0, 0, 0)
158163
.unwrap()
164+
.and_utc()
159165
.timestamp_nanos_opt()
160166
.unwrap()
161167
);
@@ -176,6 +182,7 @@ fn dt_to_ltod_conversion() {
176182
.unwrap()
177183
.and_hms_milli_opt(15, 36, 30, 123)
178184
.unwrap()
185+
.and_utc()
179186
.timestamp_nanos_opt()
180187
.unwrap()
181188
);
@@ -196,6 +203,7 @@ fn dt_to_tod_conversion() {
196203
.unwrap()
197204
.and_hms_milli_opt(20, 15, 11, 543)
198205
.unwrap()
206+
.and_utc()
199207
.timestamp_nanos_opt()
200208
.unwrap()
201209
);
@@ -216,6 +224,7 @@ fn ltod_to_tod_conversion() {
216224
.unwrap()
217225
.and_hms_opt(10, 20, 30)
218226
.unwrap()
227+
.and_utc()
219228
.timestamp_nanos_opt()
220229
.unwrap()
221230
);
@@ -236,6 +245,7 @@ fn tod_to_ltod_conversion() {
236245
.unwrap()
237246
.and_hms_opt(10, 20, 30)
238247
.unwrap()
248+
.and_utc()
239249
.timestamp_nanos_opt()
240250
.unwrap()
241251
);

libs/stdlib/tests/date_time_extra_functions_tests.rs

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ fn concat_date_tod() {
3434
.unwrap()
3535
.and_hms_nano_opt(12, 30, 15, 121121121)
3636
.unwrap()
37+
.and_utc()
3738
.timestamp_nanos_opt()
3839
.unwrap();
3940
assert_eq!(res, dt_2010y_3m_12d_12h_30m_15s_121121121ns);
@@ -52,6 +53,7 @@ fn concat_date_ltod() {
5253
.unwrap()
5354
.and_hms_nano_opt(12, 30, 15, 121121121)
5455
.unwrap()
56+
.and_utc()
5557
.timestamp_nanos_opt()
5658
.unwrap();
5759
assert_eq!(res, dt_2010y_3m_12d_12h_30m_15s_121121121ns);
@@ -88,6 +90,7 @@ fn concat_date_signed_ints() {
8890
.unwrap()
8991
.and_hms_opt(0, 0, 0)
9092
.unwrap()
93+
.and_utc()
9194
.timestamp_nanos_opt()
9295
.unwrap();
9396
assert_eq!(maintype.a, date_2000y_1m_1d);
@@ -115,6 +118,7 @@ fn concat_date_unsigned_ints() {
115118
.unwrap()
116119
.and_hms_opt(0, 0, 0)
117120
.unwrap()
121+
.and_utc()
118122
.timestamp_nanos_opt()
119123
.unwrap();
120124
assert_eq!(maintype.a, date_2000y_1m_1d);
@@ -140,8 +144,9 @@ fn concat_tod_signed_ints() {
140144
let sources = add_std!(src, "date_time_extra_functions.st");
141145
let mut maintype = MainType::<i64>::default();
142146
let _: i64 = compile_and_run(sources, &mut maintype);
143-
assert_eq!(maintype.a, get_time_from_hms_milli(20, 15, 12, 34).timestamp_nanos_opt().unwrap());
144-
let tod_20h_15m_12s_341ms = get_time_from_hms_milli(20, 15, 12, 341).timestamp_nanos_opt().unwrap();
147+
assert_eq!(maintype.a, get_time_from_hms_milli(20, 15, 12, 34).and_utc().timestamp_nanos_opt().unwrap());
148+
let tod_20h_15m_12s_341ms =
149+
get_time_from_hms_milli(20, 15, 12, 341).and_utc().timestamp_nanos_opt().unwrap();
145150
assert_eq!(maintype.b, tod_20h_15m_12s_341ms);
146151
assert_eq!(maintype.c, tod_20h_15m_12s_341ms);
147152
assert_eq!(maintype.d, tod_20h_15m_12s_341ms);
@@ -165,8 +170,9 @@ fn concat_tod_unsigned_ints() {
165170
let sources = add_std!(src, "date_time_extra_functions.st");
166171
let mut maintype = MainType::<i64>::default();
167172
let _: i64 = compile_and_run(sources, &mut maintype);
168-
assert_eq!(maintype.a, get_time_from_hms_milli(20, 15, 12, 34).timestamp_nanos_opt().unwrap());
169-
let tod_20h_15m_12s_341ms = get_time_from_hms_milli(20, 15, 12, 341).timestamp_nanos_opt().unwrap();
173+
assert_eq!(maintype.a, get_time_from_hms_milli(20, 15, 12, 34).and_utc().timestamp_nanos_opt().unwrap());
174+
let tod_20h_15m_12s_341ms =
175+
get_time_from_hms_milli(20, 15, 12, 341).and_utc().timestamp_nanos_opt().unwrap();
170176
assert_eq!(maintype.b, tod_20h_15m_12s_341ms);
171177
assert_eq!(maintype.c, tod_20h_15m_12s_341ms);
172178
assert_eq!(maintype.d, tod_20h_15m_12s_341ms);
@@ -190,8 +196,9 @@ fn concat_ltod_signed_ints() {
190196
let sources = add_std!(src, "date_time_extra_functions.st");
191197
let mut maintype = MainType::<i64>::default();
192198
let _: i64 = compile_and_run(sources, &mut maintype);
193-
assert_eq!(maintype.a, get_time_from_hms_milli(20, 15, 12, 34).timestamp_nanos_opt().unwrap());
194-
let tod_20h_15m_12s_341ms = get_time_from_hms_milli(20, 15, 12, 341).timestamp_nanos_opt().unwrap();
199+
assert_eq!(maintype.a, get_time_from_hms_milli(20, 15, 12, 34).and_utc().timestamp_nanos_opt().unwrap());
200+
let tod_20h_15m_12s_341ms =
201+
get_time_from_hms_milli(20, 15, 12, 341).and_utc().timestamp_nanos_opt().unwrap();
195202
assert_eq!(maintype.b, tod_20h_15m_12s_341ms);
196203
assert_eq!(maintype.c, tod_20h_15m_12s_341ms);
197204
assert_eq!(maintype.d, tod_20h_15m_12s_341ms);
@@ -215,8 +222,9 @@ fn concat_ltod_unsigned_ints() {
215222
let sources = add_std!(src, "date_time_extra_functions.st");
216223
let mut maintype = MainType::<i64>::default();
217224
let _: i64 = compile_and_run(sources, &mut maintype);
218-
assert_eq!(maintype.a, get_time_from_hms_milli(20, 15, 12, 34).timestamp_nanos_opt().unwrap());
219-
let tod_20h_15m_12s_341ms = get_time_from_hms_milli(20, 15, 12, 341).timestamp_nanos_opt().unwrap();
225+
assert_eq!(maintype.a, get_time_from_hms_milli(20, 15, 12, 34).and_utc().timestamp_nanos_opt().unwrap());
226+
let tod_20h_15m_12s_341ms =
227+
get_time_from_hms_milli(20, 15, 12, 341).and_utc().timestamp_nanos_opt().unwrap();
220228
assert_eq!(maintype.b, tod_20h_15m_12s_341ms);
221229
assert_eq!(maintype.c, tod_20h_15m_12s_341ms);
222230
assert_eq!(maintype.d, tod_20h_15m_12s_341ms);
@@ -242,6 +250,7 @@ fn concat_dt_signed_ints() {
242250
.unwrap()
243251
.and_hms_milli_opt(20, 15, 12, 111)
244252
.unwrap()
253+
.and_utc()
245254
.timestamp_nanos_opt()
246255
.unwrap();
247256
assert_eq!(maintype.a, dt_2000y_1m_2d_20h_15m_12s_111ms);
@@ -269,6 +278,7 @@ fn concat_dt_unsigned_ints() {
269278
.unwrap()
270279
.and_hms_milli_opt(20, 15, 12, 111)
271280
.unwrap()
281+
.and_utc()
272282
.timestamp_nanos_opt()
273283
.unwrap();
274284
assert_eq!(maintype.a, dt_2000y_1m_2d_20h_15m_12s_111ms);
@@ -296,6 +306,7 @@ fn concat_ldt_signed_ints() {
296306
.unwrap()
297307
.and_hms_milli_opt(20, 15, 12, 111)
298308
.unwrap()
309+
.and_utc()
299310
.timestamp_nanos_opt()
300311
.unwrap();
301312
assert_eq!(maintype.a, dt_2000y_1m_2d_20h_15m_12s_111ms);
@@ -323,6 +334,7 @@ fn concat_ldt_unsigned_ints() {
323334
.unwrap()
324335
.and_hms_milli_opt(20, 15, 12, 111)
325336
.unwrap()
337+
.and_utc()
326338
.timestamp_nanos_opt()
327339
.unwrap();
328340
assert_eq!(maintype.a, dt_2000y_1m_2d_20h_15m_12s_111ms);

0 commit comments

Comments
 (0)