Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tzdb::local_tz() function get None ,existing problems tzdb 0.4.1 version #105

Closed
baoyachi opened this issue Aug 11, 2022 · 4 comments
Closed

Comments

@baoyachi
Copy link

baoyachi commented Aug 11, 2022

ref #101

  • rust code
fn main() {

    const PREFIX: &str = "/usr/share/zoneinfo/";

    let mut s = std::fs::read_link("/etc/localtime").unwrap()
        .into_os_string()
        .into_string().unwrap();
 
   println!("{:?}",s);
}
  • exec cargo run
~ cargo run
"../usr/share/zoneinfo/Asia/Shanghai"
~
  • Then ,see etc/localtime path
~ ls -als /etc/localtime
0 lrwxrwxrwx 1 root root 35 Jul  1 00:46 /etc/localtime -> ../usr/share/zoneinfo/Asia/Shanghai
~

So,above code return Relative path,not a Absolute path. It's seems a bug:https://github.com/strawlab/iana-time-zone/blob/main/src/tz_linux.rs#L18-L29

In iana-time-zone respsitory not include Relative path, May be need fix.

...
    const PREFIX: &str = "/usr/share/zoneinfo/";

    let mut s = read_link("/etc/localtime")?
        .into_os_string()
        .into_string()
        .map_err(|_| crate::GetTimezoneError::FailedParsingString)?;
    if !s.starts_with(PREFIX) {
        return Err(crate::GetTimezoneError::FailedParsingString);
    }

    // Trim to the correct length without allocating.
    s.replace_range(..PREFIX.len(), "");
...

recommend link:https://stackoverflow.com/questions/30511331/getting-the-absolute-path-from-a-pathbuf

@Kijewski
Copy link
Owner

Please verify if

     let mut s = read_link("/etc/localtime")?
+        .canonicalize()?
         .into_os_string()

would fix the problem for you.

@baoyachi
Copy link
Author

Please verify if

     let mut s = read_link("/etc/localtime")?
+        .canonicalize()?
         .into_os_string()

would fix the problem for you.

Not work:

fn main() {

    const PREFIX: &str = "/usr/share/zoneinfo/";

    let mut s = std::fs::read_link("/etc/localtime").unwrap()
+        .canonicalize().unwrap()
        .into_os_string()
        .into_string().unwrap();
    println!("{:?}",s);
}
~ cargo run
+ thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: Os { code: 2, kind: NotFound, message: "No such file or directory" }', src/main.rs:6:25
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
~ 

@Kijewski
Copy link
Owner

Yes, I noticed the problem: The link has to be relative to /etc not the working directory.

@baoyachi
Copy link
Author

Yes, I noticed the problem: The link has to be relative to /etc not the working directory.

You are right

Kijewski added a commit to Kijewski/iana-time-zone that referenced this issue Aug 11, 2022
Kijewski added a commit to Kijewski/iana-time-zone that referenced this issue Aug 11, 2022
Kijewski added a commit to Kijewski/iana-time-zone that referenced this issue Aug 11, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants