timekit 0.2.0

A simple Rust library for working with timezones and displaying current time in multiple zones.
Documentation
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
// Bring in the constants from const.rs
pub mod constants;

use constants::*;
use std::time::{SystemTime, UNIX_EPOCH};
use std::{fmt, ops::Add, ops::Sub};

/// Struct for holding the full date and time information.
#[derive(Debug, Clone, Copy)]
pub struct DateTime {
    pub year: u64,
    pub month: u64,
    pub day: u64,
    pub hour: u64,
    pub minute: u64,
    pub second: u64,
    pub timezone: TimeZone,
}

impl DateTime {
    /// Creates a new `DateTime` object.
    pub fn new(
        year: u64,
        month: u64,
        day: u64,
        hour: u64,
        minute: u64,
        second: u64,
        timezone: TimeZone,
    ) -> Result<Self, String> {
        if month < 1 || month > 12 {
            return Err("Invalid month".to_string());
        }
        if day < 1 || day > days_in_month(month, year) {
            return Err("Invalid day".to_string());
        }
        if hour > 23 {
            return Err("Invalid hour".to_string());
        }
        if minute > 59 {
            return Err("Invalid minute".to_string());
        }
        if second > 59 {
            return Err("Invalid second".to_string());
        }

        // Unix 초를 계산하기 위해 UTC 시간 기준으로 보정
        let mut total_seconds = 0;

        // 연도 계산
        for y in 1970..year {
            total_seconds += if is_leap_year(y) { 366 } else { 365 } * SECONDS_IN_DAY;
        }

        // 월 계산
        for m in 1..month {
            total_seconds += days_in_month(m, year) as i64 * SECONDS_IN_DAY;
        }

        // 일, 시, 분, 초 계산
        total_seconds += (day - 1) as i64 * SECONDS_IN_DAY;
        total_seconds += hour as i64 * SECONDS_IN_HOUR;
        total_seconds += minute as i64 * SECONDS_IN_MINUTE;
        total_seconds += second as i64;

        // 시간대 오프셋 적용 (UTC 기준으로 보정)
        total_seconds -= timezone.offset_in_seconds();

        // UTC 기준의 `DateTime` 생성
        let utc_datetime = Self::from_unix_seconds(total_seconds, timezone)?;

        Ok(utc_datetime)
    }

    /// Calculate the total seconds since Unix Epoch (1970-01-01 00:00:00)
    pub fn calculate_total_seconds(
        year: u64,
        month: u64,
        day: u64,
        hour: u64,
        minute: u64,
        second: u64,
    ) -> Result<i64, String> {
        // 연도는 1970년부터 시작
        if year < 1970 {
            return Err("Year must be 1970 or later".to_string());
        }

        let mut total_seconds: i64 = 0;

        // 1. 연도 계산: 1970년부터 현재 연도 이전까지의 총 초 계산
        for y in 1970..year {
            total_seconds += if is_leap_year(y) {
                SECONDS_IN_LEAPYEAR
            } else {
                SECONDS_IN_YEAR
            };
        }

        // 2. 월 계산: 1월부터 현재 월 이전까지의 총 초 계산
        for m in 1..month {
            total_seconds += days_in_month(m, year) as i64 * SECONDS_IN_DAY;
        }

        // 3. 일 계산: 현재 월에서 1일부터 현재 일 이전까지의 총 초 계산
        total_seconds += (day as i64 - 1) * SECONDS_IN_DAY;

        // 4. 시, 분, 초 계산
        total_seconds += hour as i64 * SECONDS_IN_HOUR;
        total_seconds += minute as i64 * SECONDS_IN_MINUTE;
        total_seconds += second as i64;

        Ok(total_seconds)
    }

    pub fn strftime(&self, format: &str) -> String {
        let mut result = format.to_string();
        result = result.replace("%Y", &format!("{:04}", self.year));
        result = result.replace("%m", &format!("{:02}", self.month));
        result = result.replace("%d", &format!("{:02}", self.day));
        result = result.replace("%H", &format!("{:02}", self.hour));
        result = result.replace("%M", &format!("{:02}", self.minute));
        result = result.replace("%S", &format!("{:02}", self.second));
        result
    }

