calendR icon indicating copy to clipboard operation
calendR copied to clipboard

Fun feature: Easter and Leap year

Open chlordk opened this issue 3 years ago • 3 comments

Looks great. Everything is almost here only easter is missing.

Easter can be a little tricky. Here is a simple example in C.

/* Source: https://www.rmg.co.uk/stories/topics/when-easter */

#include <stdio.h>

void easter(int y, int *m, int *d) {

*d = 225 - 11 * (y % 19);

while (*d > 50)
	*d -= 30;

if (*d > 48)
	--*d;

*d = *d + 7 - (y + y/4 + *d + 1) % 7;

if (*d > 31) {
	*m = 4;
	*d -= 31;
} else {
	*m = 3;
}

return 0;
}

int main( void ) {

int year, day, month;

for (year=1998; year<2100; year++) { // algorithm does not work after 2099
	easter(year,&month,&day);
	printf("%d-%02d-%02d\n", year, month, day);
}

return 0;
}

chlordk avatar Jul 30 '22 08:07 chlordk

Interesting fact. Didnt know calculating a date would be this hectic of a task.

kiranraaj19 avatar Jul 30 '22 10:07 kiranraaj19

It seems to be difficult to calculate the easter day, but it is really easy to specify the day with special.days and highlight it

If you need to know if a year is a leap year or not you can use a simple script:

leap <- function(year){
  
  start <- as.Date(paste0(year,"-01-01"))
  end <- as.Date(paste0(year,"-12-31"))
  ndays <- length(seq(start, end, by="1 day"))
  
  return(ndays)
}

leap(2011)

R-CoderDotCom avatar Sep 15 '23 10:09 R-CoderDotCom

@R-CoderDotCom:

Is there any case I am missing where this is better than the simple arithmetic solution?

leap <- function(year) {
  year <- as.integer(year)
  leap_every <- 4L
  leap_skip <- 100L
  leap_skip_skip <- leave_every * leap_skip_skip
  !(year %% leap_every) && (year %% leap_skip || !(year %% leap_skip_skip))
}

mschilli87 avatar Sep 15 '23 11:09 mschilli87