1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390
// This file is part of ICU4X. For terms of use, please see the file
// called LICENSE at the top level of the ICU4X source tree
// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).
//! This module contains various types used by `icu_calendar` and `icu_datetime`
use crate::error::DateTimeError;
use core::convert::TryFrom;
use core::convert::TryInto;
use core::ops::{Add, Sub};
use core::str::FromStr;
use tinystr::{TinyStr16, TinyStr8};
/// TODO(#486): Implement era codes.
#[derive(Clone, Debug, PartialEq)]
pub struct Era(pub TinyStr16);
/// Representation of a formattable year.
#[derive(Clone, Debug, PartialEq)]
pub struct Year {
/// The era containing the year.
pub era: Era,
/// The year number in the current era (usually 1-based).
pub number: i32,
/// The related ISO year. This is normally the ISO (proleptic Gregorian) year having the greatest
/// overlap with the calendar year. It is used in certain date formatting patterns.
pub related_iso: i32,
}
/// TODO(#486): Implement month codes.
#[derive(Clone, Debug, PartialEq)]
pub struct MonthCode(pub TinyStr8);
/// Representation of a formattable month.
#[derive(Clone, Debug, PartialEq)]
pub struct Month {
/// A month number in a year. In normal years, this is usually the 1-based month index. In leap
/// years, this is what the month number would have been in a non-leap year.
///
/// For example:
///
/// - January = 1
/// - December = 12
/// - Adar, Adar I, and Adar II = 6
///
/// The `code` property is used to distinguish between unique months in leap years.
pub number: u32,
/// The month code, used to distinguish months during leap years.
pub code: MonthCode,
}
/// A struct containing various details about the position of the day within a year. It is returned
// by the [`day_of_year_info()`](trait.DateInput.html#tymethod.day_of_year_info) method of the
// [`DateInput`] trait.
#[derive(Clone, Debug, PartialEq)]
pub struct DayOfYearInfo {
/// The current day of the year, 1-based.
pub day_of_year: u32,
/// The number of days in a year.
pub days_in_year: u32,
/// The previous year.
pub prev_year: Year,
/// The number of days in the previous year.
pub days_in_prev_year: u32,
/// The next year.
pub next_year: Year,
}
/// A day number in a month. Usually 1-based.
pub struct DayOfMonth(pub u32);
/// A week number in a month. Usually 1-based.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct WeekOfMonth(pub u32);
/// A week number in a year. Usually 1-based.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct WeekOfYear(pub u32);
/// This macro defines a struct for 0-based date fields: hours, minutes, and seconds. Each
/// unit is bounded by a range. The traits implemented here will return a Result on
/// whether or not the unit is in range from the given input.
macro_rules! dt_unit {
($name:ident, $value:expr, $docs:expr) => {
#[doc=$docs]
#[derive(Debug, Default, Clone, Copy, PartialEq, Hash)]
pub struct $name(u8);
impl $name {
/// Do not validate the numeric input for this component.
pub const fn new_unchecked(input: u8) -> Self {
Self(input)
}
}
impl FromStr for $name {
type Err = DateTimeError;
fn from_str(input: &str) -> Result<Self, Self::Err> {
let val: u8 = input.parse()?;
if val > $value {
Err(DateTimeError::Overflow {
field: "$name",
max: $value,
})
} else {
Ok(Self(val))
}
}
}
impl TryFrom<u8> for $name {
type Error = DateTimeError;
fn try_from(input: u8) -> Result<Self, Self::Error> {
if input > $value {
Err(DateTimeError::Overflow {
field: "$name",
max: $value,
})
} else {
Ok(Self(input))
}
}
}
impl TryFrom<usize> for $name {
type Error = DateTimeError;
fn try_from(input: usize) -> Result<Self, Self::Error> {
if input > $value {
Err(DateTimeError::Overflow {
field: "$name",
max: $value,
})
} else {
Ok(Self(input as u8))
}
}
}
impl From<$name> for u8 {
fn from(input: $name) -> Self {
input.0
}
}
impl From<$name> for usize {
fn from(input: $name) -> Self {
input.0 as Self
}
}
impl Add<u8> for $name {
type Output = Self;
fn add(self, other: u8) -> Self {
Self(self.0 + other)
}
}
impl Sub<u8> for $name {
type Output = Self;
fn sub(self, other: u8) -> Self {
Self(self.0 - other)
}
}
};
}
dt_unit!(
IsoHour,
24,
"An ISO-8601 hour component, for use with ISO calendars."
);
dt_unit!(
IsoMinute,
60,
"An ISO-8601 minute component, for use with ISO calendars."
);
dt_unit!(
IsoSecond,
61,
"An ISO-8601 second component, for use with ISO calendars."
);
#[derive(Debug, Copy, Clone)]
pub struct Time {
/// 0-based hour.
pub hour: IsoHour,
/// 0-based minute.
pub minute: IsoMinute,
/// 0-based second.
pub second: IsoSecond,
}
impl Time {
/// Do not validate the numeric input for this component.
pub const fn new(hour: IsoHour, minute: IsoMinute, second: IsoSecond) -> Self {
Self {
hour,
minute,
second,
}
}
pub fn try_new(hour: u8, minute: u8, second: u8) -> Result<Self, DateTimeError> {
Ok(Self {
hour: hour.try_into()?,
minute: minute.try_into()?,
second: second.try_into()?,
})
}
}
// TODO(#485): Improve FractionalSecond.
/// A placeholder for fractional seconds support. See [Issue #485](https://github.com/unicode-org/icu4x/issues/485)
/// for tracking the support of this feature.
#[derive(Clone, Debug, PartialEq)]
pub enum FractionalSecond {
/// The millisecond component of the fractional second.
Millisecond(u16),
/// The microsecond component of the fractional second.
Microsecond(u32),
/// The nanosecond component of the fractional second.
Nanosecond(u32),
}
/// The GMT offset in seconds for a mock time zone
#[derive(Copy, Clone, Debug, Default)]
pub struct GmtOffset(i32);
impl GmtOffset {
/// Attempt to create a [`GmtOffset`] from a seconds input. It returns an error when the seconds
/// overflows or underflows.
pub fn try_new(seconds: i32) -> Result<Self, DateTimeError> {
// Valid range is from GMT-12 to GMT+14 in seconds.
if seconds < -(12 * 60 * 60) {
Err(DateTimeError::Underflow {
field: "GmtOffset",
min: -(12 * 60 * 60),
})
} else if seconds > (14 * 60 * 60) {
Err(DateTimeError::Overflow {
field: "GmtOffset",
max: (14 * 60 * 60),
})
} else {
Ok(Self(seconds))
}
}
/// Returns the raw offset value in seconds.
pub fn raw_offset_seconds(&self) -> i32 {
self.0
}
/// Returns `true` if the [`GmtOffset`] is positive, otherwise `false`.
pub fn is_positive(&self) -> bool {
self.0 >= 0
}
/// Returns `true` if the [`GmtOffset`] is zero, otherwise `false`.
pub fn is_zero(&self) -> bool {
self.0 == 0
}
/// Returns `true` if the [`GmtOffset`] has non-zero minutes, otherwise `false`.
pub fn has_minutes(&self) -> bool {
self.0 % 3600 / 60 > 0
}
/// Returns `true` if the [`GmtOffset`] has non-zero seconds, otherwise `false`.
pub fn has_seconds(&self) -> bool {
self.0 % 3600 % 60 > 0
}
}
impl FromStr for GmtOffset {
type Err = DateTimeError;
/// Parse a [`GmtOffset`] from a string.
///
/// The offset must range from GMT-12 to GMT+14.
/// The string must be an ISO 8601 time zone designator:
/// e.g. Z
/// e.g. +05
/// e.g. +0500
/// e.g. +05:00
///
/// # Examples
///
/// ```
/// use icu::datetime::date::GmtOffset;
///
/// let offset0: GmtOffset = "Z".parse().expect("Failed to parse a GMT offset.");
/// let offset1: GmtOffset = "-09".parse().expect("Failed to parse a GMT offset.");
/// let offset2: GmtOffset = "-0930".parse().expect("Failed to parse a GMT offset.");
/// let offset3: GmtOffset = "-09:30".parse().expect("Failed to parse a GMT offset.");
/// ```
fn from_str(input: &str) -> Result<Self, Self::Err> {
let offset_sign;
match input.chars().next() {
Some('+') => offset_sign = 1,
/* ASCII */ Some('-') => offset_sign = -1,
/* U+2212 */ Some('−') => offset_sign = -1,
Some('Z') => return Ok(Self(0)),
_ => return Err(DateTimeError::InvalidTimeZoneOffset),
};
let seconds = match input.chars().count() {
/* ±hh */
3 => {
let hour: u8 = input[1..3].parse()?;
offset_sign * (hour as i32 * 60 * 60)
}
/* ±hhmm */
5 => {
let hour: u8 = input[1..3].parse()?;
let minute: u8 = input[3..5].parse()?;
offset_sign * (hour as i32 * 60 * 60 + minute as i32 * 60)
}
/* ±hh:mm */
6 => {
let hour: u8 = input[1..3].parse()?;
let minute: u8 = input[4..6].parse()?;
offset_sign * (hour as i32 * 60 * 60 + minute as i32 * 60)
}
_ => panic!("Invalid time-zone designator"),
};
Self::try_new(seconds)
}
}
/// A weekday in a 7-day week, according to ISO-8601.
///
/// The discriminant values correspond to ISO-8601 weekday numbers (Monday = 1, Sunday = 7).
///
/// # Examples
///
/// ```
/// use icu::datetime::date::IsoWeekday;
///
/// assert_eq!(1, IsoWeekday::Monday as usize);
/// assert_eq!(7, IsoWeekday::Sunday as usize);
/// ```
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[allow(missing_docs)] // The weekday variants should be self-obvious.
#[repr(i8)]
pub enum IsoWeekday {
Monday = 1,
Tuesday,
Wednesday,
Thursday,
Friday,
Saturday,
Sunday,
}
impl From<usize> for IsoWeekday {
/// Convert from an ISO-8601 weekday number to an [`IsoWeekday`] enum. 0 is automatically converted
/// to 7 (Sunday). If the number is out of range, it is interpreted modulo 7.
///
/// # Examples
///
/// ```
/// use icu::datetime::date::IsoWeekday;
///
/// assert_eq!(IsoWeekday::Sunday, IsoWeekday::from(0));
/// assert_eq!(IsoWeekday::Monday, IsoWeekday::from(1));
/// assert_eq!(IsoWeekday::Sunday, IsoWeekday::from(7));
/// assert_eq!(IsoWeekday::Monday, IsoWeekday::from(8));
/// ```
fn from(input: usize) -> Self {
let mut ordinal = (input % 7) as i8;
if ordinal == 0 {
ordinal = 7;
}
unsafe { core::mem::transmute(ordinal) }
}
}