c - 及时计算事件,相对于复活节
问题描述
假设约翰的周年纪念日正好在复活节。约翰尼的周年纪念日总是复活节后的一周。艾伦的周年纪念日是五旬节前一周(复活节后 42 天)。
我如何使用 shell 脚本计算他们生日的日期(数字和日期名称)和月份。未来的所有岁月。
我知道,我可以用ncal -e "year"
. 在 C 中是我所做的:
typedef struct {
int day;
int month;
int year;
} Date;
然后我计算复活节的日子(使用高斯算法)。并将日、月、年返回到 pentecost 函数,该函数取日+49 并减少日,并增加月。
因此:
- 在 shell 中,如何
ncal -e
仅使用 shell 将输出从“2019 年 1 月 1 日”转换为“1 1 2019”。 - 如果我有这个,你将如何开始为上面的故事制定规则。(对于生日)
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;
}
解决方案
只需编写一个调用的函数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
如果您愿意,请使用不同的日期格式。
推荐阅读
- amazon-web-services - 运行反向代理的 Elastic Beanstalk 上的 TLS
- php - PHP / Laravel - update values of JSON object
- sql-server - 更改sqlserver服务帐户后的文件和文件夹权限
- android - React-native ListView“警告:无法调用setState”
- android - 添加新项目和 notifyDataSetChanged 后如何从 RecyclerView 获取最后一个项目视图?
- mongodb - 如何为文档流水线
- python - 如何将小部件的 id 从 kivy 发送和使用到 python
- git - 尝试在两个本地存储库之间获取失败?
- android - 将 ImageView 固定到 LinearLayout 中最右侧的位置
- c - 如何修复“错误:赋值给数组类型的表达式”?