'---------------------------------------------------------------------------------------- ' Name: TIMECONVERT_EXAMPLE_SER_V001.TIG ' Type: TIGER-BASIC(tm) Source Code ' Purpose: Example for enhanced time conversion functions. Daylight saving time is now ' correctly switched at the exact second, and TIG2DOS now has an additional ' parameter giving back the day of week ' ' (C) - Copyright Wilke Technology, P.O.Box 1727, D-52018 Aachen, Germany '---------------------------------------------------------------------------------------- ' ' Thank you for using BASIC Tigers in your products. If you have questions, ideas ' or special needs, please contact your next distributor or the Tiger support team ' and visit our web site: ' ' Wilke Technology GmbH ' The Tiger Support Team ' P.O.Box 1727, D-52018 Aachen, Germany ' Krefelder Str. 147, D-52070 Aachen, Germany ' ' email: support@wilke-technology.com (english) ' email: support@wilke.de (german) ' Phone: +49 (241) 918 900 Mo to Fr, 7:00 to 16:00 (GMT) ' Fax: +49 (241) 918 9068 ' ' New information, new drivers and free downloads see: ' ' www.wilke-technology.com (english) ' www.wilke.de (german) ' ' Sincerely, ' ' Your Tiger Support Team ' '---------------------------------------------------------------------------------------- ' ' This is an example program for the new, enhanced time conversion routines for the RTC. ' ' Most important, now the daylight saving time (DST) is calculated correctly by the ' second. The TIG time (in variable GX, would be written to the RTC) is always WITHOUT ' daylight saving time, the extra hour is deducted when writing to the RTC and added when ' reading from the RTC - if needed. There is also a global variable "DaylightSavingTime" ' indicating if we have daylight saving time (1) or not (0). ' ' Furthermore, the routine TIG2DOS (when reading the RTC) now gives back one additional ' parameter: The day of the week, with Monday = 0 and Sunday = 6. There are two global ' arrays for the names, one for the long names (Monday, Tuesday, ...) and one for the ' short names (Mon, Tue, ...). ' ' You need to copy the three subroutines (TIG2DOS, DOS2TIG and TimeCVT_init) to your ' application, as well as the defines and global variables marked below. ' ' The example itself (main task) consists of a loop, generating random date and time, ' converting these to Tiger time (seconds), and converting these seconds back to time ' and date. Every convesion step is put out on serial port SER0, with 9,600 baud, ' 8 data bit and no parity. ' '---------------------------------------------------------------------------------------- USER_VAR_STRICT ' all variables must be declared #INCLUDE UFUNC4.INC ' user function calls #INCLUDE DEFINE_A.INC ' general defines ' --- ALL DEFINES AND GLOBAL VARIABLES BELOW ARE NEEDED AND MUST BE COPIED TO YOUR CODE #DEFINE HOURS_PER_4_YEARS 35064 ' #DEFINE HOURS_PER_LEAPYEAR 8784 ' #DEFINE HOURS_PER_YEAR 8760 ' #DEFINE SEC_PER_4_YEARS 126230400 ' #DEFINE SEC_PER_365_DAYS 31536000 ' #DEFINE SEC_PER_DAY 86400 ' #DEFINE SEC_PER_HOUR 3600 ' LONG DaylightSavingTime ' flag for daylight saving time LONG GX, DayOfWeek ' Tiger seconds, day of week ARRAY YMonths$(12) OF STRINGS (5) ' output names for month ARRAY MDays(12) OF LONG ' number of days in month ARRAY WDay$(7) OF STRING(10) ' day of week (long name) ARRAY WDayShort$(7) OF STRING(3) ' day of week (short name) ARRAY Date(3) OF LONG ' globals for day, month, year ARRAY Time(3) OF LONG ' globals for hour, minute, second ' --- ALL DEFINES AND GLOBAL VARIABLES ABOVE ARE NEEDED AND MUST BE COPIED TO YOUR CODE '---------------------------------------------------------------------------------------- ' Task: Main program ' Creating random times and dates, converting to Tiger seconds and back, giving ' out the result through serial port SER0. '---------------------------------------------------------------------------------------- TASK MAIN ' begin of task MAIN BYTE EVER ' for endless loop INSTALL_DEVICE #SER, "SER1B_K1.TDD",& ' install serial driver BD_9_600, DP_8N, JA,& ' settings for SER0 BD_9_600, DP_8N, JA ' settings for SER1 CALL TimeCVT_init () ' initialize variables for conversion RANDOMIZE ' initialize random number generation FOR EVER = 0 TO 0 STEP 0 ' endless loop GX = 0 ' Tiger seconds Time(0) = RND(0) / 1110 ' seconds 0 to 59 Time(1) = RND(0) / 1110 ' minutes 0 to 59 Time(2) = RND(0) / 2849 ' hours 0 to 23 Date(0) = (RND(0) / 2184) + 1 ' day 1 to 31 Date(1) = (RND(0) / 5958) + 1 ' month 1 to 12 Date(2) = (RND(0) / 964) + 1980 ' year 1980 to 2048 ' --- CONVERT TIME AND DATE TO TIGER SECONDS ' Sec Min Hrs Day Month Year CALL DOS2TIG(GX,Time(0),Time(1),Time(2),Date(0),Date(1),Date(2)) ' --- OUTPUT VALUES TO SERIAL PORT SER0 USING "UD<2><2>0 0.0.0.0.2" PRINT_USING #SER, #0, "Initial Date/Time: ";Date(0);"-";YMonths$(Date(1)-1);"-"; USING "UD<4><4>0 0.0.0.0.4" PRINT_USING #SER, #0, Date(2); " "; USING "UD<2><2>0 0.0.0.0.2" PRINT_USING #SER, #0, Time(2);":";Time(1);":";Time(0) PRINT #SER, #0, "is converted to GX ="; GX ' --- CONVERT TIGER SECONDS BACK TO TIME AND DATE ' Sec Min Hrs Day Month Year CALL TIG2DOS(GX,Time(0),Time(1),Time(2),Date(0),Date(1),Date(2),DayOfWeek) ' --- OUTPUT VALUES TO SERIAL PORT SER0 USING "UD<2><2>0 0.0.0.0.2" PRINT_USING #SER, #0, "is converted back to: "; WDay$(DayOfWeek);", ";Date(0);"-";YMonths$(Date(1)-1);"-"; USING "UD<4><4>0 0.0.0.0.4" PRINT_USING #SER, #0, Date(2); " "; USING "UD<2><2>0 0.0.0.0.2" PRINT_USING #SER, #0, Time(2);":";Time(1);":";Time(0);" ("; IF DaylightSavingTime = 0 THEN PRINT #SER, #0, "no "; ENDIF PRINT #SER, #0, "daylight saving time)" PRINT #SER, #0, "" WAIT_DURATION 500 ' wait half a second NEXT ' end of endless loop END ' end of task MAIN '---------------------------------------------------------------------------------------- ' Subroutine: Converts date and time into Tiger format (seconds from 1-Jan-1980, 0:00) ' input: X = any (initial) value, gets set in subroutine ' Sec = seconds ' Min = minutes ' Hour = hours ' Day = day ' Mon = month ' Year = year ' output: X = number of seconds since 1-Jan-1980, 0:00 '---------------------------------------------------------------------------------------- SPEEDSUB DOS2TIG (VAR LONG X; LONG Sec, Min, Hour, Day, Mon, Year) LONG dst, DoW31, Offset, dst_day ' for daylight saving time calculation LONG Timezone ' for difference between local time and GMT LONG I, Days, Hours ' Timezone = 0 ' X = Timezone ' I = Year - 1980 ' X = X + ((I SHR 2) * SEC_PER_4_YEARS) ' X = X + ((I BITAND 3) * SEC_PER_365_DAYS) ' IF (I BITAND 3) > 0 THEN ' X = X + SEC_PER_DAY ' ENDIF ' Days = 0 ' I = Mon - 1 ' WHILE (I > 0) ' I = I - 1 ' Days = Days + MDays(I) ' ENDWHILE ' Days = Days + Day - 1 ' IF ((Mon > 2) AND (Year BITAND 3) = 0) THEN ' Days = Days + 1 ' ENDIF ' Hours = Days * 24 + Hour ' X = X + Hours * SEC_PER_HOUR ' X = X + 60 * Min + Sec ' ' ' !!!!!!!! from here added for daylight saving time check !!!!!!!! ' dst = 0 ' reset flag for daylight saving time (DST) IF Mon > 3 AND Mon < 10 THEN ' this is DST for sure, so we are ready dst = 1 ' set flag for DST ELSE ' IF Mon = 3 AND Day > 24 THEN ' from March 24th is becomes critical Offset = (Year+(Year/4)-(Year/100)+(Year/400)+((13*3+8)/5)+31) DoW31 = MOD (Offset, 7) ' which day of the week is March 31th? Here base: 0 = Sunday dst_day = 31 - DoW31 ' go back until Sunday IF Day > dst_day OR (Day=dst_day AND Hour>=2) THEN dst = 1 ' if after Sunday or Sunday after 2am, it is DST --> set flag ENDIF ' ENDIF ' IF Mon = 10 THEN ' when October IF Day < 25 THEN ' and before the 25th, then it is DST dst = 1 ' and we set the flag ELSE ' from October 25th is becomes critical Offset = (Year+(Year/4)-(Year/100)+(Year/400)+((13*10+8)/5)+31) DoW31 = MOD (Offset, 7) ' which day of the week is October 31th? Here base: 0 = Sunday dst_day = 31 - DoW31 ' go back until Sunday IF Day < dst_day OR (Day=dst_day AND Hour<2) THEN dst = 1 ' if after Sunday or Sunday before 3am, ii is DST --> set flag ENDIF ' ENDIF ' ENDIF ' ENDIF ' IF dst = 1 THEN ' if it is daylight saving time X = X - 3600 ' we substract one hour resp. 3600 seconds for the RTC ENDIF DaylightSavingTime = dst ' now set global variable for DST END ' '---------------------------------------------------------------------------------------- ' Subroutine: Converts time in Tiger format (seconds since 1-Jan-1980, 0:00) ' into single values for time and date ' input: Seconds = number of seconds since 1-Jan-1980, 0:00 ' output: Sec = seconds (0...60) ' Min = minutes (0...60) ' Hour = hours (0...23) ' Day = day (1...31) ' Mon = month (1...12) ' Year = year (1980...2048) ' DoW = weekday (0..6) '---------------------------------------------------------------------------------------- SPEEDSUB TIG2DOS(LONG Seconds;VAR LONG Sec,Min,Hour,Day,Mon,Year,DoW) LONG Offset, TempYear, TempMonth ' for day of week calculation LONG dst, DoW31, dst_day ' for daylight saving time calculation LONG Time ' LONG Timezone ' for difference between local time and GMT Timezone = 0 ' Time = Seconds - Timezone ' Sec = MOD (Time,60) ' Time = Time / 60 ' Min = MOD (Time,60) ' Time = Time / 60 ' Year = 1980 + ((Time / (HOURS_PER_4_YEARS)) SHL 2) Time = MOD (Time,HOURS_PER_4_YEARS) ' IF (Time >= HOURS_PER_LEAPYEAR) THEN ' Time = Time - HOURS_PER_LEAPYEAR ' Year = Year + 1 ' Year = Year + (Time / HOURS_PER_YEAR) ' Time = MOD (Time,HOURS_PER_YEAR) ' ENDIF ' Hour = MOD (Time,24) ' Time = Time / 24 ' Time = Time + 1 ' IF ((Year BITAND 3) = 0 AND (Time = 60)) THEN Mon = 2 ' Day = 29 ' ELSE ' IF ((Year BITAND 3) = 0 AND (Time > 60)) THEN Time = Time - 1 ' ENDIF ' Mon = 0 ' WHILE (MDays(Mon) < Time) ' Time = Time - MDays(Mon) ' Mon = Mon + 1 ' ENDWHILE ' Mon = Mon + 1 ' Day = Time ' ENDIF ' ' ' !!!!!!!! from here added for day of week !!!!!!!! ' TempYear = Year ' place year in help variable TempMonth = Mon ' place month in help variable IF TempMonth < 3 THEN ' if month January or February TempMonth = TempMonth + 12 ' add 12 month TempYear = TempYear - 1 ' and substract one year ENDIF ' Offset = (TempYear+(TempYear/4)-(TempYear/100)+(TempYear/400)+((13*TempMonth+8)/5)+Day-1) DoW = MOD (Offset, 7) ' calculate day of the week ' ' !!!!!!!! from here added for daylight saving time check !!!!!!!! ' dst = 0 ' reset flag for daylight saving time (DST) IF Mon > 3 AND Mon < 10 THEN ' this is DST for sure, so we are ready dst = 1 ' set flag for DST ELSE ' IF Mon = 3 AND Day > 24 THEN ' from March 24th is becomes critical Offset = (Year+(Year/4)-(Year/100)+(Year/400)+((13*3+8)/5)+31) DoW31 = MOD (Offset, 7) ' which day of the week is March 31th? Here base: 0 = Sunday dst_day = 31 - DoW31 ' go back until Sunday IF Day > dst_day OR (Day=dst_day AND Hour>=2) THEN dst = 1 ' if after Sunday or Sunday after 2am, it is DST --> set flag ENDIF ' ENDIF ' IF Mon = 10 THEN ' when October IF Day < 25 THEN ' and before the 25th, then it is DST dst = 1 ' and we set the flag ELSE ' from October 25th is becomes critical Offset = (Year+(Year/4)-(Year/100)+(Year/400)+((13*10+8)/5)+31) DoW31 = MOD (Offset, 7) ' which day of the week is October 31th? Here base: 0 = Sunday dst_day = 31 - DoW31 ' go back until Sunday IF Day < dst_day OR (Day=dst_day AND Hour<2) THEN dst = 1 ' if after Sunday or Sunday before 3am, ii is DST --> set flag ENDIF ' ENDIF ' ENDIF ' ENDIF ' IF dst = 1 THEN ' if it is daylight saving time Hour = Hour + 1 ' add one hour IF Hour > 23 THEN ' if the day changes because of that Hour = 0 ' set hour to 0 Day = Day + 1 ' increase day IF Day > MDays(Mon-1) THEN ' if the month changes because of that Day = 1 ' set day to 1 Mon = Mon + 1 ' increase month ENDIF ' ENDIF ' ENDIF ' DaylightSavingTime = dst ' now set global variable for DST END ' '---------------------------------------------------------------------------------------- ' Subroutine: Initializes all global variables with starting values '---------------------------------------------------------------------------------------- SUB TimeCVT_init () ' BYTE pos ' MDays(0) = 31 ' MDays(1) = 28 ' Mdays(2) = 31 ' MDays(3) = 30 ' MDays(4) = 31 ' MDays(5) = 30 ' MDays(6) = 31 ' MDays(7) = 31 ' MDays(8) = 30 ' MDays(9) = 31 ' MDays(10) = 30 ' MDays(11) = 31 ' Date(0) = 30 ' Date(1) = 12 ' Date(2) = 1997 ' FOR pos = 0 TO 2 ' Time(pos) = 0 ' NEXT ' YMonths$(0) = "Jan" ' YMonths$(1) = "Feb" ' YMonths$(2) = "Mar" ' YMonths$(3) = "Apr" ' YMonths$(4) = "May" ' YMonths$(5) = "Jun" ' YMonths$(6) = "Jul" ' YMonths$(7) = "Aug" ' YMonths$(8) = "Sep" ' YMonths$(9) = "Oct" ' YMonths$(10) = "Nov" ' YMonths$(11) = "Dec" ' WDay$(0) = "Monday" ' WDay$(1) = "Tuesday" ' WDay$(2) = "Wednesday" ' WDay$(3) = "Thursday" ' WDay$(4) = "Friday" ' WDay$(5) = "Saturday" ' WDay$(6) = "Sunday" ' WDayShort$(0) = "Mon" ' WDayShort$(1) = "Tue" ' WDayShort$(2) = "Wed" ' WDayShort$(3) = "Thu" ' WDayShort$(4) = "Fri" ' WDayShort$(5) = "Sat" ' WDayShort$(6) = "Sun" ' END '