It is currently April 18th, 2024, 10:11 am

[DONE] Need Help for Holidays getting into Calendar

Discuss the use of Lua in Script measures.
EliteLucker
Posts: 46
Joined: June 29th, 2012, 9:06 am

[DONE] Need Help for Holidays getting into Calendar

Post by EliteLucker »

i have some code in c# to calculate the holidays of germany

Code: Select all

using System;
using System.Collections.Generic;
using System.Text;

namespace thorstenhans.Utilities
{
    public class Feiertag : IComparable<Feiertag>
    {
        private bool isFix;
        private DateTime datum;
        private string name;

        public Feiertag(bool isFix, DateTime datum, string name)
        {
            this.IsFix = isFix;
            this.Datum = datum;
            this.Name = name;

        }

        /// <summary>
        /// Beschreibung: 
        /// </summary>
        public string Name
        {
            get
            {
                return name;
            }
            set
            {
                name = value;
            }
        }
	

        /// <summary>
        /// Beschreibung: 
        /// </summary>
        public DateTime Datum
        {
            get
            {
                return datum;
            }
            set
            {
                datum = value;
            }   
        }
	

        /// <summary>
        /// Beschreibung: 
        /// </summary>
        public bool IsFix
        {
            get
            {
                return isFix;
            }
            set
            {
                isFix = value;
            }
        }


        #region IComparable<Feiertag> Member

        public int CompareTo(Feiertag other)
        {
            return this.datum.Date.CompareTo(other.datum.Date);
        }

        #endregion
    }
    public class FeiertagLogic
    {
        private static FeiertagLogic Instance;
        private List<Feiertag> feiertage;
        private int year;

        /// <summary>
        /// Beschreibung: 
        /// </summary>
        public int CurrentYear
        {
            get
            {
                return year;
            }
            set
            {
                year = value;
            }
        }
	
        public static FeiertagLogic GetInstance(int year)
        {
            if (Instance == null || year != Instance.CurrentYear)
            {                
                Instance = new FeiertagLogic(year);
                return Instance;
            }
            return Instance;
        }

        /// <summary>
        /// Beschreibung: Gibt variable Feiertage zurueck
        /// </summary>
        public List<Feiertag> VariableFeiertage
        {
            get
            {
                return feiertage.FindAll(delegate(Feiertag f) { return !f.IsFix; });
            }
            
        }

        public bool isFeiertag(DateTime value)
        {
            return (feiertage.Find(delegate(Feiertag f) { return f.Datum.Date == value.Date; }) != null);
        }

        public Feiertag GetFeiertagName(DateTime value)
        {
            return (feiertage.Find(delegate(Feiertag f) { return f.Datum.Date == value.Date; }));
        }
        /// <summary>
        /// Beschreibung: gibt feste Feiertage zurueck
        /// </summary>
        public List<Feiertag> FesteFeiertage
        {
            get
            {
                return feiertage.FindAll(delegate(Feiertag f) { return f.IsFix; });
            }
        }
	
