Skip to content

Commit a4621b4

Browse files
committed
editoast: change stdcm environment date type
Signed-off-by: SarahBellaha <[email protected]>
1 parent 5af6621 commit a4621b4

File tree

3 files changed

+147
-75
lines changed

3 files changed

+147
-75
lines changed

editoast/src/client/stdcm_search_env_commands.rs

+55-53
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@ use crate::CliError;
88
use crate::Exists;
99
use crate::Model;
1010
use crate::Retrieve;
11+
use chrono::DateTime;
1112
use chrono::Duration;
12-
use chrono::NaiveDateTime;
13+
use chrono::Utc;
1314
use clap::Args;
1415
use clap::Subcommand;
1516
use editoast_models::DbConnection;
@@ -62,10 +63,10 @@ pub struct SetSTDCMSearchEnvFromScenarioArgs {
6263
pub work_schedule_group_id: Option<i64>,
6364
/// If omitted, set to the earliest train start time in the timetable
6465
#[arg(long)]
65-
pub search_window_begin: Option<NaiveDateTime>,
66+
pub search_window_begin: Option<DateTime<Utc>>,
6667
/// If omitted, set to the latest train start time in the timetable plus one day
6768
#[arg(long)]
68-
pub search_window_end: Option<NaiveDateTime>,
69+
pub search_window_end: Option<DateTime<Utc>>,
6970
}
7071

