Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fun feature: Easter and Leap year #17

Open
chlordk opened this issue Jul 30, 2022 · 3 comments
Open

Fun feature: Easter and Leap year #17

chlordk opened this issue Jul 30, 2022 · 3 comments

Comments

@chlordk
Copy link

chlordk commented Jul 30, 2022

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;
}
@kiranraaj19
Copy link

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

@R-CoderDotCom
Copy link
Owner

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)

@mschilli87
Copy link

@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))
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants