|
| 1 | +// This file is part of the uutils coreutils package. |
| 2 | +// |
| 3 | +// For the full copyright and license information, please view the LICENSE |
| 4 | +// file that was distributed with this source code. |
| 5 | + |
| 6 | +//! Locale detection for time format preferences |
| 7 | +
|
| 8 | +// nl_langinfo is available on glibc (Linux), Apple platforms, and BSDs |
| 9 | +// but not on Android, Redox or other minimal Unix systems |
| 10 | +#[cfg(any( |
| 11 | + target_os = "linux", |
| 12 | + target_vendor = "apple", |
| 13 | + target_os = "freebsd", |
| 14 | + target_os = "netbsd", |
| 15 | + target_os = "openbsd", |
| 16 | + target_os = "dragonfly" |
| 17 | +))] |
| 18 | +use std::ffi::CStr; |
| 19 | +#[cfg(any( |
| 20 | + target_os = "linux", |
| 21 | + target_vendor = "apple", |
| 22 | + target_os = "freebsd", |
| 23 | + target_os = "netbsd", |
| 24 | + target_os = "openbsd", |
| 25 | + target_os = "dragonfly" |
| 26 | +))] |
| 27 | +use std::sync::OnceLock; |
| 28 | + |
| 29 | +/// Cached result of locale time format detection |
| 30 | +#[cfg(any( |
| 31 | + target_os = "linux", |
| 32 | + target_vendor = "apple", |
| 33 | + target_os = "freebsd", |
| 34 | + target_os = "netbsd", |
| 35 | + target_os = "openbsd", |
| 36 | + target_os = "dragonfly" |
| 37 | +))] |
| 38 | +static TIME_FORMAT_CACHE: OnceLock<bool> = OnceLock::new(); |
| 39 | + |
| 40 | +/// Internal function that performs the actual locale detection |
| 41 | +#[cfg(any( |
| 42 | + target_os = "linux", |
| 43 | + target_vendor = "apple", |
| 44 | + target_os = "freebsd", |
| 45 | + target_os = "netbsd", |
| 46 | + target_os = "openbsd", |
| 47 | + target_os = "dragonfly" |
| 48 | +))] |
| 49 | +fn detect_12_hour_format() -> bool { |
| 50 | + unsafe { |
| 51 | + // Set locale from environment |
| 52 | + libc::setlocale(libc::LC_TIME, c"".as_ptr()); |
| 53 | + |
| 54 | + // Get the date/time format string from locale |
| 55 | + let d_t_fmt_ptr = libc::nl_langinfo(libc::D_T_FMT); |
| 56 | + if d_t_fmt_ptr.is_null() { |
| 57 | + return false; |
| 58 | + } |
| 59 | + |
| 60 | + let Ok(format) = CStr::from_ptr(d_t_fmt_ptr).to_str() else { |
| 61 | + return false; |
| 62 | + }; |
| 63 | + |
| 64 | + // Check for 12-hour indicators first (higher priority) |
| 65 | + // %I = hour (01-12), %l = hour (1-12) space-padded, %r = 12-hour time with AM/PM |
| 66 | + if format.contains("%I") || format.contains("%l") || format.contains("%r") { |
| 67 | + return true; |
| 68 | + } |
| 69 | + |
| 70 | + // If we find 24-hour indicators, it's definitely not 12-hour |
| 71 | + // %H = hour (00-23), %k = hour (0-23) space-padded, %R = %H:%M, %T = %H:%M:%S |
| 72 | + if format.contains("%H") |
| 73 | + || format.contains("%k") |
| 74 | + || format.contains("%R") |
| 75 | + || format.contains("%T") |
| 76 | + { |
| 77 | + return false; |
| 78 | + } |
| 79 | + |
| 80 | + // Also check the time-only format as a fallback |
| 81 | + let t_fmt_ptr = libc::nl_langinfo(libc::T_FMT); |
| 82 | + if !t_fmt_ptr.is_null() { |
| 83 | + if let Ok(time_format) = CStr::from_ptr(t_fmt_ptr).to_str() { |
| 84 | + if time_format.contains("%I") |
| 85 | + || time_format.contains("%l") |
| 86 | + || time_format.contains("%r") |
| 87 | + { |
| 88 | + return true; |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + // Check if there's a specific 12-hour format defined |
| 94 | + let t_fmt_ampm_ptr = libc::nl_langinfo(libc::T_FMT_AMPM); |
| 95 | + if !t_fmt_ampm_ptr.is_null() { |
| 96 | + if let Ok(ampm_format) = CStr::from_ptr(t_fmt_ampm_ptr).to_str() { |
| 97 | + // If T_FMT_AMPM is non-empty and not the same as T_FMT, locale supports 12-hour |
| 98 | + if !ampm_format.is_empty() && ampm_format != format { |
| 99 | + return true; |
| 100 | + } |
| 101 | + } |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + // Default to 24-hour format if we can't determine |
| 106 | + false |
| 107 | +} |
| 108 | + |
| 109 | +/// Detects whether the current locale prefers 12-hour or 24-hour time format |
| 110 | +/// Results are cached for performance |
| 111 | +#[cfg(any( |
| 112 | + target_os = "linux", |
| 113 | + target_vendor = "apple", |
| 114 | + target_os = "freebsd", |
| 115 | + target_os = "netbsd", |
| 116 | + target_os = "openbsd", |
| 117 | + target_os = "dragonfly" |
| 118 | +))] |
| 119 | +pub fn uses_12_hour_format() -> bool { |
| 120 | + *TIME_FORMAT_CACHE.get_or_init(detect_12_hour_format) |
| 121 | +} |
| 122 | + |
| 123 | +/// Cached default format string |
| 124 | +#[cfg(any( |
| 125 | + target_os = "linux", |
| 126 | + target_vendor = "apple", |
| 127 | + target_os = "freebsd", |
| 128 | + target_os = "netbsd", |
| 129 | + target_os = "openbsd", |
| 130 | + target_os = "dragonfly" |
| 131 | +))] |
| 132 | +static DEFAULT_FORMAT_CACHE: OnceLock<&'static str> = OnceLock::new(); |
| 133 | + |
| 134 | +/// Get the locale-appropriate default format string for date output |
| 135 | +/// This respects the locale's preference for 12-hour vs 24-hour time |
| 136 | +/// Results are cached for performance (following uucore patterns) |
| 137 | +#[cfg(any( |
| 138 | + target_os = "linux", |
| 139 | + target_vendor = "apple", |
| 140 | + target_os = "freebsd", |
| 141 | + target_os = "netbsd", |
| 142 | + target_os = "openbsd", |
| 143 | + target_os = "dragonfly" |
| 144 | +))] |
| 145 | +pub fn get_locale_default_format() -> &'static str { |
| 146 | + DEFAULT_FORMAT_CACHE.get_or_init(|| { |
| 147 | + if uses_12_hour_format() { |
| 148 | + // Use 12-hour format with AM/PM |
| 149 | + "%a %b %e %r %Z %Y" |
| 150 | + } else { |
| 151 | + // Use 24-hour format |
| 152 | + "%a %b %e %X %Z %Y" |
| 153 | + } |
| 154 | + }) |
| 155 | +} |
| 156 | + |
| 157 | +/// On platforms without nl_langinfo support, use 24-hour format by default |
| 158 | +#[cfg(not(any( |
| 159 | + target_os = "linux", |
| 160 | + target_vendor = "apple", |
| 161 | + target_os = "freebsd", |
| 162 | + target_os = "netbsd", |
| 163 | + target_os = "openbsd", |
| 164 | + target_os = "dragonfly" |
| 165 | +)))] |
| 166 | +pub fn get_locale_default_format() -> &'static str { |
| 167 | + "%a %b %e %X %Z %Y" |
| 168 | +} |
| 169 | + |
| 170 | +#[cfg(test)] |
| 171 | +mod tests { |
| 172 | + use super::*; |
| 173 | + |
| 174 | + #[test] |
| 175 | + #[cfg(any( |
| 176 | + target_os = "linux", |
| 177 | + target_vendor = "apple", |
| 178 | + target_os = "freebsd", |
| 179 | + target_os = "netbsd", |
| 180 | + target_os = "openbsd", |
| 181 | + target_os = "dragonfly" |
| 182 | + ))] |
| 183 | + fn test_locale_detection() { |
| 184 | + // Just verify the function doesn't panic |
| 185 | + let _ = uses_12_hour_format(); |
| 186 | + let _ = get_locale_default_format(); |
| 187 | + } |
| 188 | + |
| 189 | + #[test] |
| 190 | + fn test_default_format_contains_valid_codes() { |
| 191 | + let format = get_locale_default_format(); |
| 192 | + assert!(format.contains("%a")); // abbreviated weekday |
| 193 | + assert!(format.contains("%b")); // abbreviated month |
| 194 | + assert!(format.contains("%Y")); // year |
| 195 | + assert!(format.contains("%Z")); // timezone |
| 196 | + } |
| 197 | +} |
0 commit comments