Find the date given the year, the month and the "nth" occurrance of day within the month C/C++ -


in order device (with limited memory) able manage own timezone , daylight savings, i'm trying calculate daylight savings triggers 85 time zones based on simplified description of each timezone. have access minimal c , c++ libraries within device. format of timezone (inc. dst) description each time zone follows:

  • utc - base time , date system clock
  • gmtoffsetminutes - offset gmt dst inactive
  • dstdeltaminutes - modifier above dst active (as applicable tz)
  • dststartmonth - month in dst becomes active
  • dststartnthoccurranceofday - nth occurrence of day name in month
  • dstdayofweek - sun = 0 through sat = 6
  • dststarthour - hour @ dst becomes active
  • dststartminute - minute @ dst becomes active
  • and corresponding endmonth, endnth..., endhour, endminute

i have found numerous examples going other way, i.e. starting date, involve using modulus, keeping remainder , dropping quotient hence have been unable transpose formula suit needs.

i tried reuse standard "jan = 6, feb = 2, mar = 2, apr = 5, may = 0, etc. modifier table , year modifiers "tell me day 25th of june, 2067 is?" party trick , developed following algorithm.

date = dayofweek + ((nthoccuranceofday - 1) x 7 ) - monthcode - yearcode 

this worked first 6 random test dates selected started see dates failed. possible basic algorithm sound i'm missing further modifier or maybe i'm applying modifiers incorrectly?

is there solution utilize?

using this open source, cross platform date library, 1 can write:

#include "date.h" #include <iostream>  int main() {     using namespace date;     year_month_day us_daylight_starts = sys_days(sun[2]/mar/2015);     year_month_day us_daylight_ends   = sys_days(sun[1]/nov/2015);     std::cout << us_daylight_starts << '\n';     std::cout << us_daylight_ends << '\n'; } 

which output:

2015-03-08 2015-11-01 

the formulas library based on in public domain , documented here.

the algorithms paper has complete unit tests validating date algorithms on range of millions of years (a far larger range necessary).

sometimes daylight savings rules written in terms of last weekday of month. handled:

year_month_day ymd = sys_days(sun[last]/nov/2015); std::cout << ymd << '\n';  // 2015-11-29 

Comments

Popular posts from this blog

html - Firefox flex bug applied to buttons? -

html - Missing border-right in select on Firefox -

c# - two queries in same method -