        private FeiertagLogic (int year)
        {
            this.CurrentYear = year;
            #region fillList
            this.feiertage = new List<Feiertag>();
            this.feiertage.Add(new Feiertag(true, new DateTime(year, 1, 1), "Neujahr"));
            this.feiertage.Add(new Feiertag(true, new DateTime(year, 1, 6), "Heilige Drei Könige"));
            this.feiertage.Add(new Feiertag(true, new DateTime(year, 5, 1), "Tag der Arbeit"));
            this.feiertage.Add(new Feiertag(true, new DateTime(year, 8, 15), "Mariä Himmelfahrt"));
            this.feiertage.Add(new Feiertag(true, new DateTime(year, 10, 3), "Tag der dt. Einheit"));
            this.feiertage.Add(new Feiertag(true, new DateTime(year, 10, 31), "Reformationstag"));
            this.feiertage.Add(new Feiertag(true, new DateTime(year, 11, 1), "Allerheiligen "));
            this.feiertage.Add(new Feiertag(true, new DateTime(year, 12, 25), "1. Weihnachtstag"));
            this.feiertage.Add(new Feiertag(true, new DateTime(year, 12, 26), "2. Weihnachtstag"));
            DateTime osterSonntag = GetOsterSonntag();
            this.feiertage.Add(new Feiertag(false, osterSonntag, "Ostersonntag"));
            this.feiertage.Add(new Feiertag(false, osterSonntag.AddDays(-3), "Gründonnerstag"));
            this.feiertage.Add(new Feiertag(false, osterSonntag.AddDays(-2), "Karfreitag"));
            this.feiertage.Add(new Feiertag(false, osterSonntag.AddDays(1), "Ostermontag"));
            this.feiertage.Add(new Feiertag(false, osterSonntag.AddDays(39), "Christi Himmelfahrt"));
            this.feiertage.Add(new Feiertag(false, osterSonntag.AddDays(49), "Pfingstsonntag"));
            this.feiertage.Add(new Feiertag(false, osterSonntag.AddDays(50), "Pfingstmontag"));
            this.feiertage.Add(new Feiertag(false, osterSonntag.AddDays(60), "Fronleichnam"));


            #endregion
        }

        private DateTime GetOsterSonntag()
        {
           
            int  g,h,c,j,l,i;

            g = year % 19;
            c = this.year / 100;
            h = ((c-(c/4)) - (((8*c)+13)/25) + (19*g) + 15) % 30;
            i = h - (h/28) *(1- (29/(h+1)) * ((21-g)/11));
            j = (year + (year / 4) + i + 2 - c + (c / 4)) % 7;

            l = i - j;
            int month = (int)(3+ ((l+40)/44));
            int day = (int)(l + 28 - 31 * (month / 4));
            
            return new DateTime(year, month, day);

        }
    }
}
i need only the year to get the fix and the variable holidays.

yesterday i saw the LuaCalendar and see that they use holidays.


I will try to calculate them with a lua script and return an array/list of all holidays

the exactly way i dont know at the moment, because the first problem i need to solve is the calculate (convert the above code)


ok i found one :o ok the next weekend is to look at this

http://wiki.rainlendar.info/images/0/04/Holidays.lua
Last edited by EliteLucker on July 7th, 2012, 6:41 pm, edited 2 times in total.
User avatar
MaroonED
Posts: 61
Joined: November 6th, 2011, 4:50 pm
Location: Germany

Re: its is possible to convert existing code to lua script?

Post by MaroonED »

Hi EliteLucker,

I've attached a lua script that I wrote some time back, by translating a javascript file. It's for the Swedish calendar, but most of the comments are in English. At least I hope it will get you going.

Regards,
MaroonED
You do not have the required permissions to view the files attached to this post.
EliteLucker
Posts: 46
Joined: June 29th, 2012, 9:06 am

Re: its is possible to convert existing code to lua script?

Post by EliteLucker »

Many Thanks i will look at the weekend i bit more intensive in your script.

on the first look i found them good, with enough comments that should helps me


Edit:
Maybe i take a look under the week (after works), ;)
EliteLucker
Posts: 46
Joined: June 29th, 2012, 9:06 am

Re: its is possible to convert existing code to lua script?

Post by EliteLucker »

I had no rest, and had to try it :oops:

Lua (first version)

Code: Select all

PROPERTIES = {VariablePrefix = 'Holidays.';
			  Year = 0;}
			  
function Initialize()
	sVariablePrefix = PROPERTIES.VariablePrefix
	iYear = tonumber(PROPERTIES.Year)
	
	iEasterSunday = tonumber(SKIN:GetVariable(sVariablePrefix.."EasterSunday"))	
	iOldYear = tonumber(SKIN:GetVariable("Statistic.Last.Year"))
	
	if (iOldYear ~= iYear) then
		setFixHolidays()
		calculateEasterSunday(iYear);
		setVariableHolidays(-3)
		setVariableHolidays(-2)
		setVariableHolidays(1)
		setVariableHolidays(39)
		setVariableHolidays(49)
		setVariableHolidays(50)
		setVariableHolidays(60)
	end
