'+---------------------------------------------------------------------------+ '| | '| Date routines for PowerBASIC | '| Copyright (c) 1995 by PowerBASIC, Inc. All Rights Reserved. | '| | '| Note: All routines expect dates and times to be in U.S. format. | '| For international support, all you need to do is modify the | '| SplitDate(), SplitTime(), MakeDate$(), MakeTime[12]$() | '| routines to support your specified format. All other routines | '| call these routines to get the component parts. | '| | '| All dates used must be between 01-01-1900 and 06-05-2079 | '| | '| If you pass a null (zero-length) string to a given routine, | '| the current date/time is used. | '| | '+---------------------------------------------------------------------------+ '+---------------------------------------------------------------------------+ '| | '| CDOW - Return the name for the Day of the Week of a date | '| DOW - Return the day of the week for a given date | '| DOY - Return the day of the year for a given date | '| DateMath - Add/Subtract days to a given date | '| DateToDays - Convert a date into the number of days since 1900 | '| Days - Return the number of days between two dates | '| DaysToDate - Convert the number of days since 1900 into a date | '| Elapsed - Return the number of seconds between two time strings | '| Leap - Check a year to see if it's a leap year | '| MakeDate - Create a date string from month, day, and year | '| MakeTime - Create a time string from hour, minutes, and seconds | '| MakeTime12 - Create a 12 hour time string from hour and minutes | '| MonthName - Return the name of a given month | '| SecondsToTime - Convert number of seconds since midnight into a time | '| SplitDate - Split a date into its component parts | '| SplitTime - Split a time into its component parts | '| TimeToSeconds - Convert a time string into seconds since midnight | '| ValidDate - Check a date to see if it's valid | '| | '+---------------------------------------------------------------------------+ $CPU 8086 ' Make compatible with XT systems $LIB ALL OFF ' Turn off all PowerBASIC libraries $ERROR ALL OFF ' Turn off all PowerBASIC error checking $OPTIMIZE SIZE ' Optimize for smaller code $COMPILE UNIT ' Compile to a UNIT (.PBU) DEFINT A-Z 'Required for all numeric functions, forces PB to not 'include floating point in UNIT (makes it smaller) '=========================================================================== ' SplitDate - Split a date passed in a string to its Month, Day, and Year ' components. ' ' Note: All other date routines call this routine to get the ' component parts. ' ' TempDate = Date string in U.S. format (MM-DD-YY, MM-DD-YYYY) ' Month = Returns the month ' Day = Returns the day of the month ' Year = Returns the year ' SUB SplitDate(BYVAL InDate AS STRING, Month AS INTEGER, _ Day AS INTEGER, Year AS INTEGER) PUBLIC IF LEN(InDate) = 0 THEN InDate = DATE$ END IF Month = VAL(MID$(InDate$,1,2)) Day = VAL(MID$(InDate$,4,2)) Year = VAL(MID$(InDate$,7)) IF Year < 80 THEN 'assume we've rolled into the next century INCR Year, 2000 ELSEIF Year < 100 THEN 'between 80 and 99, use this century INCR Year, 1900 END IF END SUB '=========================================================================== ' SplitTime - Split a time passed in a string to its Hour, Minute, and ' Second components. ' ' Note: All other time routines call this routine to get the ' component parts. ' ' TempTime = Time string in U.S. format (HH:MM:SS, HH:MM, HH:MMx) ' Hour = Returns the hour (in military time) ' Minute = Returns the minute ' Second = Returns the second ' SUB SplitTime(BYVAL InTime AS STRING, Hour AS INTEGER, _ Minute AS INTEGER, Second AS INTEGER) PUBLIC IF LEN(InTime) = 0 THEN InTime$ = TIME$ END IF Hour = VAL(InTime) Minute = VAL(MID$(InTime,4,2)) IF LEN(InTime) > 6 THEN Second = VAL(MID$(InTime,7)) ELSE Second = 0 END IF IF INSTR(LEFT$(InTime,2), ANY "pP") THEN IF Hour < 12 THEN INCR Hour, 12 END IF ELSEIF INSTR(LEFT$(InTime,2), ANY "aA") THEN IF Hour = 12 THEN Hour = 0 END IF END IF END SUB '=========================================================================== ' MakeDate - Combine the component parts of a date into a displayable ' string in U.S. format. ' ' Note: All other date routines call this routine to combine ' the component parts. ' ' Month = ' Day = ' Year = ' FUNCTION MakeDate(BYVAL Month AS INTEGER, BYVAL Day AS INTEGER, _ BYVAL Year AS INTEGER) PUBLIC AS STRING DIM Temp AS STRING IF Year < 80 THEN 'assume we've rolled into the next century INCR Year, 2000 ELSEIF Year < 100 THEN 'between 1980 and 1999 INCR Year, 1900 END IF Temp = RIGHT$(STR$(Month), 2) + "-" + _ RIGHT$(STR$(Day), 2) + "-" + _ RIGHT$(STR$(Year), 4) REPLACE " " WITH "0" IN Temp FUNCTION = Temp END FUNCTION '=========================================================================== ' MakeTime12 - Combine the component parts of a time into a displayable ' string in 12 hour U.S. format. ' ' Hour = ' Minute = ' FUNCTION MakeTime12(BYVAL Hour AS INTEGER, _ BYVAL Minute AS INTEGER) PUBLIC AS STRING DIM Pm AS INTEGER DIM H AS STRING DIM M AS STRING DIM T AS STRING IF Hour > 12 THEN DECR Hour,12 Pm = -1 END IF H$ = RIGHT$(STR$(Hour),2) M$ = RIGHT$(STR$(Minute),2) IF Pm THEN M$ = M$ + "p" ELSE M$ = M$ + "a" END IF T = H + ":" + M REPLACE " " WITH "0" IN T FUNCTION = T END FUNCTION '=========================================================================== ' MakeTime - Combine the component parts of a time into a displayable ' string in 24 hour U.S. format. ' ' Note: All other time routines call this routine to combine ' the component parts. ' ' Hour = ' Minute = ' Second = ' FUNCTION MakeTime(BYVAL Hour AS INTEGER, BYVAL Minute AS INTEGER, _ BYVAL Second AS INTEGER) PUBLIC AS STRING DIM H AS STRING DIM M AS STRING DIM S AS STRING DIM T AS STRING H = RIGHT$(STR$(Hour),2) M = RIGHT$(STR$(Minute),2) S = RIGHT$(STR$(Second),2) T = H + ":" + M + ":" + S REPLACE " " WITH "0" IN T FUNCTION = T END FUNCTION '=========================================================================== ' Leap - Return a one (1) if specified year is a leap year. ' ' Year = year to check ' FUNCTION Leap(BYVAL Year AS INTEGER) PUBLIC AS INTEGER IF (Year MOD 400) = 0 THEN FUNCTION = 1 ELSEIF (Year MOD 100) = 0 THEN FUNCTION = 0 ELSEIF (Year MOD 4) = 0 THEN FUNCTION = 1 END IF END FUNCTION '=========================================================================== ' DaysInMonth - Return the number of days in a given month. It does not ' account for leap years. ' ' Month = month to return number of days for ' FUNCTION DaysInMonth(BYVAL Month AS INTEGER) PUBLIC AS INTEGER DIM Buff AS STRING Buff = "312831303130313130313031" IF Month > 0 AND Month < 13 THEN FUNCTION = VAL( MID$(Buff, (Month-1) * 2, 2) ) END IF END FUNCTION '=========================================================================== ' DateToDays - Convert the specified date into the number of days since ' 01-01-1900. Supports dates up to 06-05-2079. ' ' InDate = Date to convert ' FUNCTION DateToDays(InDate AS STRING) PUBLIC AS WORD DIM Buffer AS STATIC STRING DIM Temp AS WORD DIM z AS INTEGER DIM Month AS INTEGER DIM Day AS INTEGER DIM Year AS INTEGER Buffer = "000031059090120151181212243273304334366" SplitDate InDate, Month, Day, Year IF Year < 80 THEN INCR Year, 2000 ELSEIF Year < 100 THEN INCR Year, 1900 ELSEIF Year < 1900 THEN EXIT FUNCTION END IF IF Year > 1900 THEN FOR z = Year - 1 TO 1900 STEP -1 INCR Temp, 365 + Leap(z) NEXT z END IF INCR Temp, VAL(MID$(Buffer, 1 + ((Month-1) * 3), 3)) IF (Month > 2) THEN INCR Temp, Leap(Year) END IF INCR Temp, Day FUNCTION = Temp END FUNCTION '=========================================================================== ' DaysToDate - Convert the number of days since 01-01-1900 to a date string. ' ' NumDays = number of days since 1900 ' FUNCTION DaysToDate(BYVAL NumDays AS WORD) PUBLIC AS STRING DIM Month AS INTEGER DIM Year AS INTEGER DIM DaysInMnth AS INTEGER DIM DaysInYear AS INTEGER DIM Temp AS STRING Year = 1900 DaysInYear = 365 + Leap(Year) DO UNTIL NumDays < DaysInYear DECR NumDays, 365 + Leap(Year) INCR Year DaysInYear = 365 + Leap(Year) LOOP FOR Month = 1 TO 12 DaysInMnth = DaysInMonth(Month) IF NumDays <= DaysInMnth THEN EXIT FOR END IF DECR NumDays, DaysInMnth NEXT IF NumDays = 0 THEN Month = 12 NumDays = 31 DECR Year END IF FUNCTION = MakeDate(Month, NumDays, Year) END FUNCTION '=========================================================================== ' DateMath - Add the specified number of days the specified date. ' To subtract days, use a negative value. ' ' InDate = starting date ' NumberOfDays = number of days to add ' FUNCTION DateMath(BYVAL InDate AS STRING, _ BYVAL NumberOfDays AS INTEGER) PUBLIC AS STRING FUNCTION = DaysToDate(DateToDays(InDate) + NumberOfDays) END FUNCTION '=========================================================================== ' Days - Return the number of days between two dates. If the first date ' comes after the second date, a negative value is returned. ' ' Date1 = starting date ' Date2 = ending date ' FUNCTION Days(BYVAL Date1 AS STRING, BYVAL Date2 AS STRING) PUBLIC AS INTEGER FUNCTION = DateToDays(Date1) - DateToDays(Date2) END FUNCTION '=========================================================================== ' ValidDate - Returns true (-1) if the specified date is valid, or false (0) ' if the date is invalid. ' ' InDate = date to check ' FUNCTION ValidDate(BYVAL InDate AS STRING) PUBLIC AS INTEGER DIM Month AS INTEGER DIM Day AS INTEGER DIM Year AS INTEGER DIM TotalDays AS INTEGER IF LEN(InDate) = 0 THEN EXIT FUNCTION END IF SplitDate InDate, Month, Day, Year IF (Month < 1) OR (Month > 12) THEN EXIT FUNCTION END IF TotalDays = DaysInMonth(Month) IF Month = 2 THEN INCR TotalDays, Leap(Year) END IF IF (Day < 1) OR (Day > TotalDays) THEN EXIT FUNCTION END IF IF Year < 1900 THEN EXIT FUNCTION END IF FUNCTION = -1 END FUNCTION '=========================================================================== ' TimeToSeconds - Returns the number of seconds since midnight for a given ' time string. ' ' InTime = Time to convert into seconds ' FUNCTION TimeToSeconds(BYVAL InTime AS STRING) PUBLIC AS LONG DIM Hour AS INTEGER DIM Minute AS INTEGER DIM Second AS INTEGER SplitTime InTime, Hour, Minute, Second FUNCTION = (Hour * 3600) + (Minute * 60) + Second END FUNCTION '=========================================================================== ' SecondsToTime$ - Return a time string for a given number of seconds since ' midnight. ' ' Secs = seconds to convert into a time string ' FUNCTION SecondsToTime(BYVAL Secs AS LONG) PUBLIC AS STRING DIM Hour AS INTEGER DIM Minute AS INTEGER DIM Second AS INTEGER Hour = Secs \ 3600 Minute = (Secs MOD 3600) \ 60 Second = ((Secs MOD 3600) \ 60) MOD 60 FUNCTION = MakeTime(Hour, Minute, Second) END FUNCTION '=========================================================================== ' Elapsed - Return the number of seconds elapsed between two time strings. ' If the second time is earlier than the first, a midnight rollover ' is assumed. ' ' Time1 = start time ' Time2 = end time ' FUNCTION Elapsed(BYVAL Time1 AS STRING, BYVAL Time2 AS STRING) PUBLIC AS LONG DIM Temp1 AS LONG DIM Temp2 AS LONG Temp1 = TimeToSeconds(Time1) Temp2 = TimeToSeconds(Time2) IF Temp2 < Temp1 THEN 'account for midnight rollover INCR Temp2, 86400 END IF FUNCTION = Temp2 - Temp1 END FUNCTION '=========================================================================== ' DOW - Return the Day of the Week as an integer. ' ' InDate = date to convert into the day of the week ' FUNCTION DOW(InDate AS STRING) PUBLIC AS INTEGER FUNCTION = 1 + DateToDays(InDate) MOD 7 END FUNCTION '=========================================================================== ' DOY - Return the Day of the Year ' ' InDate = date to convert into day of the year ' FUNCTION DOY(InDate AS STRING) PUBLIC AS INTEGER DIM Month AS INTEGER DIM Day AS INTEGER DIM Year AS INTEGER DIM Buffer AS STATIC STRING DIM Temp AS INTEGER Buffer = "000031059090120151181212243273304334366" SplitDate InDate, Month, Day, Year Temp = VAL(MID$(Buffer, 1 + ((Month-1) * 3), 3)) + Day IF Month > 2 THEN INCR Temp, Leap(Year) END IF FUNCTION = Temp END FUNCTION '=========================================================================== ' CDOW - Return the name for the Day of the Week for the specified date. ' ' InDate = Date to return day of week name for ' FUNCTION CDOW(InDate AS STRING) PUBLIC AS STRING DIM Names AS STATIC STRING DIM DW AS INTEGER Names = "Sunday " + _ "Monday " + _ "Tuesday " + _ "Wednesday" + _ "Thursday " + _ "Friday " + _ "Saturday " DW = DateToDays(InDate) MOD 7 FUNCTION = RTRIM$( MID$(names, 1 + DW * 9, 9) ) END FUNCTION '=========================================================================== ' MonthName$ - Return the name for a specified month. ' ' Month = month to return name for ' FUNCTION MonthName(BYVAL Month AS INTEGER) PUBLIC AS STRING DIM Names AS STATIC STRING Names = "January " + _ "February " + _ "March " + _ "April " + _ "May " + _ "June " + _ "July " + _ "August " + _ "September" + _ "October " + _ "November " + _ "December " IF Month < 1 OR Month > 12 THEN EXIT FUNCTION END IF FUNCTION = RTRIM$( MID$( Names, 1 + (Month - 1) * 9, 9) ) END FUNCTION