In order that a device (with limited memory) is able to manage its own timezone and daylight savings, I'm trying to calculate daylight savings triggers for 85 time zones based on a simplified description of each timezone. I have access to minimal C and C++ libraries within the device. The format of the timezone (inc. DST) description for each time zone is as follows:
- UTC - the base time and date from system clock
- GMTOffsetMinutes - offset from GMT with DST inactive
- DSTDeltaMinutes - modifier to above with DST active (as applicable to TZ)
- DSTStartMonth - month in which DST becomes active
- DSTStartNthOccurranceOfDay - the nth occurrence of the day name in month
- DSTDayOfWeek - Sun = 0 through to Sat = 6
- DSTStartHour - hour at which DST becomes active
- DSTStartMinute - minute at which DST becomes active
- and corresponding EndMonth, EndNth..., EndHour, EndMinute
I have found numerous examples going the other way, i.e. starting with the date, but they involve using the modulus, keeping the remainder and dropping the quotient hence I have been unable to transpose the formula to suit my needs.
I also tried to reuse the standard "Jan = 6, Feb = 2, Mar = 2, Apr = 5, May = 0, etc. modifier table and year modifiers from the "tell me what day the 25th of June, 2067 is?" party trick and developed the following algorithm.
Date = DayOfWeek + ((NthOccuranceOfDay - 1) x 7 ) - MonthCode - YearCode
This worked for the first 6 random test dates I selected but then I started to see dates for which it failed. Is it possible that the basic algorithm is sound but I'm missing a further modifier or maybe that I'm applying the modifiers incorrectly?
Is there another solution I could utilize?
I have access to minimal C and C++ libraries within the device
. – Mateusz Grzejek