Skip to content

Instantly share code, notes, and snippets.

@MarkGoldberg
Last active April 22, 2017 13:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MarkGoldberg/5abbc8a0afec532f87b7b6ecd34db155 to your computer and use it in GitHub Desktop.
Save MarkGoldberg/5abbc8a0afec532f87b7b6ecd34db155 to your computer and use it in GitHub Desktop.
Function to return number of weekend days in a date range, written in Clarion for Windows
PROGRAM
Day ITEMIZE,PRE(Day)
Sunday EQUATE(0)
Monday EQUATE
Tuesday EQUATE
Wednesday EQUATE
Thursday EQUATE
Friday EQUATE
Saturday EQUATE
END
INCLUDE('debuger.inc'),ONCE
DBG debuger
MAP
WeekendDaysInDateRange (LONG StartDate, LONG EndDate),LONG ! Returns the count of Saturdays and Sundays in the date range (inclusive)
END
StartD LONG,AUTO
AddDays LONG,AUTO
AddStartDays LONG,AUTO
CODE
DBG.mg_init('WeekendDays')
StartD = DATE(4, 21, 2017)
LOOP AddStartDays = 0 TO 6
! ----------v a debugging statement, will be sent to OutputDebugString
ASSERT(0,eqDBG & 'v-{42} AddStartDays['& AddStartDays &']')
LOOP AddDays = 0 TO 20
Bogus# = WeekendDaysInDateRange( StartD + AddStartDays, StartD + AddStartDays + AddDays)
END
END
WeekendDaysInDateRange PROCEDURE(LONG StartDate, LONG EndDate) !,LONG
! Returns the count of Saturdays and Sundays in the date range (inclusive)
DOW:Start LONG,AUTO
DOW:End LONG,AUTO
FirstMonday LONG,AUTO
LastSunday LONG,AUTO
FullWeeks LONG,AUTO
InPartialWeek LONG,AUTO ! Weekend Days in Partial Week (before full weeks, and after full weeks)
Answer LONG,AUTO
CODE
!assert(0,eqDBG& 'StartD['& FORMAT( StartDate, @D1) &']|EndD['& FORMAT(EndDate, @D1) &']')
IF EndDate < StartDate
Answer = 0
ELSE
DOW:Start = StartDate % 7 ! DOW => Day Of Week
DOW:End = EndDate % 7
CASE DOW:Start
OF DAY:Sunday ; FirstMonday = StartDate + 1
OF DAY:Monday ; FirstMonday = StartDate + 0
OF DAY:Tuesday ; FirstMonday = StartDate + 6
OF DAY:Wednesday ; FirstMonday = StartDate + 5
OF DAY:Thursday ; FirstMonday = StartDate + 4
OF DAY:Friday ; FirstMonday = StartDate + 3
OF DAY:Saturday ; FirstMonday = StartDate + 2
END
LastSunday = EndDate - DOW:End
IF LastSunday < FirstMonday
FullWeeks = 0
ELSE
FullWeeks = INT ( (1 + LastSunday - FirstMonday) / 7 )
END
IF EndDate >= FirstMonday
InPartialWeek = CHOOSE( DOW:Start = Day:Sunday, 1, 2)
InPartialWeek += CHOOSE( DOW:End = Day:Saturday ) ! With implied, 1, 0
ELSE
CASE DOW:End
OF Day:Saturday; InPartialWeek = 1
OF Day:Sunday ; InPartialWeek = CHOOSE( DOW:Start = Day:Sunday, 1, 2)
ELSE ; InPartialWeek = 0
END
END
Answer = (FullWeeks * 2) + InPartialWeek
ASSERT(0,eqDBG&'StartD['& FORMAT( StartDate, @D1) &'] EndD['& FORMAT(EndDate, @D1) &'] Answer['& Answer &'] FullWeeks['& FullWeeks &'] InPartialWeek['& InPartialWeek &']')
END
RETURN Answer
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment