Search code examples
xqueryosb

Flight duration from diffrent time zones


I am facing the issue with time zones. Code:

{

let $hour := hours-from-duration(fn:subtract-dateTimes-yielding-dayTimeDuration(xs:dateTime(data($FlightSegment/@ArrivalDateTime)),xs:dateTime(data($FlightSegment/@DepartureDateTime)))) let $min := minutes-from-duration(fn:subtract-dateTimes-yielding-dayTimeDuration(xs:dateTime(data($FlightSegment/@ArrivalDateTime)),xs:dateTime(data($FlightSegment/@DepartureDateTime))))

return

    if($hour < 10  and $min < 10) then
        <flt-seg:FlightDuration> {concat('0',$hour,':','0',$min)}</flt-seg:FlightDuration>
        else if ($min < 10 ) then 
         <flt-seg:FlightDuration> {concat($hour,':','0',$min)}</flt-seg:FlightDuration>
         else if ($hour < 10 ) then
          <flt-seg:FlightDuration> {concat('0',$hour,':',$min)}</flt-seg:FlightDuration>
         else
          <flt-seg:FlightDuration> {concat($hour,':',$min)}</flt-seg:FlightDuration>


    }

Departure airport code is :BNE(Brisbane)

Arrival airport code is :LAX(Lass angels)

Response i am getting:

DepartureDateTime:2015-08-15T09:50:00

ArrivalDateTime:2015-08-15T06:30:00

FlightDuration: 0-3:0-20

flight duration (0-3:0-20) iam getting negative value, so request you to pls help me on this how can i get the positive value.

could you pls let me know how i will get proper flight duration with different time zones. Thanks in Advance.


Solution

  • Looks like you have two problems.

    First problem is formatting the duration, which you can do using functx:pad-integer-to-length() or using the bea date functions (or number format functions) if you swing that way. That's the easy bit.

    Your second, harder problem is your input data is airport-local, which cannot be compared the way you're doing it. You need to calculate the actual UTC date time from the local date time + port IATA code, probably via an interim lookup, so

    1. airport -> timezone ID (cacheable)
    2. port-local dateTime + tzID -> UTC dateTime.

    It doesn't have to be UTC; as long as you're comparing dateTimes in the same time zone. However, UTC is a convenient timezone to choose, so it's probably best to use it.

    Oh yeah. Make sure your OSB JVM has the latest tzdata or you'll find that your date calculation will be inaccurate for many destinations - Fiji loves to change daylight savings times at short notice.