首页 > 解决方案 > 及时计算事件,相对于复活节

问题描述

假设约翰的周年纪念日正好在复活节。约翰尼的周年纪念日总是复活节后的一周。艾伦的周年纪念日是五旬节前一周(复活节后 42 天)。

我如何使用 shell 脚本计算他们生日的日期(数字和日期名称)和月份。未来的所有岁月。

我知道,我可以用ncal -e "year". 在 C 中是我所做的:

typedef struct {
  int day;
  int month;
  int year;
} Date;

然后我计算复活节的日子(使用高斯算法)。并将日、月、年返回到 pentecost 函数,该函数取日+49 并减少日,并增加月。

因此:

  1. 在 shell 中,如何ncal -e仅使用 shell 将输出从“2019 年 1 月 1 日”转换为“1 1 2019”。
  2. 如果我有这个,你将如何开始为上面的故事制定规则。(对于生日)
Date easter(int Y)
{
    Date date;
    int C = floor(Y/100);
    int N = Y - 19*floor(Y/19);
    int K = floor((C - 17)/25);
    int I = C - floor(C/4) - floor((C - K)/3) + 19*N + 15;
    I = I - 30*floor((I/30));
    I = I - floor(I/28)*(1 - floor(I/28)*floor(29/(I + 1))*floor((21 - N)/11));
    int J = Y + floor(Y/4) + I + 2 - C + floor(C/4);
    J = J - 7*floor(J/7);
    int L = I - J;
    int M = 3 + floor((L + 40)/44);
    int D = L + 28 - 31*floor(M/4);

    date.d = D;
    date.m = M;
    date.y = Y;
    return date;
}

标签: cshellunixcalendar

解决方案


只需编写一个调用的函数ncal -e

easter() {
    local year=${1:-$(date "+%Y")}   # use this year if no arg provided
    local easter=$(ncal -e "$year")  # month day year
    date -d "$easter" "+%F"          # YYYY-mm-dd
}

然后

$ easter
2019-04-21
$ easter 2018
2018-04-01
$ easter 2020
2020-04-12
$ date -d "$(easter) - 1 week" "+%F"
2019-04-14
$ date -d "$(easter) + 1 week" "+%F"
2019-04-28

如果您愿意,请使用不同的日期格式。


推荐阅读