| 1 | use std::cell::Cell; |
| 2 | |
| 3 | use chrono::{DateTime, Datelike, FixedOffset, Local, NaiveDate, Utc, Weekday}; |
| 4 | |
| 5 | use crate::theme; |
| 6 | |
| 7 | /// `Local` follows the daylight saving rules at the given instant, a fixed offset is the same all year. |
| 8 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 9 | pub enum Zone { |
| 10 | Local, |
| 11 | Fixed(FixedOffset), |
| 12 | } |
| 13 | |
| 14 | impl Zone { |
| 15 | pub fn at(self, time: DateTime<Utc>) -> DateTime<FixedOffset> { |
| 16 | match self { |
| 17 | Zone::Local => time.with_timezone(&Local).fixed_offset(), |
| 18 | Zone::Fixed(offset) => time.with_timezone(&offset), |
| 19 | } |
| 20 | } |
| 21 | } |
| 22 | |
| 23 | impl From<FixedOffset> for Zone { |
| 24 | fn from(offset: FixedOffset) -> Self { |
| 25 | Zone::Fixed(offset) |
| 26 | } |
| 27 | } |
| 28 | |
| 29 | const BADGE_CAP: u32 = 99; |
| 30 | const WEEKDAY_WINDOW_DAYS: i64 = 6; |
| 31 | const TWELVE_HOUR_LOCALES: [&str; 6] = ["en-us", "en-ca", "en-ph", "en-au", "en-nz", "en-in"]; |
| 32 | const DOTTED_LOCALES: [&str; 7] = ["de", "da", "nb", "no", "fi", "cs", "sk"]; |
| 33 | const PADDED_DOT_LOCALES: [&str; 4] = ["ru", "pl", "tr", "uk"]; |
| 34 | /// Regions whose week starts on Sunday (CLDR first-day data for the common locales). |
| 35 | const SUNDAY_FIRST_LOCALES: [&str; 13] = [ |
| 36 | "en-us", "en-ca", "en-ph", "en-au", "en-nz", "en-in", "ja", "ko", "zh-tw", "he", "pt-br", |
| 37 | "es-mx", "zh-hk", |
| 38 | ]; |
| 39 | /// Arabic regions that start on Monday instead of Saturday. |
| 40 | const ARABIC_MONDAY_LOCALES: [&str; 2] = ["ar-ma", "ar-tn"]; |
| 41 | |
| 42 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 43 | pub struct DateStyle { |
| 44 | day_month: &'static str, |
| 45 | day_month_year: &'static str, |
| 46 | clock: &'static str, |
| 47 | first_day: Weekday, |
| 48 | } |
| 49 | |
| 50 | thread_local! { |
| 51 | static DATE_STYLE: Cell<Option<DateStyle>> = const { Cell::new(None) }; |
| 52 | } |
| 53 | |
| 54 | pub fn set_date_locale(locale: Option<&str>) { |
| 55 | DATE_STYLE.with(|style| style.set(locale.map(date_style))); |
| 56 | } |
| 57 | |
| 58 | /// Teams desktop formats dates and times with the OS regional format, not the account language. |
| 59 | pub fn os_regional_locale() -> Option<String> { |
| 60 | sys_locale::get_locale() |
| 61 | .as_deref() |
| 62 | .and_then(normalize_locale) |
| 63 | } |
| 64 | |
| 65 | fn normalize_locale(raw: &str) -> Option<String> { |
| 66 | let tag = raw |
| 67 | .split(['.', '@']) |
| 68 | .next() |
| 69 | .unwrap_or_default() |
| 70 | .replace('_', "-"); |
| 71 | (!tag.is_empty() && tag != "C" && tag != "POSIX").then_some(tag) |
| 72 | } |
| 73 | |
| 74 | fn current_date_style() -> Option<DateStyle> { |
| 75 | DATE_STYLE.with(Cell::get) |
| 76 | } |
| 77 | |
| 78 | /// First day of the week of the regional format; Monday when it is unknown. |
| 79 | pub fn regional_first_day() -> Weekday { |
| 80 | current_date_style().map_or(Weekday::Mon, |style| style.first_day) |
| 81 | } |
| 82 | |
| 83 | /// First day of the week for a locale tag such as `en-CH` (CLDR region data). |
| 84 | pub fn first_day_for_locale(locale: &str) -> Weekday { |
| 85 | let locale = locale.to_ascii_lowercase().replace('_', "-"); |
| 86 | let language = locale.split('-').next().unwrap_or_default().to_owned(); |
| 87 | first_day_for(&locale, &language) |
| 88 | } |
| 89 | |
| 90 | fn first_day_for(locale: &str, language: &str) -> Weekday { |
| 91 | if SUNDAY_FIRST_LOCALES.contains(&locale) || SUNDAY_FIRST_LOCALES.contains(&language) { |
| 92 | Weekday::Sun |
| 93 | } else if (language == "ar" && !ARABIC_MONDAY_LOCALES.contains(&locale)) || language == "fa" { |
| 94 | Weekday::Sat |
| 95 | } else { |
| 96 | Weekday::Mon |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | pub fn clock_format() -> &'static str { |
| 101 | current_date_style().map_or("%H:%M", |style| style.clock) |
| 102 | } |
| 103 | |
| 104 | impl DateStyle { |
| 105 | fn month_first(&self) -> bool { |
| 106 | self.day_month.starts_with("%-m") |
| 107 | } |
| 108 | |
| 109 | fn day_number(&self) -> &'static str { |
| 110 | if self.day_month.starts_with("%d") { |
| 111 | "%d" |
| 112 | } else { |
| 113 | "%-d" |
| 114 | } |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | pub fn calendar_day_number(date: NaiveDate) -> String { |
| 119 | date.format(current_date_style().map_or("%-d", |style| style.day_number())) |
| 120 | .to_string() |
| 121 | } |
| 122 | |
| 123 | /// Month name in the app language (`calendar.month.mNN`), not chrono's English `%B`. |
| 124 | pub fn month_name(date: NaiveDate) -> String { |
| 125 | crate::ui_locale::tr(&format!("calendar.month.m{:02}", date.month())) |
| 126 | } |
| 127 | |
| 128 | /// "November 2026" / "November 2026" — month title of the month grid. |
| 129 | pub fn month_year_label(date: NaiveDate) -> String { |
| 130 | format!("{} {}", month_name(date), date.year()) |
| 131 | } |
| 132 | |
| 133 | const WEEKDAY_KEYS: [&str; 7] = ["mon", "tue", "wed", "thu", "fri", "sat", "sun"]; |
| 134 | |
| 135 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 136 | pub enum WeekdayWidth { |
| 137 | /// "Monday" / "Montag" |
| 138 | Full, |
| 139 | /// "Mon" / "Mo" |
| 140 | Short, |
| 141 | /// "M" / "M" |
| 142 | Letter, |
| 143 | } |
| 144 | |
| 145 | /// Weekday name in the app language; chrono's `%A`/`%a` are always English. |
| 146 | pub fn weekday_name(day: Weekday, width: WeekdayWidth) -> String { |
| 147 | let key = WEEKDAY_KEYS[day.num_days_from_monday() as usize]; |
| 148 | match width { |
| 149 | WeekdayWidth::Full => crate::ui_locale::tr(&format!("calendar.weekday.{key}")), |
| 150 | WeekdayWidth::Short => crate::ui_locale::tr(&format!("calendar.weekday_short.{key}")), |
| 151 | WeekdayWidth::Letter => crate::ui_locale::tr(&format!("calendar.weekday_short.{key}")) |
| 152 | .chars() |
| 153 | .next() |
| 154 | .map(String::from) |
| 155 | .unwrap_or_default(), |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | pub fn calendar_range_label(start: NaiveDate, end: NaiveDate) -> String { |
| 160 | let style = current_date_style(); |
| 161 | let day = |date: NaiveDate| { |
| 162 | date.format(style.map_or("%-d", |style| style.day_number())) |
| 163 | .to_string() |
| 164 | }; |
| 165 | let (start_month, end_month) = (month_name(start), month_name(end)); |
| 166 | if style.is_none_or(|style| style.month_first()) { |
| 167 | let to = format!("{end_month} {}, {}", day(end), end.year()); |
| 168 | return if start == end { |
| 169 | to |
| 170 | } else if start.year() != end.year() { |
| 171 | format!("{start_month} {}, {} - {to}", day(start), start.year()) |
| 172 | } else if start.month() != end.month() { |
| 173 | format!("{start_month} {} - {to}", day(start)) |
| 174 | } else { |
| 175 | format!("{start_month} {}-{}, {}", day(start), day(end), end.year()) |
| 176 | }; |
| 177 | } |
| 178 | let to = format!("{} {end_month}, {}", day(end), end.year()); |
| 179 | if start == end { |
| 180 | to |
| 181 | } else if start.year() != end.year() { |
| 182 | format!("{} {start_month}, {} - {to}", day(start), start.year()) |
| 183 | } else if start.month() != end.month() { |
| 184 | format!("{} {start_month} - {to}", day(start)) |
| 185 | } else { |
| 186 | format!("{}-{to}", day(start)) |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | /// Numeric day and month in the order of the account's display language, as `Intl.DateTimeFormat` does in Teams. |
| 191 | pub fn date_style(locale: &str) -> DateStyle { |
| 192 | let locale = locale.to_ascii_lowercase(); |
| 193 | let language = locale.split(['-', '_']).next().unwrap_or_default(); |
| 194 | let twelve_hour = TWELVE_HOUR_LOCALES.contains(&locale.as_str()); |
| 195 | let clock = match language { |
| 196 | _ if !twelve_hour => "%H:%M", |
| 197 | _ if locale == "en-au" || locale == "en-nz" || locale == "en-in" => "%-I:%M %P", |
| 198 | _ => "%-I:%M %p", |
| 199 | }; |
| 200 | let (day_month, day_month_year) = match language { |
| 201 | _ if locale == "en-us" || locale == "en-ph" || locale == "en-ca" => { |
| 202 | ("%-m/%-d", "%-m/%-d/%Y") |
| 203 | } |
| 204 | "ja" | "zh" => ("%-m/%-d", "%Y/%-m/%-d"), |
| 205 | "nl" => ("%-d-%-m", "%-d-%-m-%Y"), |
| 206 | "sv" => ("%-d/%-m", "%Y-%m-%d"), |
| 207 | language if DOTTED_LOCALES.contains(&language) => ("%-d.%-m.", "%-d.%-m.%Y"), |
| 208 | language if PADDED_DOT_LOCALES.contains(&language) => ("%d.%m", "%d.%m.%Y"), |
| 209 | _ => ("%d/%m", "%d/%m/%Y"), |
| 210 | }; |
| 211 | DateStyle { |
| 212 | day_month, |
| 213 | day_month_year, |
| 214 | clock, |
| 215 | first_day: first_day_for(&locale, language), |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | pub fn palette_index(key: &str) -> usize { |
| 220 | let mut hash: u32 = 0x811c_9dc5; |
| 221 | for byte in key.bytes() { |
| 222 | hash ^= u32::from(byte); |
| 223 | hash = hash.wrapping_mul(0x0100_0193); |
| 224 | } |
| 225 | hash as usize % theme::avatar_palette_len() |
| 226 | } |
| 227 | |
| 228 | pub fn initials(name: &str) -> String { |
| 229 | let words: Vec<&str> = name |
| 230 | .split(|character: char| !character.is_alphanumeric()) |
| 231 | .filter(|word| !word.is_empty()) |
| 232 | .collect(); |
| 233 | let letters: Vec<char> = match words.as_slice() { |
| 234 | [] => return "?".to_owned(), |
| 235 | [single] => single.chars().take(2).collect(), |
| 236 | [first, .., last] => first.chars().take(1).chain(last.chars().take(1)).collect(), |
| 237 | }; |
| 238 | letters.into_iter().flat_map(char::to_uppercase).collect() |
| 239 | } |
| 240 | |
| 241 | /// Teams letters a team tile from the first and last word, split at spaces and underscores, in their own case. |
| 242 | pub fn team_initials(name: &str) -> String { |
| 243 | let words: Vec<&str> = name |
| 244 | .split([' ', '_']) |
| 245 | .filter(|word| word.chars().next().is_some_and(char::is_alphanumeric)) |
| 246 | .collect(); |
| 247 | match words.as_slice() { |
| 248 | [] => "?".to_owned(), |
| 249 | [single] => single.chars().take(1).collect(), |
| 250 | [first, .., last] => first.chars().take(1).chain(last.chars().take(1)).collect(), |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | pub fn first_name(name: &str) -> &str { |
| 255 | name.split_whitespace().next().unwrap_or(name) |
| 256 | } |
| 257 | |
| 258 | pub fn badge_text(count: u32) -> String { |
| 259 | if count > BADGE_CAP { |
| 260 | format!("{BADGE_CAP}+") |
| 261 | } else { |
| 262 | count.to_string() |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | fn weekday_label(weekday: Weekday) -> &'static str { |
| 267 | match weekday { |
| 268 | Weekday::Mon => "Mon", |
| 269 | Weekday::Tue => "Tue", |
| 270 | Weekday::Wed => "Wed", |
| 271 | Weekday::Thu => "Thu", |
| 272 | Weekday::Fri => "Fri", |
| 273 | Weekday::Sat => "Sat", |
| 274 | Weekday::Sun => "Sun", |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | pub fn file_size_label(bytes: u64) -> String { |
| 279 | const KIB: f64 = 1024.; |
| 280 | let value = bytes as f64; |
| 281 | if value < KIB { |
| 282 | format!("{bytes} B") |
| 283 | } else if value < KIB * KIB { |
| 284 | format!("{:.0} KB", value / KIB) |
| 285 | } else { |
| 286 | format!("{:.1} MB", value / (KIB * KIB)) |
| 287 | } |
| 288 | } |
| 289 | |
| 290 | pub fn short_version(build_id: &str, package_version: &str) -> String { |
| 291 | let commit = build_id.split('-').next().unwrap_or(build_id); |
| 292 | format!("v{package_version} ({commit})") |
| 293 | } |
| 294 | |
| 295 | pub fn list_time_label(time: DateTime<Utc>, today: NaiveDate, offset: impl Into<Zone>) -> String { |
| 296 | list_time_label_in(current_date_style(), time, today, offset) |
| 297 | } |
| 298 | |
| 299 | pub fn list_time_label_in( |
| 300 | style: Option<DateStyle>, |
| 301 | time: DateTime<Utc>, |
| 302 | today: NaiveDate, |
| 303 | offset: impl Into<Zone>, |
| 304 | ) -> String { |
| 305 | let local = offset.into().at(time); |
| 306 | let day = local.date_naive(); |
| 307 | let days_ago = (today - day).num_days(); |
| 308 | if days_ago <= 0 { |
| 309 | local |
| 310 | .format(style.map_or("%H:%M", |style| style.clock)) |
| 311 | .to_string() |
| 312 | } else if days_ago <= WEEKDAY_WINDOW_DAYS { |
| 313 | weekday_label(local.weekday()).to_owned() |
| 314 | } else { |
| 315 | local |
| 316 | .format(style.map_or("%b %-d", |style| style.day_month)) |
| 317 | .to_string() |
| 318 | } |
| 319 | } |
| 320 | |
| 321 | /// The last activity next to a channel name: the time today, else day and month, with the year once older. |
| 322 | pub fn channel_time_label( |
| 323 | time: DateTime<Utc>, |
| 324 | today: NaiveDate, |
| 325 | offset: impl Into<Zone>, |
| 326 | ) -> String { |
| 327 | channel_time_label_in(current_date_style(), time, today, offset) |
| 328 | } |
| 329 | |
| 330 | pub fn channel_time_label_in( |
| 331 | style: Option<DateStyle>, |
| 332 | time: DateTime<Utc>, |
| 333 | today: NaiveDate, |
| 334 | offset: impl Into<Zone>, |
| 335 | ) -> String { |
| 336 | let local = offset.into().at(time); |
| 337 | let pattern = if local.date_naive() >= today { |
| 338 | style.map_or("%H:%M", |style| style.clock) |
| 339 | } else if local.year() == today.year() { |
| 340 | style.map_or("%b %-d", |style| style.day_month) |
| 341 | } else { |
| 342 | style.map_or("%b %-d, %Y", |style| style.day_month_year) |
| 343 | }; |
| 344 | local.format(pattern).to_string() |
| 345 | } |
| 346 | |
| 347 | pub fn post_time_label(time: DateTime<Utc>, today: NaiveDate, offset: impl Into<Zone>) -> String { |
| 348 | post_time_label_in(current_date_style(), time, today, offset) |
| 349 | } |
| 350 | |
| 351 | pub fn post_time_label_in( |
| 352 | style: Option<DateStyle>, |
| 353 | time: DateTime<Utc>, |
| 354 | today: NaiveDate, |
| 355 | offset: impl Into<Zone>, |
| 356 | ) -> String { |
| 357 | let local = offset.into().at(time); |
| 358 | let day = local.date_naive(); |
| 359 | let days_ago = (today - day).num_days(); |
| 360 | let clock = local.format(style.map_or("%H:%M", |style| style.clock)); |
| 361 | let same_year = day.year() == today.year(); |
| 362 | if days_ago <= 0 { |
| 363 | clock.to_string() |
| 364 | } else if days_ago == 1 { |
| 365 | format!("Yesterday {clock}") |
| 366 | } else if days_ago <= WEEKDAY_WINDOW_DAYS { |
| 367 | format!("{} {clock}", weekday_label(local.weekday())) |
| 368 | } else if let Some(style) = style { |
| 369 | let date = if same_year { |
| 370 | style.day_month |
| 371 | } else { |
| 372 | style.day_month_year |
| 373 | }; |
| 374 | format!("{} {clock}", local.format(date)) |
| 375 | } else if same_year { |
| 376 | format!("{} {clock}", local.format("%b %-d")) |
| 377 | } else { |
| 378 | format!("{} {clock}", local.format("%b %-d, %Y")) |
| 379 | } |
| 380 | } |
| 381 | |
| 382 | pub fn day_label(day: NaiveDate, today: NaiveDate) -> String { |
| 383 | if day == today { |
| 384 | "Today".to_owned() |
| 385 | } else if today.pred_opt() == Some(day) { |
| 386 | "Yesterday".to_owned() |
| 387 | } else { |
| 388 | format!( |
| 389 | "{}, {}", |
| 390 | weekday_label(day.weekday()), |
| 391 | day.format("%b %-d, %Y") |
| 392 | ) |
| 393 | } |
| 394 | } |
| 395 | |
| 396 | #[cfg(test)] |
| 397 | mod tests { |
| 398 | use chrono::TimeZone; |
| 399 | |
| 400 | use super::*; |
| 401 | |
| 402 | #[test] |
| 403 | fn the_first_day_of_week_follows_the_region() { |
| 404 | let first = |locale| { |
| 405 | set_date_locale(Some(locale)); |
| 406 | regional_first_day() |
| 407 | }; |
| 408 | assert_eq!(first("en-US"), Weekday::Sun); |
| 409 | assert_eq!(first("de-CH"), Weekday::Mon); |
| 410 | assert_eq!(first("en-GB"), Weekday::Mon); |
| 411 | assert_eq!(first("ar-SA"), Weekday::Sat); |
| 412 | assert_eq!(first("ar-MA"), Weekday::Mon); |
| 413 | assert_eq!(first("ja-JP"), Weekday::Sun); |
| 414 | assert_eq!(first_day_for_locale("en-CH"), Weekday::Mon); |
| 415 | assert_eq!(first_day_for_locale("en_US"), Weekday::Sun); |
| 416 | set_date_locale(None); |
| 417 | assert_eq!(regional_first_day(), Weekday::Mon); |
| 418 | } |
| 419 | |
| 420 | #[test] |
| 421 | fn calendar_names_follow_the_app_language() { |
| 422 | let _guard = crate::ui_locale::locale_test_lock(); |
| 423 | let date = NaiveDate::from_ymd_opt(2026, 11, 16).unwrap(); |
| 424 | rust_i18n::set_locale("de"); |
| 425 | assert_eq!(weekday_name(Weekday::Mon, WeekdayWidth::Full), "Montag"); |
| 426 | assert_eq!(weekday_name(Weekday::Thu, WeekdayWidth::Short), "Do"); |
| 427 | assert_eq!(weekday_name(Weekday::Sun, WeekdayWidth::Letter), "S"); |
| 428 | assert_eq!(month_year_label(date), "November 2026"); |
| 429 | assert_eq!( |
| 430 | month_name(NaiveDate::from_ymd_opt(2026, 3, 1).unwrap()), |
| 431 | "März" |
| 432 | ); |
| 433 | rust_i18n::set_locale("en"); |
| 434 | assert_eq!(weekday_name(Weekday::Mon, WeekdayWidth::Full), "Monday"); |
| 435 | assert_eq!(weekday_name(Weekday::Thu, WeekdayWidth::Letter), "T"); |
| 436 | } |
| 437 | |
| 438 | #[test] |
| 439 | fn calendar_range_follows_the_account_locale() { |
| 440 | let _guard = crate::ui_locale::locale_test_lock(); |
| 441 | rust_i18n::set_locale("en"); |
| 442 | let date = |month, day| NaiveDate::from_ymd_opt(2026, month, day).unwrap(); |
| 443 | set_date_locale(Some("en-GB")); |
| 444 | assert_eq!( |
| 445 | calendar_range_label(date(11, 9), date(11, 15)), |
| 446 | "09-15 November, 2026" |
| 447 | ); |
| 448 | assert_eq!( |
| 449 | calendar_range_label(date(11, 30), date(12, 4)), |
| 450 | "30 November - 04 December, 2026" |
| 451 | ); |
| 452 | assert_eq!(calendar_day_number(date(11, 9)), "09"); |
| 453 | set_date_locale(Some("en-US")); |
| 454 | assert_eq!( |
| 455 | calendar_range_label(date(11, 9), date(11, 13)), |
| 456 | "November 9-13, 2026" |
| 457 | ); |
| 458 | assert_eq!(calendar_day_number(date(11, 9)), "9"); |
| 459 | set_date_locale(Some("de-DE")); |
| 460 | assert_eq!( |
| 461 | calendar_range_label(date(11, 9), date(11, 9)), |
| 462 | "9 November, 2026" |
| 463 | ); |
| 464 | set_date_locale(None); |
| 465 | } |
| 466 | |
| 467 | #[test] |
| 468 | fn team_tiles_letter_first_and_last_word_like_teams() { |
| 469 | assert_eq!(team_initials("e_IT-Support"), "eI"); |
| 470 | assert_eq!(team_initials("e_ComponentLibrary"), "eC"); |
| 471 | assert_eq!(team_initials("e_Human Resources"), "eR"); |
| 472 | assert_eq!(team_initials("e_NLP / AI"), "eA"); |
| 473 | assert_eq!(team_initials("Platform"), "P"); |
| 474 | assert_eq!(team_initials(""), "?"); |
| 475 | } |
| 476 | |
| 477 | #[test] |
| 478 | fn os_locale_names_become_language_tags() { |
| 479 | assert_eq!(normalize_locale("en-GB").as_deref(), Some("en-GB")); |
| 480 | assert_eq!(normalize_locale("en_GB.UTF-8").as_deref(), Some("en-GB")); |
| 481 | assert_eq!(normalize_locale("de_DE@euro").as_deref(), Some("de-DE")); |
| 482 | assert_eq!(normalize_locale("C"), None); |
| 483 | assert_eq!(normalize_locale("POSIX"), None); |
| 484 | assert_eq!(normalize_locale(""), None); |
| 485 | } |
| 486 | |
| 487 | #[test] |
| 488 | fn the_clock_follows_the_display_language() { |
| 489 | set_date_locale(Some("en-US")); |
| 490 | assert_eq!(clock_format(), "%-I:%M %p"); |
| 491 | set_date_locale(Some("de-DE")); |
| 492 | assert_eq!(clock_format(), "%H:%M"); |
| 493 | set_date_locale(None); |
| 494 | assert_eq!(clock_format(), "%H:%M"); |
| 495 | } |
| 496 | |
| 497 | fn utc(day: u32, hour: u32) -> DateTime<Utc> { |
| 498 | Utc.with_ymd_and_hms(2026, 10, day, hour, 5, 0).unwrap() |
| 499 | } |
| 500 | |
| 501 | fn today() -> NaiveDate { |
| 502 | NaiveDate::from_ymd_opt(2026, 10, 7).unwrap() |
| 503 | } |
| 504 | |
| 505 | fn zero() -> FixedOffset { |
| 506 | FixedOffset::east_opt(0).unwrap() |
| 507 | } |
| 508 | |
| 509 | #[test] |
| 510 | fn channel_times_show_the_clock_today_then_day_and_month_then_the_year() { |
| 511 | let style = Some(date_style("en-gb")); |
| 512 | let label = |time| channel_time_label_in(style, time, today(), zero()); |
| 513 | assert_eq!(label(utc(7, 8)), "08:05"); |
| 514 | assert_eq!(label(utc(1, 8)), "01/10"); |
| 515 | assert_eq!( |
| 516 | label(Utc.with_ymd_and_hms(2025, 10, 23, 8, 0, 0).unwrap()), |
| 517 | "23/10/2025" |
| 518 | ); |
| 519 | } |
| 520 | |
| 521 | #[test] |
| 522 | fn palette_index_is_stable_and_in_range() { |
| 523 | assert_eq!(palette_index("user-1"), palette_index("user-1")); |
| 524 | for key in [ |
| 525 | "a", |
| 526 | "b", |
| 527 | "demo-ada", |
| 528 | "", |
| 529 | "00000000-0000-0000-0000-000000000001", |
| 530 | ] { |
| 531 | assert!(palette_index(key) < theme::avatar_palette_len()); |
| 532 | } |
| 533 | } |
| 534 | |
| 535 | #[test] |
| 536 | fn palette_index_spreads_over_several_colors() { |
| 537 | let used: std::collections::HashSet<usize> = (0..40) |
| 538 | .map(|number| palette_index(&format!("user-{number}"))) |
| 539 | .collect(); |
| 540 | assert!(used.len() >= 4); |
| 541 | } |
| 542 | |
| 543 | #[test] |
| 544 | fn initials_use_first_and_last_word() { |
| 545 | assert_eq!(initials("Mara Lindqvist"), "ML"); |
| 546 | assert_eq!(initials("Ada"), "AD"); |
| 547 | assert_eq!(initials("anna maria von Berg"), "AB"); |
| 548 | assert_eq!(initials(" "), "?"); |
| 549 | assert_eq!(initials("Release planning"), "RP"); |
| 550 | } |
| 551 | |
| 552 | #[test] |
| 553 | fn first_name_takes_the_first_word() { |
| 554 | assert_eq!(first_name("Priya Nair"), "Priya"); |
| 555 | assert_eq!(first_name("Priya"), "Priya"); |
| 556 | } |
| 557 | |
| 558 | #[test] |
| 559 | fn file_sizes_use_binary_units() { |
| 560 | assert_eq!(file_size_label(900), "900 B"); |
| 561 | assert_eq!(file_size_label(48_213), "47 KB"); |
| 562 | assert_eq!(file_size_label(5 * 1024 * 1024 + 300_000), "5.3 MB"); |
| 563 | } |
| 564 | |
| 565 | #[test] |
| 566 | fn badge_caps_at_99_plus() { |
| 567 | assert_eq!(badge_text(1), "1"); |
| 568 | assert_eq!(badge_text(99), "99"); |
| 569 | assert_eq!(badge_text(100), "99+"); |
| 570 | assert_eq!(badge_text(4000), "99+"); |
| 571 | } |
| 572 | |
| 573 | #[test] |
| 574 | fn list_time_is_clock_today_weekday_this_week_date_before() { |
| 575 | assert_eq!(list_time_label(utc(7, 13), today(), zero()), "13:05"); |
| 576 | assert_eq!(list_time_label(utc(5, 13), today(), zero()), "Mon"); |
| 577 | assert_eq!(list_time_label(utc(6, 9), today(), zero()), "Tue"); |
| 578 | assert_eq!(list_time_label(utc(1, 13), today(), zero()), "Thu"); |
| 579 | let september = Utc.with_ymd_and_hms(2026, 9, 30, 13, 0, 0).unwrap(); |
| 580 | assert_eq!(list_time_label(september, today(), zero()), "Sep 30"); |
| 581 | } |
| 582 | |
| 583 | #[test] |
| 584 | fn list_time_respects_the_offset_for_the_day_boundary() { |
| 585 | let plus_two = FixedOffset::east_opt(2 * 3600).unwrap(); |
| 586 | assert_eq!(list_time_label(utc(6, 23), today(), plus_two), "01:05"); |
| 587 | } |
| 588 | |
| 589 | #[test] |
| 590 | fn short_version_keeps_the_commit_only() { |
| 591 | assert_eq!( |
| 592 | short_version("687e2dc-20261007T124355Z", "0.1.0"), |
| 593 | "v0.1.0 (687e2dc)" |
| 594 | ); |
| 595 | assert_eq!(short_version("dev", "0.1.0"), "v0.1.0 (dev)"); |
| 596 | } |
| 597 | |
| 598 | #[test] |
| 599 | fn post_time_adds_the_day_when_not_today() { |
| 600 | assert_eq!(post_time_label(utc(7, 13), today(), zero()), "13:05"); |
| 601 | assert_eq!( |
| 602 | post_time_label(utc(6, 9), today(), zero()), |
| 603 | "Yesterday 09:05" |
| 604 | ); |
| 605 | assert_eq!(post_time_label(utc(5, 13), today(), zero()), "Mon 13:05"); |
| 606 | let september = Utc.with_ymd_and_hms(2026, 9, 30, 8, 42, 0).unwrap(); |
| 607 | assert_eq!(post_time_label(september, today(), zero()), "Sep 30 08:42"); |
| 608 | let last_year = Utc.with_ymd_and_hms(2020, 5, 22, 8, 42, 0).unwrap(); |
| 609 | assert_eq!( |
| 610 | post_time_label(last_year, today(), zero()), |
| 611 | "May 22, 2020 08:42" |
| 612 | ); |
| 613 | } |
| 614 | |
| 615 | #[test] |
| 616 | fn post_time_follows_the_display_language_like_teams() { |
| 617 | let september = Utc.with_ymd_and_hms(2026, 9, 24, 20, 54, 0).unwrap(); |
| 618 | let label = |
| 619 | |locale: &str| post_time_label_in(Some(date_style(locale)), september, today(), zero()); |
| 620 | assert_eq!(label("en-GB"), "24/09 20:54"); |
| 621 | assert_eq!(label("de-DE"), "24.9. 20:54"); |
| 622 | assert_eq!(label("en-US"), "9/24 8:54 PM"); |
| 623 | let last_year = Utc.with_ymd_and_hms(2020, 5, 2, 8, 42, 0).unwrap(); |
| 624 | assert_eq!( |
| 625 | post_time_label_in(Some(date_style("en-GB")), last_year, today(), zero()), |
| 626 | "02/05/2020 08:42" |
| 627 | ); |
| 628 | } |
| 629 | |
| 630 | #[test] |
| 631 | fn list_time_follows_the_display_language_like_teams() { |
| 632 | let september = Utc.with_ymd_and_hms(2026, 9, 24, 20, 54, 0).unwrap(); |
| 633 | let style = Some(date_style("en-GB")); |
| 634 | assert_eq!( |
| 635 | list_time_label_in(style, september, today(), zero()), |
| 636 | "24/09" |
| 637 | ); |
| 638 | assert_eq!( |
| 639 | list_time_label_in(style, utc(7, 13), today(), zero()), |
| 640 | "13:05" |
| 641 | ); |
| 642 | } |
| 643 | |
| 644 | #[test] |
| 645 | fn day_labels() { |
| 646 | assert_eq!(day_label(today(), today()), "Today"); |
| 647 | assert_eq!(day_label(today().pred_opt().unwrap(), today()), "Yesterday"); |
| 648 | assert_eq!( |
| 649 | day_label(NaiveDate::from_ymd_opt(2026, 10, 1).unwrap(), today()), |
| 650 | "Thu, Oct 1, 2026" |
| 651 | ); |
| 652 | } |
| 653 | } |