Change GMT offset

George KrietsepisGeorge Krietsepis Junior MemberPosts: 151
Dear All,
although I have read many topics about this, I have not come to a clear conclusion . I'm using the ASTRO_CLOCK to keep the sunrise/sunset times, but I need to manually change the GMT offset parameter from the code every 6 months for the sunrise/sunset values to be synchronized .
In my case the astro_clock(22.572563, 40.282520, 2.0, LDATE, sSunRise, sSunSet) works for summer time while the astro_clock(22.572563, 40.282520, 3.0, LDATE, sSunRise, sSunSet) for winter time. If I don't change the GMT offset ( from 2 to 3 and vice versa) , the sunrise/sunset values will be an hour onward or backward for 6 months period.
My question :
How can I get from code when daylight saving and standard time change ? If I have this information, I can make some piece of code to change the GMT offset value and pass it into the ASTRO_CLOCK function, without the need to be manually changed by me every 6 months.
For example :

if ( date = x )
nGMT = 2 //daylight_saving_time

if ( date = y )
nGMT = 3 //standard_time

astro_clock(22.572563, 40.282520,nGMT, LDATE, sSunRise, sSunSet)

Thanks,
George

Comments

  • viningvining X Member Posts: 4,301
    There's a function in this .axi file that I use to determine if DST is active or not. I haven't played with this file for quite some time now but you find anything that helps more power to ya.
    PROGRAM_NAME='VAV_Date_&_Time'
    (***********************************************************)
    (*  FILE_LAST_MODIFIED_ON: 03/29/2016  AT: 18:58:55        *)
    (***********************************************************)
    
    DEFINE_CONSTANT //DST                           <<<*************************  CHANGE SETTING PER JOB *********************************>>>
    //Typically clocks are adjusted forward one hour near the start of spring and are adjusted backward in autumn.[1] 
    //Starting in 2007, Daylight Saving Time (DTS) has been extended by one month. Therefore, the start date of DTS in the United States has been
    //changed to the 2nd Sunday in March and the end date of DTS has been changed to the 1st Sunday in November
    
    //If Daylight Savings Time is currently being observed (between spring and fall), set this item to "1". If Daylight Savings Time
    //is not currently being observed (between fall and spring) or is not observed in your geographic location, set this item to "0".
    //This item is used to correctly calculate the times for sunrise and sunset.
    
    INTEGER DST_OBSERVED        = 1;          //1 for yes and 0 for no 
    INTEGER DST_OFFSET        = 1;          //number of hours time is offset 
    INTEGER DST_BEGIN_MONTH        = 3;          //March (months 1-12 January to December)
    INTEGER DST_END_MONTH        = 11;         //November (months 1-12 January to December)
    INTEGER DST_BEGIN_DAY        = 1;          //Sunday (day of week 1-7 Sunday to Saturday)
    INTEGER DST_END_DAY        = 1;          //Sunday (day of week 1-7 Sunday to Saturday)
    INTEGER DST_BEGINDAY_OCCURANCE    = 2;          //Number of occurances of that day in that month (2nd Sunday)
    INTEGER DST_ENDDAY_OCCURANCE    = 1;          //Number of occurances of that day in that month (1st Sunday)
    CHAR DST_BEGIN_TIME[]        = '02:00:00';    //Time change occurs 
    CHAR DST_END_TIME[]        = '02:00:00';    //Time change occurs 
    
    DEFINE_CONSTANT //ASTRO_CLOCK LAT/LONG          <<<*************************  CHANGE SETTING PER JOB *********************************>>>
    
    INTEGER CLKMGR_DST        = 1;         //1 for ON and 0 for OFF
    INTEGER CLKMGR_DST_OFFSET_HR    = 1;         //1 for 1 hour
    INTEGER CLKMGR_DST_OFFSET_MIN    = 0;         //0 for 0 minutes
    INTEGER CLKMGR_DST_OFFSET_SEC    = 0;         //0 for 0 seconds
    INTEGER CLKMGR_RESYNC_PERIOD    = 240;         //in minutes 480 = 8 hours, choices: 5 min, 15 min, 2 hour, 4 hours & (8 hours?)
                                //the web interface doesn't show an 8 hour option but the keyword example only shows
                                //an hour hour example. So I choose the highest the web gui lists 4 hours/240 minutes. 
    
    DOUBLE CLKMGR_LAT        = -73.42;    //Longitude - Longitude in Degrees.Fraction of Degrees. West longitudes must be negative.
    DOUBLE CLKMGR_LONG        = 41.98;     //Latitude - Latitude in Degrees.Fraction of Degrees. South latitudes must be negative 
    DOUBLE CLKMGR_OFFSET_GMT_EST    = -5.0;      //HoursFromGMT - Number of hours from GMT. Hours West of GMT can be entered as 
    DOUBLE CLKMGR_OFFSET_GMT_EDT    = -4.0;      //HoursFromGMT - Number of hours from GMT. Hours West of GMT can be entered as 
    
    CHAR CLKMGR_TIME_ZONE[]        = 'UTC-05:00';    //Input string must have the correct format: UTC[+|-]HH:MM
    CHAR CLKMGR_ACTIVESERVER[]     = '132.163.4.101'; 
    
    DEFINE_CONSTANT //TIME CONSTANTS Days of Week & Month of Year
    
    SUNDAY            = 1;
    MONDAY            = 2;
    TUESDAY           = 3;
    WEDNESDAY         = 4;
    THURSDAY          = 5;
    FRIDAY            = 6;
    SATURDAY          = 7;
    
    JANUARY        = 1;
    FEBRUARY    = 2;
    MARCH        = 3;
    APRIL        = 4;
    MAY        = 5;
    JUNE        = 6;
    JULY         = 7;
    AUGUST        = 8;
    SEPTEMBER    = 9;
    OCTOBER        = 10;
    NOVEMBER    = 11;
    DECEMBER    = 12;
    
    FIRST           = 1;
    SECOND          = 2;
    THIRD           = 3;
    FOURTH          = 4;
    FIFTH           = 5;
    
    EXPIRES_IN_SECONDS          = 10; 
    INTEGER NUM_SECONDS_MINUTE    = 60;
    INTEGER NUM_SECONDS_30MIN    = 1800;
    INTEGER NUM_SECONDS_HOUR    = 3600;
    LONG NUM_SECONDS_DAY            = 86400;
    
    INTEGER NUM_MINUTES_HOUR    = 60;
    INTEGER NUM_MINUTES_DAY        = 1440;
    
    CHAR DAY_OF_WEEK_ABREV[7][3]   = {'SUN','MON','TUE','WED','THU','FRI','SAT'}
    CHAR DAY_OF_WEEK_FULL[7][9]    = {'Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'}
    
    CHAR MONTH_OF_YR_ABREV[12][3]   = {'Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'}; 
    CHAR MONTH_OF_YR_FULL[12][9]    = {'January','February','March','April','May','June','July','August','September','October','November','December'}; 
    CHAR DAYS_MONTHs[12]            = {31   ,28   ,31   ,30   ,31   ,30   ,31   ,31   ,30   ,31   ,30   ,31};
    CHAR DAYS_LEAPMONTHs[12]        = {31   ,29   ,31   ,30   ,31   ,30   ,31   ,31   ,30   ,31   ,30   ,31};
    
    DEFINE_CONSTANT //UI FEEDBACK EVENTS
    
    FB_TIME_TIME           = 1;
    FB_TIME_SUNRISE        = 2;
    FB_TIME_SUNSET        = 3;
    FB_TIME_DST        = 4;
    FB_TIME_HOLIDAY        = 5;
    
    DEFINE_TYPE     //TIME STUFF
    
    STRUCTURE _sMyTime
    
         {
         INTEGER     nYear;
         INTEGER     nMonth;
         INTEGER     n24Hour;
         INTEGER     n12Hour;
         INTEGER     nMinute;
         INTEGER     nSecond;
         INTEGER     nDayOfMonth;
         INTEGER     nDayOfWeek;
         INTEGER      nDayOccurance[7];
         LONG    nTime_Seconds;
         LONG    nTime_Minutes;
         CHAR    c24Time[8];
         CHAR    c12Time[8];
         INTEGER     nWeekOfMonth;
         INTEGER     nPlusDays;
         INTEGER     nDaytime;
         INTEGER    nTimePM_flag;
         CHAR       cAMPM[2];
         LONG         nSunriseTime;
         LONG         nSunSetTime;
         INTEGER    nSunRiseTimeMinutes;
         INTEGER    nSunSetTimeMinutes;
         CHAR     cSunrise[10];
         CHAR     cSunset[10];
         INTEGER     nSunset24Hour;
         INTEGER     nSunset12Hour;
         INTEGER     nSunsetMinute;
         INTEGER     nSunsetSecond;
         INTEGER     nSunriseHour;
         INTEGER     nSunriseMinute;
         INTEGER     nSunriseSecond;
         CHAR         cHoliday[24];
         CHAR         cDay[3];
         CHAR         cDate[8];
         CHAR     cWebCurDateTime[29];
         CHAR     cWebExpDateTime[29];
         }
    
    DEFINE_TYPE     //DST
    
    STRUCTURE _sMyDST
    
         {
         INTEGER nDST_Active;
         INTEGER nOffset;
         INTEGER nObserved;
         INTEGER nBEG_Month;
         INTEGER nEND_Month;
         INTEGER nBEG_Day;
         INTEGER nEND_Day;
         INTEGER nBEG_DAY_Occurance;
         INTEGER nEND_DAY_Occurance;
         }
    
    DEFINE_TYPE     //_sGet_ClkMgr
    
    STRUCTURE _sGet_ClkMgr
         {
         INTEGER nReSyncPeriod;
         INTEGER nIs_DST_On;
         CHAR cTimeZone[9];
         CHAR cDST_StartRule[128];
         CHAR cDST_EndRule[128];
         CLKMGR_TIMESERVER_STRUCT sActiveTimeServer;
         CLKMGR_TIMEOFFSET_STRUCT sOffset;
         CLKMGR_TIMESERVER_STRUCT sTimeServers[4];
         }
    
    DEFINE_TYPE     //CLKMGR_TIMEOFFSET_STRUCT FROM NETLINX.AXI FOR REFERNCE ONLY
    
    (*    
    STRUCTURE CLKMGR_TIMEOFFSET_STRUCT
    
         {
         INTEGER     HOURS;
         INTEGER     MINUTES;
         INTEGER     SECONDS;
         }
    *)
    
    DEFINE_VARIABLE //STRUCTURES
    
    PERSISTENT _sMyTime sMyTime;
    VOLATILE   _sMyDST sMyDST;
    VOLATILE   _sGet_ClkMgr sGet_ClkMgr;//just holds the get queries
    
    VOLATILE INTEGER nTimeWait_Force = 1;
    VOLATILE INTEGER nTime_DeBug = 1;
    
    DEFINE_VARIABLE //TIMELINE
    
    VOLATILE LONG nTL_TIME_RT[] = {250};
    
    DEFINE_FUNCTION LONG fnConv_24HrTimeToSeconds(CHAR iTime[])
    
         {
         RETURN ((((ATOI(REMOVE_STRING(iTime,"':'",1)) * 60) + ATOI(REMOVE_STRING(iTime,"':'",1))) * 60) + ATOI(iTime));
         }
    
    DEFINE_FUNCTION CHAR[8] fnConv_SecondsTo24HrTime(LONG iTimeInSeconds)
    
         {
         RETURN   "right_string("'0',ITOA(iTimeInSeconds / 3600)",2),':',
                right_string("'0',ITOA((iTimeInSeconds % 3600) / 60)",2),':',
                 right_string("'0',ITOA((iTimeInSeconds % 3600) % 60)",2)";
         }
    
    DEFINE_FUNCTION CHAR[30] fnDo_24hrTimeAddition(CHAR iLDate[],CHAR i24HourTime[],LONG iSecondsAdded)     
    
         {
         STACK_VAR INTEGER nYear;       //change to all stack_var's
         STACK_VAR INTEGER nMonth;
         STACK_VAR INTEGER nLeap;
         STACK_VAR CHAR c24HrTime[8];
         STACK_VAR CHAR cNewDate[10];
         STACK_VAR LONG nDay;
         STACK_VAR LONG nDaysOver;
         STACK_VAR LONG nSecondsRemain;
         STACK_VAR LONG nTotalTimeSeconds;
    
         nTotalTimeSeconds = fnConv_24HrTimeToSeconds(i24HourTime) + iSecondsAdded;
         nMonth = atoi("REMOVE_STRING(iLDATE,"'/'",1)");
         nDay   = atoi("REMOVE_STRING(iLDATE,"'/'",1)");
         nYear  = atoi("iLDATE");
    
         if(nTotalTimeSeconds > NUM_SECONDS_DAY)
          {
          STACK_VAR INTEGER i;
    
          nDaysOver = nTotalTimeSeconds / NUM_SECONDS_DAY;
          nSecondsRemain = nTotalTimeSeconds % NUM_SECONDS_DAY;
          if(!(nYear % 4))//http://en.wikipedia.org/wiki/Leap_year
               {
               nLeap = 1;
               if(!(nYear % 100))//double check these rules?????? 
                {
                if(nYear % 400)
                 {
                 nLeap = 0;
                 }
                }
               }
          if(nLeap)
               {
               if((nDaysOver + nDay) > DAYS_LEAPMONTHs[nMonth])
                {
                nDaysOver = nDaysOver - (DAYS_LEAPMONTHs[nMonth] - nDay);
                nMonth++;
    
                for(nMonth = nMonth; nDaysOver > DAYS_LEAPMONTHs[nMonth]; nMonth++)
                 {
                 nDaysOver = (nDaysOver - DAYS_LEAPMONTHs[nMonth]);
                 if(nMonth == 12)
                      {
                      nMonth = 1;
                      nYear ++;
                      }
                 }
                nDay = nDaysOver;
                }
               else
                {
                nDay = nDaysOver + nDay;
                }
               }
          else
               {
               if((nDaysOver + nDay) > DAYS_MONTHs[nMonth])
                {
                nDaysOver = nDaysOver - (DAYS_MONTHs[nMonth] - nDay);
                nMonth++;
    
               for(nMonth = nMonth; nDaysOver > DAYS_MONTHs[nMonth]; nMonth++)
                 {
                 nDaysOver = (nDaysOver - DAYS_MONTHs[nMonth]);
                 if(nMonth == 12)
                      {
                      nMonth = 1;
                      nYear ++;
                      }
                 }
                nDay = nDaysOver;
                }
               else
                {
                nDay = nDaysOver + nDay;
                }
               }
          c24HrTime = fnConv_SecondsTo24HrTime(nSecondsRemain); 
          }
         else
          {
          c24HrTime = fnConv_SecondsTo24HrTime(nTotalTimeSeconds);
          }
         cNewDate = "right_string("'0',ITOA(nMonth)",2),'/',right_string("'0',ITOA(nDay)",2),'/',right_string("'0',ITOA(nYear)",2)";
    
         RETURN "DAY_OF_WEEK_ABREV[TYPE_CAST(DAY_OF_WEEK(cNewDate))],', ',itoa(nDay),' ',MONTH_OF_YR_ABREV[nMonth],' ',itoa(nYear),' ',c24HrTime";
         }
    
    DEFINE_FUNCTION fnFB_Time(INTEGER iUI_Indx,INTEGER iEvent) //<< SET VT ADDRESS CHANNELS  CHANGE SETTING PER JOB **********************>>>
    
         {
         SWITCH(iEvent)
          {
          CASE FB_TIME_TIME:
               {
               //panels can handle time on their own 
               }
          CASE FB_TIME_SUNRISE:
               {
               fnFB_TimeSend_VT(iUI_Indx,1,"'Sunrise at ',itoa(sMyTime.nSunriseHour),':',itoa(sMyTime.nSunriseMinute),' AM'");
               }
          CASE FB_TIME_SUNSET:
               {
               fnFB_TimeSend_VT(iUI_Indx,2,"'Sunset at ',itoa(sMyTime.nSunset12Hour),':',itoa(sMyTime.nSunsetMinute),' PM'");
               }
          CASE FB_TIME_DST:
               {
               if(sMyDST.nDST_Active)
                {
                //fnFB_TimeSend_VT(iUI_Indx,??,"'DST: IN EFFECT'"); 
                }
               else
                {
                //fnFB_TimeSend_VT(iUI_Indx,??,"'DST: NOT IN EFFECT'"); 
                }
               }
          CASE FB_TIME_HOLIDAY:
               {
               fnFB_TimeSend_VT(iUI_Indx,4,"sMyTime.cHoliday");
               }
          }
         }
    
    DEFINE_FUNCTION fnFB_TimeSend_VT(INTEGER iUI_Indx,INTEGER iCHAN,CHAR iStrMSG[500]) 
    
         {
         STACK_VAR INTEGER n;
         STACK_VAR INTEGER nTPCount; 
         STACK_VAR INTEGER nLoopStart; 
    
         if(iUI_Indx)
          {
          nTPCount = iUI_Indx;
          nLoopStart = iUI_Indx;
          }
         else
          {
          nTPCount = NUM_UIs_IN_SYSTEM;
          nLoopStart = 1;
          }
    
         //fnTime_DeBug("'SEND_VT: ',iStrMSG,' :DEBUG <',itoa(__LINE__),'>'");
    
         for(n = nLoopStart; n <= nTPCount; n++)
          {
          SWITCH(nUI_TypeArry[n])
               {
               CASE UI_TYPE_G4:
               CASE UI_TYPE_R4:
               CASE UI_TYPE_MIO_DMS:
               (* no need for uni strings!!!
                {
                STACK_VAR WIDECHAR cSTRING1[500];
                STACK_VAR CHAR cSTRING2[500];
    
                cSTRING1 = WC_DECODE(iStrMSG,WC_FORMAT_UTF8,1); 
                cSTRING2 = WC_ENCODE(cSTRING1,WC_FORMAT_TP,1);
                //fnTime_DeBug("'^UNI- TP INDX: ',itoa(n),', CHNL: ',itoa(iCHAN),' :DEBUG <',ITOA(__LINE__),'>'");
                SEND_COMMAND dvUI_Arry[n], "'^UNI-',itoa(iCHAN),',0,',cSTRING2"; 
                }
               *)
               CASE UI_TYPE_G3:
               CASE UI_TYPE_iPHONE://uni not supported yet        
               CASE UI_TYPE_iPAD:        
               CASE UI_TYPE_iTOUCH:    
                {
                //fnTime_DeBug("'^TXT- TP INDX: ',itoa(n),', CHNL: ',itoa(iCHAN),' :DEBUG <',ITOA(__LINE__),'>'");
                SEND_COMMAND dvUI_Arry[n], "'TEXT',itoa(iCHAN),'-',iStrMSG"; 
                }
               CASE UI_TYPE_METKP:
               CASE UI_TYPE_UNKNOWN:
               CASE UI_TYPE_VIRTUAL:
                {
                //DO NOTHING
                }
               }
          }
    
         RETURN;
         }
    
    DEFINE_FUNCTION fnTime_DeBug(CHAR iStr[])
    
         {
         if(nTime_DeBug)
          {
          STACK_VAR CHAR cCopyStr[1024];
          STACK_VAR INTEGER nLineCount;
    
          cCopyStr = iStr;
    
          nLineCount ++;
          WHILE(length_string(cCopyStr) > 100)
               {
               SEND_STRING 0,"'TIME DEBUG (',itoa(nLineCount),'): ',get_buffer_string(cCopyStr,100)";
               nLineCount ++;
               }
          if(length_string(cCopyStr))
               {
               SEND_STRING 0,"'TIME DEBUG (',itoa(nLineCount),'): ',cCopyStr";
               }
          }
    
         RETURN;
         } 
    
    DEFINE_FUNCTION fnGet_DST_State() 
    
         {//don't care about the hour of day it actually occurs!
         fnTime_DeBug("'fnCheckDST_Rule(): Checking DST State :DEBUG <',ITOA(__LINE__),'>'");
         SELECT
          {
          ACTIVE(sMyTime.nMonth < DST_BEGIN_MONTH || sMyTime.nMonth > DST_END_MONTH):
               {//quick elimination
               sMyDST.nDST_Active = 0;
               }
          ACTIVE(sMyTime.nMonth > DST_BEGIN_MONTH && sMyTime.nMonth < DST_END_MONTH):
               {//quick elimination
               sMyDST.nDST_Active = 1;
               }
          ACTIVE(sMyTime.nMonth == DST_BEGIN_MONTH): 
               {
               if(sMyTime.nDayOccurance[sMyDST.nBEG_Day] >= sMyDST.nBEG_DAY_Occurance)
                {
                sMyDST.nDST_Active = 1;
                }
               else
                {
                sMyDST.nDST_Active = 0;
                }
               }
          ACTIVE(sMyTime.nMonth == DST_END_MONTH): 
               {
               if(sMyTime.nDayOccurance[sMyDST.nEND_Day] >= sMyDST.nEND_DAY_Occurance)
                {
                sMyDST.nDST_Active = 0;
                }
               else
                {
                sMyDST.nDST_Active = 1;
                }
               }
          ACTIVE(TRUE):
               {
               fnTime_DeBug("'fnCheckDST_Rule(): DST Check Error! :DEBUG <',ITOA(__LINE__),'>'");
    
               RETURN;
               }
          } 
         if(sMyDST.nDST_Active)
          {
          fnTime_DeBug("'fnCheckDST_Rule(): DST is Active! :DEBUG <',ITOA(__LINE__),'>'");
          }
         else
          {
          fnTime_DeBug("'fnCheckDST_Rule(): DST is In-Active! :DEBUG <',ITOA(__LINE__),'>'");
          }
    
         fnFB_Time(UI_UPDATE_ACTIVE,FB_TIME_DST);
    
         RETURN;
         }
    
    DEFINE_FUNCTION fnGet_AstroClock()
    
         {
         LOCAL_VAR SINTEGER nTimeMngr_Result;
    
         fnTime_DeBug("'fnAstro_Clock(): Running ArtoClock! :DEBUG <',ITOA(__LINE__),'>'");
    
         sGet_ClkMgr.cTimeZone = CLKMGR_GET_TIMEZONE();
         sGet_ClkMgr.nReSyncPeriod = CLKMGR_GET_RESYNC_PERIOD();
         sGet_ClkMgr.nIs_DST_On = CLKMGR_IS_DAYLIGHTSAVINGS_ON();// this only return whether it's observed or not not if it's active, very stupid!
         sGet_ClkMgr.cDST_EndRule = CLKMGR_GET_END_DAYLIGHTSAVINGS_RULE();
         sGet_ClkMgr.cDST_StartRule = CLKMGR_GET_START_DAYLIGHTSAVINGS_RULE();
         CLKMGR_GET_TIMESERVERS(sGet_ClkMgr.sTimeServers);
         CLKMGR_GET_DAYLIGHTSAVINGS_OFFSET(sGet_ClkMgr.sOffset);
         CLKMGR_GET_ACTIVE_TIMESERVER(sGet_ClkMgr.sActiveTimeServer);
    
         if(sMyDST.nObserved)
          {
          fnGet_DST_State();
          }
    
         //SINTEGER ASTRO_CLOCK(DOUBLE Longitude,DOUBLE Latitude,DOUBLE HoursFromGMT,CHAR[] Date,CHAR[] Sunrise,CHAR[] Sunset)
    
         if(sMyDST.nDST_Active)//
          {
          nTimeMngr_Result = ASTRO_CLOCK(CLKMGR_LAT,CLKMGR_LONG,CLKMGR_OFFSET_GMT_EDT,LDATE,sMyTime.cSunrise,sMyTime.cSunset);
          }
         else
          {
          nTimeMngr_Result = ASTRO_CLOCK(CLKMGR_LAT,CLKMGR_LONG,CLKMGR_OFFSET_GMT_EST,LDATE,sMyTime.cSunrise,sMyTime.cSunset);
          }
    
         fnTime_DeBug("'fnAstro_Clock(): Sunset Time = ',sMyTime.cSunset,' :DEBUG <',ITOA(__LINE__),'>'");
         sMyTime.nSunset24Hour = atoi(left_string(sMyTime.cSunset,2));
         sMyTime.nSunsetMinute = atoi(mid_string(sMyTime.cSunset,4,2));
         sMyTime.nSunsetSecond = atoi(right_string(sMyTime.cSunset,2));
         sMyTime.nSunsetTime   =  ((((sMyTime.nSunset24Hour * 60) + sMyTime.nSunsetMinute) * 60) + sMyTime.nSunsetSecond);
         sMyTime.nSunSetTimeMinutes = TYPE_CAST(sMyTime.nSunSetTime / 60);
         if(sMyTime.nSunset24Hour >= 12)
              {
          sMyTime.nSunset12Hour = sMyTime.nSunset24Hour - 12;
          }
         else//yeah, I know!
          {
          sMyTime.nSunset12Hour = sMyTime.nSunset24Hour;
          }
         fnTime_DeBug("'fnAstro_Clock(): Sunrise Time = ',sMyTime.cSunrise,' :DEBUG <',ITOA(__LINE__),'>'");
         sMyTime.nSunriseHour   = atoi(left_string(sMyTime.cSunrise,2));
         sMyTime.nSunriseMinute = atoi(mid_string(sMyTime.cSunrise,4,2));
         sMyTime.nSunriseSecond = atoi(right_string(sMyTime.cSunrise,2));
         sMyTime.nSunriseTime   = ((((sMyTime.nSunriseHour * 60) + sMyTime.nSunriseMinute) * 60) + sMyTime.nSunriseSecond);
         sMyTime.nSunRiseTimeMinutes = TYPE_CAST(sMyTime.nSunriseTime / 60);
         fnFB_Time(UI_UPDATE_ACTIVE,FB_TIME_SUNRISE);
         fnFB_Time(UI_UPDATE_ACTIVE,FB_TIME_SUNSET);
         }
    
    DEFINE_FUNCTION fnGet_DayOccurrences()
    
         {
         if(sMyTime.nDayOfWeek && sMyTime.nDayOfMonth)
          {
          STACK_VAR INTEGER i;
          STACK_VAR INTEGER nD_O_W;
    
          sMyTime.nWeekOfMonth = sMyTime.nDayOfMonth / 7;
          sMyTime.nPlusDays  = sMyTime.nDayOfMonth % 7;
    
          for(i = 1; i <= 7; i ++)//first erase stored values 
               {
               sMyTime.nDayOccurance[i] = 0;
               }
    
          nD_O_W = sMyTime.nDayOfWeek;
          sMyTime.nDayOccurance[nD_O_W] ++;
    
          for(i = sMyTime.nDayOfMonth; i > 1; i --)
               {
               if(nD_O_W > 1) 
                {
                nD_O_W -- ;
                }            
               else 
                {
                nD_O_W = 7;
                }
               sMyTime.nDayOccurance[nD_O_W] ++;
               }
          fnTime_DeBug("'fnFindDayOccurences(): The First Day of the Month= ',DAY_OF_WEEK_ABREV[nD_O_W],' (',itoa(nD_O_W),') :DEBUG <',ITOA(__LINE__),'>'");
          fnTime_DeBug("'fnFindDayOccurences(): Weeks= ',itoa(sMyTime.nWeekOfMonth),', Days= ',itoa(sMyTime.nPlusDays),
                 ', Day= ',itoa((sMyTime.nWeekOfMonth * 7) + sMyTime.nPlusDays),' of ',MONTH_OF_YR_ABREV[sMyTime.nMonth],' :DEBUG <',ITOA(__LINE__),'>'");
    
          }
         else
          {
          fnTime_DeBug("'fnFindDayOccurences(): Error! No Value in sMyTime.nDayOfWeek or sMyTime.nDayOfMonth :DEBUG <',ITOA(__LINE__),'>'");
          }
         }
    
    DEFINE_FUNCTION fnGet_Holiday()
    
         {
         sMyTime.cHoliday = '';
    
         SWITCH(sMyTime.nMonth)
          {
          CASE JANUARY:
               {
               SELECT
                {
                ACTIVE(sMyTime.nDayOfMonth == 1):
                 {
                 sMyTime.cHoliday = 'New Years Day';
                 }
                ACTIVE(sMyTime.nDayOccurance[MONDAY] == THIRD && sMyTime.nDayOfWeek == MONDAY):
                 {
                 sMyTime.cHoliday = "'M.L.King',39,'s Birthday'";
                 }
                }
               }
          CASE FEBRUARY:
               {
               SELECT
                {
                ACTIVE(sMyTime.nDayOfMonth == 2):
                 {
                 sMyTime.cHoliday = 'Ground Hog Day';
                 }
                ACTIVE(sMyTime.nDayOfMonth == 14):
                 {
                 sMyTime.cHoliday = "'Valentine',39,'s Day'";
                 }
                ACTIVE(sMyTime.nDayOfMonth == 22):
                 {
                 sMyTime.cHoliday = "'G. Washington',39,'s Birthday'";
                 }
                ACTIVE(sMyTime.nDayOccurance[MONDAY] == THIRD && sMyTime.nDayOfWeek == MONDAY):
                 {
                 sMyTime.cHoliday = "'President',39,'s Day'";
                 }
                }
               }
          CASE MARCH:
               {
               SELECT
                {
                ACTIVE(sMyTime.nDayOfMonth == 9):
                 {
                 sMyTime.cHoliday = "'Employee Appreciation Day'";
                 }
                ACTIVE(sMyTime.nDayOfMonth >= 10 && sMyTime.nDayOfMonth < 14):
                 {
                 sMyTime.cHoliday = "'Aniversary in ',itoa(14-sMyTime.nDayOfMonth),' days'";
                 }
                ACTIVE(sMyTime.nDayOfMonth >= 10 && sMyTime.nDayOfMonth < 14):
                 {
                 STACK_VAR INTEGER nYears;
    
                 nYears = sMyTime.nYear - 1984;
    
                 SWITCH(nYears)
                      {
                      CASE 1:{sMyTime.cHoliday = "'Jack & Jill''s ',itoa(nYears),'st Aniversary!'";}
                      CASE 2:{sMyTime.cHoliday = "'Jack & Jill''s ',itoa(nYears),'nd Aniversary!'";}
                      CASE 3:{sMyTime.cHoliday = "'Jack & Jill''s ',itoa(nYears),'rd Aniversary!'";}
                      DEFAULT:
                       {
                       sMyTime.cHoliday = "'Jack & Jill''s ',itoa(nYears),'th Aniversary!'";
                       }
                      }
                 }
                ACTIVE(sMyTime.nDayOfMonth == 17):
                 {
                 sMyTime.cHoliday = "'ST Patrick',39,'s Day'";
                 }
                }
               //EASTER?
               }
          CASE APRIL:
               {
               SELECT
                {
                ACTIVE(sMyTime.nDayOfMonth == 1):
                 {
                 sMyTime.cHoliday = "'April Fool',39,'s Day'";
                 }
                ACTIVE(sMyTime.nDayOfMonth == 14):
                 {
                 sMyTime.cHoliday = "'Valentine',39,'s Day'";
                 }
                ACTIVE(sMyTime.nDayOccurance[MONDAY] == THIRD && sMyTime.nDayOfWeek == MONDAY):
                 {
                 sMyTime.cHoliday = "'Patriot',39,'s Day'";
                 }
                }
               }
          CASE MAY:
               {
               SELECT
                {
                ACTIVE(sMyTime.nDayOfMonth >= 1 && sMyTime.nDayOfMonth < 5):
                 {
                 sMyTime.cHoliday = "'Jill''s birthday in ',itoa(5-sMyTime.nDayOfMonth),' days'";
                 }
                ACTIVE(sMyTime.nDayOfMonth >= 10 && sMyTime.nDayOfMonth < 14):
                 {
                 sMyTime.cHoliday = "'Jill''s birthday!'";
                 }
                ACTIVE(sMyTime.nDayOccurance[SUNDAY] == SECOND && sMyTime.nDayOfWeek == SUNDAY):
                 {
                 sMyTime.cHoliday = "'Mother''s Day'";
                 }
                ACTIVE(sMyTime.nDayOccurance[SATURDAY] == THIRD && sMyTime.nDayOfWeek == SATURDAY):
                 {
                 sMyTime.cHoliday = "'Armed Forces Day'";
                 }
                ACTIVE(sMyTime.nDayOccurance[MONDAY] == FOURTH && sMyTime.nDayOfMonth > 24 && sMyTime.nDayOfWeek == MONDAY):
                 {
                 sMyTime.cHoliday = 'Memorial Day';
                 }
                ACTIVE(sMyTime.nDayOccurance[MONDAY] == FIFTH && sMyTime.nDayOfWeek == MONDAY):
                 {
                 sMyTime.cHoliday = 'Memorial Day';
                 }
                }
               }
          CASE JUNE:
               {
               SELECT
                {
                ACTIVE(sMyTime.nDayOfMonth == 14):
                 {
                 sMyTime.cHoliday = "'Flag Day'";
                 }
                ACTIVE(sMyTime.nDayOfMonth == 21):
                 {
                 sMyTime.cHoliday = "'Summer Solstice'";
                 }
                ACTIVE(sMyTime.nDayOccurance[SUNDAY] == THIRD && sMyTime.nDayOfWeek == SUNDAY):
                 {
                 sMyTime.cHoliday = "'Father',39,'s Day'";
                 } 
                }
               }
          CASE JULY:
               {
               if(sMyTime.nDayOfMonth == 4)
                {
                sMyTime.cHoliday = 'Independence Day';
                }
               }
          CASE AUGUST:
               {
               if(sMyTime.nDayOccurance[SUNDAY] == FIRST && sMyTime.nDayOfWeek == SUNDAY)
                {
                sMyTime.cHoliday = "'Friendship Day'";
                } 
               }
          CASE SEPTEMBER:
               {
               if(sMyTime.nDayOccurance[MONDAY] == FIRST && sMyTime.nDayOfWeek == MONDAY)
                {
                sMyTime.cHoliday = 'Labor Day';
                }
               }
          CASE OCTOBER:
               {
               if(sMyTime.nDayOfMonth == 31)
                {
                sMyTime.cHoliday = 'Halloween';
                }
               }
          CASE NOVEMBER:
               {
               SELECT
                {
                ACTIVE(sMyTime.nDayOfMonth == 1):
                 {
                 sMyTime.cHoliday = "'All Saint',39,'s Day'";
                 }
                ACTIVE(sMyTime.nDayOfMonth == 11):
                 {
                 sMyTime.cHoliday = "'Veteran',39,'s Day'";
                 }
                ACTIVE(sMyTime.nDayOccurance[THURSDAY] == FOURTH && sMyTime.nDayOfWeek == THURSDAY):
                 {
                 sMyTime.cHoliday = 'Thanksgiving Day';
                 }
                }
               }
          CASE DECEMBER:
               {
               SELECT
                {
                ACTIVE(sMyTime.nDayOfMonth >= 1 && sMyTime.nDayOfMonth <= 23):
                 {
                 sMyTime.cHoliday = "ITOA(25 - sMyTime.nDayOfMonth),' days to Christmas'";
                 }
                (*
                ACTIVE(sMyTime.nDayOfMonth == 21):
                 {
                 sMyTime.cHoliday = 'Winter Solstice';
                 }
                *)
                ACTIVE(sMyTime.nDayOfMonth == 24):
                 {
                 sMyTime.cHoliday = 'Christmas Eve';
                 }
                ACTIVE(sMyTime.nDayOfMonth == 25):
                 {
                 sMyTime.cHoliday = 'Christmas Day';
                 }
                ACTIVE(sMyTime.nDayOfMonth == 31):
                 {
                 sMyTime.cHoliday = "'New Year',39,'s Eve'";
                 }
                }
               }
          }
         if(length_string(sMyTime.cHoliday))
          {
          sMyTime.cHoliday = "'"',sMyTime.cHoliday,'"'";
          fnTime_DeBug("'fnFindHolidays(): Today is ',sMyTime.cHoliday,' :DEBUG <',ITOA(__LINE__),'>'");
          }
         else
          {
          fnTime_DeBug("'fnFindHolidays(): Today is not a "supported" holiday! :DEBUG <',ITOA(__LINE__),'>'");
          }
    
         fnFB_Time(UI_UPDATE_ACTIVE,FB_TIME_HOLIDAY);
    
         RETURN;
         }
    
    DEFINE_FUNCTION fnGet_WebDATE_TIME()
    
         {
         sMyTime.cWebCurDateTime = "fnDo_24hrTimeAddition(LDate,TIME,0),' EST'";
         sMyTime.cWebExpDateTime = "fnDo_24hrTimeAddition(LDate,TIME,EXPIRES_IN_SECONDS),' EST'";
    
         RETURN;
         }
    
    DEFINE_FUNCTION fnRun_SunRise_n_Set(INTEGER iDaytime)
    
         {
         //SWITCH(iDaytime)
    //      {
    //      CASE 0:{} //night
    //      CASE 1:{} //day
    //      }
    
         fnMain_SunRise_n_Set(iDaytime);
         }
    
    DEFINE_FUNCTION fnRun_D_O_W_Events(INTEGER iDayOfWeek)   //<<**************  CHANGE SETTING PER JOB **********************************>>>
    
         {
         STACK_VAR INTEGER nCopyDOW;
    
         nCopyDOW = iDayOfWeek;
         //SELECT ACTIVE or SWITCH CASE to do something
         }     
    
    DEFINE_FUNCTION fnRun_D_O_M_Events(INTEGER iDayOfMonth)  //<<**************  CHANGE SETTING PER JOB **********************************>>>
    
         {//uncomment call in fnUpdate_Time()
         STACK_VAR INTEGER nCopyDOM;
    
         nCopyDOM  = iDayOfMonth;
         //SELECT ACTIVE or SWITCH CASE to do something
         }
    
    DEFINE_FUNCTION fnRun_DailyEvents()                      //<<**************  CHANGE SETTING PER JOB **********************************>>>
    
         {
         //fnDailyEmailTest();
         fnGet_Holiday();
         fnGet_AstroClock();
         //SELECT ACTIVE or SWITCH CASE to do something
         fnMain_EveryDay();
         }
    
    DEFINE_FUNCTION fnRun_HourEvents(INTEGER iHour)          //<<**************  CHANGE SETTING PER JOB **********************************>>>
    
         {
         STACK_VAR INTEGER nCopyHour;
    
         nCopyHour  = iHour;
    
         fnMain_EveryHour(nCopyHour);
         //SELECT ACTIVE or SWITCH CASE to do something
         }
    
    DEFINE_FUNCTION fnRun_MinuteEvents(INTEGER iMinute)      //<<**************  CHANGE SETTING PER JOB **********************************>>>
    
         {
         STACK_VAR INTEGER nCopyMinute;
    
         nCopyMinute  = iMinute;
    
         fnMain_EveryMinute(nCopyMinute);
         } 
    
    DEFINE_FUNCTION fnRun_SecondEvents(INTEGER iSecond)      //<<**************  CHANGE SETTING PER JOB **********************************>>>
    
         {
         STACK_VAR INTEGER nCopySecond;
    
         nCopySecond = iSecond;
    
         SELECT//add as needed
          {
          ACTIVE(sMyTime.n24Hour == 23):
               {
               if(sMyTime.nMinute == 59)
                {
                if(nCopySecond == 59)
                 {//used for log entry
                 SEND_STRING 0,"'TIME-[ ',TIME,' ].  End day and begin a new!'";
                 }
                }
               }
          }
         fnGet_WebDATE_TIME();
         fnMain_EverySecond(nCopySecond);
         }
    
    DEFINE_FUNCTION fnRun_StartUpCode() 
    
         {
         LOCAL_VAR CLKMGR_TIMEOFFSET_STRUCT sMyCLKMGR_TIMEOFFSET;
    
         fnTime_DeBug("'fnRunStartUpCode(): Run Start Up! :DEBUG <',ITOA(__LINE__),'>'");
         nTimeWait_Force = 1;
    
         sMyDST.nObserved = DST_OBSERVED;
    
         if(sMyDST.nObserved)
          {
          sMyDST.nOffset    = DST_OFFSET;
          sMyDST.nBEG_Month    = DST_BEGIN_MONTH;
          sMyDST.nEND_Month     = DST_END_MONTH;
          sMyDST.nBEG_Day       = DST_BEGIN_DAY;
          sMyDST.nEND_Day     = DST_END_DAY;
          sMyDST.nBEG_DAY_Occurance = DST_BEGINDAY_OCCURANCE;
          sMyDST.nEND_DAY_Occurance = DST_ENDDAY_OCCURANCE;
          }     
    
         WAIT 300//3000
          {
          fnTime_DeBug("'fnRunStartUpCode(): Run Start Up Wait items! :DEBUG <',ITOA(__LINE__),'>'");
          CLKMGR_SET_CLK_SOURCE(CLKMGR_MODE_NETWORK); // or CLKMGR_MODE_STANDALONE
          CLKMGR_SET_ACTIVE_TIMESERVER (CLKMGR_ACTIVESERVER);
          CLKMGR_SET_TIMEZONE(CLKMGR_TIME_ZONE);
          CLKMGR_SET_DAYLIGHTSAVINGS_MODE(TRUE);//moved to DST function
          CLKMGR_SET_START_DAYLIGHTSAVINGS_RULE("'occurrence:',itoa(DST_BEGINDAY_OCCURANCE),',',itoa(DST_BEGIN_DAY),',',itoa(DST_BEGIN_MONTH),',',DST_BEGIN_TIME" );
          CLKMGR_SET_END_DAYLIGHTSAVINGS_RULE("'occurrence:',itoa(DST_ENDDAY_OCCURANCE),',',itoa(DST_END_DAY),',',itoa(DST_END_MONTH),',',DST_END_TIME" );
          CLKMGR_SET_RESYNC_PERIOD(CLKMGR_RESYNC_PERIOD);
    
          sMyCLKMGR_TIMEOFFSET.HOURS   = CLKMGR_DST_OFFSET_HR;
          sMyCLKMGR_TIMEOFFSET.MINUTES = CLKMGR_DST_OFFSET_MIN;
          sMyCLKMGR_TIMEOFFSET.SECONDS = CLKMGR_DST_OFFSET_SEC;
          CLKMGR_SET_DAYLIGHTSAVINGS_OFFSET(sMyCLKMGR_TIMEOFFSET);
    
          nTimeWait_Force = 2;
          fnUpdate_Time();
          }
         if(!TIMELINE_ACTIVE(TL_TIME))
          {
          TIMELINE_CREATE(TL_TIME,nTL_TIME_RT,1,timeline_absolute,timeline_repeat);
          }
         }
    
    DEFINE_FUNCTION fnUpdate_Time()
    
         {
         if(sMyTime.nSecond != TYPE_CAST(TIME_TO_SECOND(TIME)) || nTimeWait_Force)
          {
          sMyTime.nSecond = TYPE_CAST(TIME_TO_SECOND(TIME));
          sMyTime.c24Time = TIME;
          if(sMyTime.nMinute != TYPE_CAST(TIME_TO_MINUTE(TIME)) || nTimeWait_Force)
               {
               sMyTime.nMinute = TYPE_CAST(TIME_TO_MINUTE(TIME));
               //only 1 minute resolution for sunrise/set, could check every second but why, not that critical.....
               if(sMyTime.nTime_Seconds >= sMyTime.nSunriseTime && sMyTime.nTime_Seconds < sMyTime.nSunsetTime)
                {
                if(!sMyTime.nDaytime)
                 {
                 sMyTime.nDaytime = 1;
                 fnRun_SunRise_n_Set(sMyTime.nDaytime);
                 }
                }
               else 
                {
                if(sMyTime.nDaytime)
                 {
                 sMyTime.nDaytime = 0;
                 fnRun_SunRise_n_Set(sMyTime.nDaytime);
                 }
                }             
               if(sMyTime.n24Hour != TYPE_CAST(TIME_TO_HOUR(TIME)) || nTimeWait_Force)
                {
                sMyTime.n24Hour = TYPE_CAST(TIME_TO_HOUR(TIME));
                if(sMyTime.n24Hour >= 12)
                 {
                 sMyTime.cAMPM = 'PM';
                 sMyTime.nTimePM_flag = 1;
                 if(sMyTime.n24Hour > 12)
                      {
                      sMyTime.n12Hour = sMyTime.n24Hour - 12;
                      }
                 else
                      {
                      sMyTime.n12Hour = sMyTime.n24Hour;
                      }
                 }
                else
                 {
                 sMyTime.cAMPM = 'AM';
                 sMyTime.nTimePM_flag = 0;
                 sMyTime.n12Hour = sMyTime.n24Hour;
                 }
                sMyTime.c12Time = "itoa(sMyTime.n12Hour),':',RIGHT_STRING("'0',itoa(sMyTime.nMinute)",2),':',RIGHT_STRING("'0',itoa(sMyTime.nSecond)",2)";
                if(sMyTime.nDayOfMonth != TYPE_CAST(DATE_TO_DAY(LDATE)) || nTimeWait_Force)
                 {
                 sMyTime.nDayOfMonth = TYPE_CAST(DATE_TO_DAY(LDATE));
                 sMyTime.nDayOfWeek = TYPE_CAST(DAY_OF_WEEK(LDATE));
                 if(sMyTime.nMonth != TYPE_CAST(DATE_TO_MONTH(LDATE)))//no force here.  would erase stored values.
                      {
                      sMyTime.nMonth = TYPE_CAST(DATE_TO_MONTH(LDATE));
                      if(sMyTime.nYear != TYPE_CAST(DATE_TO_YEAR(LDATE)))
                       {
                       sMyTime.nYear = TYPE_CAST(DATE_TO_YEAR(LDATE));
                       }
                      }
                 if(sMyTime.cDay != Day || nTimeWait_Force)
                      {
                      sMyTime.cDay = Day;
                      if (sMyTime.cDate != Date)
                       {
                       sMyTime.cDate = Date;
                       }
                      }
                 fnGet_DayOccurrences();
                 fnRun_D_O_W_Events(sMyTime.nDayOfWeek);
                 fnRun_D_O_M_Events(sMyTime.nDayOfMonth);
                 fnRun_DailyEvents();
                 }
                fnRun_HourEvents(sMyTime.n24Hour);
                }
               else
                {
                sMyTime.c12Time = "itoa(sMyTime.n12Hour),':',RIGHT_STRING("'0',itoa(sMyTime.nMinute)",2),':',RIGHT_STRING("'0',itoa(sMyTime.nSecond)",2)";
                }
               sMyTime.nTime_Minutes = ((sMyTime.n24Hour * 60) + sMyTime.nMinute);
               fnRun_MinuteEvents(sMyTime.nMinute);
               }
          else
               {
               sMyTime.c12Time = "itoa(sMyTime.n12Hour),':',RIGHT_STRING("'0',itoa(sMyTime.nMinute)",2),':',RIGHT_STRING("'0',itoa(sMyTime.nSecond)",2)";
               }
          sMyTime.nTime_Seconds = ((((sMyTime.n24Hour * 60) + sMyTime.nMinute) * 60) + sMyTime.nSecond);
          fnRun_SecondEvents(sMyTime.nSecond);
          fnFB_Time(UI_UPDATE_ACTIVE,FB_TIME_TIME); //IF ANYTHING NEEDS TIME UPDATES
          }
         nTimeWait_Force  = 0;
         }
    
    DEFINE_START    //RUN START UP CODE
    
    
    fnRun_StartUpCode();
    
    DEFINE_EVENT    //TIMELINE_EVENT [TL_TIME]
    
    TIMELINE_EVENT [TL_TIME]//EVERY 250ms
    
         {
         fnUpdate_Time();
         fnMain_250ms_Clock();//put any other timing events in the main in this function
         }
    
    DEFINE_PROGRAM  //CALL fnUpdate_Time() if you don't have a timeline elsewhere to call it every second
    (*
    WAIT 10
         {
         fnUpdate_Time();
         }
    *)
    if(!TIMELINE_ACTIVE(TL_TIME))
         {
         TIMELINE_CREATE(TL_TIME,nTL_TIME_RT,1,timeline_absolute,timeline_repeat);
         }
    
Sign In or Register to comment.