end

function calculateEasterSunday()
	SKIN:Bang("!WriteKeyValue Variables Statistic.Last.Year "..iYear.." #RootConfigPath#\DynamicVariable.inc")
	
	golden = ((iYear % 19) + 1)
	c = (math.floor(iYear / 400) + math.floor(8 * (math.floor(iYear / 100) + 11) / 25) - math.floor(iYear / 100))
	s = ((11 * golden + c) % 30)
	
	if (0 > s) then
		s = (s + 30)
	end
	
	pfm = os.time{year = iYear, month = 4, day = 19} - s * 24 * 60 * 60
	
	if (pfm == os.time{year = iYear, month = 4, day = 19}) then
		pfm = (pfm - 24 * 60 * 60)
	elseif ((pfm == os.time{year = iYear, month = 4, day = 18}) and (11 < golden)) then 
		pfm = (pfm - 24 * 60 * 60)
	end
	
	sunday = (pfm + (7 - tonumber(os.date("%w", pfm))) * 24 * 60 * 60)	
	
	iEasterSunday = os.time{year = iYear, month = tonumber(os.date("%m", sunday)), day = tonumber(os.date("%d", sunday))}
	
	SKIN:Bang("!SetVariable "..sVariablePrefix.."EasterSunday "..iEasterSunday)
	SKIN:Bang("!WriteKeyValue Variables "..sVariablePrefix.."EasterSunday "..iEasterSunday.." #RootConfigPath#\DynamicVariable.inc")
	
	return 'Success!' 
end

function setVariableHolidays(days)
	if (-3 == days) then
		SKIN:Bang("!WriteKeyValue Variables "..sVariablePrefix.."HolyThursday "..(iEasterSunday + days * 24 * 60 * 60).." #RootConfigPath#\DynamicVariable.inc")
	elseif (-2 == days) then
		SKIN:Bang("!WriteKeyValue Variables "..sVariablePrefix.."GoodFriday "..(iEasterSunday + days * 24 * 60 * 60).." #RootConfigPath#\DynamicVariable.inc")
	elseif (1 == days) then
		SKIN:Bang("!WriteKeyValue Variables "..sVariablePrefix.."EasterMonday "..(iEasterSunday + days * 24 * 60 * 60).." #RootConfigPath#\DynamicVariable.inc")
	elseif (39 == days) then
		SKIN:Bang("!WriteKeyValue Variables "..sVariablePrefix.."AscensionDay "..(iEasterSunday + days * 24 * 60 * 60).." #RootConfigPath#\DynamicVariable.inc")
	elseif (49 == days) then
		SKIN:Bang("!WriteKeyValue Variables "..sVariablePrefix.."Whitsunday "..(iEasterSunday + days * 24 * 60 * 60).." #RootConfigPath#\DynamicVariable.inc")
	elseif (50 == days) then
		SKIN:Bang("!WriteKeyValue Variables "..sVariablePrefix.."Whitmonday "..(iEasterSunday + days * 24 * 60 * 60).." #RootConfigPath#\DynamicVariable.inc")
	elseif (60 == days) then
		SKIN:Bang("!WriteKeyValue Variables "..sVariablePrefix.."CorpusChristi "..(iEasterSunday + days * 24 * 60 * 60).." #RootConfigPath#\DynamicVariable.inc")
	end	
	
	return 'Success!' 
end