    pub fn to_unix_seconds(&self) -> i64 {
        let mut total_seconds: i64 = 0;

        // 연도 계산
        for year in 1970..self.year {
            total_seconds += if is_leap_year(year) { 366 } else { 365 } * SECONDS_IN_DAY;
        }

        // 월 계산
        for month in 1..self.month {
            total_seconds += days_in_month(month, self.year) as i64 * SECONDS_IN_DAY;
        }

        // 일, 시, 분, 초 계산
        total_seconds += (self.day - 1) as i64 * SECONDS_IN_DAY;
        total_seconds += self.hour as i64 * SECONDS_IN_HOUR;
        total_seconds += self.minute as i64 * SECONDS_IN_MINUTE;
        total_seconds += self.second as i64;

        // 시간대 오프셋 적용 (UTC 기준으로 변환)
        total_seconds - self.timezone.offset_in_seconds()
    }

    pub fn from_unix_seconds(unix_seconds: i64, timezone: TimeZone) -> Result<Self, String> {
        // 시간대 오프셋 적용 (UTC에서 로컬 시간으로 변환)
        let adjusted_seconds = unix_seconds + timezone.offset_in_seconds();
        let mut remaining_seconds = adjusted_seconds;

        if remaining_seconds < 0 {
            return Err("Unix seconds cannot represent a date before 1970-01-01".to_string());
        }

        // 연도 계산
        let mut year = 1970;
        while remaining_seconds >= (if is_leap_year(year) { 366 } else { 365 }) * SECONDS_IN_DAY {
            remaining_seconds -= (if is_leap_year(year) { 366 } else { 365 }) * SECONDS_IN_DAY;
            year += 1;
        }

        // 월 계산
        let mut month = 1;
        while remaining_seconds >= days_in_month(month, year) as i64 * SECONDS_IN_DAY {
            remaining_seconds -= days_in_month(month, year) as i64 * SECONDS_IN_DAY;
            month += 1;
        }

        // 일, 시, 분, 초 계산
        let day = (remaining_seconds / SECONDS_IN_DAY) as u64 + 1;
        remaining_seconds %= SECONDS_IN_DAY;
        let hour = (remaining_seconds / 3600) as u64;
        remaining_seconds %= 3600;
        let minute = (remaining_seconds / 60) as u64;
        let second = (remaining_seconds % 60) as u64;

        Ok(Self {
            year,
            month,
            day,
            hour,
            minute,
            second,
            timezone,
        })
    }

    pub fn add_timedelta(&self, delta: TimeDelta) -> Result<Self, String> {
        let current_unix = self.to_unix_seconds(); // 현재 시간을 Unix 시간으로 변환
        let delta_seconds = compute_total_seconds(
            delta.weeks,
            delta.days,
            delta.hours,
            delta.minutes,
            delta.seconds,
        );
        let timezone = self.timezone.clone();
        let new_unix = current_unix + delta_seconds; // 초 단위로 더하기
        if new_unix < 0 {
            return Err(
                "Resulting DateTime is before Unix epoch (1970-01-01 00:00:00 UTC)".to_string(),
            );
        }
        DateTime::from_unix_seconds(new_unix, timezone) // 다시 DateTime으로 변환
    }

    pub fn sub_timedelta(&self, delta: TimeDelta) -> Result<Self, String> {
        let current_unix = self.to_unix_seconds(); // 현재 시간을 Unix 시간으로 변환
        let delta_seconds = compute_total_seconds(
            delta.weeks,
            delta.days,
            delta.hours,
            delta.minutes,
            delta.seconds,
        );
        let new_unix = current_unix - delta_seconds; // 초 단위로 빼기
        let timezone = self.timezone.clone();
        if new_unix < 0 {
            return Err(
                "Resulting DateTime is before Unix epoch (1970-01-01 00:00:00 UTC)".to_string(),
            );
        }

        DateTime::from_unix_seconds(new_unix, timezone) // 다시 DateTime으로 변환
    }
}

