summaryrefslogtreecommitdiffstats
path: root/vendor/iana-time-zone/src/tz_netbsd.rs
blob: 84cf8b05ec71a1e174ca036cbe17bdc84a29b6f8 (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
use std::fs::read_link;

pub(crate) fn get_timezone_inner() -> Result<String, crate::GetTimezoneError> {
    // see https://www.cyberciti.biz/faq/openbsd-time-zone-howto/

    // This is a backport of the Linux implementation.
    // NetBSDs is less than thorough how the softlink should be set up.

    const PREFIXES: &[&str] = &[
        "/usr/share/zoneinfo/",   // absolute path
        "../usr/share/zoneinfo/", // relative path
    ];
    let mut s = read_link("/etc/localtime")?
        .into_os_string()
        .into_string()
        .map_err(|_| crate::GetTimezoneError::FailedParsingString)?;
    for &prefix in PREFIXES {
        if s.starts_with(prefix) {
            // Trim to the correct length without allocating.
            s.replace_range(..prefix.len(), "");
            return Ok(s);
        }
    }
    Err(crate::GetTimezoneError::FailedParsingString)
}