1
1
/* eslint-disable import/prefer-default-export */
2
2
import { dateToHHMM } from 'utils/date' ;
3
- import { ISO8601Duration2sec } from 'utils/timeManipulation' ;
3
+ import { ISO8601Duration2sec , calculateTimeDifferenceInDays } from 'utils/timeManipulation' ;
4
4
5
5
import type { ScheduleEntry , TimeExtraDays } from '../types' ;
6
6
@@ -15,8 +15,7 @@ const computeDayTimeFromStartTime = (
15
15
16
16
const timeExtraDay = {
17
17
time : dateToHHMM ( arrivalDatetime , { withSeconds : true } ) ,
18
- daySinceDeparture :
19
- Math . abs ( arrivalDatetime . getTime ( ) - startDatetime . getTime ( ) ) / ( 1000 * 60 * 60 * 24 ) ,
18
+ daySinceDeparture : calculateTimeDifferenceInDays ( startDatetime , arrivalDatetime ) ,
20
19
dayDisplayed : isAfterMidnight ,
21
20
} ;
22
21
@@ -25,34 +24,40 @@ const computeDayTimeFromStartTime = (
25
24
26
25
export const computeInputDatetimes = (
27
26
startDatetime : Date ,
28
- previousDatetime : Date ,
29
- schedule : ScheduleEntry ,
27
+ lastReferenceDate : Date ,
28
+ schedule : ScheduleEntry | undefined ,
30
29
{ isDeparture } : { isDeparture : boolean }
31
30
) => {
31
+ let theoriticalArrival : Date | undefined ;
32
32
let arrival : TimeExtraDays | undefined ;
33
33
let departure : TimeExtraDays | undefined ;
34
- let prev = previousDatetime ;
34
+ let refDate = lastReferenceDate ;
35
+
36
+ let arrivalInSeconds ;
37
+ // if is departure, use the startDatetime
38
+ if ( isDeparture ) {
39
+ arrivalInSeconds = 0 ;
40
+ } else if ( schedule ?. arrival ) {
41
+ arrivalInSeconds = ISO8601Duration2sec ( schedule . arrival ) ; // duration from startTime
42
+ }
35
43
36
- if ( schedule && schedule . arrival ) {
37
- const arrivalInSeconds = ISO8601Duration2sec ( schedule . arrival ) ; // duration from startTime
38
- const result = computeDayTimeFromStartTime ( startDatetime , arrivalInSeconds , prev ) ;
44
+ if ( arrivalInSeconds !== undefined ) {
45
+ theoriticalArrival = new Date ( startDatetime . getTime ( ) + arrivalInSeconds * 1000 ) ;
46
+ const result = computeDayTimeFromStartTime ( startDatetime , arrivalInSeconds , refDate ) ;
39
47
arrival = result . timeExtraDay ;
40
- prev = result . previousTime ;
41
-
42
- if ( schedule . stop_for ) {
43
- const departureInSeconds =
44
- ISO8601Duration2sec ( schedule . arrival ) + ISO8601Duration2sec ( schedule . stop_for ) ;
45
- const resultDeparture = computeDayTimeFromStartTime ( startDatetime , departureInSeconds , prev ) ;
48
+ refDate = result . previousTime ;
49
+
50
+ if ( schedule ?. stop_for ) {
51
+ const departureInSeconds = arrivalInSeconds + ISO8601Duration2sec ( schedule . stop_for ) ;
52
+ const resultDeparture = computeDayTimeFromStartTime (
53
+ startDatetime ,
54
+ departureInSeconds ,
55
+ refDate
56
+ ) ;
46
57
departure = resultDeparture . timeExtraDay ;
47
- prev = resultDeparture . previousTime ;
58
+ refDate = resultDeparture . previousTime ;
48
59
}
49
60
}
50
61
51
- // if is departure and no arrival is defined, use the startDatetime
52
- if ( isDeparture && ! arrival ) {
53
- const result = computeDayTimeFromStartTime ( startDatetime , 0 , prev ) ;
54
- arrival = result . timeExtraDay ;
55
- }
56
-
57
- return { arrival, departure, prev } ;
62
+ return { theoriticalArrival, arrival, departure, refDate } ;
58
63
} ;
0 commit comments