7172
async fn set_stdcm_search_env_from_scenario(
@@ -126,10 +127,10 @@ pub struct SetSTDCMSearchEnvFromScratchArgs {
126127
#[arg(long)]
127128
/// If omitted, set to the earliest train start time in the timetable
128129
#[arg(long)]
129-
pub search_window_begin: Option<NaiveDateTime>,
130+
pub search_window_begin: Option<DateTime<Utc>>,
130131
/// If omitted, set to the latest train start time in the timetable plus one day
131132
#[arg(long)]
132-
pub search_window_end: Option<NaiveDateTime>,
133+
pub search_window_end: Option<DateTime<Utc>>,
133134
}
134135

135136
async fn set_stdcm_search_env_from_scratch(
@@ -178,10 +179,10 @@ async fn set_stdcm_search_env_from_scratch(
178179

179180
async fn resolve_search_window(
180181
timetable_id: i64,
181-
search_window_begin: Option<NaiveDateTime>,
182-
search_window_end: Option<NaiveDateTime>,
182+
search_window_begin: Option<DateTime<Utc>>,
183+
search_window_end: Option<DateTime<Utc>>,
183184
conn: &mut DbConnection,
184-
) -> Result<(NaiveDateTime, NaiveDateTime), Box<dyn Error + Send + Sync>> {
185+
) -> Result<(DateTime<Utc>, DateTime<Utc>), Box<dyn Error + Send + Sync>> {
185186
let (begin, end) = if let (Some(begin), Some(end)) = (search_window_begin, search_window_end) {
186187
(begin, end)
187188
} else {
@@ -193,8 +194,8 @@ async fn resolve_search_window(
193194
return Err(Box::new(CliError::new(1, error_msg)));
194195
};
195196

196-
let begin = search_window_begin.unwrap_or(min.naive_utc());
197-
let end = search_window_end.unwrap_or(max.naive_utc() + Duration::days(1));
197+
let begin = search_window_begin.unwrap_or(*min);
198+
let end = search_window_end.unwrap_or(*max + Duration::days(1));
198199
(begin, end)
199200
};
200201

@@ -235,22 +236,23 @@ mod tests {
235236
};
236237

237238
use super::*;
238-
use chrono::NaiveDateTime;
239+
use chrono::DateTime;
240+
use chrono::Utc;
239241
use editoast_models::{DbConnection, DbConnectionPoolV2};
240242
use rstest::rstest;
241243

242-
fn make_naive_datetime(s: &str) -> NaiveDateTime {
243-
NaiveDateTime::parse_from_str(s, "%Y-%m-%d %H:%M:%S").unwrap()
244+
fn make_datetime(s: &str) -> DateTime<Utc> {
245+
DateTime::parse_from_rfc3339(s).unwrap().to_utc()
244246
}
245247

246248
async fn create_train_schedules_from_start_times(
247-
start_times: Vec<NaiveDateTime>,
249+
start_times: Vec<DateTime<Utc>>,
248250
timetable_id: i64,
249251
conn: &mut DbConnection,
250252
) {
251253
for start_time in start_times {
252254
simple_train_schedule_changeset(timetable_id)
253-
.start_time(start_time.and_utc())
255+
.start_time(start_time)
254256
.create(conn)
255257
.await
256258
.expect("Should be able to create train schedules");
@@ -261,45 +263,45 @@ mod tests {
261263
#[case::both_none(
262264
None,
263265
None,
264-
make_naive_datetime("2000-01-01 11:59:59"),
265-
make_naive_datetime("2000-02-03 00:00:01")
266+
make_datetime("2000-01-01 11:59:59Z"),
267+
make_datetime("2000-02-03 00:00:01Z")
266268
)]
267269
#[case::begin_none(
268270
None,
269-
Some(make_naive_datetime("2000-02-01 00:00:00")),
270-
make_naive_datetime("2000-01-01 11:59:59"),
271-
make_naive_datetime("2000-02-01 00:00:00")
271+
Some(make_datetime("2000-02-01 00:00:00Z")),
272+
make_datetime("2000-01-01 11:59:59Z"),
273+
make_datetime("2000-02-01 00:00:00Z")
272274
)]
273275
#[case::end_none(
274-
Some(make_naive_datetime("2000-02-01 08:00:00")),
276+
Some(make_datetime("2000-02-01 08:00:00Z")),
275277
None,
276-
make_naive_datetime("2000-02-01 08:00:00"),
277-
make_naive_datetime("2000-02-03 00:00:01")
278+
make_datetime("2000-02-01 08:00:00Z"),
279+
make_datetime("2000-02-03 00:00:01Z")
278280
)]
279281
#[case::both_some(
280-
Some(make_naive_datetime("2000-02-01 08:00:00")),
281-
Some(make_naive_datetime("2000-05-22 09:00:50")),
282-
make_naive_datetime("2000-02-01 08:00:00"),
283-
make_naive_datetime("2000-05-22 09:00:50")
282+
Some(make_datetime("2000-02-01 08:00:00Z")),
283+
Some(make_datetime("2000-05-22 09:00:50Z")),
284+
make_datetime("2000-02-01 08:00:00Z"),
285+
make_datetime("2000-05-22 09:00:50Z")
284286
)]
285287
async fn test_resolve_search_window(
286-
#[case] search_window_begin: Option<NaiveDateTime>,
287-
#[case] search_window_end: Option<NaiveDateTime>,
288-
#[case] expected_begin: NaiveDateTime,
289-
#[case] expected_end: NaiveDateTime,
288+
#[case] search_window_begin: Option<DateTime<Utc>>,
289+
#[case] search_window_end: Option<DateTime<Utc>>,
290+
#[case] expected_begin: DateTime<Utc>,
291+
#[case] expected_end: DateTime<Utc>,
290292
) {
291293
let db_pool = DbConnectionPoolV2::for_tests();
292294
let conn = &mut db_pool.get_ok();
293295

294296
let timetable = create_timetable(conn).await;
295297

296298
let start_times = vec![
297-
make_naive_datetime("2000-01-01 12:00:00"),
298-
make_naive_datetime("2000-02-02 00:00:00"),
299-
make_naive_datetime("2000-01-01 11:59:59"), // earliest
300-
make_naive_datetime("2000-01-15 08:59:59"),
301-
make_naive_datetime("2000-02-02 00:00:01"), // latest
302-
make_naive_datetime("2000-01-19 17:00:00"),
299+
make_datetime("2000-01-01 12:00:00Z"),
300+
make_datetime("2000-02-02 00:00:00Z"),
301+
make_datetime("2000-01-01 11:59:59Z"), // earliest
302+
make_datetime("2000-01-15 08:59:59Z"),
303+
make_datetime("2000-02-02 00:00:01Z"), // latest
304+
make_datetime("2000-01-19 17:00:00Z"),
303305
];
304306

305307
create_train_schedules_from_start_times(start_times, timetable.id, conn).await;
@@ -327,23 +329,23 @@ mod tests {
327329

328330
#[rstest]
329331
#[case::both_some(
330-
Some(make_naive_datetime("2000-02-01 08:00:00")),
331-
Some(make_naive_datetime("2000-02-01 00:00:00"))
332+
Some(make_datetime("2000-02-01 08:00:00Z")),
333+
Some(make_datetime("2000-02-01 00:00:00Z"))
332334
)]
333-
#[case::end_none(Some(make_naive_datetime("2000-03-01 00:00:00")), None)]
334-
#[case::begin_none(None, Some(make_naive_datetime("2000-01-01 08:00:00")))]
335+
#[case::end_none(Some(make_datetime("2000-03-01 00:00:00Z")), None)]
336+
#[case::begin_none(None, Some(make_datetime("2000-01-01 08:00:00Z")))]
335337
async fn test_resolve_search_window_incompatible_dates(
336-
#[case] search_window_begin: Option<NaiveDateTime>,
337-
#[case] search_window_end: Option<NaiveDateTime>,
338+
#[case] search_window_begin: Option<DateTime<Utc>>,
339+
#[case] search_window_end: Option<DateTime<Utc>>,
338340
) {
339341
let db_pool = DbConnectionPoolV2::for_tests();
340342
let conn = &mut db_pool.get_ok();
341343

342344
let timetable = create_timetable(conn).await;
343345

344346
let start_times = vec![
345-
make_naive_datetime("2000-01-01 12:00:00"),
346-
make_naive_datetime("2000-02-02 00:00:01"),
347+
make_datetime("2000-01-01 12:00:00Z"),
348+
make_datetime("2000-02-02 00:00:01Z"),
347349
];
348350

349351
create_train_schedules_from_start_times(start_times, timetable.id, conn).await;
@@ -366,8 +368,8 @@ mod tests {
366368
let work_schedule_group = create_work_schedule_group(conn).await;
367369

368370
let start_times = vec![
369-
make_naive_datetime("2000-01-01 12:00:00"),
370-
make_naive_datetime("2000-02-02 08:00:00"),
371+
make_datetime("2000-01-01 12:00:00Z"),
372+
make_datetime("2000-02-02 08:00:00Z"),
371373
];
372374

373375
create_train_schedules_from_start_times(
@@ -394,11 +396,11 @@ mod tests {
394396

395397
assert_eq!(
396398
search_env.search_window_begin,
397-
make_naive_datetime("2000-01-01 12:00:00")
399+
make_datetime("2000-01-01 12:00:00Z")
398400
);
399401
assert_eq!(
400402
search_env.search_window_end,
401-
make_naive_datetime("2000-02-03 08:00:00")
403+
make_datetime("2000-02-03 08:00:00Z")
402404
);
403405
}
404406

@@ -413,8 +415,8 @@ mod tests {
413415
let electrical_profile_set = create_electrical_profile_set(conn).await;
414416

415417
let start_times = vec![
416-
make_naive_datetime("2000-01-01 12:00:00"),
417-
make_naive_datetime("2000-02-02 08:00:00"),
418+
make_datetime("2000-01-01 12:00:00Z"),
419+
make_datetime("2000-02-02 08:00:00Z"),
418420
];
419421

420422
create_train_schedules_from_start_times(start_times, timetable.id, conn).await;
@@ -438,11 +440,11 @@ mod tests {
438440

439441
assert_eq!(
440442
search_env.search_window_begin,
441-
make_naive_datetime("2000-01-01 12:00:00")
443+
make_datetime("2000-01-01 12:00:00Z")
442444
);
443445
assert_eq!(
444446
search_env.search_window_end,
445-
make_naive_datetime("2000-02-03 08:00:00")
447+
make_datetime("2000-02-03 08:00:00Z")
446448
);
447449
}
448450
}

editoast/src/models/stdcm_search_environment.rs

+66-13
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
use chrono::NaiveDateTime;
1+
use chrono::DateTime;
2+
use chrono::Utc;
23
use diesel::ExpressionMethods;
34
use diesel::QueryDsl;
45
use diesel_async::RunQueryDsl;
@@ -28,8 +29,8 @@ pub struct StdcmSearchEnvironment {
2829
#[serde(skip_serializing_if = "Option::is_none")]
2930
pub work_schedule_group_id: Option<i64>,
3031
pub timetable_id: i64,
31-
pub search_window_begin: NaiveDateTime,
32-
pub search_window_end: NaiveDateTime,
32+
pub search_window_begin: DateTime<Utc>,
33+
pub search_window_end: DateTime<Utc>,
3334
#[schema(nullable = false)]
3435
#[serde(skip_serializing_if = "Option::is_none")]
3536
pub temporary_speed_limit_group_id: Option<i64>,
@@ -127,11 +128,31 @@ pub mod tests {
127128
.work_schedule_group_id(Some(work_schedule_group.id))
128129
.temporary_speed_limit_group_id(Some(temporary_speed_limit_group.id))
129130
.timetable_id(timetable.id)
130-
.search_window_begin(NaiveDate::from_ymd_opt(2024, 1, 1).unwrap().into())
131-
.search_window_end(NaiveDate::from_ymd_opt(2024, 1, 15).unwrap().into());
131+
.search_window_begin(
132+
NaiveDate::from_ymd_opt(2024, 1, 1)
133+
.unwrap()
134+
.and_hms_opt(0, 0, 0)
135+
.unwrap()
136+
.and_utc(),
137+
)
138+
.search_window_end(
139+
NaiveDate::from_ymd_opt(2024, 1, 15)
140+
.unwrap()
141+
.and_hms_opt(0, 0, 0)
142+
.unwrap()
143+
.and_utc(),
144+
);
132145

133-
let begin = NaiveDate::from_ymd_opt(2024, 1, 16).unwrap().into();
134-
let end = NaiveDate::from_ymd_opt(2024, 1, 31).unwrap().into();
146+
let begin = NaiveDate::from_ymd_opt(2024, 1, 16)
147+
.unwrap()
148+
.and_hms_opt(0, 0, 0)
149+
.unwrap()
150+
.and_utc();
151+
let end = NaiveDate::from_ymd_opt(2024, 1, 31)
152+
.unwrap()
153+
.and_hms_opt(0, 0, 0)
154+
.unwrap()
155+
.and_utc();
135156

136157
let changeset_2 = changeset_1
137158
.clone()
@@ -190,16 +211,48 @@ pub mod tests {
190211
.work_schedule_group_id(Some(work_schedule_group.id))
191212
.temporary_speed_limit_group_id(Some(temporary_speed_limit_group.id))
192213
.timetable_id(timetable.id)
193-
.search_window_begin(NaiveDate::from_ymd_opt(2024, 1, 1).unwrap().into())
194-
.search_window_end(NaiveDate::from_ymd_opt(2024, 1, 15).unwrap().into());
214+
.search_window_begin(
215+
NaiveDate::from_ymd_opt(2024, 1, 1)
216+
.unwrap()
217+
.and_hms_opt(0, 0, 0)
218+
.unwrap()
219+
.and_utc(),
220+
)
221+
.search_window_end(
222+
NaiveDate::from_ymd_opt(2024, 1, 15)
223+
.unwrap()
224+
.and_hms_opt(0, 0, 0)
225+
.unwrap()
226+
.and_utc(),
227+
);
195228

196229
let too_young = too_old
197230
.clone()
198-
.search_window_begin(NaiveDate::from_ymd_opt(2024, 1, 16).unwrap().into())
199-
.search_window_end(NaiveDate::from_ymd_opt(2024, 1, 31).unwrap().into());
231+
.search_window_begin(
232+
NaiveDate::from_ymd_opt(2024, 1, 16)
233+
.unwrap()
234+
.and_hms_opt(0, 0, 0)
235+
.unwrap()
236+
.and_utc(),
237+
)
238+
.search_window_end(
239+
NaiveDate::from_ymd_opt(2024, 1, 31)
240+
.unwrap()
241+
.and_hms_opt(0, 0, 0)
242+
.unwrap()
243+
.and_utc(),
244+
);
200245

201-
let begin = NaiveDate::from_ymd_opt(2024, 1, 7).unwrap().into();
202-
let end = NaiveDate::from_ymd_opt(2024, 1, 31).unwrap().into();
246+
let begin = NaiveDate::from_ymd_opt(2024, 1, 7)
247+
.unwrap()
248+
.and_hms_opt(0, 0, 0)
249+
.unwrap()
250+
.and_utc();
251+
let end = NaiveDate::from_ymd_opt(2024, 1, 31)
252+
.unwrap()
253+
.and_hms_opt(0, 0, 0)
254+
.unwrap()
255+
.and_utc();
203256

204257
let the_best = too_old
205258
.clone()

0 commit comments

Comments
 (0)