impl fmt::Display for DateTime {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        // Format the DateTime struct to a readable "YYYY-MM-DD HH:MM:SS" format.
        write!(
            f,
            "{:04}-{:02}-{:02} {:02}:{:02}:{:02}",
            self.year, self.month, self.day, self.hour, self.minute, self.second
        )
    }
}

impl Add<TimeDelta> for DateTime {
    type Output = Result<DateTime, String>;

    fn add(self, delta: TimeDelta) -> Self::Output {
        DateTime::add_timedelta(&self, delta)
    }
}

impl Sub<TimeDelta> for DateTime {
    type Output = Result<DateTime, String>;

    fn sub(self, delta: TimeDelta) -> Self::Output {
        DateTime::sub_timedelta(&self, delta)
    }
}

impl PartialEq for DateTime {
    fn eq(&self, other: &Self) -> bool {
        self.year == other.year
            && self.month == other.month
            && self.day == other.day
            && self.hour == other.hour
            && self.minute == other.minute
            && self.second == other.second
    }
}

/// TimeDelta struct to represent a time difference similar to Python's timedelta.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct TimeDelta {
    pub weeks: i64,
    pub days: i64,
    pub hours: i64,
    pub minutes: i64,
    pub seconds: i64,
}

impl std::fmt::Display for TimeDelta {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let mut components = Vec::new();

        if self.weeks != 0 {
            components.push(format!(
                "{} week{}",
                self.weeks,
                if self.weeks.abs() == 1 { "" } else { "s" }
            ));
        }
        if self.days != 0 {
            components.push(format!(
                "{} day{}",
                self.days,
                if self.days.abs() == 1 { "" } else { "s" }
            ));
        }
        if self.hours != 0 {
            components.push(format!(
                "{} hour{}",
                self.hours,
                if self.hours.abs() == 1 { "" } else { "s" }
            ));
        }
        if self.minutes != 0 {
            components.push(format!(
                "{} minute{}",
                self.minutes,
                if self.minutes.abs() == 1 { "" } else { "s" }
            ));
        }
        if self.seconds != 0 || components.is_empty() {
            // 항상 seconds는 출력
            components.push(format!(
                "{} second{}",
                self.seconds,
                if self.seconds.abs() == 1 { "" } else { "s" }
            ));
        }

        write!(f, "{}", components.join(", "))
    }
}

impl Default for TimeDelta {
    fn default() -> Self {
        Self {
            weeks: 0,
            days: 0,
            hours: 0,
            minutes: 0,
            seconds: 0,
        }
    }
}

/// Enum for representing time zones with precomputed UTC offsets in seconds.
#[derive(Debug, Clone, Copy)]
pub enum TimeZone {
    UTC,
    KST,     // Korea Standard Time (UTC+9)
    EST,     // Eastern Standard Time (UTC-5)
    PST,     // Pacific Standard Time (UTC-8)
    JST,     // Japan Standard Time (UTC+9)
    IST,     // India Standard Time (UTC+5:30)
    CET,     // Central European Time (UTC+1)
    AST,     // Atlantic Standard Time (UTC-4)
    CST,     // Central Standard Time (UTC-6)
    MST,     // Mountain Standard Time (UTC-7)
    AKST,    // Alaska Standard Time (UTC-9)
    HST,     // Hawaii Standard Time (UTC-10)
    BST,     // British Summer Time (UTC+1)
    WET,     // Western European Time (UTC+0)
    EET,     // Eastern European Time (UTC+2)
    SAST,    // South Africa Standard Time (UTC+2)
    EAT,     // East Africa Time (UTC+3)
    AEST,    // Australian Eastern Standard Time (UTC+10)
    ACST,    // Australian Central Standard Time (UTC+9:30)
    AWST,    // Australian Western Standard Time (UTC+8)
    CSTAsia, // China Standard Time (UTC+8)
    SGT,     // Singapore Time (UTC+8)
    HKT,     // Hong Kong Time (UTC+8)
}