function setFixHolidays()
	SKIN:Bang("!WriteKeyValue Variables "..sVariablePrefix.."NewYear "..os.time{year = iYear, month = 1, day = 1}.." #RootConfigPath#\DynamicVariable.inc")
	SKIN:Bang("!WriteKeyValue Variables "..sVariablePrefix.."Epiphany "..os.time{year = iYear, month = 1, day = 6}.." #RootConfigPath#\DynamicVariable.inc")
	SKIN:Bang("!WriteKeyValue Variables "..sVariablePrefix.."MayDay "..os.time{year = iYear, month = 5, day = 1}.." #RootConfigPath#\DynamicVariable.inc")
	SKIN:Bang("!WriteKeyValue Variables "..sVariablePrefix.."AssumptionDay "..os.time{year = iYear, month = 8, day = 15}.." #RootConfigPath#\DynamicVariable.inc")
	SKIN:Bang("!WriteKeyValue Variables "..sVariablePrefix.."GermanUnificationDay "..os.time{year = iYear, month = 10, day = 3}.." #RootConfigPath#\DynamicVariable.inc")
	SKIN:Bang("!WriteKeyValue Variables "..sVariablePrefix.."ReformationDay "..os.time{year = iYear, month = 10, day = 31}.." #RootConfigPath#\DynamicVariable.inc")
	SKIN:Bang("!WriteKeyValue Variables "..sVariablePrefix.."AllSaintsDay "..os.time{year = iYear, month = 11, day = 1}.." #RootConfigPath#\DynamicVariable.inc")
	SKIN:Bang("!WriteKeyValue Variables "..sVariablePrefix.."ChristmasEve "..os.time{year = iYear, month = 12, day = 24}.." #RootConfigPath#\DynamicVariable.inc")
	SKIN:Bang("!WriteKeyValue Variables "..sVariablePrefix.."FirstChristmasDay "..os.time{year = iYear, month = 12, day = 25}.." #RootConfigPath#\DynamicVariable.inc")
	SKIN:Bang("!WriteKeyValue Variables "..sVariablePrefix.."SecondChristmasDay "..os.time{year = iYear, month = 12, day = 26}.." #RootConfigPath#\DynamicVariable.inc")
	SKIN:Bang("!WriteKeyValue Variables "..sVariablePrefix.."NewYearsEve "..os.time{year = iYear, month = 12, day = 31}.." #RootConfigPath#\DynamicVariable.inc")

	return 'Success!' 
end
the ini for test it

Code: Select all

[Variables]
@include3=#RootConfigPath#\DynamicVariable.inc

[MeasureHolidays]
Measure						=	Script
ScriptFile					=	Holidays.lua
Year						=	2012
UpdateDivider				=	86.400

[TextLabelDay1]
Meter						=	String
and at least the dynamicVariable

Code: Select all

Statistic.Last.Year				=2012

Holidays.NewYear				=1325415600
Holidays.Epiphany				=1325847600
Holidays.MayDay					=1335866400
Holidays.AssumptionDay			=1345024800
Holidays.GermanUnificationDay	=1349258400
Holidays.ReformationDay			=1351681200
Holidays.AllSaintsDay			=1351767600
Holidays.ChristmasEve			=1356346800
Holidays.FirstChristmasDay		=1356433200
Holidays.SecondChristmasDay		=1356519600
Holidays.NewYearsEve			=1356951600
Holidays.EasterSunday			=1333879200
Holidays.HolyThursday			=1333620000
Holidays.GoodFriday				=1333706400
Holidays.EasterMonday			=1333965600
Holidays.AscensionDay			=1337248800
Holidays.Whitsunday				=1338112800
Holidays.Whitmonday				=1338199200
Holidays.CorpusChristi			=1339063200


it works fine for the beginning, when i calculated the holidays for this year the script do on the next run nothing (the Holidays are all saved ;) )

