summaryrefslogtreecommitdiffstats
path: root/rust/src/helpers.rs
blob: 364075acef6efbec15678f9faa9485f61592ab0e (plain)
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
use crate::constants::{
    DAYS_PER_L_YEAR, DAYS_PER_N_YEAR, DAY_OF_WEEK_TABLE, EPOCH_YEAR, MONTHS_OFFSETS,
    SECS_PER_100_YEARS, SECS_PER_400_YEARS, SECS_PER_4_YEARS, SECS_PER_DAY, SECS_PER_HOUR,
    SECS_PER_MIN, SECS_PER_YEAR, TM_DECEMBER, TM_JANUARY,
};

fn p(year: i32) -> i32 {
    year + year / 4 - year / 100 + year / 400
}

pub fn is_leap(year: i32) -> bool {
    year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)
}

pub fn is_long_year(year: i32) -> bool {
    (p(year) % 7 == 4) || (p(year - 1) % 7 == 3)
}

pub fn days_in_year(year: i32) -> u32 {
    if is_leap(year) {
        return DAYS_PER_L_YEAR;
    }

    DAYS_PER_N_YEAR
}

pub fn week_day(year: i32, month: u32, day: u32) -> u32 {
    let y: i32 = year - i32::from(month < 3);

    let w: i32 = (p(y) + DAY_OF_WEEK_TABLE[(month - 1) as usize] as i32 + day as i32) % 7;

    if w == 0 {
        return 7;
    }

    w.unsigned_abs()
}

pub fn day_number(year: i32, month: u8, day: u8) -> i32 {
    let m = i32::from((month + 9) % 12);
    let y = year - m / 10;

    365 * y + y / 4 - y / 100 + y / 400 + (m * 306 + 5) / 10 + (i32::from(day) - 1)
}

pub fn local_time(
    unix_time: f64,
    utc_offset: isize,
    microsecond: usize,
) -> (usize, usize, usize, usize, usize, usize, usize) {
    let mut year: usize = EPOCH_YEAR as usize;
    let mut seconds: isize = unix_time.floor() as isize;

    // Shift to a base year that is 400-year aligned.
    if seconds >= 0 {
        seconds -= (10957 * SECS_PER_DAY as usize) as isize;
        year += 30; // == 2000
    } else {
        seconds += ((146_097 - 10957) * SECS_PER_DAY as usize) as isize;
        year -= 370; // == 1600
    }

    seconds += utc_offset;

    // Handle years in chunks of 400/100/4/1
    year += 400 * (seconds / SECS_PER_400_YEARS as isize) as usize;
    seconds %= SECS_PER_400_YEARS as isize;
    if seconds < 0 {
        seconds += SECS_PER_400_YEARS as isize;
        year -= 400;
    }

    let mut leap_year = 1; // 4-century aligned
    let mut sec_per_100years = SECS_PER_100_YEARS[leap_year] as isize;

    while seconds >= sec_per_100years {
        seconds -= sec_per_100years;
        year += 100;
        leap_year = 0; // 1-century, non 4-century aligned
        sec_per_100years = SECS_PER_100_YEARS[leap_year] as isize;
    }

    let mut sec_per_4years = SECS_PER_4_YEARS[leap_year] as isize;
    while seconds >= sec_per_4years {
        seconds -= sec_per_4years;
        year += 4;
        leap_year = 1; // 4-year, non century aligned
        sec_per_4years = SECS_PER_4_YEARS[leap_year] as isize;
    }

    let mut sec_per_year = SECS_PER_YEAR[leap_year] as isize;
    while seconds >= sec_per_year {
        seconds -= sec_per_year;
        year += 1;
        leap_year = 0; // non 4-year aligned
        sec_per_year = SECS_PER_YEAR[leap_year] as isize;
    }

    // Handle months and days
    let mut month = TM_DECEMBER + 1;
    let mut day: usize = (seconds / (SECS_PER_DAY as isize) + 1) as usize;
    seconds %= SECS_PER_DAY as isize;

    let mut month_offset: usize;
    while month != (TM_JANUARY + 1) {
        month_offset = MONTHS_OFFSETS[leap_year][month] as usize;
        if day > month_offset {
            day -= month_offset;
            break;
        }

        month -= 1;
    }

    // Handle hours, minutes and seconds
    let hour: usize = (seconds / SECS_PER_HOUR as isize) as usize;
    seconds %= SECS_PER_HOUR as isize;
    let minute: usize = (seconds / SECS_PER_MIN as isize) as usize;
    let second: usize = (seconds % SECS_PER_MIN as isize) as usize;

    (year, month, day, hour, minute, second, microsecond)
}