impl TimeZone {
    /// Returns the precomputed UTC offset in seconds for each time zone.
    pub const fn offset_in_seconds(&self) -> i64 {
        match self {
            TimeZone::UTC => OFFSET_UTC,
            TimeZone::KST => OFFSET_KST,
            TimeZone::EST => OFFSET_EST,
            TimeZone::PST => OFFSET_PST,
            TimeZone::JST => OFFSET_JST,
            TimeZone::IST => OFFSET_IST,
            TimeZone::CET => OFFSET_CET,
            TimeZone::AST => OFFSET_AST,
            TimeZone::CST => OFFSET_CST,
            TimeZone::MST => OFFSET_MST,
            TimeZone::AKST => OFFSET_AKST,
            TimeZone::HST => OFFSET_HST,
            TimeZone::BST => OFFSET_BST,
            TimeZone::WET => OFFSET_WET,
            TimeZone::EET => OFFSET_EET,
            TimeZone::SAST => OFFSET_SAST,
            TimeZone::EAT => OFFSET_EAT,
            TimeZone::AEST => OFFSET_AEST,
            TimeZone::ACST => OFFSET_ACST,
            TimeZone::AWST => OFFSET_AWST,
            TimeZone::CSTAsia => OFFSET_CST_ASIA,
            TimeZone::SGT => OFFSET_SGT,
            TimeZone::HKT => OFFSET_HKT,
        }
    }
}

/// Returns the current date and time adjusted for the specified time zone.
///
/// This function calculates the current date and time based on the system's current time
/// (measured as the number of seconds since the UNIX Epoch: 1970-01-01 00:00:00 UTC)
/// and adjusts it according to the time zone provided. The time zone offsets are hardcoded
/// to avoid unnecessary runtime computation.
///
/// The function follows these steps:
/// 1. Retrieves the current system time as seconds since the UNIX Epoch.
/// 2. Applies the specified time zone's UTC offset to the seconds.
/// 3. Converts the adjusted seconds into days, hours, minutes, and seconds.
/// 4. Determines the corresponding year, month, and day using the leap year rules.
/// 5. Returns the computed time as a `DateTime` object containing the year, month, day, hour, minute, and second.
///
/// # Parameters:
/// * `timezone`: The `TimeZone` enum that specifies the time zone for which the current time should be adjusted.
///
/// # Returns:
/// * `DateTime`: A struct containing the current year, month, day, hour, minute, and second, adjusted to the specified time zone.
///
/// # Panics:
/// * The function will panic if the system's time goes backwards (i.e., if the current time is somehow earlier than the UNIX Epoch).
///
/// # Example:
/// ```
/// use timekit;
/// use timekit::TimeZone;
/// let current_time_kst = timekit::now(TimeZone::KST);  // Returns current time in Korea Standard Time (KST).
/// let current_time_utc = timekit::now(TimeZone::UTC);  // Returns current time in UTC.
/// ```
pub fn now(timezone: TimeZone) -> Result<DateTime, String> {
    // Get the current system time since UNIX_EPOCH in seconds and milliseconds.
    let now = SystemTime::now();
    let duration_since_epoch = now.duration_since(UNIX_EPOCH).expect("Time went backwards");

    // Total seconds since UNIX epoch.
    let total_seconds = duration_since_epoch.as_secs();

    // Get the time zone offset in seconds.
    //let timezone_offset = timezone.offset_in_seconds();
    //let adjusted_seconds = (total_seconds as i64 + timezone_offset) as u64;
    // Adjust total seconds based on the time zone offset.
    let adjusted_seconds = adjust_second_with_timezone(total_seconds, timezone);

    calculate_date_since_epoch(adjusted_seconds as i64, timezone)
}

