opendal/raw/
chrono_util.rs

1// Licensed to the Apache Software Foundation (ASF) under one
2// or more contributor license agreements.  See the NOTICE file
3// distributed with this work for additional information
4// regarding copyright ownership.  The ASF licenses this file
5// to you under the Apache License, Version 2.0 (the
6// "License"); you may not use this file except in compliance
7// with the License.  You may obtain a copy of the License at
8//
9//   http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing,
12// software distributed under the License is distributed on an
13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14// KIND, either express or implied.  See the License for the
15// specific language governing permissions and limitations
16// under the License.
17
18use std::time::Duration;
19use std::time::UNIX_EPOCH;
20
21use chrono::DateTime;
22use chrono::Utc;
23
24use crate::*;
25
26/// Parse datetime from rfc2822.
27///
28/// For example: `Fri, 28 Nov 2014 21:00:09 +0900`
29pub fn parse_datetime_from_rfc2822(s: &str) -> Result<DateTime<Utc>> {
30    DateTime::parse_from_rfc2822(s)
31        .map(|v| v.into())
32        .map_err(|e| {
33            Error::new(ErrorKind::Unexpected, "parse datetime from rfc2822 failed").set_source(e)
34        })
35}
36
37/// Parse datetime from rfc3339.
38///
39/// # Examples
40///
41/// With a time zone:
42///
43/// ```
44/// use chrono::Datelike;
45/// use opendal::Error;
46/// use opendal::raw::parse_datetime_from_rfc3339;
47///
48/// let date_time = parse_datetime_from_rfc3339("2014-11-28T21:00:09+09:00")?;
49/// assert_eq!(date_time.date_naive().day(), 28);
50/// # Ok::<(), Error>(())
51/// ```
52///
53/// With the UTC offset of 00:00:
54///
55/// ```
56/// use chrono::Timelike;
57/// # use opendal::Error;
58/// # use opendal::raw::parse_datetime_from_rfc3339;
59///
60/// let date_time = parse_datetime_from_rfc3339("2014-11-28T21:00:09Z")?;
61/// assert_eq!(date_time.hour(), 21);
62/// # Ok::<(), Error>(())
63/// ```
64pub fn parse_datetime_from_rfc3339(s: &str) -> Result<DateTime<Utc>> {
65    DateTime::parse_from_rfc3339(s)
66        .map(|v| v.into())
67        .map_err(|e| {
68            Error::new(ErrorKind::Unexpected, "parse datetime from rfc3339 failed").set_source(e)
69        })
70}
71
72/// parse datetime from given timestamp_millis
73pub fn parse_datetime_from_from_timestamp_millis(s: i64) -> Result<DateTime<Utc>> {
74    let st = UNIX_EPOCH
75        .checked_add(Duration::from_millis(s as u64))
76        .ok_or_else(|| Error::new(ErrorKind::Unexpected, "input timestamp overflow"))?;
77
78    Ok(st.into())
79}
80
81/// parse datetime from given timestamp
82pub fn parse_datetime_from_from_timestamp(s: i64) -> Result<DateTime<Utc>> {
83    let st = UNIX_EPOCH
84        .checked_add(Duration::from_secs(s as u64))
85        .ok_or_else(|| Error::new(ErrorKind::Unexpected, "input timestamp overflow"))?;
86
87    Ok(st.into())
88}
89
90/// format datetime into http date, this format is required by:
91/// https://httpwg.org/specs/rfc9110.html#field.if-modified-since
92pub fn format_datetime_into_http_date(s: DateTime<Utc>) -> String {
93    s.format("%a, %d %b %Y %H:%M:%S GMT").to_string()
94}
95
96#[cfg(test)]
97mod tests {
98    use super::*;
99
100    #[test]
101    fn test_format_datetime_into_http_date() {
102        let s = "Sat, 29 Oct 1994 19:43:31 +0000";
103        let v = parse_datetime_from_rfc2822(s).unwrap();
104        assert_eq!(
105            format_datetime_into_http_date(v),
106            "Sat, 29 Oct 1994 19:43:31 GMT"
107        );
108    }
109}