| 1 |
// This file is part of ICU4X. For terms of use, please see the file |
| 2 |
// called LICENSE at the top level of the ICU4X source tree |
| 3 |
// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ). |
| 4 |
|
| 5 |
use crate::scaffold::{ConvertCalendar, GetField, InFixedCalendar, UnstableSealed}; |
| 6 |
use chrono::{Datelike, Offset}; |
| 7 |
use icu_calendar::types::{DayOfMonth, DayOfYear, MonthInfo, RataDie, Weekday, YearInfo}; |
| 8 |
use icu_calendar::{AnyCalendar, Date, Gregorian}; |
| 9 |
use icu_time::zone::models::AtTime; |
| 10 |
use icu_time::zone::{UtcOffset, ZoneNameTimestamp}; |
| 11 |
use icu_time::{DateTime, Hour, Minute, Nanosecond, Second, Time, ZonedDateTime}; |
| 12 |
use icu_time::{TimeZone, TimeZoneInfo}; |
| 13 |
|
| 14 |
impl UnstableSealed for chrono::NaiveTime {} |
| 15 |
impl<C> InFixedCalendar<C> for chrono::NaiveTime {} |
| 16 |
impl GetField<()> for chrono::NaiveTime { |
| 17 |
fn get_field(&self) {} |
| 18 |
} |
| 19 |
|
| 20 |
impl GetField<Hour> for chrono::NaiveTime { |
| 21 |
fn get_field(&self) -> Hour { |
| 22 |
Time::from(*self).hour |
| 23 |
} |
| 24 |
} |
| 25 |
|
| 26 |
impl GetField<Minute> for chrono::NaiveTime { |
| 27 |
fn get_field(&self) -> Minute { |
| 28 |
Time::from(*self).minute |
| 29 |
} |
| 30 |
} |
| 31 |
|
| 32 |
impl GetField<Second> for chrono::NaiveTime { |
| 33 |
fn get_field(&self) -> Second { |
| 34 |
Time::from(*self).second |
| 35 |
} |
| 36 |
} |
| 37 |
|
| 38 |
impl GetField<Nanosecond> for chrono::NaiveTime { |
| 39 |
fn get_field(&self) -> Nanosecond { |
| 40 |
Time::from(*self).subsecond |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
impl UnstableSealed for chrono::NaiveDate {} |
| 45 |
impl GetField<()> for chrono::NaiveDate { |
| 46 |
fn get_field(&self) {} |
| 47 |
} |
| 48 |
|
| 49 |
impl GetField<YearInfo> for chrono::NaiveDate { |
| 50 |
#[inline] |
| 51 |
fn get_field(&self) -> YearInfo { |
| 52 |
Date::from(*self).year() |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
impl GetField<MonthInfo> for chrono::NaiveDate { |
| 57 |
#[inline] |
| 58 |
fn get_field(&self) -> MonthInfo { |
| 59 |
Date::from(*self).month() |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
impl GetField<DayOfMonth> for chrono::NaiveDate { |
| 64 |
#[inline] |
| 65 |
fn get_field(&self) -> DayOfMonth { |
| 66 |
DayOfMonth(self.day() as u8) |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
impl GetField<Weekday> for chrono::NaiveDate { |
| 71 |
#[inline] |
| 72 |
fn get_field(&self) -> Weekday { |
| 73 |
self.weekday().get_field() |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
impl GetField<DayOfYear> for chrono::NaiveDate { |
| 78 |
#[inline] |
| 79 |
fn get_field(&self) -> DayOfYear { |
| 80 |
DayOfYear(self.ordinal() as u16) |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
impl GetField<RataDie> for chrono::NaiveDate { |
| 85 |
#[inline] |
| 86 |
fn get_field(&self) -> RataDie { |
| 87 |
RataDie::new(0) + self.num_days_from_ce() as i64 |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
impl UnstableSealed for chrono::FixedOffset {} |
| 92 |
impl<C> InFixedCalendar<C> for chrono::FixedOffset {} |
| 93 |
impl GetField<()> for chrono::FixedOffset { |
| 94 |
fn get_field(&self) {} |
| 95 |
} |
| 96 |
|
| 97 |
impl GetField<Option<UtcOffset>> for chrono::FixedOffset { |
| 98 |
#[inline] |
| 99 |
fn get_field(&self) -> Option<UtcOffset> { |
| 100 |
Some((*self).into()) |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
impl UnstableSealed for chrono::NaiveDateTime {} |
| 105 |
impl GetField<()> for chrono::NaiveDateTime { |
| 106 |
fn get_field(&self) {} |
| 107 |
} |
| 108 |
|
| 109 |
impl GetField<Hour> for chrono::NaiveDateTime { |
| 110 |
fn get_field(&self) -> Hour { |
| 111 |
self.time().get_field() |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
impl GetField<Minute> for chrono::NaiveDateTime { |
| 116 |
fn get_field(&self) -> Minute { |
| 117 |
self.time().get_field() |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
impl GetField<Second> for chrono::NaiveDateTime { |
| 122 |
fn get_field(&self) -> Second { |
| 123 |
self.time().get_field() |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
impl GetField<Nanosecond> for chrono::NaiveDateTime { |
| 128 |
fn get_field(&self) -> Nanosecond { |
| 129 |
self.time().get_field() |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
impl GetField<YearInfo> for chrono::NaiveDateTime { |
| 134 |
#[inline] |
| 135 |
fn get_field(&self) -> YearInfo { |
| 136 |
self.date().get_field() |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
impl GetField<MonthInfo> for chrono::NaiveDateTime { |
| 141 |
#[inline] |
| 142 |
fn get_field(&self) -> MonthInfo { |
| 143 |
self.date().get_field() |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
impl GetField<DayOfMonth> for chrono::NaiveDateTime { |
| 148 |
#[inline] |
| 149 |
fn get_field(&self) -> DayOfMonth { |
| 150 |
self.date().get_field() |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
impl GetField<Weekday> for chrono::NaiveDateTime { |
| 155 |
#[inline] |
| 156 |
fn get_field(&self) -> Weekday { |
| 157 |
self.date().get_field() |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
impl GetField<DayOfYear> for chrono::NaiveDateTime { |
| 162 |
#[inline] |
| 163 |
fn get_field(&self) -> DayOfYear { |
| 164 |
self.date().get_field() |
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
impl GetField<RataDie> for chrono::NaiveDateTime { |
| 169 |
#[inline] |
| 170 |
fn get_field(&self) -> RataDie { |
| 171 |
self.date().get_field() |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
impl UnstableSealed for chrono::Weekday {} |
| 176 |
impl<C> InFixedCalendar<C> for chrono::Weekday {} |
| 177 |
impl GetField<()> for chrono::Weekday { |
| 178 |
fn get_field(&self) {} |
| 179 |
} |
| 180 |
|
| 181 |
impl GetField<Weekday> for chrono::Weekday { |
| 182 |
fn get_field(&self) -> Weekday { |
| 183 |
(*self).into() |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
impl<Tz: chrono::TimeZone> UnstableSealed for chrono::DateTime<Tz> {} |
| 188 |
impl<Tz: chrono::TimeZone> GetField<()> for chrono::DateTime<Tz> { |
| 189 |
fn get_field(&self) {} |
| 190 |
} |
| 191 |
|
| 192 |
impl<Tz: chrono::TimeZone> GetField<Hour> for chrono::DateTime<Tz> { |
| 193 |
fn get_field(&self) -> Hour { |
| 194 |
self.time().get_field() |
| 195 |
} |
| 196 |
} |
| 197 |
|
| 198 |
impl<Tz: chrono::TimeZone> GetField<Minute> for chrono::DateTime<Tz> { |
| 199 |
fn get_field(&self) -> Minute { |
| 200 |
self.time().get_field() |
| 201 |
} |
| 202 |
} |
| 203 |
|
| 204 |
impl<Tz: chrono::TimeZone> GetField<Second> for chrono::DateTime<Tz> { |
| 205 |
fn get_field(&self) -> Second { |
| 206 |
self.time().get_field() |
| 207 |
} |
| 208 |
} |
| 209 |
|
| 210 |
impl<Tz: chrono::TimeZone> GetField<Nanosecond> for chrono::DateTime<Tz> { |
| 211 |
fn get_field(&self) -> Nanosecond { |
| 212 |
self.time().get_field() |
| 213 |
} |
| 214 |
} |
| 215 |
|
| 216 |
impl<Tz: chrono::TimeZone> GetField<YearInfo> for chrono::DateTime<Tz> { |
| 217 |
#[inline] |
| 218 |
fn get_field(&self) -> YearInfo { |
| 219 |
self.naive_local().date().get_field() |
| 220 |
} |
| 221 |
} |
| 222 |
|
| 223 |
impl<Tz: chrono::TimeZone> GetField<MonthInfo> for chrono::DateTime<Tz> { |
| 224 |
#[inline] |
| 225 |
fn get_field(&self) -> MonthInfo { |
| 226 |
self.naive_local().date().get_field() |
| 227 |
} |
| 228 |
} |
| 229 |
|
| 230 |
impl<Tz: chrono::TimeZone> GetField<DayOfMonth> for chrono::DateTime<Tz> { |
| 231 |
#[inline] |
| 232 |
fn get_field(&self) -> DayOfMonth { |
| 233 |
self.naive_local().date().get_field() |
| 234 |
} |
| 235 |
} |
| 236 |
|
| 237 |
impl<Tz: chrono::TimeZone> GetField<Weekday> for chrono::DateTime<Tz> { |
| 238 |
#[inline] |
| 239 |
fn get_field(&self) -> Weekday { |
| 240 |
self.naive_local().date().get_field() |
| 241 |
} |
| 242 |
} |
| 243 |
|
| 244 |
impl<Tz: chrono::TimeZone> GetField<DayOfYear> for chrono::DateTime<Tz> { |
| 245 |
#[inline] |
| 246 |
fn get_field(&self) -> DayOfYear { |
| 247 |
self.naive_local().date().get_field() |
| 248 |
} |
| 249 |
} |
| 250 |
|
| 251 |
impl<Tz: chrono::TimeZone> GetField<RataDie> for chrono::DateTime<Tz> { |
| 252 |
#[inline] |
| 253 |
fn get_field(&self) -> RataDie { |
| 254 |
self.naive_local().date().get_field() |
| 255 |
} |
| 256 |
} |
| 257 |
|
| 258 |
impl<Tz: chrono::TimeZone> GetField<TimeZone> for chrono::DateTime<Tz> |
| 259 |
where |
| 260 |
TimeZone: From<Tz>, |
| 261 |
{ |
| 262 |
#[inline] |
| 263 |
fn get_field(&self) -> TimeZone { |
| 264 |
TimeZone::from(self.timezone()) |
| 265 |
} |
| 266 |
} |
| 267 |
|
| 268 |
impl<Tz: chrono::TimeZone> GetField<Option<UtcOffset>> for chrono::DateTime<Tz> { |
| 269 |
#[inline] |
| 270 |
fn get_field(&self) -> Option<UtcOffset> { |
| 271 |
self.offset().fix().get_field() |
| 272 |
} |
| 273 |
} |
| 274 |
|
| 275 |
impl<Tz: chrono::TimeZone> GetField<ZoneNameTimestamp> for chrono::DateTime<Tz> { |
| 276 |
#[inline] |
| 277 |
fn get_field(&self) -> ZoneNameTimestamp { |
| 278 |
ZoneNameTimestamp::from_epoch_seconds(self.timestamp()) |
| 279 |
} |
| 280 |
} |
| 281 |
|
| 282 |
impl ConvertCalendar for chrono::NaiveTime { |
| 283 |
type Converted<'a> = Self; |
| 284 |
#[inline] |
| 285 |
fn to_calendar<'a>(&self, _: &'a AnyCalendar) -> Self::Converted<'a> { |
| 286 |
*self |
| 287 |
} |
| 288 |
} |
| 289 |
|
| 290 |
impl ConvertCalendar for chrono::NaiveDate { |
| 291 |
type Converted<'a> = <Date<Gregorian> as ConvertCalendar>::Converted<'a>; |
| 292 |
|
| 293 |
fn to_calendar<'a>(&self, calendar: &'a AnyCalendar) -> Self::Converted<'a> { |
| 294 |
ConvertCalendar::to_calendar(&Date::from(*self), calendar) |
| 295 |
} |
| 296 |
} |
| 297 |
|
| 298 |
impl ConvertCalendar for chrono::NaiveDateTime { |
| 299 |
type Converted<'a> = <DateTime<Gregorian> as ConvertCalendar>::Converted<'a>; |
| 300 |
|
| 301 |
fn to_calendar<'a>(&self, calendar: &'a AnyCalendar) -> Self::Converted<'a> { |
| 302 |
ConvertCalendar::to_calendar(&DateTime::from(*self), calendar) |
| 303 |
} |
| 304 |
} |
| 305 |
|
| 306 |
impl ConvertCalendar for chrono::Weekday { |
| 307 |
type Converted<'a> = Self; |
| 308 |
#[inline] |
| 309 |
fn to_calendar<'a>(&self, _: &'a AnyCalendar) -> Self::Converted<'a> { |
| 310 |
*self |
| 311 |
} |
| 312 |
} |
| 313 |
|
| 314 |
impl<Tz: chrono::TimeZone> ConvertCalendar for chrono::DateTime<Tz> |
| 315 |
where |
| 316 |
for<'a> ZonedDateTime<Gregorian, TimeZoneInfo<AtTime>>: From<&'a chrono::DateTime<Tz>>, |
| 317 |
{ |
| 318 |
type Converted<'a> = |
| 319 |
<ZonedDateTime<Gregorian, TimeZoneInfo<AtTime>> as ConvertCalendar>::Converted<'a>; |
| 320 |
|
| 321 |
fn to_calendar<'a>(&self, calendar: &'a AnyCalendar) -> Self::Converted<'a> { |
| 322 |
ConvertCalendar::to_calendar(&ZonedDateTime::from(self), calendar) |
| 323 |
} |
| 324 |
} |
| 325 |
|
| 326 |
impl ConvertCalendar for chrono::FixedOffset { |
| 327 |
type Converted<'a> = Self; |
| 328 |
#[inline] |
| 329 |
fn to_calendar<'a>(&self, _: &'a AnyCalendar) -> Self::Converted<'a> { |
| 330 |
*self |
| 331 |
} |
| 332 |
} |
| 333 |
|
| 334 |
#[test] |
| 335 |
fn chrono() { |
| 336 |
use crate::{DateTimeFormatter, fieldsets}; |
| 337 |
use icu_locale_core::locale; |
| 338 |
use writeable::assert_writeable_eq; |
| 339 |
|
| 340 |
let chrono = chrono::DateTime::from_timestamp_nanos(1726011440123456789) |
| 341 |
.with_timezone(&"Asia/Tokyo".parse::<chrono_tz::Tz>().unwrap()); |
| 342 |
|
| 343 |
let ymdto = DateTimeFormatter::try_new( |
| 344 |
locale!("ja-u-ca-japanese").into(), |
| 345 |
fieldsets::YMDT::medium().with_zone(fieldsets::zone::LocalizedOffsetShort), |
| 346 |
) |
| 347 |
.unwrap(); |
| 348 |
assert_writeable_eq!( |
| 349 |
ymdto.format(&chrono.with_timezone(&chrono.offset().fix())), |
| 350 |
"令和6年9月11日 8:37:20 GMT+9" |
| 351 |
); |
| 352 |
|
| 353 |
let ymdtz = DateTimeFormatter::try_new( |
| 354 |
locale!("ja-u-ca-japanese").into(), |
| 355 |
fieldsets::YMDT::medium().with_zone(fieldsets::zone::SpecificLong), |
| 356 |
) |
| 357 |
.unwrap(); |
| 358 |
assert_writeable_eq!(ymdtz.format(&chrono), "令和6年9月11日 8:37:20 日本標準時"); |
| 359 |
|
| 360 |
let ymdt = DateTimeFormatter::try_new( |
| 361 |
locale!("ja-u-ca-japanese").into(), |
| 362 |
fieldsets::YMDT::medium(), |
| 363 |
) |
| 364 |
.unwrap(); |
| 365 |
assert_writeable_eq!(ymdt.format(&chrono.naive_local()), "令和6年9月11日 8:37:20"); |
| 366 |
|
| 367 |
let ymd = |
| 368 |
DateTimeFormatter::try_new(locale!("ja-u-ca-japanese").into(), fieldsets::YMD::medium()) |
| 369 |
.unwrap(); |
| 370 |
assert_writeable_eq!(ymd.format(&chrono.date_naive()), "令和6年9月11日"); |
| 371 |
|
| 372 |
let t = DateTimeFormatter::try_new(locale!("ja-u-ca-japanese").into(), fieldsets::T::medium()) |
| 373 |
.unwrap(); |
| 374 |
assert_writeable_eq!(t.format(&chrono.time()), "8:37:20"); |
| 375 |
|
| 376 |
let e = DateTimeFormatter::try_new(locale!("ja-u-ca-japanese").into(), fieldsets::E::long()) |
| 377 |
.unwrap(); |
| 378 |
assert_writeable_eq!(e.format(&chrono.weekday()), "水曜日"); |
| 379 |
|
| 380 |
let offset = DateTimeFormatter::try_new( |
| 381 |
locale!("ja-u-ca-japanese").into(), |
| 382 |
fieldsets::zone::LocalizedOffsetLong, |
| 383 |
) |
| 384 |
.unwrap(); |
| 385 |
assert_writeable_eq!(offset.format(&chrono.offset().fix()), "GMT+09:00"); |
| 386 |
} |
| 387 |
|
| 388 |
#[test] |
| 389 |
fn chrono_fixed_calendar() { |
| 390 |
use crate::{DateTimeFormatter, FixedCalendarDateTimeFormatter, fieldsets}; |
| 391 |
use icu_locale_core::locale; |
| 392 |
use writeable::assert_writeable_eq; |
| 393 |
|
| 394 |
let chrono = chrono::DateTime::from_timestamp_nanos(1726011440123456789) |
| 395 |
.with_timezone(&"Europe/Paris".parse::<chrono_tz::Tz>().unwrap()); |
| 396 |
|
| 397 |
let ymdto = DateTimeFormatter::try_new( |
| 398 |
locale!("fr").into(), |
| 399 |
fieldsets::YMDT::medium().with_zone(fieldsets::zone::LocalizedOffsetShort), |
| 400 |
) |
| 401 |
.unwrap(); |
| 402 |
assert_writeable_eq!( |
| 403 |
ymdto.format(&chrono.with_timezone(&chrono.offset().fix())), |
| 404 |
"11 sept. 2024, 01:37:20 UTC+2" |
| 405 |
); |
| 406 |
|
| 407 |
let ymdtz = DateTimeFormatter::try_new( |
| 408 |
locale!("fr").into(), |
| 409 |
fieldsets::YMDT::medium().with_zone(fieldsets::zone::SpecificLong), |
| 410 |
) |
| 411 |
.unwrap(); |
| 412 |
assert_writeable_eq!( |
| 413 |
ymdtz.format(&chrono), |
| 414 |
"11 sept. 2024, 01:37:20 heure d’été d’Europe centrale" |
| 415 |
); |
| 416 |
|
| 417 |
let ymdt = DateTimeFormatter::try_new(locale!("fr").into(), fieldsets::YMDT::medium()).unwrap(); |
| 418 |
assert_writeable_eq!( |
| 419 |
ymdt.format(&chrono.naive_local()), |
| 420 |
"11 sept. 2024, 01:37:20" |
| 421 |
); |
| 422 |
|
| 423 |
let ymd = DateTimeFormatter::try_new(locale!("fr").into(), fieldsets::YMD::medium()).unwrap(); |
| 424 |
assert_writeable_eq!(ymd.format(&chrono.date_naive()), "11 sept. 2024"); |
| 425 |
|
| 426 |
let t = FixedCalendarDateTimeFormatter::<Gregorian, _>::try_new( |
| 427 |
locale!("fr").into(), |
| 428 |
fieldsets::T::medium(), |
| 429 |
) |
| 430 |
.unwrap(); |
| 431 |
assert_writeable_eq!(t.format(&chrono.time()), "01:37:20"); |
| 432 |
|
| 433 |
let e = FixedCalendarDateTimeFormatter::<Gregorian, _>::try_new( |
| 434 |
locale!("fr").into(), |
| 435 |
fieldsets::E::long(), |
| 436 |
) |
| 437 |
.unwrap(); |
| 438 |
assert_writeable_eq!(e.format(&chrono.weekday()), "mercredi"); |
| 439 |
|
| 440 |
let offset = FixedCalendarDateTimeFormatter::<Gregorian, _>::try_new( |
| 441 |
locale!("fr").into(), |
| 442 |
fieldsets::zone::LocalizedOffsetLong, |
| 443 |
) |
| 444 |
.unwrap(); |
| 445 |
assert_writeable_eq!(offset.format(&chrono.offset().fix()), "UTC+02:00"); |
| 446 |
} |
| 447 |
|
| 448 |
#[test] |
| 449 |
fn chrono_no_calendar() { |
| 450 |
use crate::{NoCalendarFormatter, fieldsets}; |
| 451 |
use icu_locale_core::locale; |
| 452 |
use writeable::assert_writeable_eq; |
| 453 |
|
| 454 |
let chrono = chrono::DateTime::from_timestamp_nanos(1726011440123456789) |
| 455 |
.with_timezone(&"Europe/Zurich".parse::<chrono_tz::Tz>().unwrap()); |
| 456 |
|
| 457 |
let t = NoCalendarFormatter::try_new(locale!("de-CH").into(), fieldsets::T::medium()).unwrap(); |
| 458 |
assert_writeable_eq!(t.format(&chrono.time()), "01:37:20"); |
| 459 |
|
| 460 |
let offset = |
| 461 |
NoCalendarFormatter::try_new(locale!("de").into(), fieldsets::zone::LocalizedOffsetLong) |
| 462 |
.unwrap(); |
| 463 |
assert_writeable_eq!(offset.format(&chrono.offset().fix()), "GMT+02:00"); |
| 464 |
} |