timelib/
lib.rs

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
mod internal;

use std::{
    ffi::{CStr, CString},
    time::{SystemTime, UNIX_EPOCH},
};

use internal::*;

/// Returns a timestamp (in seconds since the epoch) or an error (string).
///
/// # Arguments
///
/// * `date_time` - A string that holds the relative date you wish to compute.
/// * `base_timestamp` - An optional timestamp (in seconds) to use as your base (defaults to the current timestamp).
/// * `timezone` - An address of a Timezone object.
///
/// # Examples
///
/// ```
/// let tz = timelib::Timezone::parse("America/Chicago").expect("Error parsing timezone!");
/// timelib::strtotime("tomorrow", None, &tz);
/// timelib::strtotime("next tuesday", Some(1654318823), &tz);
/// ```
pub fn strtotime(
    date_time: &str,
    base_timestamp: Option<i64>,
    timezone: &Timezone,
) -> Result<i64, String> {
    if date_time.is_empty() {
        return Err("Empty date_time string.".into());
    }

    let Ok(date_time_c_str) = CString::new(date_time) else {
        return Err("Malformed date_time string.".into());
    };

    unsafe {
        let mut error = std::mem::MaybeUninit::uninit();
        let parsed_time = timelib_strtotime(
            date_time_c_str.as_ptr(),
            date_time_c_str.to_bytes().len(),
            error.as_mut_ptr(),
            timelib_builtin_db(),
            Some(timelib_tz_get_wrapper_cached),
        );
        let err_count = (*error.assume_init()).error_count;
        timelib_error_container_dtor(error.assume_init());
        if err_count != 0 {
            timelib_time_dtor(parsed_time);
            // TODO expose error message(s)
            return Err("Invalid date_time string.".into());
        }

        let base = timelib_time_ctor();
        (*base).tz_info = timezone.tzi;
        (*base).zone_type = TIMELIB_ZONETYPE_ID;
        timelib_unixtime2local(base, base_timestamp.unwrap_or_else(rust_now_sec));

        timelib_fill_holes(parsed_time, base, TIMELIB_NO_CLONE as i32);
        timelib_update_ts(parsed_time, timezone.tzi);
        let result = (*parsed_time).sse;
        timelib_time_dtor(parsed_time);
        timelib_time_dtor(base);

        Ok(result)
    }
}

fn rust_now_sec() -> i64 {
    SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .unwrap()
        .as_secs() as i64
}

/// A Timezone wrapper.
#[derive(Debug, PartialEq)]
pub struct Timezone {
    tzi: *mut timelib_tzinfo,
}

impl Drop for Timezone {
    fn drop(&mut self) {
        unsafe {
            timelib_tzinfo_dtor(self.tzi);
        }
    }
}

impl Timezone {
    /// Parses a String into a Timezone instance.
    ///
    /// # Arguments
    ///
    /// * `timezone` - A String with your IANA Timezone name.
    ///
    /// # Examples
    ///
    /// ```
    /// let tz = timelib::Timezone::parse("UTC");
    /// let tz = timelib::Timezone::parse("America/Chicago");
    /// ```
    pub fn parse(timezone: &str) -> Result<Timezone, String> {
        let Ok(tz_c_str) = CString::new(timezone) else {
            return Err("Malformed timezone string.".into());
        };
        let mut error_code: i32 = 0;
        let error_code_ptr = &mut error_code as *mut i32;
        unsafe {
            let tzi = timelib_parse_tzfile(tz_c_str.as_ptr(), timelib_builtin_db(), error_code_ptr);
            if tzi.is_null() {
                return Err(format!("Invalid timezone. Err: {error_code}."));
            }
            Ok(Self { tzi })
        }
    }

    /// Returns the underlying timezone database version.
    pub fn db_version() -> String {
        let cstr = unsafe { CStr::from_ptr((*timelib_builtin_db()).version) };
        String::from_utf8_lossy(cstr.to_bytes()).to_string()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn strtotime_empty_input() {
        let tz = Timezone::parse("UTC").unwrap();
        let result = strtotime("", None, &tz);
        assert_eq!(Err("Empty date_time string.".to_string()), result);
    }

    #[test]
    fn strtotime_invalid_date_time() {
        let tz = Timezone::parse("UTC").unwrap();
        let result = strtotime("derp", None, &tz);
        assert_eq!(Err("Invalid date_time string.".to_string()), result);
    }

    #[test]
    fn strtotime_invalid_date_time_string() {
        let tz = Timezone::parse("UTC").unwrap();
        let result = strtotime("today\0", None, &tz);
        assert_eq!(Err("Malformed date_time string.".to_string()), result);
    }

    #[test]
    fn strtotime_valid_date_time_fixed() {
        let tz = Timezone::parse("UTC").unwrap();
        let result = strtotime("jun 4 2022", None, &tz);
        assert_eq!(Ok(1654300800), result);
    }

    #[test]
    fn strtotime_valid_date_time_with_timezone_fixed() {
        let tz = Timezone::parse("UTC").unwrap();
        let result = strtotime("2006-05-12 13:00:00 America/New_York", None, &tz);
        assert_eq!(Ok(1147453200), result);
        // Get again - should use underlying TZ cache.
        let result = strtotime("2006-05-12 13:00:00 America/New_York", None, &tz);
        assert_eq!(Ok(1147453200), result);
    }

    #[test]
    fn strtotime_valid_date_time_fixed_timezone() {
        let tz = Timezone::parse("America/Chicago").unwrap();
        let result = strtotime("jun 4 2022", None, &tz);
        assert_eq!(Ok(1654318800), result);
    }

    const SEC_PER_DAY: i64 = 86_400;

    #[test]
    fn strtotime_valid_date_time_relative() {
        let tz = Timezone::parse("UTC").unwrap();
        let result = strtotime("tomorrow", None, &tz);
        assert!(result.is_ok());
        let result = result.unwrap();
        let now = rust_now_sec();
        assert!(now <= result);
        assert!(now + SEC_PER_DAY >= result);
    }

    #[test]
    fn strtotime_valid_date_time_relative_base() {
        let tz = Timezone::parse("UTC").unwrap();
        let today = 1654318823; // Saturday, June 4, 2022 5:00:23 AM GMT
        let tomorrow = 1654387200; // Sunday, June 5, 2022 12:00:00 AM GMT
        let result = strtotime("tomorrow", Some(today), &tz);
        assert_eq!(Ok(tomorrow), result);
    }

    #[test]
    fn strtotime_valid_date_time_relative_base_timezone() {
        let tz = Timezone::parse("America/Chicago").unwrap();
        let today = 1654318823; // Saturday, June 4, 2022 12:00:23 AM GMT-05:00 DST
        let tomorrow = 1654405200; // Sunday, June 5, 2022 12:00:00 AM GMT-05:00 DST
        let result = strtotime("tomorrow", Some(today), &tz);
        assert_eq!(Ok(tomorrow), result);
    }

    #[test]
    fn timezone_invalid_timezone() {
        let result = Timezone::parse("pizza");
        assert_eq!(Err("Invalid timezone. Err: 6.".to_string()), result);
    }

    #[test]
    fn timezone_invalid_timezone_string() {
        let result = Timezone::parse("UTC\0");
        assert_eq!(Err("Malformed timezone string.".to_string()), result);
    }

    #[test]
    fn timezone_valid_timezone() {
        let result = Timezone::parse("America/Chicago");
        assert!(result.is_ok());
    }

    #[test]
    fn timezone_db_version() {
        assert_eq!("2024.2", Timezone::db_version());
    }
}