Comparing time values when using *DATETIME

This Q&A forum allows users to post and respond to "How Do I Do ....." questions. Please do not use to report (suspected) errors - you must use your regional help desk for this. The information contained in this forum has not been validated by LANSA and, as such, LANSA cannot guarantee the accuracy of the information.
Post Reply
adale
Posts: 114
Joined: Wed Apr 08, 2020 9:18 pm
Location: Poplarville, MS

Comparing time values when using *DATETIME

Post by adale » Fri Aug 05, 2022 5:24 am

I am looking for an intrinsic or other to compare the time difference between two time fields that are *DATETIME format.
I don't need to span multiple days, but I do need to account for times crossing over midnight from one day to the next.
I find several Date comparisons (DateDifference) but am missing any Time comparisons, and was hoping someone here on the forum could point me in the right direction.

PS. or if there is an intrinsic or bif to compare a date & time value from a stored value to .Now()

Bottom line is that I am looking for how many minutes from DateTime value in the file to Now.
Arlyn Dale
Servias LLC

User avatar
Dino
Posts: 402
Joined: Fri Jul 19, 2019 7:49 am
Location: Robbinsville, NC
Contact:

Re: Comparing time values when using *DATETIME

Post by Dino » Fri Aug 05, 2022 8:33 am

midnight always tricky.... .AsLocalizedDateTime is your best friend or your worst enemy, depending how international or not your business is, etc.
be very careful eating fish, be careful what date time UTC time are the date/times stored and what you want to use for calculation.

Assuming you are talking midnight in the city where you live, and not London, you can play with this form which basically uses Difference to find the difference in days and adds that when needed to the time converted in seconds used to calculate the difference between two times:

Code: Select all