the next ist to compare the date of the calendar with the saved holidays :( (that will be the hard work now)

i saved them as number to work later with e.g.

Code: Select all

os.date("%m", Holidays.ChristmasEve)
EliteLucker
Posts: 46
Joined: June 29th, 2012, 9:06 am

Re: Need Help for Holidays getting into Calendar

Post by EliteLucker »

So i have combined the working calendar with the creation of my last post

now i need help to compar the dates (calendar days with array/list of holidays) and then change the style (color) of the text (the color change i have)

i attached all files and hope someone can give me good tips for this problem

MaroonED your code has inspired me a lot


Oh and i need something like a tooltip for the holidays (the variable with the name wille be added then)
You do not have the required permissions to view the files attached to this post.
EliteLucker
Posts: 46
Joined: June 29th, 2012, 9:06 am

Re: Need Help for Holidays getting into Calendar

Post by EliteLucker »

Ok i dont find my failure

Code: Select all

for a = 1, iRange  do
			if ('Week' == sRange) then
				b = (iToday + ((a - 1) - iWeekDay))
			elseif ('Month' == sRange) then
				b = (a - iStartDay)
			end
			if (b < 1) then
				b = (b + tPrevMonth[iMonth])
				
				for e = 1, 19 do
					if (os.date("%Y.%m.%d", os.time({year = iYear, month = iMonth, day = b})) == os.date("%Y.%m.%d", tHoliday[e])) then
						SKIN:Bang('!SetOption "'..sMeterPrefix..'Day'..a..'" "MeterStyle" "'..tHolidayStyles[a]..iEDaysColor..'"')
					else
						SKIN:Bang('!SetOption "'..sMeterPrefix..'Day'..a..'" "MeterStyle" "'..tMeterStyles[a]..iEDaysColor..'"')
					end				
				end				
			elseif (b > tCurrMonth[iMonth]) then
				b = (b - tCurrMonth[iMonth])
				
				for e = 1, 19 do
					if (os.date("%Y.%m.%d", os.time({year = iYear, month = iMonth, day = b})) == os.date("%Y.%m.%d", tHoliday[e])) then
						SKIN:Bang('!SetOption "'..sMeterPrefix..'Day'..a..'" "MeterStyle" "'..tHolidayStyles[a]..iEDaysColor..'"')
					else
						SKIN:Bang('!SetOption "'..sMeterPrefix..'Day'..a..'" "MeterStyle" "'..tMeterStyles[a]..iEDaysColor..'"')
					end	
				end
			else
				for e = 1, 19 do
					if (os.date("%Y.%m.%d", os.time({year = iYear, month = iMonth, day = b})) == os.date("%Y.%m.%d", tHoliday[e])) then
						SKIN:Bang('!SetOption "'..sMeterPrefix..'Day'..a..'" "MeterStyle" "'..tHolidayStyles[a]..'"')
					else
						SKIN:Bang('!SetOption "'..sMeterPrefix..'Day'..a..'" "MeterStyle" "'..tMeterStyles[a]..'"')
					end	
				end					
			end
if (os.date("%Y.%m.%d", os.time({year = iYear, month = iMonth, day = b})) == os.date("%Y.%m.%d", tHoliday[e])) then
I think that i m check here some wrong things

the whole script in attachment
You do not have the required permissions to view the files attached to this post.
EliteLucker
Posts: 46
Joined: June 29th, 2012, 9:06 am

Re: Need Help for Holidays getting into Calendar

Post by EliteLucker »

I think i got it runable

:thumbup: :great: :bow: :rolmfao:

Ihope the newer version of enigma (with the fixed calendar) is coming soon (i will wait patiently) so that it could perfect :)

here is the script that seems to run (the ini files are should the same) only remove a meter)
You do not have the required permissions to view the files attached to this post.
User avatar
smurfier
Moderator
Posts: 1931
Joined: January 29th, 2010, 1:43 am
Location: Willmar, MN

Re: [DONE] Need Help for Holidays getting into Calendar

Post by smurfier »

FYI: The new Enigma Calendar and the upcoming version of LuaCalendar will have better support for adding new Built in Events such as those you are working with.
GitHub | DeviantArt | Tumblr
This is the song that never ends. It just goes on and on my friends. Some people started singing it not knowing what it was, and they'll continue singing it forever just because . . .