| 1 |
use core::time::Duration; |
| 2 |
|
| 3 |
use jcore::tz::posix; |
| 4 |
|
| 5 |
use crate::{ |
| 6 |
civil::{Date, DateTime, ISOWeekDate, Time}, |
| 7 |
error::{fmt::temporal::Error as E, Error}, |
| 8 |
fmt::{ |
| 9 |
buffer::{ArrayBuffer, BorrowedBuffer, BorrowedWriter}, |
| 10 |
temporal::{Pieces, PiecesOffset, TimeZoneAnnotationKind}, |
| 11 |
Write, |
| 12 |
}, |
| 13 |
span::{Span, UnitSet}, |
| 14 |
tz::{Offset, TimeZone}, |
| 15 |
SignedDuration, Timestamp, Unit, Zoned, |
| 16 |
}; |
| 17 |
|
| 18 |
/// Defines the "maximum" possible length (in bytes) of an RFC 9557 zoned |
| 19 |
/// datetime. |
| 20 |
/// |
| 21 |
/// In practice, the actual maximal string possible is I believe |
| 22 |
/// `-009999-03-14T17:30:00.999999999-04:23[America/Argentina/ComodRivadavia]`, |
| 23 |
/// which is 72 bytes. Obviously, the length of the IANA tzdb identifier |
| 24 |
/// matters and can be highly variable. |
| 25 |
/// |
| 26 |
/// So why is this called "reasonable" and not "maximum"? Well, it's the |
| 27 |
/// IANA tzdb identifiers. They can be arbitrarily long. There also aren't |
| 28 |
/// any rules about how long they can be. So in theory, IANA could allocate |
| 29 |
/// a new identifier longer than `America/Argentina/ComodRivadavia`. With |
| 30 |
/// that said, they do generally try to keep them succinct. |
| 31 |
/// |
| 32 |
/// Separately from what IANA does, Jiff itself doesn't impose any restrictions |
| 33 |
/// on the length of identifiers. Callers can pass in arbitrarily long |
| 34 |
/// identifiers via `TimeZone::tzif` or by simply futzing with the names of |
| 35 |
/// files in `/usr/share/zoneinfo`. It's also possible to use an arbitrarily |
| 36 |
/// long identifier via the "pieces" `TimeZoneAnnotationName` API. Since we |
| 37 |
/// don't impose any restrictions, we really do want to at least try to handle |
| 38 |
/// arbitrarily long identifiers here. |
| 39 |
/// |
| 40 |
/// Thus, we define a "reasonable" upper bound. When the RFC 9557 string we |
| 41 |
/// want to serialize is known to be under this bound, then we'll use a "fast" |
| 42 |
/// path with a fixed size buffer on the stack (or perhaps even write directly |
| 43 |
/// into the spare capacity of a caller providd `String`). But when it's above |
| 44 |
/// this bound, then we fall back to a slower path that uses a buffering |
| 45 |
/// mechanism to permit arbitrarily long IANA tzdb identifiers. |
| 46 |
/// |
| 47 |
/// For the most part, doing this dance doesn't come with a runtime cost, |
| 48 |
/// primarily because we choose to sacrifice code size a bit by duplicating |
| 49 |
/// some functions. We could have our cake and eat it too if we enforce a |
| 50 |
/// maximum length on IANA tzdb identifiers. Then we could set a true |
| 51 |
/// `MAX_ZONED_LEN` and avoid the case where buffering is needed. |
| 52 |
const REASONABLE_ZONED_LEN: usize = 72; |
| 53 |
|
| 54 |
/// Defines the "maximum" possible length (in bytes) of an RFC 9557 zoned |
| 55 |
/// datetime via the "pieces" API. |
| 56 |
/// |
| 57 |
/// This generally should be identical to `REASONABLE_ZONED_LEN`, except its |
| 58 |
/// expected maximum is one byte longer. Namely, the pieces API currently |
| 59 |
/// lets the caller roundtrip the "criticality" of the timestamp. i.e., |
| 60 |
/// the `!` in the time zone annotation. So it's one extra byte longer |
| 61 |
/// than zoned datetimes. |
| 62 |
/// |
| 63 |
/// Note that all the same considerations from variable length IANA tzdb |
| 64 |
/// identifiers apply to the pieces printing just as it does to zoned datetime |
| 65 |
/// printing. |
| 66 |
const REASONABLE_PIECES_LEN: usize = 73; |
| 67 |
|
| 68 |
/// This is my estimate for the "maximum" length of a time zone serialization. |
| 69 |
/// |
| 70 |
/// This is effectively |
| 71 |
/// |
| 72 |
/// ```text |
| 73 |
/// max(max_iana_tzdb_id, max_posix_tz_string, offset, len('Etc/Unknown')) |
| 74 |
/// ``` |
| 75 |
/// |
| 76 |
/// where "max" is not a strict upper bound but an expected upper bound based |
| 77 |
/// on common or "reasonable" values. For example, a valid POSIX time zone |
| 78 |
/// string can have 30 byte abbreviations for both standard and daylight saving |
| 79 |
/// time. |
| 80 |
const REASONABLE_TIME_ZONE_LEN: usize = 32; |
| 81 |
|
| 82 |
/// Defines the "maximum" possible length (in bytes) of a POSIX time zone |
| 83 |
/// string. |
| 84 |
/// |
| 85 |
/// Most POSIX time zone strings are just two 3 letter abbreviations and an |
| 86 |
/// offset indicating standard time. e.g., `EST5EDT`. |
| 87 |
/// |
| 88 |
/// We set this to `20` because we write integers and those APIs require a |
| 89 |
/// minimum buffer length of `20`. (Even though a POSIX time zone will never |
| 90 |
/// have integers nearly that large.) |
| 91 |
const REASONABLE_POSIX_TIME_ZONE_LEN: usize = 20; |
| 92 |
|
| 93 |
/// Defines the maximum possible length (in bytes) of an RFC 3339 timestamp |
| 94 |
/// that is always in Zulu time. |
| 95 |
/// |
| 96 |
/// The longest possible string is `-009999-03-14T21:53:08.999999999Z`. |
| 97 |
const MAX_TIMESTAMP_ZULU_LEN: usize = 33; |
| 98 |
|
| 99 |
/// Defines the maximum possible length (in bytes) of an RFC 3339 timestamp |
| 100 |
/// with an offset (up to minute precision). |
| 101 |
/// |
| 102 |
/// The longest possible string is `-009999-03-15T23:53:07.999999999+25:59`. |
| 103 |
const MAX_TIMESTAMP_OFFSET_LEN: usize = 38; |
| 104 |
|
| 105 |
/// Defines the maximum possible length (in bytes) of an ISO 8601 datetime. |
| 106 |
/// |
| 107 |
/// The longest possible string is `-009999-03-14T17:30:00.999999999`. |
| 108 |
const MAX_DATETIME_LEN: usize = 32; |
| 109 |
|
| 110 |
/// Defines the maximum possible length (in bytes) of an ISO 8601 date. |
| 111 |
/// |
| 112 |
/// The longest possible string is `-009999-03-14`. |
| 113 |
const MAX_DATE_LEN: usize = 13; |
| 114 |
|
| 115 |
/// Defines the maximum possible length (in bytes) of an ISO 8601 time. |
| 116 |
/// |
| 117 |
/// The longest possible string is `17:30:00.999999999`. |
| 118 |
const MAX_TIME_LEN: usize = 18; |
| 119 |
|
| 120 |
/// Defines the maximum possible length (in bytes) of an ISO 8601 week date. |
| 121 |
/// |
| 122 |
/// The longest possible string is `-009999-W11-3`. |
| 123 |
const MAX_ISO_WEEK_DATE_LEN: usize = 13; |
| 124 |
|
| 125 |
/// Defines the maximum possible length (in bytes) of a `Span` printed in the |
| 126 |
/// Temporal ISO 8601 format. |
| 127 |
/// |
| 128 |
/// The way I computed this length was by using the default printer (since |
| 129 |
/// there's only one knob and it doesn't impact the length of the string) |
| 130 |
/// and using a negative `Span` with each unit set to |
| 131 |
/// its minimum value. |
| 132 |
const MAX_SPAN_LEN: usize = 78; |
| 133 |
|
| 134 |
/// Defines the maximum possible length (in bytes) of a duration printed in the |
| 135 |
/// Temporal ISO 8601 format. |
| 136 |
/// |
| 137 |
/// This applies to both signed and unsigned durations. Unsigned durations have |
| 138 |
/// one more digit, but signed durations can have a negative sign. |
| 139 |
const MAX_DURATION_LEN: usize = 35; |
| 140 |
|
| 141 |
#[derive(Clone, Debug)] |
| 142 |
pub(super) struct DateTimePrinter { |
| 143 |
lowercase: bool, |
| 144 |
separator: u8, |
| 145 |
precision: Option<u8>, |
| 146 |
} |
| 147 |
|
| 148 |
impl DateTimePrinter { |
| 149 |
pub(super) const fn new() -> DateTimePrinter { |
| 150 |
DateTimePrinter { lowercase: false, separator: b'T', precision: None } |
| 151 |
} |
| 152 |
|
| 153 |
pub(super) const fn lowercase(self, yes: bool) -> DateTimePrinter { |
| 154 |
DateTimePrinter { lowercase: yes, ..self } |
| 155 |
} |
| 156 |
|
| 157 |
pub(super) const fn separator(self, ascii_char: u8) -> DateTimePrinter { |
| 158 |
assert!(ascii_char.is_ascii(), "RFC3339 separator must be ASCII"); |
| 159 |
DateTimePrinter { separator: ascii_char, ..self } |
| 160 |
} |
| 161 |
|
| 162 |
pub(super) const fn precision( |
| 163 |
self, |
| 164 |
precision: Option<u8>, |
| 165 |
) -> DateTimePrinter { |
| 166 |
DateTimePrinter { precision, ..self } |
| 167 |
} |
| 168 |
|
| 169 |
pub(super) fn print_zoned( |
| 170 |
&self, |
| 171 |
zdt: &Zoned, |
| 172 |
wtr: &mut dyn Write, |
| 173 |
) -> Result<(), Error> { |
| 174 |
/// The base size of an RFC 9557 zoned datetime string. |
| 175 |
/// |
| 176 |
/// 19 comes from the datetime component, e.g., `2025-01-01T00:00:00` |
| 177 |
/// 6 comes from the offset, e.g., `+05:30` |
| 178 |
/// 2 comes from the `[` and `]` for the time zone annotation |
| 179 |
/// |
| 180 |
/// Basically, we always need space for *at least* the above stuff. |
| 181 |
/// The actual time zone annotation name, negative year and fractional |
| 182 |
/// second component are all optional and can vary quite a bit in |
| 183 |
/// length. |
| 184 |
/// |
| 185 |
/// We do this calculation to get a tighter bound on the spare capacity |
| 186 |
/// needed when the caller provides a `&mut String` or a `&mut Vec<u8>` |
| 187 |
/// to write into. That is, we want to try hard not to over-allocate. |
| 188 |
/// |
| 189 |
/// Note that memory safety does not depend on us getting this |
| 190 |
/// calculation right. If we get it wrong, the printer will panic if |
| 191 |
/// it tries to print a string that exceeds the calculated amount. |
| 192 |
const BASE: usize = 19 + 6 + 2; |
| 193 |
|
| 194 |
// An IANA tzdb identifier is variable length data, so add its length |
| 195 |
// to the `BASE` for runtime allocation size. When there is no IANA |
| 196 |
// identifier, we could write a fixed offset (that's always 6 bytes) or |
| 197 |
// `Etc/Unknown` (11 bytes) for when the offset from UTC is not known. |
| 198 |
// In the non-IANA case, we just use an upper bound of 11, so we will |
| 199 |
// over-allocate a little in the fixed offset case. |
| 200 |
let mut runtime_allocation = BASE |
| 201 |
+ zdt.time_zone().iana_name().map(|name| name.len()).unwrap_or(11); |
| 202 |
// A datetime before year 0 means we add a `-00` prefix. e.g., |
| 203 |
// `-001234-01-01`. |
| 204 |
if zdt.year() < 0 { |
| 205 |
runtime_allocation += 3; |
| 206 |
} |
| 207 |
// If we're printing fractional seconds, then we need more room for |
| 208 |
// that. This potentially overallocates because we don't do the extra |
| 209 |
// work required to find a tighter bound. |
| 210 |
if zdt.subsec_nanosecond() != 0 || self.precision.is_some() { |
| 211 |
runtime_allocation += 10; |
| 212 |
} |
| 213 |
|
| 214 |
// The runtime allocation could be greater than what we assume is a |
| 215 |
// "reasonable" upper bound on the length of an RFC 9557 string. This |
| 216 |
// can only happen when an IANA tzdb identifier is very long. When |
| 217 |
// we're under the limit, we use a fast path. |
| 218 |
if runtime_allocation <= REASONABLE_ZONED_LEN { |
| 219 |
return BorrowedBuffer::with_writer::<REASONABLE_ZONED_LEN>( |
| 220 |
wtr, |
| 221 |
runtime_allocation, |
| 222 |
|bbuf| Ok(self.print_zoned_buf(zdt, bbuf)), |
| 223 |
); |
| 224 |
} |
| 225 |
|
| 226 |
// ... otherwise, we use a path with a buffered writer that is slower |
| 227 |
// but can deal with arbitrarily long IANA tzdb identifiers. |
| 228 |
let mut buf = ArrayBuffer::<REASONABLE_ZONED_LEN>::default(); |
| 229 |
let mut bbuf = buf.as_borrowed(); |
| 230 |
let mut wtr = BorrowedWriter::new(&mut bbuf, wtr); |
| 231 |
self.print_zoned_wtr(zdt, &mut wtr)?; |
| 232 |
wtr.finish() |
| 233 |
} |
| 234 |
|
| 235 |
fn print_zoned_buf(&self, zdt: &Zoned, bbuf: &mut BorrowedBuffer<'_>) { |
| 236 |
self.print_datetime_buf(&zdt.datetime(), bbuf); |
| 237 |
let tz = zdt.time_zone(); |
| 238 |
if tz.is_unknown() { |
| 239 |
bbuf.write_str("Z[Etc/Unknown]"); |
| 240 |
} else { |
| 241 |
self.print_offset_rounded_buf(&zdt.offset(), bbuf); |
| 242 |
self.print_time_zone_annotation_buf(&tz, &zdt.offset(), bbuf); |
| 243 |
} |
| 244 |
} |
| 245 |
|
| 246 |
fn print_zoned_wtr( |
| 247 |
&self, |
| 248 |
zdt: &Zoned, |
| 249 |
wtr: &mut BorrowedWriter<'_, '_, '_>, |
| 250 |
) -> Result<(), Error> { |
| 251 |
self.print_datetime_wtr(&zdt.datetime(), wtr)?; |
| 252 |
let tz = zdt.time_zone(); |
| 253 |
if tz.is_unknown() { |
| 254 |
wtr.write_str("Z[Etc/Unknown]")?; |
| 255 |
} else { |
| 256 |
self.print_offset_rounded_wtr(&zdt.offset(), wtr)?; |
| 257 |
self.print_time_zone_annotation_wtr(&tz, &zdt.offset(), wtr)?; |
| 258 |
} |
| 259 |
Ok(()) |
| 260 |
} |
| 261 |
|
| 262 |
pub(super) fn print_timestamp( |
| 263 |
&self, |
| 264 |
timestamp: &Timestamp, |
| 265 |
wtr: &mut dyn Write, |
| 266 |
) -> Result<(), Error> { |
| 267 |
let mut runtime_allocation = MAX_TIMESTAMP_ZULU_LEN; |
| 268 |
// Don't reserve room for fractional seconds if we don't use them. |
| 269 |
if timestamp.subsec_nanosecond() == 0 && self.precision.is_none() { |
| 270 |
runtime_allocation -= 10; |
| 271 |
} |
| 272 |
BorrowedBuffer::with_writer::<MAX_TIMESTAMP_ZULU_LEN>( |
| 273 |
wtr, |
| 274 |
runtime_allocation, |
| 275 |
|bbuf| Ok(self.print_timestamp_buf(timestamp, bbuf)), |
| 276 |
) |
| 277 |
} |
| 278 |
|
| 279 |
fn print_timestamp_buf( |
| 280 |
&self, |
| 281 |
timestamp: &Timestamp, |
| 282 |
bbuf: &mut BorrowedBuffer<'_>, |
| 283 |
) { |
| 284 |
let dt = Offset::UTC.to_datetime(*timestamp); |
| 285 |
self.print_datetime_buf(&dt, bbuf); |
| 286 |
self.print_zulu_buf(bbuf); |
| 287 |
} |
| 288 |
|
| 289 |
pub(super) fn print_timestamp_with_offset( |
| 290 |
&self, |
| 291 |
timestamp: &Timestamp, |
| 292 |
offset: Offset, |
| 293 |
wtr: &mut dyn Write, |
| 294 |
) -> Result<(), Error> { |
| 295 |
let mut runtime_allocation = MAX_TIMESTAMP_OFFSET_LEN; |
| 296 |
// Don't reserve room for fractional seconds if we don't use them. |
| 297 |
if timestamp.subsec_nanosecond() == 0 && self.precision.is_none() { |
| 298 |
runtime_allocation -= 10; |
| 299 |
} |
| 300 |
BorrowedBuffer::with_writer::<MAX_TIMESTAMP_OFFSET_LEN>( |
| 301 |
wtr, |
| 302 |
runtime_allocation, |
| 303 |
|bbuf| { |
| 304 |
Ok(self |
| 305 |
.print_timestamp_with_offset_buf(timestamp, offset, bbuf)) |
| 306 |
}, |
| 307 |
) |
| 308 |
} |
| 309 |
|
| 310 |
fn print_timestamp_with_offset_buf( |
| 311 |
&self, |
| 312 |
timestamp: &Timestamp, |
| 313 |
offset: Offset, |
| 314 |
bbuf: &mut BorrowedBuffer<'_>, |
| 315 |
) { |
| 316 |
let dt = offset.to_datetime(*timestamp); |
| 317 |
self.print_datetime_buf(&dt, bbuf); |
| 318 |
self.print_offset_rounded_buf(&offset, bbuf); |
| 319 |
} |
| 320 |
|
| 321 |
pub(super) fn print_datetime( |
| 322 |
&self, |
| 323 |
dt: &DateTime, |
| 324 |
wtr: &mut dyn Write, |
| 325 |
) -> Result<(), Error> { |
| 326 |
let mut runtime_allocation = MAX_DATETIME_LEN; |
| 327 |
// Don't reserve room for fractional seconds if we don't use them. |
| 328 |
if dt.subsec_nanosecond() == 0 && self.precision.is_none() { |
| 329 |
runtime_allocation -= 10; |
| 330 |
} |
| 331 |
BorrowedBuffer::with_writer::<MAX_DATETIME_LEN>( |
| 332 |
wtr, |
| 333 |
runtime_allocation, |
| 334 |
|bbuf| Ok(self.print_datetime_buf(dt, bbuf)), |
| 335 |
) |
| 336 |
} |
| 337 |
|
| 338 |
fn print_datetime_buf( |
| 339 |
&self, |
| 340 |
dt: &DateTime, |
| 341 |
bbuf: &mut BorrowedBuffer<'_>, |
| 342 |
) { |
| 343 |
self.print_date_buf(&dt.date(), bbuf); |
| 344 |
bbuf.write_ascii_char(if self.lowercase { |
| 345 |
self.separator.to_ascii_lowercase() |
| 346 |
} else { |
| 347 |
self.separator |
| 348 |
}); |
| 349 |
self.print_time_buf(&dt.time(), bbuf); |
| 350 |
} |
| 351 |
|
| 352 |
fn print_datetime_wtr( |
| 353 |
&self, |
| 354 |
dt: &DateTime, |
| 355 |
wtr: &mut BorrowedWriter<'_, '_, '_>, |
| 356 |
) -> Result<(), Error> { |
| 357 |
self.print_date_wtr(&dt.date(), wtr)?; |
| 358 |
wtr.write_ascii_char(if self.lowercase { |
| 359 |
self.separator.to_ascii_lowercase() |
| 360 |
} else { |
| 361 |
self.separator |
| 362 |
})?; |
| 363 |
self.print_time_wtr(&dt.time(), wtr)?; |
| 364 |
Ok(()) |
| 365 |
} |
| 366 |
|
| 367 |
pub(super) fn print_date( |
| 368 |
&self, |
| 369 |
date: &Date, |
| 370 |
wtr: &mut dyn Write, |
| 371 |
) -> Result<(), Error> { |
| 372 |
BorrowedBuffer::with_writer::<MAX_DATE_LEN>( |
| 373 |
wtr, |
| 374 |
MAX_DATE_LEN, |
| 375 |
|bbuf| Ok(self.print_date_buf(date, bbuf)), |
| 376 |
) |
| 377 |
} |
| 378 |
|
| 379 |
fn print_date_buf(&self, date: &Date, bbuf: &mut BorrowedBuffer<'_>) { |
| 380 |
let year = date.year(); |
| 381 |
if year < 0 { |
| 382 |
bbuf.write_str("-00"); |
| 383 |
} |
| 384 |
bbuf.write_int_pad4(year.unsigned_abs()); |
| 385 |
bbuf.write_ascii_char(b'-'); |
| 386 |
bbuf.write_int_pad2(date.month().unsigned_abs()); |
| 387 |
bbuf.write_ascii_char(b'-'); |
| 388 |
bbuf.write_int_pad2(date.day().unsigned_abs()); |
| 389 |
} |
| 390 |
|
| 391 |
fn print_date_wtr( |
| 392 |
&self, |
| 393 |
date: &Date, |
| 394 |
wtr: &mut BorrowedWriter<'_, '_, '_>, |
| 395 |
) -> Result<(), Error> { |
| 396 |
let year = date.year(); |
| 397 |
if year < 0 { |
| 398 |
wtr.write_str("-00")?; |
| 399 |
} |
| 400 |
wtr.write_int_pad4(year.unsigned_abs())?; |
| 401 |
wtr.write_ascii_char(b'-')?; |
| 402 |
wtr.write_int_pad2(date.month().unsigned_abs())?; |
| 403 |
wtr.write_ascii_char(b'-')?; |
| 404 |
wtr.write_int_pad2(date.day().unsigned_abs())?; |
| 405 |
Ok(()) |
| 406 |
} |
| 407 |
|
| 408 |
pub(super) fn print_time( |
| 409 |
&self, |
| 410 |
time: &Time, |
| 411 |
wtr: &mut dyn Write, |
| 412 |
) -> Result<(), Error> { |
| 413 |
let mut runtime_allocation = MAX_TIME_LEN; |
| 414 |
// Don't reserve room for fractional seconds if we don't use them. |
| 415 |
if time.subsec_nanosecond() == 0 && self.precision.is_none() { |
| 416 |
runtime_allocation -= 10; |
| 417 |
} |
| 418 |
BorrowedBuffer::with_writer::<MAX_TIME_LEN>( |
| 419 |
wtr, |
| 420 |
runtime_allocation, |
| 421 |
|bbuf| Ok(self.print_time_buf(time, bbuf)), |
| 422 |
) |
| 423 |
} |
| 424 |
|
| 425 |
fn print_time_buf(&self, time: &Time, bbuf: &mut BorrowedBuffer<'_>) { |
| 426 |
bbuf.write_int_pad2(time.hour().unsigned_abs()); |
| 427 |
bbuf.write_ascii_char(b':'); |
| 428 |
bbuf.write_int_pad2(time.minute().unsigned_abs()); |
| 429 |
bbuf.write_ascii_char(b':'); |
| 430 |
bbuf.write_int_pad2(time.second().unsigned_abs()); |
| 431 |
let fractional_nanosecond = time.subsec_nanosecond(); |
| 432 |
if self.precision.map_or(fractional_nanosecond != 0, |p| p > 0) { |
| 433 |
bbuf.write_ascii_char(b'.'); |
| 434 |
bbuf.write_fraction( |
| 435 |
self.precision, |
| 436 |
fractional_nanosecond.unsigned_abs(), |
| 437 |
); |
| 438 |
} |
| 439 |
} |
| 440 |
|
| 441 |
fn print_time_wtr( |
| 442 |
&self, |
| 443 |
time: &Time, |
| 444 |
wtr: &mut BorrowedWriter<'_, '_, '_>, |
| 445 |
) -> Result<(), Error> { |
| 446 |
wtr.write_int_pad2(time.hour().unsigned_abs())?; |
| 447 |
wtr.write_ascii_char(b':')?; |
| 448 |
wtr.write_int_pad2(time.minute().unsigned_abs())?; |
| 449 |
wtr.write_ascii_char(b':')?; |
| 450 |
wtr.write_int_pad2(time.second().unsigned_abs())?; |
| 451 |
let fractional_nanosecond = time.subsec_nanosecond(); |
| 452 |
if self.precision.map_or(fractional_nanosecond != 0, |p| p > 0) { |
| 453 |
wtr.write_ascii_char(b'.')?; |
| 454 |
wtr.write_fraction( |
| 455 |
self.precision, |
| 456 |
fractional_nanosecond.unsigned_abs(), |
| 457 |
)?; |
| 458 |
} |
| 459 |
Ok(()) |
| 460 |
} |
| 461 |
|
| 462 |
pub(super) fn print_time_zone( |
| 463 |
&self, |
| 464 |
tz: &TimeZone, |
| 465 |
wtr: &mut dyn Write, |
| 466 |
) -> Result<(), Error> { |
| 467 |
let mut buf = ArrayBuffer::<REASONABLE_TIME_ZONE_LEN>::default(); |
| 468 |
let mut bbuf = buf.as_borrowed(); |
| 469 |
let mut wtr = BorrowedWriter::new(&mut bbuf, wtr); |
| 470 |
self.print_time_zone_wtr(tz, &mut wtr)?; |
| 471 |
wtr.finish() |
| 472 |
} |
| 473 |
|
| 474 |
fn print_time_zone_wtr( |
| 475 |
&self, |
| 476 |
tz: &TimeZone, |
| 477 |
wtr: &mut BorrowedWriter<'_, '_, '_>, |
| 478 |
) -> Result<(), Error> { |
| 479 |
if let Some(iana_name) = tz.iana_name() { |
| 480 |
return wtr.write_str(iana_name); |
| 481 |
} |
| 482 |
if tz.is_unknown() { |
| 483 |
return wtr.write_str("Etc/Unknown"); |
| 484 |
} |
| 485 |
if let Ok(offset) = tz.to_fixed_offset() { |
| 486 |
return self.print_offset_full_precision(&offset, wtr); |
| 487 |
} |
| 488 |
if let Some(posix_tz) = tz.posix_tz() { |
| 489 |
return self.print_posix_time_zone(posix_tz, wtr); |
| 490 |
} |
| 491 |
// Ideally this never actually happens, but it can, and there |
| 492 |
// are likely system configurations out there in which it does. |
| 493 |
// I can imagine "lightweight" installations that just have a |
| 494 |
// `/etc/localtime` as a TZif file that doesn't point to any IANA time |
| 495 |
// zone. In which case, serializing a time zone probably doesn't make |
| 496 |
// much sense. |
| 497 |
// |
| 498 |
// Anyway, if you're seeing this error and think there should be a |
| 499 |
// different behavior, please file an issue. |
| 500 |
Err(Error::from(E::PrintTimeZoneFailure)) |
| 501 |
} |
| 502 |
|
| 503 |
pub(super) fn print_posix_time_zone( |
| 504 |
&self, |
| 505 |
posix_tz: &posix::TimeZone, |
| 506 |
wtr: &mut dyn Write, |
| 507 |
) -> Result<(), Error> { |
| 508 |
let mut buf = ArrayBuffer::<REASONABLE_POSIX_TIME_ZONE_LEN>::default(); |
| 509 |
let mut bbuf = buf.as_borrowed(); |
| 510 |
let mut wtr = BorrowedWriter::new(&mut bbuf, wtr); |
| 511 |
self.print_posix_time_zone_wtr(posix_tz, &mut wtr)?; |
| 512 |
wtr.finish() |
| 513 |
} |
| 514 |
|
| 515 |
fn print_posix_time_zone_wtr( |
| 516 |
&self, |
| 517 |
tz: &posix::TimeZone, |
| 518 |
wtr: &mut BorrowedWriter<'_, '_, '_>, |
| 519 |
) -> Result<(), Error> { |
| 520 |
self.print_posix_abbreviation_wtr(tz.std_abbrev.as_str(), wtr)?; |
| 521 |
self.print_posix_offset_wtr(&tz.std_offset, wtr)?; |
| 522 |
if let Some(ref dst) = tz.dst { |
| 523 |
self.print_posix_dst_wtr(dst, &tz.std_offset, wtr)?; |
| 524 |
} |
| 525 |
Ok(()) |
| 526 |
} |
| 527 |
|
| 528 |
fn print_posix_dst_wtr( |
| 529 |
&self, |
| 530 |
dst: &posix::Dst, |
| 531 |
std_offset: &jcore::tz::Offset, |
| 532 |
wtr: &mut BorrowedWriter<'_, '_, '_>, |
| 533 |
) -> Result<(), Error> { |
| 534 |
self.print_posix_abbreviation_wtr(dst.abbrev.as_ref(), wtr)?; |
| 535 |
// The overwhelmingly common case is that DST is exactly one hour |
| 536 |
// ahead of standard time. So common that this is the default. So |
| 537 |
// don't write the offset if we don't need to. |
| 538 |
if Some(dst.offset) != std_offset.checked_add(3600).ok() { |
| 539 |
self.print_posix_offset_wtr(&dst.offset, wtr)?; |
| 540 |
} |
| 541 |
wtr.write_ascii_char(b',')?; |
| 542 |
self.print_posix_rule_wtr(&dst.rule, wtr)?; |
| 543 |
Ok(()) |
| 544 |
} |
| 545 |
|
| 546 |
fn print_posix_rule_wtr( |
| 547 |
&self, |
| 548 |
rule: &posix::Rule, |
| 549 |
wtr: &mut BorrowedWriter<'_, '_, '_>, |
| 550 |
) -> Result<(), Error> { |
| 551 |
self.print_posix_day_time_wtr(&rule.start, wtr)?; |
| 552 |
wtr.write_ascii_char(b',')?; |
| 553 |
self.print_posix_day_time_wtr(&rule.end, wtr)?; |
| 554 |
Ok(()) |
| 555 |
} |
| 556 |
|
| 557 |
fn print_posix_day_time_wtr( |
| 558 |
&self, |
| 559 |
day_time: &posix::DayTime, |
| 560 |
wtr: &mut BorrowedWriter<'_, '_, '_>, |
| 561 |
) -> Result<(), Error> { |
| 562 |
self.print_posix_day_wtr(&day_time.date, wtr)?; |
| 563 |
// This is the default time, so don't write it if we don't need to. |
| 564 |
if day_time.time != posix::TransitionCivilTime::DEFAULT { |
| 565 |
wtr.write_ascii_char(b'/')?; |
| 566 |
self.print_posix_time_wtr(&day_time.time, wtr)?; |
| 567 |
} |
| 568 |
Ok(()) |
| 569 |
} |
| 570 |
|
| 571 |
fn print_posix_day_wtr( |
| 572 |
&self, |
| 573 |
day: &posix::Day, |
| 574 |
wtr: &mut BorrowedWriter<'_, '_, '_>, |
| 575 |
) -> Result<(), Error> { |
| 576 |
match *day { |
| 577 |
posix::Day::JulianOne(n) => { |
| 578 |
wtr.write_ascii_char(b'J')?; |
| 579 |
wtr.write_int(n.unsigned_abs())?; |
| 580 |
Ok(()) |
| 581 |
} |
| 582 |
posix::Day::JulianZero(n) => wtr.write_int(n.unsigned_abs()), |
| 583 |
posix::Day::WeekdayOfMonth { month, week, weekday } => { |
| 584 |
wtr.write_ascii_char(b'M')?; |
| 585 |
wtr.write_int(month.unsigned_abs())?; |
| 586 |
wtr.write_ascii_char(b'.')?; |
| 587 |
wtr.write_int1(week.unsigned_abs())?; |
| 588 |
wtr.write_ascii_char(b'.')?; |
| 589 |
wtr.write_int1( |
| 590 |
weekday.to_sunday_zero_offset().unsigned_abs(), |
| 591 |
)?; |
| 592 |
Ok(()) |
| 593 |
} |
| 594 |
} |
| 595 |
} |
| 596 |
|
| 597 |
fn print_posix_time_wtr( |
| 598 |
&self, |
| 599 |
time: &posix::TransitionCivilTime, |
| 600 |
wtr: &mut BorrowedWriter<'_, '_, '_>, |
| 601 |
) -> Result<(), Error> { |
| 602 |
if time.second.is_negative() { |
| 603 |
wtr.write_ascii_char(b'-')?; |
| 604 |
// The default is positive, so when |
| 605 |
// positive, we write nothing. |
| 606 |
} |
| 607 |
self.print_posix_time_offset_seconds_wtr(time.second, wtr) |
| 608 |
} |
| 609 |
|
| 610 |
fn print_posix_offset_wtr( |
| 611 |
&self, |
| 612 |
offset: &jcore::tz::Offset, |
| 613 |
wtr: &mut BorrowedWriter<'_, '_, '_>, |
| 614 |
) -> Result<(), Error> { |
| 615 |
// Yes, this is backwards. Blame POSIX. |
| 616 |
// N.B. `+` is the default, so we don't |
| 617 |
// need to write that out. |
| 618 |
if offset.seconds() > 0 { |
| 619 |
wtr.write_ascii_char(b'-')?; |
| 620 |
} |
| 621 |
self.print_posix_time_offset_seconds_wtr(offset.seconds(), wtr) |
| 622 |
} |
| 623 |
|
| 624 |
fn print_posix_time_offset_seconds_wtr( |
| 625 |
&self, |
| 626 |
second: i32, |
| 627 |
wtr: &mut BorrowedWriter<'_, '_, '_>, |
| 628 |
) -> Result<(), Error> { |
| 629 |
let second = second.unsigned_abs(); |
| 630 |
let h = second / 3600; |
| 631 |
let m = (second / 60) % 60; |
| 632 |
let s = second % 60; |
| 633 |
wtr.write_int(h)?; |
| 634 |
if m != 0 || s != 0 { |
| 635 |
wtr.write_ascii_char(b':')?; |
| 636 |
wtr.write_int_pad2(m)?; |
| 637 |
if s != 0 { |
| 638 |
wtr.write_ascii_char(b':')?; |
| 639 |
wtr.write_int_pad2(s)?; |
| 640 |
} |
| 641 |
} |
| 642 |
Ok(()) |
| 643 |
} |
| 644 |
|
| 645 |
fn print_posix_abbreviation_wtr( |
| 646 |
&self, |
| 647 |
abbreviation: &str, |
| 648 |
wtr: &mut BorrowedWriter<'_, '_, '_>, |
| 649 |
) -> Result<(), Error> { |
| 650 |
if abbreviation.chars().any(|ch| ch == '+' || ch == '-') { |
| 651 |
wtr.write_ascii_char(b'<')?; |
| 652 |
wtr.write_str(abbreviation)?; |
| 653 |
wtr.write_ascii_char(b'>')?; |
| 654 |
} else { |
| 655 |
wtr.write_str(abbreviation)?; |
| 656 |
} |
| 657 |
Ok(()) |
| 658 |
} |
| 659 |
|
| 660 |
pub(super) fn print_pieces( |
| 661 |
&self, |
| 662 |
pieces: &Pieces, |
| 663 |
wtr: &mut dyn Write, |
| 664 |
) -> Result<(), Error> { |
| 665 |
// N.B. We don't bother with writing into the spare capacity of a |
| 666 |
// `&mut String` here because with `Pieces` it's a little more |
| 667 |
// complicated to find a more precise upper bound on the length. |
| 668 |
// Plus, I don't think this API is commonly used, so it's not clear |
| 669 |
// that it's worth optimizing. But I'm open to a PR with benchmarks |
| 670 |
// if there's a good use case. ---AG |
| 671 |
let mut buf = ArrayBuffer::<REASONABLE_PIECES_LEN>::default(); |
| 672 |
let mut bbuf = buf.as_borrowed(); |
| 673 |
let mut wtr = BorrowedWriter::new(&mut bbuf, wtr); |
| 674 |
self.print_pieces_wtr(pieces, &mut wtr)?; |
| 675 |
wtr.finish() |
| 676 |
} |
| 677 |
|
| 678 |
fn print_pieces_wtr( |
| 679 |
&self, |
| 680 |
pieces: &Pieces, |
| 681 |
wtr: &mut BorrowedWriter<'_, '_, '_>, |
| 682 |
) -> Result<(), Error> { |
| 683 |
if let Some(time) = pieces.time() { |
| 684 |
let dt = DateTime::from_parts(pieces.date(), time); |
| 685 |
self.print_datetime_wtr(&dt, wtr)?; |
| 686 |
if let Some(poffset) = pieces.offset() { |
| 687 |
self.print_pieces_offset(&poffset, wtr)?; |
| 688 |
} |
| 689 |
} else if let Some(poffset) = pieces.offset() { |
| 690 |
// In this case, we have an offset but no time component. Since |
| 691 |
// `2025-01-02-05:00` isn't valid, we forcefully write out the |
| 692 |
// default time (which is what would be assumed anyway). |
| 693 |
let dt = DateTime::from_parts(pieces.date(), Time::midnight()); |
| 694 |
self.print_datetime_wtr(&dt, wtr)?; |
| 695 |
self.print_pieces_offset(&poffset, wtr)?; |
| 696 |
} else { |
| 697 |
// We have no time and no offset, so we can just write the date. |
| 698 |
// It's okay to write this followed by an annotation, e.g., |
| 699 |
// `2025-01-02[America/New_York]` or even `2025-01-02[-05:00]`. |
| 700 |
self.print_date_wtr(&pieces.date(), wtr)?; |
| 701 |
} |
| 702 |
// For the time zone annotation, a `Pieces` gives us the annotation |
| 703 |
// name or offset directly, where as with `Zoned`, we have a |
| 704 |
// `TimeZone`. So we hand-roll our own formatter directly from the |
| 705 |
// annotation. |
| 706 |
if let Some(ann) = pieces.time_zone_annotation() { |
| 707 |
wtr.write_ascii_char(b'[')?; |
| 708 |
if ann.is_critical() { |
| 709 |
wtr.write_ascii_char(b'!')?; |
| 710 |
} |
| 711 |
match *ann.kind() { |
| 712 |
TimeZoneAnnotationKind::Named(ref name) => { |
| 713 |
wtr.write_str(name.as_str())?; |
| 714 |
} |
| 715 |
TimeZoneAnnotationKind::Offset(offset) => { |
| 716 |
self.print_offset_rounded_wtr(&offset, wtr)?; |
| 717 |
} |
| 718 |
} |
| 719 |
wtr.write_ascii_char(b']')?; |
| 720 |
} |
| 721 |
Ok(()) |
| 722 |
} |
| 723 |
|
| 724 |
pub(super) fn print_iso_week_date( |
| 725 |
&self, |
| 726 |
iso_week_date: &ISOWeekDate, |
| 727 |
wtr: &mut dyn Write, |
| 728 |
) -> Result<(), Error> { |
| 729 |
BorrowedBuffer::with_writer::<MAX_ISO_WEEK_DATE_LEN>( |
| 730 |
wtr, |
| 731 |
MAX_ISO_WEEK_DATE_LEN, |
| 732 |
|bbuf| Ok(self.print_iso_week_date_buf(iso_week_date, bbuf)), |
| 733 |
) |
| 734 |
} |
| 735 |
|
| 736 |
fn print_iso_week_date_buf( |
| 737 |
&self, |
| 738 |
iso_week_date: &ISOWeekDate, |
| 739 |
bbuf: &mut BorrowedBuffer<'_>, |
| 740 |
) { |
| 741 |
let year = iso_week_date.year(); |
| 742 |
if year < 0 { |
| 743 |
bbuf.write_str("-00"); |
| 744 |
} |
| 745 |
bbuf.write_int_pad4(year.unsigned_abs()); |
| 746 |
bbuf.write_ascii_char(b'-'); |
| 747 |
bbuf.write_ascii_char(if self.lowercase { b'w' } else { b'W' }); |
| 748 |
bbuf.write_int_pad2(iso_week_date.week().unsigned_abs()); |
| 749 |
bbuf.write_ascii_char(b'-'); |
| 750 |
bbuf.write_int1( |
| 751 |
iso_week_date.weekday().to_monday_one_offset().unsigned_abs(), |
| 752 |
); |
| 753 |
} |
| 754 |
|
| 755 |
fn print_pieces_offset( |
| 756 |
&self, |
| 757 |
poffset: &PiecesOffset, |
| 758 |
wtr: &mut BorrowedWriter<'_, '_, '_>, |
| 759 |
) -> Result<(), Error> { |
| 760 |
match *poffset { |
| 761 |
PiecesOffset::Zulu => self.print_zulu_wtr(wtr), |
| 762 |
PiecesOffset::Numeric(ref noffset) => { |
| 763 |
if noffset.offset().is_zero() && noffset.is_negative() { |
| 764 |
wtr.write_str("-00:00") |
| 765 |
} else { |
| 766 |
self.print_offset_rounded_wtr(&noffset.offset(), wtr) |
| 767 |
} |
| 768 |
} |
| 769 |
} |
| 770 |
} |
| 771 |
|
| 772 |
/// Formats the given offset into the writer given. |
| 773 |
/// |
| 774 |
/// If the given offset has non-zero seconds, then they are rounded to |
| 775 |
/// the nearest minute. |
| 776 |
fn print_offset_rounded_buf( |
| 777 |
&self, |
| 778 |
offset: &Offset, |
| 779 |
bbuf: &mut BorrowedBuffer<'_>, |
| 780 |
) { |
| 781 |
bbuf.write_ascii_char(if offset.is_negative() { b'-' } else { b'+' }); |
| 782 |
let (offset_hours, offset_minutes) = offset.round_to_nearest_minute(); |
| 783 |
bbuf.write_int_pad2(offset_hours); |
| 784 |
bbuf.write_ascii_char(b':'); |
| 785 |
bbuf.write_int_pad2(offset_minutes); |
| 786 |
} |
| 787 |
|
| 788 |
/// Formats the given offset into the writer given. |
| 789 |
/// |
| 790 |
/// If the given offset has non-zero seconds, then they are rounded to |
| 791 |
/// the nearest minute. |
| 792 |
fn print_offset_rounded_wtr( |
| 793 |
&self, |
| 794 |
offset: &Offset, |
| 795 |
wtr: &mut BorrowedWriter<'_, '_, '_>, |
| 796 |
) -> Result<(), Error> { |
| 797 |
wtr.write_ascii_char(if offset.is_negative() { b'-' } else { b'+' })?; |
| 798 |
let (offset_hours, offset_minutes) = offset.round_to_nearest_minute(); |
| 799 |
wtr.write_int_pad2(offset_hours)?; |
| 800 |
wtr.write_ascii_char(b':')?; |
| 801 |
wtr.write_int_pad2(offset_minutes)?; |
| 802 |
Ok(()) |
| 803 |
} |
| 804 |
|
| 805 |
/// Formats the given offset into the writer given. |
| 806 |
/// |
| 807 |
/// If the given offset has non-zero seconds, then they are emitted as a |
| 808 |
/// third `:`-delimited component of the offset. If seconds are zero, then |
| 809 |
/// only the hours and minute components are emitted. |
| 810 |
fn print_offset_full_precision( |
| 811 |
&self, |
| 812 |
offset: &Offset, |
| 813 |
wtr: &mut BorrowedWriter<'_, '_, '_>, |
| 814 |
) -> Result<(), Error> { |
| 815 |
wtr.write_ascii_char(if offset.is_negative() { b'-' } else { b'+' })?; |
| 816 |
let hours = offset.part_hours().unsigned_abs(); |
| 817 |
let minutes = offset.part_minutes().unsigned_abs(); |
| 818 |
let seconds = offset.part_seconds().unsigned_abs(); |
| 819 |
wtr.write_int_pad2(hours)?; |
| 820 |
wtr.write_ascii_char(b':')?; |
| 821 |
wtr.write_int_pad2(minutes)?; |
| 822 |
if seconds > 0 { |
| 823 |
wtr.write_ascii_char(b':')?; |
| 824 |
wtr.write_int_pad2(seconds)?; |
| 825 |
} |
| 826 |
Ok(()) |
| 827 |
} |
| 828 |
|
| 829 |
/// Prints the "zulu" indicator. |
| 830 |
/// |
| 831 |
/// This should only be used when the offset is not known. For example, |
| 832 |
/// when printing a `Timestamp`. |
| 833 |
fn print_zulu_buf(&self, bbuf: &mut BorrowedBuffer<'_>) { |
| 834 |
bbuf.write_ascii_char(if self.lowercase { b'z' } else { b'Z' }); |
| 835 |
} |
| 836 |
|
| 837 |
/// Prints the "zulu" indicator. |
| 838 |
/// |
| 839 |
/// This should only be used when the offset is not known. For example, |
| 840 |
/// when printing a `Timestamp`. |
| 841 |
fn print_zulu_wtr( |
| 842 |
&self, |
| 843 |
wtr: &mut BorrowedWriter<'_, '_, '_>, |
| 844 |
) -> Result<(), Error> { |
| 845 |
wtr.write_ascii_char(if self.lowercase { b'z' } else { b'Z' }) |
| 846 |
} |
| 847 |
|
| 848 |
/// Formats the given time zone name into the writer given as an RFC 9557 |
| 849 |
/// time zone annotation. |
| 850 |
/// |
| 851 |
/// When the given time zone is not an IANA time zone name, then the offset |
| 852 |
/// is printed instead. (This means the offset will be printed twice, which |
| 853 |
/// is indeed an intended behavior of RFC 9557 for cases where a time zone |
| 854 |
/// name is not used or unavailable.) |
| 855 |
fn print_time_zone_annotation_buf( |
| 856 |
&self, |
| 857 |
time_zone: &TimeZone, |
| 858 |
offset: &Offset, |
| 859 |
bbuf: &mut BorrowedBuffer<'_>, |
| 860 |
) { |
| 861 |
bbuf.write_ascii_char(b'['); |
| 862 |
if let Some(iana_name) = time_zone.iana_name() { |
| 863 |
bbuf.write_str(iana_name); |
| 864 |
} else { |
| 865 |
self.print_offset_rounded_buf(offset, bbuf); |
| 866 |
} |
| 867 |
bbuf.write_ascii_char(b']'); |
| 868 |
} |
| 869 |
|
| 870 |
/// Formats the given time zone name into the writer given as an RFC 9557 |
| 871 |
/// time zone annotation. |
| 872 |
/// |
| 873 |
/// When the given time zone is not an IANA time zone name, then the offset |
| 874 |
/// is printed instead. (This means the offset will be printed twice, which |
| 875 |
/// is indeed an intended behavior of RFC 9557 for cases where a time zone |
| 876 |
/// name is not used or unavailable.) |
| 877 |
fn print_time_zone_annotation_wtr( |
| 878 |
&self, |
| 879 |
time_zone: &TimeZone, |
| 880 |
offset: &Offset, |
| 881 |
wtr: &mut BorrowedWriter<'_, '_, '_>, |
| 882 |
) -> Result<(), Error> { |
| 883 |
wtr.write_ascii_char(b'[')?; |
| 884 |
if let Some(iana_name) = time_zone.iana_name() { |
| 885 |
wtr.write_str(iana_name)?; |
| 886 |
} else { |
| 887 |
self.print_offset_rounded_wtr(offset, wtr)?; |
| 888 |
} |
| 889 |
wtr.write_ascii_char(b']')?; |
| 890 |
Ok(()) |
| 891 |
} |
| 892 |
} |
| 893 |
|
| 894 |
impl Default for DateTimePrinter { |
| 895 |
fn default() -> DateTimePrinter { |
| 896 |
DateTimePrinter::new() |
| 897 |
} |
| 898 |
} |
| 899 |
|
| 900 |
/// A printer for Temporal spans. |
| 901 |
/// |
| 902 |
/// Note that in Temporal, a "span" is called a "duration." |
| 903 |
#[derive(Debug)] |
| 904 |
pub(super) struct SpanPrinter { |
| 905 |
/// The designators to use. |
| 906 |
designators: &'static Designators, |
| 907 |
} |
| 908 |
|
| 909 |
impl SpanPrinter { |
| 910 |
/// Create a new Temporal span printer with the default configuration. |
| 911 |
pub(super) const fn new() -> SpanPrinter { |
| 912 |
SpanPrinter { designators: DESIGNATORS_UPPERCASE } |
| 913 |
} |
| 914 |
|
| 915 |
/// Use lowercase for unit designator labels. |
| 916 |
/// |
| 917 |
/// By default, unit designator labels are written in uppercase. |
| 918 |
pub(super) const fn lowercase(self, yes: bool) -> SpanPrinter { |
| 919 |
SpanPrinter { |
| 920 |
designators: if yes { |
| 921 |
DESIGNATORS_LOWERCASE |
| 922 |
} else { |
| 923 |
DESIGNATORS_UPPERCASE |
| 924 |
}, |
| 925 |
} |
| 926 |
} |
| 927 |
|
| 928 |
/// Print the given span to the writer given. |
| 929 |
/// |
| 930 |
/// This only returns an error when the given writer returns an error. |
| 931 |
pub(super) fn print_span( |
| 932 |
&self, |
| 933 |
span: &Span, |
| 934 |
wtr: &mut dyn Write, |
| 935 |
) -> Result<(), Error> { |
| 936 |
let mut buf = ArrayBuffer::<MAX_SPAN_LEN>::default(); |
| 937 |
let mut bbuf = buf.as_borrowed(); |
| 938 |
self.print_span_impl(span, &mut bbuf); |
| 939 |
wtr.write_str(bbuf.filled()) |
| 940 |
} |
| 941 |
|
| 942 |
fn print_span_impl(&self, span: &Span, bbuf: &mut BorrowedBuffer<'_>) { |
| 943 |
static SUBSECOND: UnitSet = UnitSet::from_slice(&[ |
| 944 |
Unit::Millisecond, |
| 945 |
Unit::Microsecond, |
| 946 |
Unit::Nanosecond, |
| 947 |
]); |
| 948 |
|
| 949 |
if span.is_negative() { |
| 950 |
bbuf.write_ascii_char(b'-'); |
| 951 |
} |
| 952 |
bbuf.write_ascii_char(b'P'); |
| 953 |
|
| 954 |
let units = span.units(); |
| 955 |
if units.contains(Unit::Year) { |
| 956 |
bbuf.write_int(span.get_years_unsigned()); |
| 957 |
bbuf.write_ascii_char(self.label(Unit::Year)); |
| 958 |
} |
| 959 |
if units.contains(Unit::Month) { |
| 960 |
bbuf.write_int(span.get_months_unsigned()); |
| 961 |
bbuf.write_ascii_char(self.label(Unit::Month)); |
| 962 |
} |
| 963 |
if units.contains(Unit::Week) { |
| 964 |
bbuf.write_int(span.get_weeks_unsigned()); |
| 965 |
bbuf.write_ascii_char(self.label(Unit::Week)); |
| 966 |
} |
| 967 |
if units.contains(Unit::Day) { |
| 968 |
bbuf.write_int(span.get_days_unsigned()); |
| 969 |
bbuf.write_ascii_char(self.label(Unit::Day)); |
| 970 |
} |
| 971 |
|
| 972 |
if units.only_time().is_empty() { |
| 973 |
if units.only_calendar().is_empty() { |
| 974 |
bbuf.write_ascii_char(b'T'); |
| 975 |
bbuf.write_ascii_char(b'0'); |
| 976 |
bbuf.write_ascii_char(self.label(Unit::Second)); |
| 977 |
} |
| 978 |
return; |
| 979 |
} |
| 980 |
|
| 981 |
bbuf.write_ascii_char(b'T'); |
| 982 |
|
| 983 |
if units.contains(Unit::Hour) { |
| 984 |
bbuf.write_int(span.get_hours_unsigned()); |
| 985 |
bbuf.write_ascii_char(self.label(Unit::Hour)); |
| 986 |
} |
| 987 |
if units.contains(Unit::Minute) { |
| 988 |
bbuf.write_int(span.get_minutes_unsigned()); |
| 989 |
bbuf.write_ascii_char(self.label(Unit::Minute)); |
| 990 |
} |
| 991 |
|
| 992 |
// ISO 8601 (and Temporal) don't support writing out milliseconds, |
| 993 |
// microseconds or nanoseconds as separate components like for all |
| 994 |
// the other units. Instead, they must be incorporated as fractional |
| 995 |
// seconds. But we only want to do that work if we need to. |
| 996 |
let has_subsecond = !units.intersection(SUBSECOND).is_empty(); |
| 997 |
if units.contains(Unit::Second) && !has_subsecond { |
| 998 |
bbuf.write_int(span.get_seconds_unsigned()); |
| 999 |
bbuf.write_ascii_char(self.label(Unit::Second)); |
| 1000 |
} else if has_subsecond { |
| 1001 |
// We want to combine our seconds, milliseconds, microseconds and |
| 1002 |
// nanoseconds into one single value in terms of nanoseconds. Then |
| 1003 |
// we can "balance" that out so that we have a number of seconds |
| 1004 |
// and a number of nanoseconds not greater than 1 second. (Which is |
| 1005 |
// our fraction.) |
| 1006 |
let (seconds, millis, micros, nanos) = ( |
| 1007 |
Duration::from_secs(span.get_seconds_unsigned()), |
| 1008 |
Duration::from_millis(span.get_milliseconds_unsigned()), |
| 1009 |
Duration::from_micros(span.get_microseconds_unsigned()), |
| 1010 |
Duration::from_nanos(span.get_nanoseconds_unsigned()), |
| 1011 |
); |
| 1012 |
// OK because the maximums for a span's seconds, millis, micros |
| 1013 |
// and nanos combined all fit into a 96-bit integer. (This is |
| 1014 |
// guaranteed by `Span::to_duration_invariant`.) |
| 1015 |
let total = seconds + millis + micros + nanos; |
| 1016 |
let (secs, subsecs) = (total.as_secs(), total.subsec_nanos()); |
| 1017 |
bbuf.write_int(secs); |
| 1018 |
if subsecs != 0 { |
| 1019 |
bbuf.write_ascii_char(b'.'); |
| 1020 |
bbuf.write_fraction(None, subsecs); |
| 1021 |
} |
| 1022 |
bbuf.write_ascii_char(self.label(Unit::Second)); |
| 1023 |
} |
| 1024 |
} |
| 1025 |
|
| 1026 |
/// Print the given signed duration to the writer given. |
| 1027 |
/// |
| 1028 |
/// This only returns an error when the given writer returns an error. |
| 1029 |
pub(super) fn print_signed_duration( |
| 1030 |
&self, |
| 1031 |
dur: &SignedDuration, |
| 1032 |
wtr: &mut dyn Write, |
| 1033 |
) -> Result<(), Error> { |
| 1034 |
let mut buf = ArrayBuffer::<MAX_DURATION_LEN>::default(); |
| 1035 |
let mut bbuf = buf.as_borrowed(); |
| 1036 |
if dur.is_negative() { |
| 1037 |
bbuf.write_ascii_char(b'-'); |
| 1038 |
} |
| 1039 |
self.print_unsigned_duration_impl(&dur.unsigned_abs(), &mut bbuf); |
| 1040 |
wtr.write_str(bbuf.filled()) |
| 1041 |
} |
| 1042 |
|
| 1043 |
/// Print the given unsigned duration to the writer given. |
| 1044 |
/// |
| 1045 |
/// This only returns an error when the given writer returns an error. |
| 1046 |
pub(super) fn print_unsigned_duration( |
| 1047 |
&self, |
| 1048 |
dur: &Duration, |
| 1049 |
wtr: &mut dyn Write, |
| 1050 |
) -> Result<(), Error> { |
| 1051 |
let mut buf = ArrayBuffer::<MAX_DURATION_LEN>::default(); |
| 1052 |
let mut bbuf = buf.as_borrowed(); |
| 1053 |
self.print_unsigned_duration_impl(dur, &mut bbuf); |
| 1054 |
wtr.write_str(bbuf.filled()) |
| 1055 |
} |
| 1056 |
|
| 1057 |
fn print_unsigned_duration_impl( |
| 1058 |
&self, |
| 1059 |
dur: &Duration, |
| 1060 |
bbuf: &mut BorrowedBuffer<'_>, |
| 1061 |
) { |
| 1062 |
bbuf.write_ascii_char(b'P'); |
| 1063 |
bbuf.write_ascii_char(b'T'); |
| 1064 |
|
| 1065 |
let (mut secs, nanos) = (dur.as_secs(), dur.subsec_nanos()); |
| 1066 |
let non_zero_greater_than_second = secs >= 60; |
| 1067 |
if non_zero_greater_than_second { |
| 1068 |
let hours = secs / (60 * 60); |
| 1069 |
secs %= 60 * 60; |
| 1070 |
let minutes = secs / 60; |
| 1071 |
secs = secs % 60; |
| 1072 |
if hours != 0 { |
| 1073 |
bbuf.write_int(hours); |
| 1074 |
bbuf.write_ascii_char(self.label(Unit::Hour)); |
| 1075 |
} |
| 1076 |
if minutes != 0 { |
| 1077 |
bbuf.write_int(minutes); |
| 1078 |
bbuf.write_ascii_char(self.label(Unit::Minute)); |
| 1079 |
} |
| 1080 |
} |
| 1081 |
if !non_zero_greater_than_second || secs != 0 || nanos != 0 { |
| 1082 |
bbuf.write_int(secs); |
| 1083 |
if nanos != 0 { |
| 1084 |
bbuf.write_ascii_char(b'.'); |
| 1085 |
bbuf.write_fraction(None, nanos); |
| 1086 |
} |
| 1087 |
bbuf.write_ascii_char(self.label(Unit::Second)); |
| 1088 |
} |
| 1089 |
} |
| 1090 |
|
| 1091 |
/// Converts the ASCII uppercase unit designator label to lowercase if this |
| 1092 |
/// printer is configured to use lowercase. Otherwise the label is returned |
| 1093 |
/// unchanged. |
| 1094 |
fn label(&self, unit: Unit) -> u8 { |
| 1095 |
self.designators.designator(unit) |
| 1096 |
} |
| 1097 |
} |
| 1098 |
|
| 1099 |
#[derive(Clone, Debug)] |
| 1100 |
struct Designators { |
| 1101 |
// Indexed by `Unit as usize` |
| 1102 |
map: [u8; 10], |
| 1103 |
} |
| 1104 |
|
| 1105 |
const DESIGNATORS_UPPERCASE: &'static Designators = &Designators { |
| 1106 |
// N.B. ISO 8601 duration format doesn't have unit |
| 1107 |
// designators for sub-second units. |
| 1108 |
map: [0, 0, 0, b'S', b'M', b'H', b'D', b'W', b'M', b'Y'], |
| 1109 |
}; |
| 1110 |
|
| 1111 |
const DESIGNATORS_LOWERCASE: &'static Designators = &Designators { |
| 1112 |
// N.B. ISO 8601 duration format doesn't have unit |
| 1113 |
// designators for sub-second units. |
| 1114 |
map: [0, 0, 0, b's', b'm', b'h', b'd', b'w', b'm', b'y'], |
| 1115 |
}; |
| 1116 |
|
| 1117 |
impl Designators { |
| 1118 |
/// Returns the designator for the given unit. |
| 1119 |
fn designator(&self, unit: Unit) -> u8 { |
| 1120 |
self.map[unit as usize] |
| 1121 |
} |
| 1122 |
} |
| 1123 |
|
| 1124 |
#[cfg(feature = "alloc")] |
| 1125 |
#[cfg(test)] |
| 1126 |
mod tests { |
| 1127 |
use alloc::string::{String, ToString}; |
| 1128 |
|
| 1129 |
use crate::{ |
| 1130 |
civil::{date, time, Weekday}, |
| 1131 |
fmt::StdFmtWrite, |
| 1132 |
span::ToSpan, |
| 1133 |
util::b, |
| 1134 |
}; |
| 1135 |
|
| 1136 |
use super::*; |
| 1137 |
|
| 1138 |
#[test] |
| 1139 |
fn print_zoned() { |
| 1140 |
if crate::tz::db().is_definitively_empty() { |
| 1141 |
return; |
| 1142 |
} |
| 1143 |
|
| 1144 |
let p = || DateTimePrinter::new(); |
| 1145 |
let via_io = |dtp: DateTimePrinter, zdt| { |
| 1146 |
let mut buf = String::new(); |
| 1147 |
dtp.print_zoned(&zdt, &mut StdFmtWrite(&mut buf)).unwrap(); |
| 1148 |
buf |
| 1149 |
}; |
| 1150 |
let to_string = |dtp: DateTimePrinter, zdt| { |
| 1151 |
let mut buf = String::new(); |
| 1152 |
dtp.print_zoned(&zdt, &mut buf).unwrap(); |
| 1153 |
let got_via_io = via_io(dtp, zdt); |
| 1154 |
assert_eq!( |
| 1155 |
buf, got_via_io, |
| 1156 |
"expected writes to `&mut String` to match `&mut StdFmtWrite`" |
| 1157 |
); |
| 1158 |
buf |
| 1159 |
}; |
| 1160 |
|
| 1161 |
let dt = date(2024, 3, 10).at(5, 34, 45, 0); |
| 1162 |
let zdt = dt.in_tz("America/New_York").unwrap(); |
| 1163 |
let got = to_string(p(), zdt); |
| 1164 |
assert_eq!(got, "2024-03-10T05:34:45-04:00[America/New_York]"); |
| 1165 |
|
| 1166 |
let dt = date(2024, 3, 10).at(5, 34, 45, 0); |
| 1167 |
let zdt = dt.in_tz("America/New_York").unwrap(); |
| 1168 |
let zdt = zdt.with_time_zone(TimeZone::UTC); |
| 1169 |
let got = to_string(p(), zdt); |
| 1170 |
assert_eq!(got, "2024-03-10T09:34:45+00:00[UTC]"); |
| 1171 |
|
| 1172 |
let dt = date(2024, 3, 10).at(5, 34, 45, 0); |
| 1173 |
let zdt = dt.to_zoned(TimeZone::fixed(Offset::MIN)).unwrap(); |
| 1174 |
let got = to_string(p(), zdt); |
| 1175 |
assert_eq!(got, "2024-03-10T05:34:45-25:59[-25:59]"); |
| 1176 |
|
| 1177 |
let dt = date(2024, 3, 10).at(5, 34, 45, 0); |
| 1178 |
let zdt = dt.to_zoned(TimeZone::fixed(Offset::MAX)).unwrap(); |
| 1179 |
let got = to_string(p(), zdt); |
| 1180 |
assert_eq!(got, "2024-03-10T05:34:45+25:59[+25:59]"); |
| 1181 |
|
| 1182 |
let dt = date(2024, 3, 10).at(5, 34, 45, 123_456_789); |
| 1183 |
let zdt = dt.in_tz("America/New_York").unwrap(); |
| 1184 |
let got = to_string(p(), zdt); |
| 1185 |
assert_eq!( |
| 1186 |
got, |
| 1187 |
"2024-03-10T05:34:45.123456789-04:00[America/New_York]" |
| 1188 |
); |
| 1189 |
|
| 1190 |
let dt = date(2024, 3, 10).at(5, 34, 45, 0); |
| 1191 |
let zdt = dt.in_tz("America/New_York").unwrap(); |
| 1192 |
let got = to_string(p().precision(Some(9)), zdt); |
| 1193 |
assert_eq!( |
| 1194 |
got, |
| 1195 |
"2024-03-10T05:34:45.000000000-04:00[America/New_York]" |
| 1196 |
); |
| 1197 |
|
| 1198 |
let dt = date(-9999, 3, 1).at(23, 59, 59, 999_999_999); |
| 1199 |
let zdt = dt.in_tz("America/Argentina/ComodRivadavia").unwrap(); |
| 1200 |
let got = to_string(p().precision(Some(9)), zdt); |
| 1201 |
assert_eq!( |
| 1202 |
got, |
| 1203 |
"-009999-03-01T23:59:59.999999999-04:23[America/Argentina/ComodRivadavia]", |
| 1204 |
); |
| 1205 |
|
| 1206 |
// Inject a very long IANA tzdb identifier to ensure it's handled |
| 1207 |
// properly. |
| 1208 |
let tz = TimeZone::tzif( |
| 1209 |
"Abc/Def/Ghi/Jkl/Mno/Pqr/Stu/Vwx/Yzz/Abc/Def/Ghi/Jkl/Mno/Pqr/Stu/Vwx/Yzz/Abc/Def/Ghi/Jkl/Mno/Pqr/Stu/Vwx/Yzz", |
| 1210 |
crate::tz::testdata::TzifTestFile::get("America/New_York").data, |
| 1211 |
).unwrap(); |
| 1212 |
let dt = date(-9999, 3, 1).at(23, 59, 59, 999_999_999); |
| 1213 |
let zdt = dt.to_zoned(tz).unwrap(); |
| 1214 |
let got = to_string(p().precision(Some(9)), zdt); |
| 1215 |
assert_eq!( |
| 1216 |
got, |
| 1217 |
"-009999-03-01T23:59:59.999999999-04:56[Abc/Def/Ghi/Jkl/Mno/Pqr/Stu/Vwx/Yzz/Abc/Def/Ghi/Jkl/Mno/Pqr/Stu/Vwx/Yzz/Abc/Def/Ghi/Jkl/Mno/Pqr/Stu/Vwx/Yzz]", |
| 1218 |
); |
| 1219 |
} |
| 1220 |
|
| 1221 |
#[test] |
| 1222 |
fn print_timestamp_zulu() { |
| 1223 |
let p = || DateTimePrinter::new(); |
| 1224 |
let via_io = |dtp: DateTimePrinter, ts| { |
| 1225 |
let mut buf = String::new(); |
| 1226 |
dtp.print_timestamp(&ts, &mut StdFmtWrite(&mut buf)).unwrap(); |
| 1227 |
buf |
| 1228 |
}; |
| 1229 |
let to_string = |dtp: DateTimePrinter, ts| { |
| 1230 |
let mut buf = String::new(); |
| 1231 |
dtp.print_timestamp(&ts, &mut buf).unwrap(); |
| 1232 |
let got_via_io = via_io(dtp, ts); |
| 1233 |
assert_eq!( |
| 1234 |
buf, got_via_io, |
| 1235 |
"expected writes to `&mut String` to match `&mut StdFmtWrite`" |
| 1236 |
); |
| 1237 |
buf |
| 1238 |
}; |
| 1239 |
|
| 1240 |
let tz = TimeZone::fixed(-Offset::constant(4)); |
| 1241 |
|
| 1242 |
let dt = date(2024, 3, 10).at(5, 34, 45, 0); |
| 1243 |
let zoned: Zoned = dt.to_zoned(tz.clone()).unwrap(); |
| 1244 |
let got = to_string(p(), zoned.timestamp()); |
| 1245 |
assert_eq!(got, "2024-03-10T09:34:45Z"); |
| 1246 |
|
| 1247 |
let dt = date(-2024, 3, 10).at(5, 34, 45, 0); |
| 1248 |
let zoned: Zoned = dt.to_zoned(tz.clone()).unwrap(); |
| 1249 |
let got = to_string(p(), zoned.timestamp()); |
| 1250 |
assert_eq!(got, "-002024-03-10T09:34:45Z"); |
| 1251 |
|
| 1252 |
let dt = date(2024, 3, 10).at(5, 34, 45, 123_456_789); |
| 1253 |
let zoned: Zoned = dt.to_zoned(tz.clone()).unwrap(); |
| 1254 |
let got = to_string(p(), zoned.timestamp()); |
| 1255 |
assert_eq!(got, "2024-03-10T09:34:45.123456789Z"); |
| 1256 |
|
| 1257 |
let dt = date(2024, 3, 10).at(5, 34, 45, 0); |
| 1258 |
let zoned: Zoned = dt.to_zoned(tz.clone()).unwrap(); |
| 1259 |
let got = to_string(p().precision(Some(9)), zoned.timestamp()); |
| 1260 |
assert_eq!(got, "2024-03-10T09:34:45.000000000Z"); |
| 1261 |
} |
| 1262 |
|
| 1263 |
#[test] |
| 1264 |
fn print_timestamp_with_offset() { |
| 1265 |
let p = || DateTimePrinter::new(); |
| 1266 |
let via_io = |dtp: DateTimePrinter, ts, offset| { |
| 1267 |
let mut buf = String::new(); |
| 1268 |
dtp.print_timestamp_with_offset( |
| 1269 |
&ts, |
| 1270 |
offset, |
| 1271 |
&mut StdFmtWrite(&mut buf), |
| 1272 |
) |
| 1273 |
.unwrap(); |
| 1274 |
buf |
| 1275 |
}; |
| 1276 |
let to_string = |dtp: DateTimePrinter, ts, offset| { |
| 1277 |
let mut buf = String::new(); |
| 1278 |
dtp.print_timestamp_with_offset(&ts, offset, &mut buf).unwrap(); |
| 1279 |
let got_via_io = via_io(dtp, ts, offset); |
| 1280 |
assert_eq!( |
| 1281 |
buf, got_via_io, |
| 1282 |
"expected writes to `&mut String` to match `&mut StdFmtWrite`" |
| 1283 |
); |
| 1284 |
buf |
| 1285 |
}; |
| 1286 |
|
| 1287 |
let tz = TimeZone::fixed(-Offset::constant(4)); |
| 1288 |
|
| 1289 |
let dt = date(2024, 3, 10).at(5, 34, 45, 0); |
| 1290 |
let zoned: Zoned = dt.to_zoned(tz.clone()).unwrap(); |
| 1291 |
let got = to_string(p(), zoned.timestamp(), zoned.offset()); |
| 1292 |
assert_eq!(got, "2024-03-10T05:34:45-04:00"); |
| 1293 |
|
| 1294 |
let dt = date(-2024, 3, 10).at(5, 34, 45, 0); |
| 1295 |
let zoned: Zoned = dt.to_zoned(tz.clone()).unwrap(); |
| 1296 |
let got = to_string(p(), zoned.timestamp(), zoned.offset()); |
| 1297 |
assert_eq!(got, "-002024-03-10T05:34:45-04:00"); |
| 1298 |
|
| 1299 |
let dt = date(2024, 3, 10).at(5, 34, 45, 123_456_789); |
| 1300 |
let zoned: Zoned = dt.to_zoned(tz.clone()).unwrap(); |
| 1301 |
let got = to_string(p(), zoned.timestamp(), zoned.offset()); |
| 1302 |
assert_eq!(got, "2024-03-10T05:34:45.123456789-04:00"); |
| 1303 |
|
| 1304 |
let dt = date(2024, 3, 10).at(5, 34, 45, 0); |
| 1305 |
let zoned: Zoned = dt.to_zoned(tz.clone()).unwrap(); |
| 1306 |
let got = to_string( |
| 1307 |
p().precision(Some(9)), |
| 1308 |
zoned.timestamp(), |
| 1309 |
zoned.offset(), |
| 1310 |
); |
| 1311 |
assert_eq!(got, "2024-03-10T05:34:45.000000000-04:00"); |
| 1312 |
|
| 1313 |
let dt = DateTime::MIN; |
| 1314 |
let zoned: Zoned = dt.to_zoned(TimeZone::fixed(Offset::MIN)).unwrap(); |
| 1315 |
let got = to_string( |
| 1316 |
p().precision(Some(9)), |
| 1317 |
zoned.timestamp(), |
| 1318 |
zoned.offset(), |
| 1319 |
); |
| 1320 |
assert_eq!(got, "-009999-01-01T00:00:00.000000000-25:59"); |
| 1321 |
} |
| 1322 |
|
| 1323 |
#[test] |
| 1324 |
fn print_datetime() { |
| 1325 |
let p = || DateTimePrinter::new(); |
| 1326 |
let via_io = |dtp: DateTimePrinter, dt| { |
| 1327 |
let mut buf = String::new(); |
| 1328 |
dtp.print_datetime(&dt, &mut StdFmtWrite(&mut buf)).unwrap(); |
| 1329 |
buf |
| 1330 |
}; |
| 1331 |
let to_string = |dtp: DateTimePrinter, dt| { |
| 1332 |
let mut buf = String::new(); |
| 1333 |
dtp.print_datetime(&dt, &mut buf).unwrap(); |
| 1334 |
let got_via_io = via_io(dtp, dt); |
| 1335 |
assert_eq!( |
| 1336 |
buf, got_via_io, |
| 1337 |
"expected writes to `&mut String` to match `&mut StdFmtWrite`" |
| 1338 |
); |
| 1339 |
buf |
| 1340 |
}; |
| 1341 |
|
| 1342 |
let dt = date(2024, 3, 10).at(5, 34, 45, 0); |
| 1343 |
let got = to_string(p(), dt); |
| 1344 |
assert_eq!(got, "2024-03-10T05:34:45"); |
| 1345 |
|
| 1346 |
let dt = date(-2024, 3, 10).at(5, 34, 45, 0); |
| 1347 |
let got = to_string(p(), dt); |
| 1348 |
assert_eq!(got, "-002024-03-10T05:34:45"); |
| 1349 |
|
| 1350 |
let dt = date(2024, 3, 10).at(5, 34, 45, 123_456_789); |
| 1351 |
let got = to_string(p(), dt); |
| 1352 |
assert_eq!(got, "2024-03-10T05:34:45.123456789"); |
| 1353 |
|
| 1354 |
let dt = date(2024, 3, 10).at(5, 34, 45, 0); |
| 1355 |
let got = to_string(p().precision(Some(9)), dt); |
| 1356 |
assert_eq!(got, "2024-03-10T05:34:45.000000000"); |
| 1357 |
|
| 1358 |
let dt = DateTime::MIN; |
| 1359 |
let got = to_string(p().precision(Some(9)), dt); |
| 1360 |
assert_eq!(got, "-009999-01-01T00:00:00.000000000"); |
| 1361 |
} |
| 1362 |
|
| 1363 |
#[test] |
| 1364 |
fn print_date() { |
| 1365 |
let p = || DateTimePrinter::new(); |
| 1366 |
let via_io = |dtp: DateTimePrinter, date| { |
| 1367 |
let mut buf = String::new(); |
| 1368 |
dtp.print_date(&date, &mut StdFmtWrite(&mut buf)).unwrap(); |
| 1369 |
buf |
| 1370 |
}; |
| 1371 |
let to_string = |dtp: DateTimePrinter, date| { |
| 1372 |
let mut buf = String::new(); |
| 1373 |
dtp.print_date(&date, &mut buf).unwrap(); |
| 1374 |
let got_via_io = via_io(dtp, date); |
| 1375 |
assert_eq!( |
| 1376 |
buf, got_via_io, |
| 1377 |
"expected writes to `&mut String` to match `&mut StdFmtWrite`" |
| 1378 |
); |
| 1379 |
buf |
| 1380 |
}; |
| 1381 |
|
| 1382 |
let d = date(2024, 3, 10); |
| 1383 |
let got = to_string(p(), d); |
| 1384 |
assert_eq!(got, "2024-03-10"); |
| 1385 |
|
| 1386 |
let d = date(-2024, 3, 10); |
| 1387 |
let got = to_string(p(), d); |
| 1388 |
assert_eq!(got, "-002024-03-10"); |
| 1389 |
|
| 1390 |
let d = date(2024, 3, 10); |
| 1391 |
let got = to_string(p(), d); |
| 1392 |
assert_eq!(got, "2024-03-10"); |
| 1393 |
|
| 1394 |
let d = Date::MIN; |
| 1395 |
let got = to_string(p().precision(Some(9)), d); |
| 1396 |
assert_eq!(got, "-009999-01-01"); |
| 1397 |
} |
| 1398 |
|
| 1399 |
#[test] |
| 1400 |
fn print_time() { |
| 1401 |
let p = || DateTimePrinter::new(); |
| 1402 |
let via_io = |dtp: DateTimePrinter, time| { |
| 1403 |
let mut buf = String::new(); |
| 1404 |
dtp.print_time(&time, &mut StdFmtWrite(&mut buf)).unwrap(); |
| 1405 |
buf |
| 1406 |
}; |
| 1407 |
let to_string = |dtp: DateTimePrinter, time| { |
| 1408 |
let mut buf = String::new(); |
| 1409 |
dtp.print_time(&time, &mut buf).unwrap(); |
| 1410 |
let got_via_io = via_io(dtp, time); |
| 1411 |
assert_eq!( |
| 1412 |
buf, got_via_io, |
| 1413 |
"expected writes to `&mut String` to match `&mut StdFmtWrite`" |
| 1414 |
); |
| 1415 |
buf |
| 1416 |
}; |
| 1417 |
|
| 1418 |
let t = time(5, 34, 45, 0); |
| 1419 |
let got = to_string(p(), t); |
| 1420 |
assert_eq!(got, "05:34:45"); |
| 1421 |
|
| 1422 |
let t = time(5, 34, 45, 0); |
| 1423 |
let got = to_string(p(), t); |
| 1424 |
assert_eq!(got, "05:34:45"); |
| 1425 |
|
| 1426 |
let t = time(5, 34, 45, 123_456_789); |
| 1427 |
let got = to_string(p(), t); |
| 1428 |
assert_eq!(got, "05:34:45.123456789"); |
| 1429 |
|
| 1430 |
let t = time(5, 34, 45, 0); |
| 1431 |
let got = to_string(p().precision(Some(9)), t); |
| 1432 |
assert_eq!(got, "05:34:45.000000000"); |
| 1433 |
|
| 1434 |
let t = Time::MIN; |
| 1435 |
let got = to_string(p().precision(Some(9)), t); |
| 1436 |
assert_eq!(got, "00:00:00.000000000"); |
| 1437 |
|
| 1438 |
let t = Time::MAX; |
| 1439 |
let got = to_string(p().precision(Some(9)), t); |
| 1440 |
assert_eq!(got, "23:59:59.999999999"); |
| 1441 |
} |
| 1442 |
|
| 1443 |
#[test] |
| 1444 |
fn print_iso_week_date() { |
| 1445 |
let p = || DateTimePrinter::new(); |
| 1446 |
let via_io = |dtp: DateTimePrinter, date| { |
| 1447 |
let mut buf = String::new(); |
| 1448 |
dtp.print_iso_week_date(&date, &mut StdFmtWrite(&mut buf)) |
| 1449 |
.unwrap(); |
| 1450 |
buf |
| 1451 |
}; |
| 1452 |
let to_string = |dtp: DateTimePrinter, date| { |
| 1453 |
let mut buf = String::new(); |
| 1454 |
dtp.print_iso_week_date(&date, &mut buf).unwrap(); |
| 1455 |
let got_via_io = via_io(dtp, date); |
| 1456 |
assert_eq!( |
| 1457 |
buf, got_via_io, |
| 1458 |
"expected writes to `&mut String` to match `&mut StdFmtWrite`" |
| 1459 |
); |
| 1460 |
buf |
| 1461 |
}; |
| 1462 |
|
| 1463 |
let d = ISOWeekDate::new(2024, 52, Weekday::Monday).unwrap(); |
| 1464 |
let got = to_string(p(), d); |
| 1465 |
assert_eq!(got, "2024-W52-1"); |
| 1466 |
|
| 1467 |
let d = ISOWeekDate::new(2004, 1, Weekday::Sunday).unwrap(); |
| 1468 |
let got = to_string(p(), d); |
| 1469 |
assert_eq!(got, "2004-W01-7"); |
| 1470 |
|
| 1471 |
let d = ISOWeekDate::MIN; |
| 1472 |
let got = to_string(p(), d); |
| 1473 |
assert_eq!(got, "-009999-W01-1"); |
| 1474 |
|
| 1475 |
let d = ISOWeekDate::MAX; |
| 1476 |
let got = to_string(p(), d); |
| 1477 |
assert_eq!(got, "9999-W52-5"); |
| 1478 |
} |
| 1479 |
|
| 1480 |
#[test] |
| 1481 |
fn print_pieces() { |
| 1482 |
let p = || DateTimePrinter::new(); |
| 1483 |
let via_io = |dtp: DateTimePrinter, pieces| { |
| 1484 |
let mut buf = String::new(); |
| 1485 |
dtp.print_pieces(&pieces, &mut StdFmtWrite(&mut buf)).unwrap(); |
| 1486 |
buf |
| 1487 |
}; |
| 1488 |
let to_string = |dtp: DateTimePrinter, pieces| { |
| 1489 |
let mut buf = String::new(); |
| 1490 |
dtp.print_pieces(&pieces, &mut buf).unwrap(); |
| 1491 |
let got_via_io = via_io(dtp, pieces); |
| 1492 |
assert_eq!( |
| 1493 |
buf, got_via_io, |
| 1494 |
"expected writes to `&mut String` to match `&mut StdFmtWrite`" |
| 1495 |
); |
| 1496 |
buf |
| 1497 |
}; |
| 1498 |
|
| 1499 |
let pieces = Pieces::from(date(2024, 3, 10).at(5, 34, 45, 0)) |
| 1500 |
.with_offset(Offset::constant(-4)) |
| 1501 |
.with_time_zone_name("America/New_York"); |
| 1502 |
let got = to_string(p(), pieces); |
| 1503 |
assert_eq!(got, "2024-03-10T05:34:45-04:00[America/New_York]"); |
| 1504 |
|
| 1505 |
let pieces = Pieces::from(date(2024, 3, 10).at(5, 34, 45, 0)) |
| 1506 |
.with_offset(Offset::UTC) |
| 1507 |
.with_time_zone_name("UTC"); |
| 1508 |
let got = to_string(p(), pieces); |
| 1509 |
assert_eq!(got, "2024-03-10T05:34:45+00:00[UTC]"); |
| 1510 |
|
| 1511 |
let pieces = Pieces::from(date(2024, 3, 10).at(5, 34, 45, 0)) |
| 1512 |
.with_offset(Offset::MIN) |
| 1513 |
.with_time_zone_offset(Offset::MIN); |
| 1514 |
let got = to_string(p(), pieces); |
| 1515 |
assert_eq!(got, "2024-03-10T05:34:45-25:59[-25:59]"); |
| 1516 |
|
| 1517 |
let pieces = Pieces::from(date(2024, 3, 10).at(5, 34, 45, 0)) |
| 1518 |
.with_offset(Offset::MAX) |
| 1519 |
.with_time_zone_offset(Offset::MAX); |
| 1520 |
let got = to_string(p(), pieces); |
| 1521 |
assert_eq!(got, "2024-03-10T05:34:45+25:59[+25:59]"); |
| 1522 |
|
| 1523 |
let pieces = |
| 1524 |
Pieces::from(date(2024, 3, 10).at(5, 34, 45, 123_456_789)) |
| 1525 |
.with_offset(Offset::constant(-4)) |
| 1526 |
.with_time_zone_name("America/New_York"); |
| 1527 |
let got = to_string(p(), pieces); |
| 1528 |
assert_eq!( |
| 1529 |
got, |
| 1530 |
"2024-03-10T05:34:45.123456789-04:00[America/New_York]" |
| 1531 |
); |
| 1532 |
|
| 1533 |
let pieces = Pieces::from(date(2024, 3, 10).at(5, 34, 45, 0)) |
| 1534 |
.with_offset(Offset::constant(-4)) |
| 1535 |
.with_time_zone_name("America/New_York"); |
| 1536 |
let got = to_string(p().precision(Some(9)), pieces); |
| 1537 |
assert_eq!( |
| 1538 |
got, |
| 1539 |
"2024-03-10T05:34:45.000000000-04:00[America/New_York]" |
| 1540 |
); |
| 1541 |
|
| 1542 |
let pieces = |
| 1543 |
Pieces::from(date(-9999, 3, 1).at(23, 59, 59, 999_999_999)) |
| 1544 |
.with_offset(Offset::constant(-4)) |
| 1545 |
.with_time_zone_name("America/Argentina/ComodRivadavia"); |
| 1546 |
let got = to_string(p().precision(Some(9)), pieces); |
| 1547 |
assert_eq!( |
| 1548 |
got, |
| 1549 |
"-009999-03-01T23:59:59.999999999-04:00[America/Argentina/ComodRivadavia]", |
| 1550 |
); |
| 1551 |
|
| 1552 |
let pieces = |
| 1553 |
Pieces::parse( |
| 1554 |
"-009999-03-01T23:59:59.999999999-04:00[!America/Argentina/ComodRivadavia]", |
| 1555 |
).unwrap(); |
| 1556 |
let got = to_string(p().precision(Some(9)), pieces); |
| 1557 |
assert_eq!( |
| 1558 |
got, |
| 1559 |
"-009999-03-01T23:59:59.999999999-04:00[!America/Argentina/ComodRivadavia]", |
| 1560 |
); |
| 1561 |
|
| 1562 |
// Inject a very long IANA tzdb identifier to ensure it's handled |
| 1563 |
let name = "Abc/Def/Ghi/Jkl/Mno/Pqr/Stu/Vwx/Yzz/Abc/Def/Ghi/Jkl/Mno/Pqr/Stu/Vwx/Yzz/Abc/Def/Ghi/Jkl/Mno/Pqr/Stu/Vwx/Yzz"; |
| 1564 |
let pieces = |
| 1565 |
Pieces::from(date(-9999, 3, 1).at(23, 59, 59, 999_999_999)) |
| 1566 |
.with_offset(Offset::constant(-4)) |
| 1567 |
.with_time_zone_name(name); |
| 1568 |
let got = to_string(p().precision(Some(9)), pieces); |
| 1569 |
assert_eq!( |
| 1570 |
got, |
| 1571 |
"-009999-03-01T23:59:59.999999999-04:00[Abc/Def/Ghi/Jkl/Mno/Pqr/Stu/Vwx/Yzz/Abc/Def/Ghi/Jkl/Mno/Pqr/Stu/Vwx/Yzz/Abc/Def/Ghi/Jkl/Mno/Pqr/Stu/Vwx/Yzz]", |
| 1572 |
); |
| 1573 |
} |
| 1574 |
|
| 1575 |
#[test] |
| 1576 |
fn print_time_zone() { |
| 1577 |
let p = || DateTimePrinter::new(); |
| 1578 |
let via_io = |dtp: DateTimePrinter, tz| { |
| 1579 |
let mut buf = String::new(); |
| 1580 |
dtp.print_time_zone(&tz, &mut StdFmtWrite(&mut buf)).unwrap(); |
| 1581 |
buf |
| 1582 |
}; |
| 1583 |
let to_string = |dtp: DateTimePrinter, tz| { |
| 1584 |
let mut buf = String::new(); |
| 1585 |
dtp.print_time_zone(&tz, &mut buf).unwrap(); |
| 1586 |
let got_via_io = via_io(dtp, tz); |
| 1587 |
assert_eq!( |
| 1588 |
buf, got_via_io, |
| 1589 |
"expected writes to `&mut String` to match `&mut StdFmtWrite`" |
| 1590 |
); |
| 1591 |
buf |
| 1592 |
}; |
| 1593 |
|
| 1594 |
let tztest = |
| 1595 |
crate::tz::testdata::TzifTestFile::get("America/New_York"); |
| 1596 |
let tz = TimeZone::tzif(tztest.name, tztest.data).unwrap(); |
| 1597 |
let got = to_string(p(), tz); |
| 1598 |
assert_eq!(got, "America/New_York"); |
| 1599 |
|
| 1600 |
// Inject a very long IANA tzdb identifier to ensure it's handled |
| 1601 |
// properly. |
| 1602 |
let tz = TimeZone::tzif( |
| 1603 |
"Abc/Def/Ghi/Jkl/Mno/Pqr/Stu/Vwx/Yzz/Abc/Def/Ghi/Jkl/Mno/Pqr/Stu/Vwx/Yzz/Abc/Def/Ghi/Jkl/Mno/Pqr/Stu/Vwx/Yzz", |
| 1604 |
tztest.data, |
| 1605 |
).unwrap(); |
| 1606 |
let got = to_string(p(), tz); |
| 1607 |
assert_eq!( |
| 1608 |
got, |
| 1609 |
"Abc/Def/Ghi/Jkl/Mno/Pqr/Stu/Vwx/Yzz/Abc/Def/Ghi/Jkl/Mno/Pqr/Stu/Vwx/Yzz/Abc/Def/Ghi/Jkl/Mno/Pqr/Stu/Vwx/Yzz", |
| 1610 |
); |
| 1611 |
|
| 1612 |
let tz = TimeZone::UTC; |
| 1613 |
let got = to_string(p(), tz); |
| 1614 |
assert_eq!(got, "UTC"); |
| 1615 |
|
| 1616 |
let tz = TimeZone::unknown(); |
| 1617 |
let got = to_string(p(), tz); |
| 1618 |
assert_eq!(got, "Etc/Unknown"); |
| 1619 |
|
| 1620 |
let tz = TimeZone::fixed(Offset::MIN); |
| 1621 |
let got = to_string(p(), tz); |
| 1622 |
assert_eq!(got, "-25:59:59"); |
| 1623 |
|
| 1624 |
let tz = TimeZone::fixed(Offset::MAX); |
| 1625 |
let got = to_string(p(), tz); |
| 1626 |
assert_eq!(got, "+25:59:59"); |
| 1627 |
|
| 1628 |
let tz = TimeZone::posix("EST5EDT,M3.2.0,M11.1.0").unwrap(); |
| 1629 |
let got = to_string(p(), tz); |
| 1630 |
assert_eq!(got, "EST5EDT,M3.2.0,M11.1.0"); |
| 1631 |
|
| 1632 |
let tz = TimeZone::posix("EST5EDT5,M3.2.0,M11.1.0").unwrap(); |
| 1633 |
let got = to_string(p(), tz); |
| 1634 |
assert_eq!(got, "EST5EDT5,M3.2.0,M11.1.0"); |
| 1635 |
|
| 1636 |
let tz = TimeZone::posix("EST5EDT,J1,J365/5:12:34").unwrap(); |
| 1637 |
let got = to_string(p(), tz); |
| 1638 |
assert_eq!(got, "EST5EDT,J1,J365/5:12:34"); |
| 1639 |
|
| 1640 |
let tz = |
| 1641 |
TimeZone::posix("<+09>-9<-10>-10,M3.2.0/1:23:45,J365/-1").unwrap(); |
| 1642 |
let got = to_string(p(), tz); |
| 1643 |
assert_eq!(got, "<+09>-9<-10>,M3.2.0/1:23:45,J365/-1"); |
| 1644 |
|
| 1645 |
let tz = TimeZone::posix( |
| 1646 |
"ABCDEFGHIJKLMNOPQRSTUVWXY5ABCDEFGHIJKLMNOPQRSTUVWXYT,M3.2.0,M11.1.0", |
| 1647 |
).unwrap(); |
| 1648 |
let got = to_string(p(), tz); |
| 1649 |
assert_eq!( |
| 1650 |
got, |
| 1651 |
"ABCDEFGHIJKLMNOPQRSTUVWXY5ABCDEFGHIJKLMNOPQRSTUVWXYT,M3.2.0,M11.1.0", |
| 1652 |
); |
| 1653 |
|
| 1654 |
// This isn't a public API, but this lets us test the error |
| 1655 |
// case: a valid time zone but without a succinct name. |
| 1656 |
#[cfg(feature = "tz-system")] |
| 1657 |
{ |
| 1658 |
let tz = TimeZone::tzif_system(tztest.data).unwrap(); |
| 1659 |
let mut buf = String::new(); |
| 1660 |
let err = p().print_time_zone(&tz, &mut buf).unwrap_err(); |
| 1661 |
assert_eq!( |
| 1662 |
err.to_string(), |
| 1663 |
"time zones without IANA identifiers that aren't \ |
| 1664 |
either fixed offsets or a POSIX time zone can't be \ |
| 1665 |
serialized (this typically occurs when this is a \ |
| 1666 |
system time zone derived from `/etc/localtime` on \ |
| 1667 |
Unix systems that isn't symlinked to an entry in \ |
| 1668 |
`/usr/share/zoneinfo`)", |
| 1669 |
); |
| 1670 |
} |
| 1671 |
} |
| 1672 |
|
| 1673 |
#[cfg(not(miri))] |
| 1674 |
#[test] |
| 1675 |
fn print_span_basic() { |
| 1676 |
let p = |span: Span| -> String { |
| 1677 |
let mut buf = String::new(); |
| 1678 |
SpanPrinter::new().print_span(&span, &mut buf).unwrap(); |
| 1679 |
buf |
| 1680 |
}; |
| 1681 |
|
| 1682 |
insta::assert_snapshot!(p(Span::new()), @"PT0S"); |
| 1683 |
insta::assert_snapshot!(p(1.second()), @"PT1S"); |
| 1684 |
insta::assert_snapshot!(p(-1.second()), @"-PT1S"); |
| 1685 |
insta::assert_snapshot!(p( |
| 1686 |
1.second().milliseconds(1).microseconds(1).nanoseconds(1), |
| 1687 |
), @"PT1.001001001S"); |
| 1688 |
insta::assert_snapshot!(p( |
| 1689 |
0.second().milliseconds(999).microseconds(999).nanoseconds(999), |
| 1690 |
), @"PT0.999999999S"); |
| 1691 |
insta::assert_snapshot!(p( |
| 1692 |
1.year().months(1).weeks(1).days(1) |
| 1693 |
.hours(1).minutes(1).seconds(1) |
| 1694 |
.milliseconds(1).microseconds(1).nanoseconds(1), |
| 1695 |
), @"P1Y1M1W1DT1H1M1.001001001S"); |
| 1696 |
insta::assert_snapshot!(p( |
| 1697 |
-1.year().months(1).weeks(1).days(1) |
| 1698 |
.hours(1).minutes(1).seconds(1) |
| 1699 |
.milliseconds(1).microseconds(1).nanoseconds(1), |
| 1700 |
), @"-P1Y1M1W1DT1H1M1.001001001S"); |
| 1701 |
} |
| 1702 |
|
| 1703 |
#[cfg(not(miri))] |
| 1704 |
#[test] |
| 1705 |
fn print_span_subsecond_positive() { |
| 1706 |
let p = |span: Span| -> String { |
| 1707 |
let mut buf = String::new(); |
| 1708 |
SpanPrinter::new().print_span(&span, &mut buf).unwrap(); |
| 1709 |
buf |
| 1710 |
}; |
| 1711 |
|
| 1712 |
// These are all sub-second trickery tests. |
| 1713 |
insta::assert_snapshot!(p( |
| 1714 |
0.second().milliseconds(1000).microseconds(1000).nanoseconds(1000), |
| 1715 |
), @"PT1.001001S"); |
| 1716 |
insta::assert_snapshot!(p( |
| 1717 |
1.second().milliseconds(1000).microseconds(1000).nanoseconds(1000), |
| 1718 |
), @"PT2.001001S"); |
| 1719 |
insta::assert_snapshot!(p( |
| 1720 |
0.second() |
| 1721 |
.milliseconds(b::SpanMilliseconds::MAX), |
| 1722 |
), @"PT631107417600S"); |
| 1723 |
insta::assert_snapshot!(p( |
| 1724 |
0.second() |
| 1725 |
.microseconds(b::SpanMicroseconds::MAX), |
| 1726 |
), @"PT631107417600S"); |
| 1727 |
insta::assert_snapshot!(p( |
| 1728 |
0.second() |
| 1729 |
.nanoseconds(b::SpanNanoseconds::MAX), |
| 1730 |
), @"PT9223372036.854775807S"); |
| 1731 |
|
| 1732 |
insta::assert_snapshot!(p( |
| 1733 |
0.second() |
| 1734 |
.milliseconds(b::SpanMilliseconds::MAX) |
| 1735 |
.microseconds(999_999), |
| 1736 |
), @"PT631107417600.999999S"); |
| 1737 |
// This is 1 microsecond more than the maximum number of seconds |
| 1738 |
// representable in a span. |
| 1739 |
insta::assert_snapshot!(p( |
| 1740 |
0.second() |
| 1741 |
.milliseconds(b::SpanMilliseconds::MAX) |
| 1742 |
.microseconds(1_000_000), |
| 1743 |
), @"PT631107417601S"); |
| 1744 |
insta::assert_snapshot!(p( |
| 1745 |
0.second() |
| 1746 |
.milliseconds(b::SpanMilliseconds::MAX) |
| 1747 |
.microseconds(1_000_001), |
| 1748 |
), @"PT631107417601.000001S"); |
| 1749 |
// This is 1 nanosecond more than the maximum number of seconds |
| 1750 |
// representable in a span. |
| 1751 |
insta::assert_snapshot!(p( |
| 1752 |
0.second() |
| 1753 |
.milliseconds(b::SpanMilliseconds::MAX) |
| 1754 |
.nanoseconds(1_000_000_000), |
| 1755 |
), @"PT631107417601S"); |
| 1756 |
insta::assert_snapshot!(p( |
| 1757 |
0.second() |
| 1758 |
.milliseconds(b::SpanMilliseconds::MAX) |
| 1759 |
.nanoseconds(1_000_000_001), |
| 1760 |
), @"PT631107417601.000000001S"); |
| 1761 |
|
| 1762 |
// The max millis, micros and nanos, combined. |
| 1763 |
insta::assert_snapshot!(p( |
| 1764 |
0.second() |
| 1765 |
.milliseconds(b::SpanMilliseconds::MAX) |
| 1766 |
.microseconds(b::SpanMicroseconds::MAX) |
| 1767 |
.nanoseconds(b::SpanNanoseconds::MAX), |
| 1768 |
), @"PT1271438207236.854775807S"); |
| 1769 |
// The max seconds, millis, micros and nanos, combined. |
| 1770 |
insta::assert_snapshot!(p( |
| 1771 |
Span::new() |
| 1772 |
.seconds(b::SpanSeconds::MAX) |
| 1773 |
.milliseconds(b::SpanMilliseconds::MAX) |
| 1774 |
.microseconds(b::SpanMicroseconds::MAX) |
| 1775 |
.nanoseconds(b::SpanNanoseconds::MAX), |
| 1776 |
), @"PT1902545624836.854775807S"); |
| 1777 |
} |
| 1778 |
|
| 1779 |
#[cfg(not(miri))] |
| 1780 |
#[test] |
| 1781 |
fn print_span_subsecond_negative() { |
| 1782 |
let p = |span: Span| -> String { |
| 1783 |
let mut buf = String::new(); |
| 1784 |
SpanPrinter::new().print_span(&span, &mut buf).unwrap(); |
| 1785 |
buf |
| 1786 |
}; |
| 1787 |
|
| 1788 |
// These are all sub-second trickery tests. |
| 1789 |
insta::assert_snapshot!(p( |
| 1790 |
-0.second().milliseconds(1000).microseconds(1000).nanoseconds(1000), |
| 1791 |
), @"-PT1.001001S"); |
| 1792 |
insta::assert_snapshot!(p( |
| 1793 |
-1.second().milliseconds(1000).microseconds(1000).nanoseconds(1000), |
| 1794 |
), @"-PT2.001001S"); |
| 1795 |
insta::assert_snapshot!(p( |
| 1796 |
0.second() |
| 1797 |
.milliseconds(b::SpanMilliseconds::MIN), |
| 1798 |
), @"-PT631107417600S"); |
| 1799 |
insta::assert_snapshot!(p( |
| 1800 |
0.second() |
| 1801 |
.microseconds(b::SpanMicroseconds::MIN), |
| 1802 |
), @"-PT631107417600S"); |
| 1803 |
insta::assert_snapshot!(p( |
| 1804 |
0.second() |
| 1805 |
.nanoseconds(b::SpanNanoseconds::MIN), |
| 1806 |
), @"-PT9223372036.854775807S"); |
| 1807 |
|
| 1808 |
insta::assert_snapshot!(p( |
| 1809 |
0.second() |
| 1810 |
.milliseconds(b::SpanMilliseconds::MIN) |
| 1811 |
.microseconds(999_999), |
| 1812 |
), @"-PT631107417600.999999S"); |
| 1813 |
// This is 1 microsecond more than the maximum number of seconds |
| 1814 |
// representable in a span. |
| 1815 |
insta::assert_snapshot!(p( |
| 1816 |
0.second() |
| 1817 |
.milliseconds(b::SpanMilliseconds::MIN) |
| 1818 |
.microseconds(1_000_000), |
| 1819 |
), @"-PT631107417601S"); |
| 1820 |
insta::assert_snapshot!(p( |
| 1821 |
0.second() |
| 1822 |
.milliseconds(b::SpanMilliseconds::MIN) |
| 1823 |
.microseconds(1_000_001), |
| 1824 |
), @"-PT631107417601.000001S"); |
| 1825 |
// This is 1 nanosecond more than the maximum number of seconds |
| 1826 |
// representable in a span. |
| 1827 |
insta::assert_snapshot!(p( |
| 1828 |
0.second() |
| 1829 |
.milliseconds(b::SpanMilliseconds::MIN) |
| 1830 |
.nanoseconds(1_000_000_000), |
| 1831 |
), @"-PT631107417601S"); |
| 1832 |
insta::assert_snapshot!(p( |
| 1833 |
0.second() |
| 1834 |
.milliseconds(b::SpanMilliseconds::MIN) |
| 1835 |
.nanoseconds(1_000_000_001), |
| 1836 |
), @"-PT631107417601.000000001S"); |
| 1837 |
|
| 1838 |
// The max millis, micros and nanos, combined. |
| 1839 |
insta::assert_snapshot!(p( |
| 1840 |
0.second() |
| 1841 |
.milliseconds(b::SpanMilliseconds::MIN) |
| 1842 |
.microseconds(b::SpanMicroseconds::MIN) |
| 1843 |
.nanoseconds(b::SpanNanoseconds::MIN), |
| 1844 |
), @"-PT1271438207236.854775807S"); |
| 1845 |
// The max seconds, millis, micros and nanos, combined. |
| 1846 |
insta::assert_snapshot!(p( |
| 1847 |
Span::new() |
| 1848 |
.seconds(b::SpanSeconds::MIN) |
| 1849 |
.milliseconds(b::SpanMilliseconds::MIN) |
| 1850 |
.microseconds(b::SpanMicroseconds::MIN) |
| 1851 |
.nanoseconds(b::SpanNanoseconds::MIN), |
| 1852 |
), @"-PT1902545624836.854775807S"); |
| 1853 |
} |
| 1854 |
|
| 1855 |
#[cfg(not(miri))] |
| 1856 |
#[test] |
| 1857 |
fn print_signed_duration() { |
| 1858 |
let p = |secs, nanos| -> String { |
| 1859 |
let dur = SignedDuration::new(secs, nanos); |
| 1860 |
let mut buf = String::new(); |
| 1861 |
SpanPrinter::new().print_signed_duration(&dur, &mut buf).unwrap(); |
| 1862 |
buf |
| 1863 |
}; |
| 1864 |
|
| 1865 |
insta::assert_snapshot!(p(0, 0), @"PT0S"); |
| 1866 |
insta::assert_snapshot!(p(0, 1), @"PT0.000000001S"); |
| 1867 |
insta::assert_snapshot!(p(1, 0), @"PT1S"); |
| 1868 |
insta::assert_snapshot!(p(59, 0), @"PT59S"); |
| 1869 |
insta::assert_snapshot!(p(60, 0), @"PT1M"); |
| 1870 |
insta::assert_snapshot!(p(60, 1), @"PT1M0.000000001S"); |
| 1871 |
insta::assert_snapshot!(p(61, 1), @"PT1M1.000000001S"); |
| 1872 |
insta::assert_snapshot!(p(3_600, 0), @"PT1H"); |
| 1873 |
insta::assert_snapshot!(p(3_600, 1), @"PT1H0.000000001S"); |
| 1874 |
insta::assert_snapshot!(p(3_660, 0), @"PT1H1M"); |
| 1875 |
insta::assert_snapshot!(p(3_660, 1), @"PT1H1M0.000000001S"); |
| 1876 |
insta::assert_snapshot!(p(3_661, 0), @"PT1H1M1S"); |
| 1877 |
insta::assert_snapshot!(p(3_661, 1), @"PT1H1M1.000000001S"); |
| 1878 |
|
| 1879 |
insta::assert_snapshot!(p(0, -1), @"-PT0.000000001S"); |
| 1880 |
insta::assert_snapshot!(p(-1, 0), @"-PT1S"); |
| 1881 |
insta::assert_snapshot!(p(-59, 0), @"-PT59S"); |
| 1882 |
insta::assert_snapshot!(p(-60, 0), @"-PT1M"); |
| 1883 |
insta::assert_snapshot!(p(-60, -1), @"-PT1M0.000000001S"); |
| 1884 |
insta::assert_snapshot!(p(-61, -1), @"-PT1M1.000000001S"); |
| 1885 |
insta::assert_snapshot!(p(-3_600, 0), @"-PT1H"); |
| 1886 |
insta::assert_snapshot!(p(-3_600, -1), @"-PT1H0.000000001S"); |
| 1887 |
insta::assert_snapshot!(p(-3_660, 0), @"-PT1H1M"); |
| 1888 |
insta::assert_snapshot!(p(-3_660, -1), @"-PT1H1M0.000000001S"); |
| 1889 |
insta::assert_snapshot!(p(-3_661, 0), @"-PT1H1M1S"); |
| 1890 |
insta::assert_snapshot!(p(-3_661, -1), @"-PT1H1M1.000000001S"); |
| 1891 |
|
| 1892 |
insta::assert_snapshot!( |
| 1893 |
p(i64::MIN, -999_999_999), |
| 1894 |
@"-PT2562047788015215H30M8.999999999S", |
| 1895 |
); |
| 1896 |
insta::assert_snapshot!( |
| 1897 |
p(i64::MAX, 999_999_999), |
| 1898 |
@"PT2562047788015215H30M7.999999999S", |
| 1899 |
); |
| 1900 |
} |
| 1901 |
|
| 1902 |
#[cfg(not(miri))] |
| 1903 |
#[test] |
| 1904 |
fn print_unsigned_duration() { |
| 1905 |
let p = |secs, nanos| -> String { |
| 1906 |
let dur = Duration::new(secs, nanos); |
| 1907 |
let mut buf = String::new(); |
| 1908 |
SpanPrinter::new() |
| 1909 |
.print_unsigned_duration(&dur, &mut buf) |
| 1910 |
.unwrap(); |
| 1911 |
buf |
| 1912 |
}; |
| 1913 |
|
| 1914 |
insta::assert_snapshot!(p(0, 0), @"PT0S"); |
| 1915 |
insta::assert_snapshot!(p(0, 1), @"PT0.000000001S"); |
| 1916 |
insta::assert_snapshot!(p(1, 0), @"PT1S"); |
| 1917 |
insta::assert_snapshot!(p(59, 0), @"PT59S"); |
| 1918 |
insta::assert_snapshot!(p(60, 0), @"PT1M"); |
| 1919 |
insta::assert_snapshot!(p(60, 1), @"PT1M0.000000001S"); |
| 1920 |
insta::assert_snapshot!(p(61, 1), @"PT1M1.000000001S"); |
| 1921 |
insta::assert_snapshot!(p(3_600, 0), @"PT1H"); |
| 1922 |
insta::assert_snapshot!(p(3_600, 1), @"PT1H0.000000001S"); |
| 1923 |
insta::assert_snapshot!(p(3_660, 0), @"PT1H1M"); |
| 1924 |
insta::assert_snapshot!(p(3_660, 1), @"PT1H1M0.000000001S"); |
| 1925 |
insta::assert_snapshot!(p(3_661, 0), @"PT1H1M1S"); |
| 1926 |
insta::assert_snapshot!(p(3_661, 1), @"PT1H1M1.000000001S"); |
| 1927 |
|
| 1928 |
insta::assert_snapshot!( |
| 1929 |
p(u64::MAX, 999_999_999), |
| 1930 |
@"PT5124095576030431H15.999999999S", |
| 1931 |
); |
| 1932 |
} |
| 1933 |
} |