Skip to content

Instantly share code, notes, and snippets.

@MarkGoldberg
Last active October 28, 2016 21:42
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MarkGoldberg/bcbc0893b41f4e6e97d44e83e8b4df9d to your computer and use it in GitHub Desktop.
Save MarkGoldberg/bcbc0893b41f4e6e97d44e83e8b4df9d to your computer and use it in GitHub Desktop.
Compare Time Spans
!NOTE should be worked into https://github.com/MarkGoldberg/CwUnit/blob/master/Libsrc/ctDateTimeLong.inc
gtDateTime GROUP,TYPE
_Date DATE
_Time TIME
END
gtTimeSpan GROUP,TYPE
_Start LIKE(gtDatTime)
_End LIKE(gtDatTime)
END
!=============================================================================
HasOverlap PROCEDURE(CONST *gtTimeSpan TS1, CONST *gtTimeSpan TS2)!,BOOL
RetHasOverlap BOOL(FALSE)
CODE
RetHasOverlap = CHOOSE( SELF.IsBetween( TS1._Start, TS2._Start, TS1._End ) | !is TS2_Start between TS1 start/end
OR SELF.IsBetween( TS1._Start, TS2._End , TS1._End)) !is TS2_End between TS1 start/end
RETURN RetHasOverLap
!=============================================================================
IsBetween PROCEDURE(CONST *gtDateTime xCompareLow, CONST *gtDatTime xCheck, CONST *gtDateTime xCompareHi ) !BOOL
CompareLowCheck LONG,AUTO
CompareCheckHi LONG,AUTO
RetIsBetween BOOL,AUTO
CODE
CompareLowCheck = SELF.CompareDateTime( xCompareLow, xCheck)
CompareCheckHi = SELF.CompareDateTime( xCheck , xCompareHi)
! IF Low <= Check AND Check <= Hi THEN IsBetween is true
! IF Low > Check OR Check > Hi THEN IsBetween is FALSE
RetIsBetween = CHOOSE( NOT ( CompareLowCheck = 1 OR CompareCheckHi = 1 ) )
RETURN RetIsBetween
!=============================================================================
ctMG.CompareDateTime PROCEDURE(LONG xDate1, LONG xTime1, LONG xDate2, LONG xTime2)!,LONG !returns -1 if xac1 < xaca2, 0 if xac1 = xac2, 1 if xac1 > xac2
!Feb/3/09 changed data type to LONG, to allow for TIMEs that are out of range of 0..TIME:Day
RetVal LONG !no auto
CODE
SELF.DateTime_CorrectOverflow( xDate1, xTime1 )
SELF.DateTime_CorrectOverflow( xDate2, xTime2 )
IF xDate1 < xDate2 THEN RetVal = -1 !consider -2
ELSIF xDate1 > xDate2 THEN RetVal = 1 !consider +2
ELSIF xDate1 = xDate2
IF xTime1 < xTime2 THEN RetVal = -1
ELSIF xTime1 > xTime2 THEN RetVal = 1
END
END
RETURN RetVal
!=============================================================================
ctMG.CompareDateTime PROCEDURE(CONST *gtDateTime xac1, CONST *gtDateTime xac2) !,LONG !returns -1 if xac1 < xaca2, 0 if xac1 = xac2, 1 if xac1 > xac2
CODE
RETURN SELF.CompareDateTime( xac1._DATE, xac1._TIME, xac2._DATE, xac2._TIME)
!=======================================================================================================================
ctMG.DateTime_CorrectOverflow PROCEDURE( *DATE xaAnswerDate, *TIME xaAnswerTime, LONG xInDate, LONG xInTime )
!see \\puget1\c\cla\_mg\test\TimeOverflow -- shows TIME fields cannot be out of range of 0..TIME:DAY
CODE
xaAnswerDate = xInDate + INT( xInTime / TIME:Day)
xaAnswerTime = xInTime % TIME:Day !the % TIME:Day is likely redundant give the TIME data type
!=======================================================================================================================
ctMG.DateTime_CorrectOverflow PROCEDURE( *LONG xaAnswerDate, *LONG xaAnswerTime, LONG xInDate, LONG xInTime )
!see \\puget1\c\cla\_mg\test\TimeOverflow -- shows TIME fields cannot be out of range of 0..TIME:DAY
CODE
xaAnswerDate = xInDate + INT( xInTime / TIME:Day)
xaAnswerTime = xInTime % TIME:Day !the % TIME:Day is likely redundant give the TIME data type
!=======================================================================================================================
ctMG.DateTime_CorrectOverflow PROCEDURE( *LONG xaAnswerDate, *LONG xaAnswerTime)
CODE
SELF.DateTime_CorrectOverflow ( xaAnswerDate, xaAnswerTime, xaAnswerDate, xaAnswerTime)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment