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

Update determine_time_zone function to check TZ #725

Merged
merged 2 commits into from
Oct 8, 2020
Merged

Update determine_time_zone function to check TZ #725

merged 2 commits into from
Oct 8, 2020

Conversation

kbravh
Copy link
Contributor

@kbravh kbravh commented Aug 26, 2020

Instead of defaulting immediately to /etc/filename for the timezone, we can first check whether the TZ environment variable is set. If so, we can pull the corresponding timezone file from /usr/share/zoneinfo. Closes #453.

Instead of defaulting immediately to /etc/filename for the timezone, we can first check whether the TZ environment variable is set. If so, we can pull the corresponding timezone file from /usr/share/zoneinfo. Closes #453.
@kbravh
Copy link
Contributor Author

kbravh commented Aug 31, 2020

@poliorcetics Thanks for the feedback! I tried out the suggestion you made; I definitely like deconstructing the timezone from the environment variable instead of unwrapping, so I've added that change.

The only issue is that we have to use the return keyword from inside the if block; we can only omit the return if we want to return the last expression in a function, otherwise it returns an empty type (). So I've followed the pattern on the Rust docs function page and I'm using return for the first case where the TZ variable is set. Otherwise, I've left the final expression as the last statement so it's automatically returned.

if let Ok(file) = env::var("TZ") {
        return TimeZone::from_file(format!("/usr/share/zoneinfo/{}", file));
    }
    
TimeZone::from_file("/etc/localtime")

@poliorcetics
Copy link
Contributor

That's because I put ; in my suggestion, the fix should be:

    if let Ok(file) = env::var("TZ") {
        TimeZone::from_file(format!("/usr/share/zoneinfo/{}", file)) // no ;
    } else {
        TimeZone::from_file("/etc/localtime") // no ;
    }

@kbravh
Copy link
Contributor Author

kbravh commented Sep 3, 2020

@poliorcetics That did the trick! Thanks for the help.

@ogham ogham merged commit 8b852cb into ogham:master Oct 8, 2020
@ogham
Copy link
Owner

ogham commented Oct 8, 2020

Thanks for this!

@kbravh kbravh deleted the tz-variable-patch branch October 8, 2020 21:44
@themadsens
Copy link

themadsens commented Apr 6, 2021

Just bitten by this .. Out of curisosity: Why not use libc::mktime instead of thinking you can outsmart libc? Then all of these timezone issues would just disappear!

@ariasuni
Copy link
Collaborator

ariasuni commented May 4, 2021

@themadsens Well I can’t speak for ogham, but I would gladly use a libc function if it would make things easier. Right now, it wouldn’t be very useful, and #712 which is reasonably simple would fix the remaining issues.

It’s certainly an option to drop zoneinfo_compiled and datetime and just use a few C functions, but I think trying to do things in Rust as much as possible can be useful, either to push for more Rust libraries, or simply to for exa’s code to be an example.

15cm added a commit to 15cm/nixfiles that referenced this pull request Dec 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

Successfully merging this pull request may close these issues.

Exa does not read the TZ environment variable, but it is a standard way to convey timezone data on musl
5 participants