' ' FileName: UNIX-TS.BAS ' Description: Two subs used to convert current time to a unix date ' stamp and back again. ' ' Tested Under: QBasic v1.1 ' QuickBasic v4.5 ' MS PDS v7.1 ' Visual Basic v3.0 ' ' Notes: Originally written by Andrew K. Dart, code re-worked ' and fixed slightly by David Stumpf. ' ' Released On: January 26, 1998 ' Released As: Public Domain ' ' Comments: The main reasons I changed alot of this code was due ' to the fact that; ' ' a) It had some serious bugs, including loosing ' the entire month of december. ' ' b) One of the main reasons I use this code is to ' store dates in my programs so they will be ' Y2K compliant. Andrew's original code still ' stopped in 1999. ' ' c) I also work with Visual Basic and some of the ' variables had to be renamed to comply with ' internal variables. (Same as PDS) ' ' d) VB seems to like it alot better if you empty ' all variables at the end of a sub or function. ' ' Tested under MS-DOS v5.00 with the clock set past ' the year 2000 and worked just fine. (Yes, DOS 5 ' will actually let you do that.) ' DECLARE FUNCTION Date2Unix& () DECLARE FUNCTION Unix2Date$ (UnixDate&) DIM SHARED mo(1 TO 13) AS INTEGER DIM SHARED LastDay(69 TO 138) AS INTEGER mo(1) = 0: mo(5) = 120: mo(9) = 243 mo(2) = 31: mo(6) = 151: mo(10) = 273 mo(3) = 59: mo(7) = 181: mo(11) = 304 mo(4) = 90: mo(8) = 212: mo(12) = 334 mo(13) = 365 LastDay(69) = 0 ' This is only here for an example, and to show that ' the code is at least 99.9% accurate. This example ' does not work under VB. CLS PRINT "Testing UNIX-TS.BAS. . ." WHILE INKEY$ = "" LOCATE 2, 1 PRINT DATE$; " / "; TIME$ PRINT Unix2Date$(Date2Unix&) WEND FUNCTION Date2Unix& DIM TOD AS LONG DIM leap AS LONG DIM mins AS LONG DIM Sec AS LONG DIM Hr AS LONG DIM Yr AS LONG DIM Mnth AS LONG DIM Dy AS LONG Yr = VAL(RIGHT$(DATE$, 4)) - 1900 Mnth = VAL(LEFT$(DATE$, 2)) Dy = VAL(MID$(DATE$, 4, 2)) Sec = VAL(RIGHT$(TIME$, 2)) Hr = VAL(LEFT$(TIME$, 2)) mins = VAL(MID$(TIME$, 4, 2)) TOD = (mins * 60) + Sec + (Hr * 3600) leap = INT((Yr - 71) / 4) Date2Unix& = (365 * (Yr - 70) + leap + mo(Mnth) + Dy) * 86400 + TOD IF (Mnth > 2) AND (Yr MOD 4 = 0) THEN UnixDate& = UnixDate& * 1 leap = 0 mins = 0 Sec = 0 Hr = 0 Yr = 0 Mnth = 0 Dy = 0 END FUNCTION FUNCTION Unix2Date$ (UnixDate&) DIM x AS LONG DIM y AS LONG DIM Days AS LONG DIM Mnth AS LONG DIM Yr AS LONG DIM Dy AS LONG DIM RawSeconds AS LONG DIM Hr AS LONG DIM Min AS LONG DIM Secs AS LONG DIM timedate AS STRING FOR x = 70 TO 138 LastDay(x) = 365 + LastDay(x - 1) IF x MOD 4 = 0 THEN LastDay(x) = LastDay(x) + 1 NEXT x Days = UnixDate& \ 86400 FOR x = 70 TO 138 IF Days <= LastDay(x) THEN EXIT FOR NEXT x Yr = x + 1900 FOR y = 1 TO 12 IF Days < (LastDay(x - 1) + mo(y + 1)) THEN Mnth = y Dy = (Days - LastDay(x - 1) - mo(y) + 1) EXIT FOR END IF NEXT y RawSeconds = UnixDate& MOD 86400 Hr = RawSeconds \ 3600 Min = (RawSeconds - (Hr * 3600)) \ 60 Secs = ((RawSeconds - (Hr * 3600)) - Min * 60) IF Mnth > 9 THEN timedate = timedate + LTRIM$(RTRIM$(STR$(Mnth))) ELSE timedate = timedate + "0" + LTRIM$(RTRIM$(STR$(Mnth))) END IF timedate = timedate + "-" IF Dy > 9 THEN timedate = timedate + LTRIM$(RTRIM$(STR$(Dy))) ELSE timedate = timedate + "0" + LTRIM$(RTRIM$(STR$(Dy))) END IF timedate = timedate + "-" + LTRIM$(RTRIM$(STR$(Yr))) timedate = timedate + " / " IF Hr > 9 THEN timedate = timedate + LTRIM$(RTRIM$(STR$(Hr))) ELSE timedate = timedate + "0" + LTRIM$(RTRIM$(STR$(Hr))) END IF timedate = timedate + ":" IF Min > 9 THEN timedate = timedate + LTRIM$(RTRIM$(STR$(Min))) ELSE timedate = timedate + "0" + LTRIM$(RTRIM$(STR$(Min))) END IF timedate = timedate + ":" IF Secs > 9 THEN timedate = timedate + LTRIM$(RTRIM$(STR$(Secs))) ELSE timedate = timedate + "0" + LTRIM$(RTRIM$(STR$(Secs))) END IF Unix2Date$ = timedate x = 0 y = 0 Days = 0 Mnth = 0 Yr = 0 Dy = 0 RawSeconds = 0 Hr = 0 Min = 0 timedate = "" END FUNCTION