📜  泽勒的同等|寻找约会的日子

📅  最后修改于: 2021-04-29 18:35:02             🧑  作者: Mango

Zeller的全等是Christian Zeller设计的一种算法,用于计算儒略历或公历日期中的星期几。可以认为它是基于儒略日和日历日期之间的转换。
它是一种查找任何日期的星期几的算法。
对于公历压光机,它是:
h=\left ( q + [ \frac{13(m+1)}{5} ]+K +\left [ \frac{K}{4}\right ]+\left [ \frac{J}{4}\right ]+5J\right )mod7
对于朱利安日历来说,它是:
h=\left ( q + \left [ \frac{13(m+1)}{5} \right ]+K +\left [ \frac{K}{4}\right ]+5-J\right )mod7
在哪里,

  1. h是星期几(0 =星期六,1 =星期日,2 =星期一,…,6 =星期五)
  2. q是一个月中的某天
  3. m是月份(3 = 3月,4 = 4月,5 = 5月,…,14 = 2月)
  4. K是本世纪的年份(年份%100)。
  5. J是从零开始的世纪(实际上是⌊year / 100⌋),例如,1995和2000年从零开始的世纪分别是19和20(不要与通常的序数世纪枚举相混淆,后者表示这两种情况都为20) 。
NOTE: In this algorithm January and February are
      counted as months 13 and 14 of the previous
      year.E.g. if it is 2 February 2010, the 
      algorithm counts the date as the second day 
      of the fourteenth month of 2009 (02/14/2009 
      in DD/MM/YYYY format)

对于ISO周日期,按周d(1 =星期一至7 =星期日)使用

d = ((h+5)%7) + 1 
C++
// C++ program to find Find the Day
// for a Date
#include 
#include 
#include 
using namespace std;
 
int Zellercongruence(int day, int month, int year)
{
    if (month == 1) {
        month = 13;
        year--;
    }
    if (month == 2) {
        month = 14;
        year--;
    }
    int q = day;
    int m = month;
    int k = year % 100;
    int j = year / 100;
    int h
        = q + 13 * (m + 1) / 5 + k + k / 4 +
                              j / 4 + 5 * j;
    h = h % 7;
    switch (h) {
    case 0:
        cout << "Saturday \n";
        break;
    case 1:
        cout << "Sunday \n";
        break;
    case 2:
        cout << "Monday \n";
        break;
    case 3:
        cout << "Tuesday \n";
        break;
    case 4:
        cout << "Wednesday \n";
        break;
    case 5:
        cout << "Thursday \n";
        break;
    case 6:
        cout << "Friday \n";
        break;
    }
    return 0;
}
 
// Driver code
int main()
{
    Zellercongruence(22, 10, 2017); // date (dd/mm/yyyy)
    return 0;
}


Java
// Java program to find Find the Day
// for a Date
import java.util.*;
 
class GFG
{
    // Print Day for a Date
    static void Zellercongruence(int day, int month,
                                 int year)
    {
        if (month == 1)
        {
            month = 13;
            year--;
        }
        if (month == 2)
        {
            month = 14;
            year--;
        }
        int q = day;
        int m = month;
        int k = year % 100;
        int j = year / 100;
        int h = q + 13*(m + 1) / 5 + k + k / 4 + j / 4 + 5 * j;
        h = h % 7;
        switch (h)
        {
            case 0 : System.out.println("Saturday"); break;
            case 1 : System.out.println("Sunday"); break;
            case 2 : System.out.println("Monday"); break;
            case 3 : System.out.println("Tuesday"); break;
            case 4 : System.out.println("Wednesday"); break;
            case 5 : System.out.println("Thursday"); break;
            case 6 : System.out.println("Friday"); break;
        }
    }
     
    // Driver code
    public static void main(String[] args)
    {
        Zellercongruence(22, 10, 2017); //date (dd/mm/yyyy)
    }
}
 
/* This code is contributed by Mr. Somesh Awasthi */


Python3
# Python3 program to find Find the Day
# for a Date
 
def switch(h) :
    return {
        0 : "Saturday",
        1 : "Sunday",
        2 : "Monday",
        3 : "Tuesday",
        4 : "Wednesday",
        5 : "Thursday",
        6 : "Friday",
    }[h]
 
def Zellercongruence(day, month, year) :
    if (month == 1) :
        month = 13
        year = year - 1
 
    if (month == 2) :
        month = 14
        year = year - 1
    q = day
    m = month
    k = year % 100;
    j = year // 100;
    h = q + 13 * (m + 1) // 5 + k + k // 4 + j // 4 + 5 * j
    h = h % 7
    print(switch (h))
     
     
     
# Driver code
Zellercongruence(22, 10, 2017) #date (dd/mm/yyyy)
 
# This code is contributed by Nikita Tiwari


C#
// C# program to find Find the Day
// for a Date
using System;
 
class GFG {
     
    // Print Day for a Date
    static void Zellercongruence(int day,
                      int month, int year)
    {
        if (month == 1)
        {
            month = 13;
            year--;
        }
        if (month == 2)
        {
            month = 14;
            year--;
        }
        int q = day;
        int m = month;
        int k = year % 100;
        int j = year / 100;
        int h = q + 13 * (m + 1) / 5 + k + k / 4
                                 + j / 4 + 5 * j;
        h = h % 7;
        switch (h)
        {
            case 0 : Console.WriteLine("Saturday");
                     break;
                      
            case 1 : Console.WriteLine("Sunday");
                     break;
                      
            case 2 : Console.WriteLine("Monday");
                     break;
                      
            case 3 : Console.WriteLine("Tuesday");
                     break;
                      
            case 4 : Console.WriteLine("Wednesday");
                     break;
                      
            case 5 : Console.WriteLine("Thursday");
                     break;
                      
            case 6 : Console.WriteLine("Friday");
                     break;
        }
    }
     
    // Driver code
    public static void Main()
    {
         
        //date (dd/mm/yyyy)
        Zellercongruence(22, 10, 2017);
    }
}
 
/* This code is contributed by vt_m */


PHP


输出:

Sunday

参考:

  1. Zellers Congruence
  2. Wiki –泽勒的同声传
  3. ISO时间信息