/// Determines if a given year is a leap year.
///
/// A leap year is a year that is divisible by 4 but not divisible by 100,
/// except when the year is also divisible by 400. This rule is part of the
/// Gregorian calendar, which adds an extra day to February (29 days) to
/// keep the calendar year synchronized with the astronomical year.
///
/// # Parameters:
/// * `year`: The year as a `u64` to be checked for leap year status.
///
/// # Returns:
/// * `true` if the year is a leap year, otherwise `false`.
///
/// # Example:
/// ```
/// use timekit::is_leap_year;
/// let leap_year = is_leap_year(2024);  // true
/// let common_year = is_leap_year(2023);  // false
/// ```
pub const fn is_leap_year(year: u64) -> bool {
    // A leap year is divisible by 4 but not divisible by 100,
    // except if it is divisible by 400.
    (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)
}

/// Returns the number of days in a given month and year.
///
/// This function returns the number of days in a month. It accounts for leap years
/// in February, where there are 29 days instead of the usual 28. All other months
/// follow the standard day count:
/// - January, March, May, July, August, October, and December have 31 days.
/// - April, June, September, and November have 30 days.
/// - February has 28 days in common years and 29 days in leap years.
///
/// # Parameters:
/// * `month`: The month (1-12) as a `u64`. 1 corresponds to January, and 12 corresponds to December.
/// * `year`: The year as a `u64`. The year is needed to determine whether February has 28 or 29 days in case of a leap year.
///
/// # Returns:
/// * The number of days in the specified month as a `u64`.
///
/// # Example:
/// ```
/// use timekit::days_in_month;
/// let days_in_january = days_in_month(1, 2024);  // 31
/// let days_in_february_leap_year = days_in_month(2, 2024);  // 29
/// let days_in_february_common_year = days_in_month(2, 2023);  // 28
/// ```
pub fn days_in_month(month: u64, year: u64) -> u64 {
    match month {
        1 | 3 | 5 | 7 | 8 | 10 | 12 => 31, // January, March, May, July, August, October, December have 31 days
        4 | 6 | 9 | 11 => 30,              // April, June, September, November have 30 days
        2 => {
            // February has 29 days in a leap year, otherwise it has 28 days
            if is_leap_year(year) {
                29
            } else {
                28
            }
        }
        _ => 0, // Invalid month input, returns 0 (shouldn't happen with proper input validation)
    }
}

pub const fn compute_total_seconds(
    weeks: i64,
    days: i64,
    hours: i64,
    minutes: i64,
    seconds: i64,
) -> i64 {
    weeks * SECONDS_IN_WEEK as i64
        + days * SECONDS_IN_DAY as i64
        + hours * SECONDS_IN_HOUR as i64
        + minutes * SECONDS_IN_MINUTE as i64
        + seconds
}

pub const fn adjust_second_with_timezone(total_seconds: u64, timezone: TimeZone) -> u64 {
    let timezone_offset = timezone.offset_in_seconds();
    let adjusted_seconds = (total_seconds as i64 + timezone_offset) as u64;
    adjusted_seconds
}

pub fn calculate_date_since_epoch(
    adjusted_seconds: i64,
    timezone: TimeZone,
) -> Result<DateTime, String> {
    // Convert adjusted seconds into days, hours, minutes, and seconds.
    let mut days = adjusted_seconds as u64 / SECONDS_IN_DAY as u64;
    let remainder_seconds = adjusted_seconds % SECONDS_IN_DAY;
    let hour = (remainder_seconds / SECONDS_IN_HOUR) as u64;
    let remainder_seconds = remainder_seconds % SECONDS_IN_HOUR;
    let minute = (remainder_seconds / SECONDS_IN_MINUTE) as u64;
    let second = (remainder_seconds % SECONDS_IN_MINUTE) as u64;

    // Year calculation (starting from 1970).
    let mut year = 1970;
    while days >= if is_leap_year(year) { 366 } else { 365 } {
        days -= if is_leap_year(year) { 366 } else { 365 };
        year += 1;
    }

    // Month and day calculation.
    let mut month = 1;
    while days >= days_in_month(month, year) {
        days -= days_in_month(month, year);
        month += 1;
    }
    let day = days as u64 + 1; // Days start from 1.

    // Return the DateTime object.
    DateTime::new(year, month, day, hour, minute, second, timezone)
}