generated from eigerco/beerus
-
Notifications
You must be signed in to change notification settings - Fork 105
/
zellers_congruence.cairo
73 lines (69 loc) · 2.08 KB
/
zellers_congruence.cairo
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
//! Zeller's congruence is an algorithm devised by Christian Zeller in the 19th century
//! to calculate the day of the week for any Julian or Gregorian calendar date.
//! It can be considered to be based on the conversion between Julian day and the calendar date.
/// Compute the day of the week for the given Gregorian date.
/// The returned value is an integer in the range 0 to 6, where 0 is Saturday, 1 is Sunday, 2 is
/// Monday, and so on.
/// # Arguments
/// * `date` - The date of the month
/// * `month` - The month of the year
/// * `year` - The year
/// # Returns
/// * `Option::None` - If the input parameters are invalid
/// * `Option::Some(day_of_week)` - The day of the week
/// # Examples
/// ```
/// use alexandria::math::zellers_congruence::day_of_week;
/// let day_of_week = day_of_week(1, 1, 2020);
/// ```
/// # TODO
/// - Change the return type to `Result`
pub fn day_of_week(mut date: u128, mut month: u128, mut year: u128) -> Option<u128> {
// Check input parameters
if !check_input_parameters(date, month, year) {
return Option::None;
}
if month < 3 {
month = month + 12;
year = year - 1;
}
let day = (date
+ (26 * (month + 1) / 10)
+ (year % 100)
+ ((year % 100) / 4)
+ ((year / 100) / 4)
+ 5 * (year / 100)) % 7;
Option::Some(day)
}
/// Check the input parameters for the `day_of_week` function.
/// # Arguments
/// * `date` - The date of the month
/// * `month` - The month of the year
/// * `year` - The year
/// # Returns
/// * `true` - If the input parameters are valid
/// * `false` - If the input parameters are invalid
pub fn check_input_parameters(date: u128, month: u128, year: u128) -> bool {
// Check the date
// Must be in the range 1 to 31
if date < 1 {
return false;
}
if date > 31 {
return false;
}
// Check the month
// Must be in the range 1 to 12
if month < 1 {
return false;
}
if month > 12 {
return false;
}
// Check the year
// Must be > 0
if year < 1 {
return false;
}
true
}