-3

I am trying to figure out how I can use the enum variants which are part of Chrono Tz.

For example, there is a variant called Europe__London.

https://docs.rs/chrono-tz/latest/chrono_tz/enum.Tz.html#variant.Europe__London

I don't understand how to use it, and I haven't been able to find any documentation or other resources to help. I have attempted to inspect the source code, but the browser crashed while trying to open it. (Out of memory.)

I also tried looking at the source on Github, but it appears, unless I misunderstand, that the source code is actually being auto generated by querying an IANA database via a web interface.

Can a chrono::Tz object be created from a String or &str.

FreelanceConsultant
  • 13,167
  • 27
  • 115
  • 225
  • In the title and the body of the question you're asking two different things. So what is your actual question? – Chayim Friedman Aug 29 '23 at 13:22
  • But I'll try another, last time: in the body you're asking for examples on how to use the enum. I admit it may be interpreted in various ways, but generally that means you want a small example of how to use this type with `chrono`. In the title, you're asking a completely different thing (and also more specific, which is a good thing on its own), how to parse it from a string. – Chayim Friedman Aug 29 '23 at 13:34
  • 2
    OP, at a certain point when *you* perceive "consistently poor behavior" but no one else does you might want to ask yourself where the problem really lies, I can't remember a single occasion where Chayim behaved poorly on here or anywhere else, on the contrary all I've experienced so far from them is constructive criticism, comments that help improve the question, pointing out potential errors in my answers, ... in short nothing but exemplary behavior. I can't really say the same for you. – cafce25 Aug 29 '23 at 14:28
  • 2
    Is a sign of poor research before asking. Which is not a good thing. You may (and should) want to research better before asking. I don't know how much time you invest in researching before asking, but consider e.g. doubling it. – Chayim Friedman Aug 29 '23 at 15:08

1 Answers1

0

This appears to be possible using the following code.

// timezone = &str ...

let chrono_timezone = Tz::from_str(timezone)
    .expect("failed to convert string to chrono tz");

This is how I reverse-engineered what the valid timezone strings were:

// Cargo.toml: 
// chrono-tz = "0.8.3"
use chrono::Tz;

fn main() {
    
    let chrono_timezone = Tz::Europe__London;

    println!("{:?}", chrono_timezone);

    let timezone_str = "Europe/London";
    
    let chrono_timezone = Tz::from_str(timezone_str)
        .expect("failed to convert string to chrono tz");

    println!("{:?}", chrono_timezone);
}

I knew to use Europe__London as it features in this list.

That appears to be equivalent to this:

let chrono_timezone = chrono_tz::Europe::London;

See this link.

FreelanceConsultant
  • 13,167
  • 27
  • 115
  • 225