Visualizzazione dei risultati da 1 a 1 su 1
  1. #1
    Moderatore di Windows e software L'avatar di URANIO
    Registrato dal
    Dec 1999
    residenza
    Casalpusterlengo (LO)
    Messaggi
    1,255

    [OT] Calendario e calcolo giorni festivi

    Come potrei fare per calcolare i giorni festivi italiani? Esistono algoritmi?
    Ad esempio so che per i vari calendari online esistono delle liste da importare, ad esempio iCAL delle feste italiane. Ma non ho capito se i file contengono la lista dei giorni oppure l'algoritmo per calcolarli (Immagino questo).

  2. #2
    Utente di HTML.it
    Registrato dal
    Apr 2011
    residenza
    Arezzo
    Messaggi
    194
    Di quale linguaggio parliamo?
    In realtà l'unico giorno da calcolare è la Pasqua (e Pasquetta di conseguenza) le restanti festività sono date fisse, e per feste locali (ad es. i vari Patrono di...) non credo si calcolino in nessun modo, o li sai.... o li sai.

    Comunque se ti può aiutare questo codice (C#)

    codice:
    public class Festivity
        {
            private class CalendarConstants
            {
                public int? minDate { get; set; }
                public int maxDate { get; set; }
                public int x { get; set; }
                public int y { get; set; }
            }
    
            private static IEnumerable<CalendarConstants> Costanti = new List<CalendarConstants>()
            {
                new CalendarConstants() { minDate = null , maxDate = 1582, x = 15, y= 6 },
                new CalendarConstants()  { minDate = 1583 , maxDate = 1699, x = 22, y= 2 },
                new CalendarConstants()  { minDate = 1700 , maxDate = 1799, x = 23, y= 3 },
                new CalendarConstants()  { minDate = 1800 , maxDate = 1899, x = 23, y= 4 },
                new CalendarConstants()  { minDate = 1900 , maxDate = 2099, x = 24, y= 5 },
                new CalendarConstants()  { minDate = 2100 , maxDate = 2199, x = 24, y= 6 },
                new CalendarConstants()  { minDate = 2200 , maxDate = 2299, x = 25, y= 7 },
                new CalendarConstants()  { minDate = 2300 , maxDate = 2399, x = 26, y= 1 },
                new CalendarConstants()  { minDate = 2400 , maxDate = 2499, x = 25, y= 1 }
            };
    
            public static DateTime? GetEasterDate(int year)
            {
                var constant = Costanti.First(cx =>
                    (!cx.minDate.HasValue || year >= cx.minDate.Value) &&
                    (year <= cx.maxDate));
                var x = constant.x;
                var y = constant.y;
                var a = year % 19;
                var b = year % 4;
                var c = year % 7;
                var d = (19 * a + x) % 30;
                var e = (2 * b + 4 * c + 6 * d + y) % 7;
                var sum = 22 + d + e;
                if (sum <= 31)
                {
                    return new DateTime(year, 3, sum);
                }
                else if (((sum - 31) != 26 && (sum - 31) != 25) ||
                            ((sum - 31) == 25 && (d != 28 || a <= 10)))
                {
                    return new DateTime(year, 4, sum - 31);
                }
                else if ((sum - 31) == 25 && d == 28 && a > 10)
                {
                    return new DateTime(year, 4, 18);
                }
                else
                {
                    return new DateTime(year, 4, 19);
                }
            }
    
    
    
            public static Dictionary<string, DateTime> FestivityInYear(int year)
            {
                var rtn = new Dictionary<string, DateTime>();
                rtn.Add("Primo dell'anno", new DateTime(year, 1, 1));
                rtn.Add("Epifania", new DateTime(year, 1, 6));
                rtn.Add("Festa della Liberazione", new DateTime(year, 4, 25));
                rtn.Add("Festa del Lavoro", new DateTime(year, 5, 1));
                rtn.Add("Fondazione della Repubblica", new DateTime(year, 6, 2));
                rtn.Add("Assunzione Beata Vergine Maria (Ferragosto)", new DateTime(year, 8, 15));
                rtn.Add("Ognissanti", new DateTime(year, 11, 1));
                rtn.Add("Immacolata Concezione", new DateTime(year, 12, 8));
                rtn.Add("Natale", new DateTime(year, 1, 6));
                rtn.Add("S.Stefano", new DateTime(year, 1, 6));
                rtn.Add("Pasqua", ((DateTime)GetEasterDate(year)));
                rtn.Add("Pasquetta", ((DateTime)GetEasterDate(year)).AddDays(1));
                return rtn;
            }
    
            public static bool IsLeapYear(int year)
            {
                return DateTime.IsLeapYear(year);
            }
        }
    

    Riccardo Sadocchi
    Microsoft MCP C#

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2024 vBulletin Solutions, Inc. All rights reserved.