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

build docker error, not find localtime in unix #32

Closed
baoyachi opened this issue Jul 15, 2022 · 1 comment
Closed

build docker error, not find localtime in unix #32

baoyachi opened this issue Jul 15, 2022 · 1 comment

Comments

@baoyachi
Copy link

~ docker run -it ghcr.io/cross-rs/x86_64-unknown-linux-gnu:main /bin/sh
# cd /etc
# ls
X11                     binfmt.d              debian_version  fstab      hosts           issue         legal          magic           opt         profile.d  rc5.d        selinux  sysctl.conf  update-motd.d
adduser.conf            ca-certificates       default         gai.conf   init            issue.net     libaudit.conf  magic.mime      os-release  rc.local   rc6.d        shadow   sysctl.d     xdg
alternatives            ca-certificates.conf  deluser.conf    group      init.d          kernel        localtime      mke2fs.conf     pam.conf    rc0.d      rcS.d        shells   systemd
apt                     cron.daily            dhcp            gshadow    inputrc         ld.so.cache   login.defs     modules-load.d  pam.d       rc1.d      resolv.conf  skel     terminfo
bash.bashrc             cron.weekly           dpkg            gss        insserv         ld.so.conf    logrotate.d    mtab            passwd      rc2.d      rmt          ssl      timezone
bash_completion.d       dbus-1                emacs           host.conf  insserv.conf    ld.so.conf.d  lsb-release    networks        perl        rc3.d      securetty    subgid   tmpfiles.d
bindresvport.blacklist  debconf.conf          environment     hostname   insserv.conf.d  ldap          machine-id     nsswitch.conf   profile     rc4.d      security     subuid   udev 

not find /etc/localtime file.

about code:

    pub fn local() -> Result<Self, TzError> {
        #[cfg(not(unix))]
        let local_time_zone = Self::utc();

        #[cfg(unix)]
        let local_time_zone = Self::from_posix_tz("localtime")?;

        Ok(local_time_zone)
    }

    /// Construct a time zone from the contents of a time zone file
    pub fn from_tz_data(bytes: &[u8]) -> Result<Self, TzError> {
        parse_tz_file(bytes)
    }

    /// Construct a time zone from a POSIX TZ string, as described in [the POSIX documentation of the `TZ` environment variable](https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap08.html).
    pub fn from_posix_tz(tz_string: &str) -> Result<Self, TzError> {
        if tz_string.is_empty() {
            return Err(TzError::TzStringError(TzStringError::InvalidTzString("empty TZ string")));
        }

        if tz_string == "localtime" {
            return parse_tz_file(&fs::read("/etc/localtime")?);
        }

        let read = |mut file: File| -> io::Result<_> {
            let mut bytes = Vec::new();
            file.read_to_end(&mut bytes)?;
            Ok(bytes)
        };

ref: https://github.com/x-hgg-x/tz-rs/blob/master/src/timezone/mod.rs#L500-L518

@x-hgg-x
Copy link
Owner

x-hgg-x commented Jul 15, 2022

The tzdata package is not installed in the docker, so the symlink /etc/localtime is either absent or broken.
In this case, the TimeZone::local() function returns an error, and you need to handle it in your code.
On non-UNIX platforms, since the path /etc/localtime is always invalid, the function always returns UTC.

Note: if you are willing to embed the time zone definitions in your binary, you can use the tzdb::local_tz function, which also works on Windows since it only queries for the current time zone name.

@x-hgg-x x-hgg-x closed this as completed Jul 15, 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