| 1 | use std::cell::Cell; |
| 2 | |
| 3 | use chrono::{DateTime, Datelike, FixedOffset, NaiveDate, Utc, Weekday}; |
| 4 | |
| 5 | use crate::theme; |
| 6 | |
| 7 | const BADGE_CAP: u32 = 99; |
| 8 | const WEEKDAY_WINDOW_DAYS: i64 = 6; |
| 9 | const TWELVE_HOUR_LOCALES: [&str; 6] = ["en-us", "en-ca", "en-ph", "en-au", "en-nz", "en-in"]; |
| 10 | const DOTTED_LOCALES: [&str; 7] = ["de", "da", "nb", "no", "fi", "cs", "sk"]; |
| 11 | const PADDED_DOT_LOCALES: [&str; 4] = ["ru", "pl", "tr", "uk"]; |
| 12 | |
| 13 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 14 | pub struct DateStyle { |
| 15 | day_month: &'static str, |
| 16 | day_month_year: &'static str, |
| 17 | clock: &'static str, |
| 18 | } |
| 19 | |
| 20 | thread_local! { |
| 21 | static DATE_STYLE: Cell<Option<DateStyle>> = const { Cell::new(None) }; |
| 22 | } |
| 23 | |
| 24 | pub fn set_date_locale(locale: Option<&str>) { |
| 25 | DATE_STYLE.with(|style| style.set(locale.map(date_style))); |
| 26 | } |
| 27 | |
| 28 | fn current_date_style() -> Option<DateStyle> { |
| 29 | DATE_STYLE.with(Cell::get) |
| 30 | } |
| 31 | |
| 32 | pub fn clock_format() -> &'static str { |
| 33 | current_date_style().map_or("%H:%M", |style| style.clock) |
| 34 | } |
| 35 | |
| 36 | /// Numeric day and month in the order of the account's display language, as `Intl.DateTimeFormat` does in Teams. |
| 37 | pub fn date_style(locale: &str) -> DateStyle { |
| 38 | let locale = locale.to_ascii_lowercase(); |
| 39 | let language = locale.split(['-', '_']).next().unwrap_or_default(); |
| 40 | let twelve_hour = TWELVE_HOUR_LOCALES.contains(&locale.as_str()); |
| 41 | let clock = match language { |
| 42 | _ if !twelve_hour => "%H:%M", |
| 43 | _ if locale == "en-au" || locale == "en-nz" || locale == "en-in" => "%-I:%M %P", |
| 44 | _ => "%-I:%M %p", |
| 45 | }; |
| 46 | let (day_month, day_month_year) = match language { |
| 47 | _ if locale == "en-us" || locale == "en-ph" || locale == "en-ca" => ("%-m/%-d", "%-m/%-d/%Y"), |
| 48 | "ja" | "zh" => ("%-m/%-d", "%Y/%-m/%-d"), |
| 49 | "nl" => ("%-d-%-m", "%-d-%-m-%Y"), |
| 50 | "sv" => ("%-d/%-m", "%Y-%m-%d"), |
| 51 | language if DOTTED_LOCALES.contains(&language) => ("%-d.%-m.", "%-d.%-m.%Y"), |
| 52 | language if PADDED_DOT_LOCALES.contains(&language) => ("%d.%m", "%d.%m.%Y"), |
| 53 | _ => ("%d/%m", "%d/%m/%Y"), |
| 54 | }; |
| 55 | DateStyle { |
| 56 | day_month, |
| 57 | day_month_year, |
| 58 | clock, |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | pub fn palette_index(key: &str) -> usize { |
| 63 | let mut hash: u32 = 0x811c_9dc5; |
| 64 | for byte in key.bytes() { |
| 65 | hash ^= u32::from(byte); |
| 66 | hash = hash.wrapping_mul(0x0100_0193); |
| 67 | } |
| 68 | hash as usize % theme::avatar_palette_len() |
| 69 | } |
| 70 | |
| 71 | pub fn initials(name: &str) -> String { |
| 72 | let words: Vec<&str> = name |
| 73 | .split(|character: char| !character.is_alphanumeric()) |
| 74 | .filter(|word| !word.is_empty()) |
| 75 | .collect(); |
| 76 | let letters: Vec<char> = match words.as_slice() { |
| 77 | [] => return "?".to_owned(), |
| 78 | [single] => single.chars().take(2).collect(), |
| 79 | [first, .., last] => first.chars().take(1).chain(last.chars().take(1)).collect(), |
| 80 | }; |
| 81 | letters.into_iter().flat_map(char::to_uppercase).collect() |
| 82 | } |
| 83 | |
| 84 | pub fn first_name(name: &str) -> &str { |
| 85 | name.split_whitespace().next().unwrap_or(name) |
| 86 | } |
| 87 | |
| 88 | pub fn badge_text(count: u32) -> String { |
| 89 | if count > BADGE_CAP { |
| 90 | format!("{BADGE_CAP}+") |
| 91 | } else { |
| 92 | count.to_string() |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | fn weekday_label(weekday: Weekday) -> &'static str { |
| 97 | match weekday { |
| 98 | Weekday::Mon => "Mon", |
| 99 | Weekday::Tue => "Tue", |
| 100 | Weekday::Wed => "Wed", |
| 101 | Weekday::Thu => "Thu", |
| 102 | Weekday::Fri => "Fri", |
| 103 | Weekday::Sat => "Sat", |
| 104 | Weekday::Sun => "Sun", |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | pub fn file_size_label(bytes: u64) -> String { |
| 109 | const KIB: f64 = 1024.; |
| 110 | let value = bytes as f64; |
| 111 | if value < KIB { |
| 112 | format!("{bytes} B") |
| 113 | } else if value < KIB * KIB { |
| 114 | format!("{:.0} KB", value / KIB) |
| 115 | } else { |
| 116 | format!("{:.1} MB", value / (KIB * KIB)) |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | pub fn short_version(build_id: &str, package_version: &str) -> String { |
| 121 | let commit = build_id.split('-').next().unwrap_or(build_id); |
| 122 | format!("v{package_version} ({commit})") |
| 123 | } |
| 124 | |
| 125 | pub fn list_time_label(time: DateTime<Utc>, today: NaiveDate, offset: FixedOffset) -> String { |
| 126 | list_time_label_in(current_date_style(), time, today, offset) |
| 127 | } |
| 128 | |
| 129 | pub fn list_time_label_in( |
| 130 | style: Option<DateStyle>, |
| 131 | time: DateTime<Utc>, |
| 132 | today: NaiveDate, |
| 133 | offset: FixedOffset, |
| 134 | ) -> String { |
| 135 | let local = time.with_timezone(&offset); |
| 136 | let day = local.date_naive(); |
| 137 | let days_ago = (today - day).num_days(); |
| 138 | if days_ago <= 0 { |
| 139 | local.format(style.map_or("%H:%M", |style| style.clock)).to_string() |
| 140 | } else if days_ago <= WEEKDAY_WINDOW_DAYS { |
| 141 | weekday_label(local.weekday()).to_owned() |
| 142 | } else { |
| 143 | local |
| 144 | .format(style.map_or("%b %-d", |style| style.day_month)) |
| 145 | .to_string() |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | /// The last activity next to a channel name: the time today, else day and month, with the year once older. |
| 150 | pub fn channel_time_label(time: DateTime<Utc>, today: NaiveDate, offset: FixedOffset) -> String { |
| 151 | channel_time_label_in(current_date_style(), time, today, offset) |
| 152 | } |
| 153 | |
| 154 | pub fn channel_time_label_in( |
| 155 | style: Option<DateStyle>, |
| 156 | time: DateTime<Utc>, |
| 157 | today: NaiveDate, |
| 158 | offset: FixedOffset, |
| 159 | ) -> String { |
| 160 | let local = time.with_timezone(&offset); |
| 161 | let pattern = if local.date_naive() >= today { |
| 162 | style.map_or("%H:%M", |style| style.clock) |
| 163 | } else if local.year() == today.year() { |
| 164 | style.map_or("%b %-d", |style| style.day_month) |
| 165 | } else { |
| 166 | style.map_or("%b %-d, %Y", |style| style.day_month_year) |
| 167 | }; |
| 168 | local.format(pattern).to_string() |
| 169 | } |
| 170 | |
| 171 | pub fn post_time_label(time: DateTime<Utc>, today: NaiveDate, offset: FixedOffset) -> String { |
| 172 | post_time_label_in(current_date_style(), time, today, offset) |
| 173 | } |
| 174 | |
| 175 | pub fn post_time_label_in( |
| 176 | style: Option<DateStyle>, |
| 177 | time: DateTime<Utc>, |
| 178 | today: NaiveDate, |
| 179 | offset: FixedOffset, |
| 180 | ) -> String { |
| 181 | let local = time.with_timezone(&offset); |
| 182 | let day = local.date_naive(); |
| 183 | let days_ago = (today - day).num_days(); |
| 184 | let clock = local.format(style.map_or("%H:%M", |style| style.clock)); |
| 185 | let same_year = day.year() == today.year(); |
| 186 | if days_ago <= 0 { |
| 187 | clock.to_string() |
| 188 | } else if days_ago == 1 { |
| 189 | format!("Yesterday {clock}") |
| 190 | } else if days_ago <= WEEKDAY_WINDOW_DAYS { |
| 191 | format!("{} {clock}", weekday_label(local.weekday())) |
| 192 | } else if let Some(style) = style { |
| 193 | let date = if same_year { |
| 194 | style.day_month |
| 195 | } else { |
| 196 | style.day_month_year |
| 197 | }; |
| 198 | format!("{} {clock}", local.format(date)) |
| 199 | } else if same_year { |
| 200 | format!("{} {clock}", local.format("%b %-d")) |
| 201 | } else { |
| 202 | format!("{} {clock}", local.format("%b %-d, %Y")) |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | pub fn day_label(day: NaiveDate, today: NaiveDate) -> String { |
| 207 | if day == today { |
| 208 | "Today".to_owned() |
| 209 | } else if today.pred_opt() == Some(day) { |
| 210 | "Yesterday".to_owned() |
| 211 | } else { |
| 212 | format!( |
| 213 | "{}, {}", |
| 214 | weekday_label(day.weekday()), |
| 215 | day.format("%b %-d, %Y") |
| 216 | ) |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | #[cfg(test)] |
| 221 | mod tests { |
| 222 | use chrono::TimeZone; |
| 223 | |
| 224 | use super::*; |
| 225 | |
| 226 | #[test] |
| 227 | fn the_clock_follows_the_display_language() { |
| 228 | set_date_locale(Some("en-US")); |
| 229 | assert_eq!(clock_format(), "%-I:%M %p"); |
| 230 | set_date_locale(Some("de-DE")); |
| 231 | assert_eq!(clock_format(), "%H:%M"); |
| 232 | set_date_locale(None); |
| 233 | assert_eq!(clock_format(), "%H:%M"); |
| 234 | } |
| 235 | |
| 236 | fn utc(day: u32, hour: u32) -> DateTime<Utc> { |
| 237 | Utc.with_ymd_and_hms(2026, 10, day, hour, 5, 0).unwrap() |
| 238 | } |
| 239 | |
| 240 | fn today() -> NaiveDate { |
| 241 | NaiveDate::from_ymd_opt(2026, 10, 7).unwrap() |
| 242 | } |
| 243 | |
| 244 | fn zero() -> FixedOffset { |
| 245 | FixedOffset::east_opt(0).unwrap() |
| 246 | } |
| 247 | |
| 248 | #[test] |
| 249 | fn channel_times_show_the_clock_today_then_day_and_month_then_the_year() { |
| 250 | let style = Some(date_style("en-gb")); |
| 251 | let label = |time| channel_time_label_in(style, time, today(), zero()); |
| 252 | assert_eq!(label(utc(7, 8)), "08:05"); |
| 253 | assert_eq!(label(utc(1, 8)), "01/10"); |
| 254 | assert_eq!( |
| 255 | label(Utc.with_ymd_and_hms(2025, 10, 23, 8, 0, 0).unwrap()), |
| 256 | "23/10/2025" |
| 257 | ); |
| 258 | } |
| 259 | |
| 260 | #[test] |
| 261 | fn palette_index_is_stable_and_in_range() { |
| 262 | assert_eq!(palette_index("user-1"), palette_index("user-1")); |
| 263 | for key in [ |
| 264 | "a", |
| 265 | "b", |
| 266 | "demo-ada", |
| 267 | "", |
| 268 | "00000000-0000-0000-0000-000000000001", |
| 269 | ] { |
| 270 | assert!(palette_index(key) < theme::avatar_palette_len()); |
| 271 | } |
| 272 | } |
| 273 | |
| 274 | #[test] |
| 275 | fn palette_index_spreads_over_several_colors() { |
| 276 | let used: std::collections::HashSet<usize> = (0..40) |
| 277 | .map(|number| palette_index(&format!("user-{number}"))) |
| 278 | .collect(); |
| 279 | assert!(used.len() >= 4); |
| 280 | } |
| 281 | |
| 282 | #[test] |
| 283 | fn initials_use_first_and_last_word() { |
| 284 | assert_eq!(initials("Mara Lindqvist"), "ML"); |
| 285 | assert_eq!(initials("Ada"), "AD"); |
| 286 | assert_eq!(initials("anna maria von Berg"), "AB"); |
| 287 | assert_eq!(initials(" "), "?"); |
| 288 | assert_eq!(initials("Release planning"), "RP"); |
| 289 | } |
| 290 | |
| 291 | #[test] |
| 292 | fn first_name_takes_the_first_word() { |
| 293 | assert_eq!(first_name("Priya Nair"), "Priya"); |
| 294 | assert_eq!(first_name("Priya"), "Priya"); |
| 295 | } |
| 296 | |
| 297 | #[test] |
| 298 | fn file_sizes_use_binary_units() { |
| 299 | assert_eq!(file_size_label(900), "900 B"); |
| 300 | assert_eq!(file_size_label(48_213), "47 KB"); |
| 301 | assert_eq!(file_size_label(5 * 1024 * 1024 + 300_000), "5.3 MB"); |
| 302 | } |
| 303 | |
| 304 | #[test] |
| 305 | fn badge_caps_at_99_plus() { |
| 306 | assert_eq!(badge_text(1), "1"); |
| 307 | assert_eq!(badge_text(99), "99"); |
| 308 | assert_eq!(badge_text(100), "99+"); |
| 309 | assert_eq!(badge_text(4000), "99+"); |
| 310 | } |
| 311 | |
| 312 | #[test] |
| 313 | fn list_time_is_clock_today_weekday_this_week_date_before() { |
| 314 | assert_eq!(list_time_label(utc(7, 13), today(), zero()), "13:05"); |
| 315 | assert_eq!(list_time_label(utc(5, 13), today(), zero()), "Mon"); |
| 316 | assert_eq!(list_time_label(utc(6, 9), today(), zero()), "Tue"); |
| 317 | assert_eq!(list_time_label(utc(1, 13), today(), zero()), "Thu"); |
| 318 | let september = Utc.with_ymd_and_hms(2026, 9, 30, 13, 0, 0).unwrap(); |
| 319 | assert_eq!(list_time_label(september, today(), zero()), "Sep 30"); |
| 320 | } |
| 321 | |
| 322 | #[test] |
| 323 | fn list_time_respects_the_offset_for_the_day_boundary() { |
| 324 | let plus_two = FixedOffset::east_opt(2 * 3600).unwrap(); |
| 325 | assert_eq!(list_time_label(utc(6, 23), today(), plus_two), "01:05"); |
| 326 | } |
| 327 | |
| 328 | #[test] |
| 329 | fn short_version_keeps_the_commit_only() { |
| 330 | assert_eq!( |
| 331 | short_version("687e2dc-20261007T124355Z", "0.1.0"), |
| 332 | "v0.1.0 (687e2dc)" |
| 333 | ); |
| 334 | assert_eq!(short_version("dev", "0.1.0"), "v0.1.0 (dev)"); |
| 335 | } |
| 336 | |
| 337 | #[test] |
| 338 | fn post_time_adds_the_day_when_not_today() { |
| 339 | assert_eq!(post_time_label(utc(7, 13), today(), zero()), "13:05"); |
| 340 | assert_eq!( |
| 341 | post_time_label(utc(6, 9), today(), zero()), |
| 342 | "Yesterday 09:05" |
| 343 | ); |
| 344 | assert_eq!(post_time_label(utc(5, 13), today(), zero()), "Mon 13:05"); |
| 345 | let september = Utc.with_ymd_and_hms(2026, 9, 30, 8, 42, 0).unwrap(); |
| 346 | assert_eq!(post_time_label(september, today(), zero()), "Sep 30 08:42"); |
| 347 | let last_year = Utc.with_ymd_and_hms(2020, 5, 22, 8, 42, 0).unwrap(); |
| 348 | assert_eq!( |
| 349 | post_time_label(last_year, today(), zero()), |
| 350 | "May 22, 2020 08:42" |
| 351 | ); |
| 352 | } |
| 353 | |
| 354 | #[test] |
| 355 | fn post_time_follows_the_display_language_like_teams() { |
| 356 | let september = Utc.with_ymd_and_hms(2026, 9, 24, 20, 54, 0).unwrap(); |
| 357 | let label = |locale: &str| { |
| 358 | post_time_label_in(Some(date_style(locale)), september, today(), zero()) |
| 359 | }; |
| 360 | assert_eq!(label("en-GB"), "24/09 20:54"); |
| 361 | assert_eq!(label("de-DE"), "24.9. 20:54"); |
| 362 | assert_eq!(label("en-US"), "9/24 8:54 PM"); |
| 363 | let last_year = Utc.with_ymd_and_hms(2020, 5, 2, 8, 42, 0).unwrap(); |
| 364 | assert_eq!( |
| 365 | post_time_label_in(Some(date_style("en-GB")), last_year, today(), zero()), |
| 366 | "02/05/2020 08:42" |
| 367 | ); |
| 368 | } |
| 369 | |
| 370 | #[test] |
| 371 | fn list_time_follows_the_display_language_like_teams() { |
| 372 | let september = Utc.with_ymd_and_hms(2026, 9, 24, 20, 54, 0).unwrap(); |
| 373 | let style = Some(date_style("en-GB")); |
| 374 | assert_eq!(list_time_label_in(style, september, today(), zero()), "24/09"); |
| 375 | assert_eq!(list_time_label_in(style, utc(7, 13), today(), zero()), "13:05"); |
| 376 | } |
| 377 | |
| 378 | #[test] |
| 379 | fn day_labels() { |
| 380 | assert_eq!(day_label(today(), today()), "Today"); |
| 381 | assert_eq!(day_label(today().pred_opt().unwrap(), today()), "Yesterday"); |
| 382 | assert_eq!( |
| 383 | day_label(NaiveDate::from_ymd_opt(2026, 10, 1).unwrap(), today()), |
| 384 | "Thu, Oct 1, 2026" |
| 385 | ); |
| 386 | } |
| 387 | } |