Function Options(*Direct)
Begin_Com Role(*EXTENDS #PRIM_FORM) ClientWidth(625) ClientHeight(523) ComponentVersion(2) Left(650) Top(206)

Define_Com Class(#PRIM_DTIM) Name(#DateTime1) ComponentVersion(1) DisplayPosition(1) Left(30) Parent(#COM_OWNER) TabPosition(1) Top(50) Width(180)
Define_Com Class(#PRIM_LABL) Name(#Label1) Caption('Label1') DisplayPosition(2) Ellipses(Word) Height(25) Left(30) Parent(#COM_OWNER) TabPosition(2) TabStop(False) Top(100) VerticalAlignment(Center) Width(300)
Define_Com Class(#PRIM_LABL) Name(#Label2) Caption('Label2') DisplayPosition(3) Ellipses(Word) Height(25) Left(30) Parent(#COM_OWNER) TabPosition(3) TabStop(False) Top(150) VerticalAlignment(Center) Width(300)
Define_Com Class(#PRIM_LABL) Name(#Label3) Caption('Label3') DisplayPosition(9) Ellipses(Word) Height(25) Left(30) Parent(#COM_OWNER) TabPosition(9) TabStop(False) Top(200) VerticalAlignment(Center) Width(300)
Define_Com Class(#PRIM_LABL) Name(#Label4) Caption('Label4') DisplayPosition(8) Ellipses(Word) Height(25) Left(30) Parent(#COM_OWNER) TabPosition(8) TabStop(False) Top(250) VerticalAlignment(Center) Width(300)
Define_Com Class(#PRIM_LABL) Name(#Label5) Caption('Label5') DisplayPosition(7) Ellipses(Word) Height(25) Left(30) Parent(#COM_OWNER) TabPosition(7) TabStop(False) Top(300) VerticalAlignment(Center) Width(300)
Define_Com Class(#PRIM_LABL) Name(#Label6) Caption('Label6') DisplayPosition(6) Ellipses(Word) Height(25) Left(30) Parent(#COM_OWNER) TabPosition(6) TabStop(False) Top(350) VerticalAlignment(Center) Width(300)
Define_Com Class(#PRIM_LABL) Name(#Label7) Caption('Label7') DisplayPosition(5) Ellipses(Word) Height(25) Left(30) Parent(#COM_OWNER) TabPosition(5) TabStop(False) Top(400) VerticalAlignment(Center) Width(300)
Define_Com Class(#PRIM_LABL) Name(#Label8) Caption('Label8') DisplayPosition(4) Ellipses(Word) Height(25) Left(30) Parent(#COM_OWNER) TabPosition(4) TabStop(False) Top(450) VerticalAlignment(Center) Width(300)
Define_Com Class(#PRIM_PHBN) Name(#Button1) Caption('Recalculate') DisplayPosition(10) Left(234) Parent(#COM_OWNER) TabPosition(10) Top(54) Width(159)

Define Field(#nowInSeconds) Reffld(#std_num)
Define Field(#enteredInSeconds) Reffld(#std_num)

Evtroutine Handling(#com_owner.CreateInstance)
#DateTime1.Value := #DATETIMEX.Now
Endroutine
Evtroutine Handling(#DateTime1.Changed #Button1.Click) Options(*NOCLEARMESSAGES *NOCLEARERRORS)
#Label1.Caption := 'Entered Date: ' + #DateTime1.Value.AsLocalizedDateTime.AsString
#DATETIMEX := #DATETIMEX.Now.AsLocalizedDateTime
#Label2.Caption := 'Current Date/Time: ' + #DateTimex.AsString
#std_num := #DateTime1.Value.AsLocalizedDateTime.Date.Difference( #Datetimex.Date )
#Label3.Caption := 'Difference in days: ' + #std_num.AsString
* date.difference is only difference in days. 0 if the day is the same, using negative for past, positive for future

#nowInSeconds := #DateTimex.Time.AsSeconds
#enteredInSeconds := #DateTime1.Value.AsLocalizedDateTime.Time.AsSeconds

#Label4.Caption := 'Current Time As Seconds: ' + #nowInSeconds.AsString
#Label5.Caption := 'Entered Time As Seconds: ' + #enteredInSeconds.AsString
#Label6.Caption := 'Difference in Seconds not counting days: ' + (#enteredInSeconds - #nowInSeconds).AsString
#Label7.Caption := 'Difference in Minutes not counting days: ' + ((#enteredInSeconds - #nowInSeconds) / 60).Round.AsString
#Label8.Caption := 'Difference in Minutes including days: ' + (((#enteredInSeconds - #nowInSeconds) / 60).Round + (#std_num * 24 * 60)).AsString
Endroutine
End_Com
datedifference.png
datedifference.png (6.83 KiB) Viewed 8048 times
Last edited by Dino on Fri Aug 05, 2022 8:36 am, edited 1 time in total.

BrendanB
Posts: 111
Joined: Tue Nov 24, 2015 10:29 am

Re: Comparing time values when using *DATETIME

Post by BrendanB » Fri Aug 05, 2022 8:33 am

Arlyn,

you could also try the .AsDuration Intrinsic:

#myDateTime.AsDuration(#otherDateTime)

https://docs.lansa.com/15/en/lansa016/C ... ration.htm

This gives the result in the format found at:

https://en.wikipedia.org/wiki/ISO_8601#Durations

For example, "P3Y6M4DT12H30M5S" represents a duration of "three years, six months, four days, twelve hours, thirty minutes, and five seconds".

so you can use this to determine difference between 2 datetime objects.

BrendanB
Posts: 111
Joined: Tue Nov 24, 2015 10:29 am

Re: Comparing time values when using *DATETIME

Post by BrendanB » Fri Aug 05, 2022 8:59 am

Another option that you can use is using 'Unix-epoch-time' to compare the 2 as numbers.

Unix Epoch time is 'the number of SECONDS since 1 Jan 1970 ' so you should easily be able to perform comparisons using that.

Code: Select all

if (#com_self.GetDateTimeInEpoch(#myDatetime1) < #com_self.GetDateTimeInEpoch(#myDatetime2) )

* #myDateTime1 is earliest

else

* #myDateTime2 is earliest

endif

Mthroutine Name(GetDateTimeInEpoch)
Define_Map For(*INPUT) Class(#DATETIMEX) Name(#From) Mandatory(*SQLNULL)

Define_Map For(*RESULT) Class(#std_int) Name(#Result)

* Check if have a date
If (#From.IsSqlNull)

* We don't, so set now as default
#From := #From.Now

Endif

* Time to send Result
#Result := (#From.Date.Difference( 1970-01-01 ) * 86400) + #From.Time.AsSeconds

Endroutine

adale
Posts: 114
Joined: Wed Apr 08, 2020 9:18 pm
Location: Poplarville, MS

Re: Comparing time values when using *DATETIME

Post by adale » Sat Aug 06, 2022 2:03 am

Brendan,
Is there an example or sample of the .AsDuration intrinsic?
Arlyn Dale
Servias LLC

User avatar
Dino
Posts: 402
Joined: Fri Jul 19, 2019 7:49 am
Location: Robbinsville, NC
Contact:

Re: Comparing time values when using *DATETIME

Post by Dino » Thu Aug 11, 2022 7:17 am

Code: Select all

Begin_Com Role(*EXTENDS #PRIM_SRVM)
Srvroutine Name(duration)
Field_Map For(*Input) Field(#DATETIME1)
Field_Map For(*Input) Field(#DATETIME2)
Field_Map For(*Output) Field(#STD_STRNG)

#STD_STRNG := #DateTime2.AsDuration( #DateTime1 ).AsString
Endroutine
End_Com
asduration.png
asduration.png (12.99 KiB) Viewed 7874 times

Post Reply