1 // This is a part of Chrono.
2 // See README.md and LICENSE.txt for details.
3
4 //! ISO 8601 week.
5
6 use core::fmt;
7
8 use super::internals::{DateImpl, Of, YearFlags};
9
10 /// ISO 8601 week.
11 ///
12 /// This type, combined with [`Weekday`](../enum.Weekday.html),
13 /// constitues the ISO 8601 [week date](./struct.NaiveDate.html#week-date).
14 /// One can retrieve this type from the existing [`Datelike`](../trait.Datelike.html) types
15 /// via the [`Datelike::iso_week`](../trait.Datelike.html#tymethod.iso_week) method.
16 #[derive(PartialEq, Eq, PartialOrd, Ord, Copy, Clone)]
17 pub struct IsoWeek {
18 // note that this allows for larger year range than `NaiveDate`.
19 // this is crucial because we have an edge case for the first and last week supported,
20 // which year number might not match the calendar year number.
21 ywf: DateImpl, // (year << 10) | (week << 4) | flag
22 }
23
24 /// Returns the corresponding `IsoWeek` from the year and the `Of` internal value.
25 //
26 // internal use only. we don't expose the public constructor for `IsoWeek` for now,
27 // because the year range for the week date and the calendar date do not match and
28 // it is confusing to have a date that is out of range in one and not in another.
29 // currently we sidestep this issue by making `IsoWeek` fully dependent of `Datelike`.
iso_week_from_yof(year: i32, of: Of) -> IsoWeek30 pub fn iso_week_from_yof(year: i32, of: Of) -> IsoWeek {
31 let (rawweek, _) = of.isoweekdate_raw();
32 let (year, week) = if rawweek < 1 {
33 // previous year
34 let prevlastweek = YearFlags::from_year(year - 1).nisoweeks();
35 (year - 1, prevlastweek)
36 } else {
37 let lastweek = of.flags().nisoweeks();
38 if rawweek > lastweek {
39 // next year
40 (year + 1, 1)
41 } else {
42 (year, rawweek)
43 }
44 };
45 IsoWeek { ywf: (year << 10) | (week << 4) as DateImpl | DateImpl::from(of.flags().0) }
46 }
47
48 impl IsoWeek {
49 /// Returns the year number for this ISO week.
50 ///
51 /// # Example
52 ///
53 /// ~~~~
54 /// use chrono::{NaiveDate, Datelike, Weekday};
55 ///
56 /// let d = NaiveDate::from_isoywd(2015, 1, Weekday::Mon);
57 /// assert_eq!(d.iso_week().year(), 2015);
58 /// ~~~~
59 ///
60 /// This year number might not match the calendar year number.
61 /// Continuing the example...
62 ///
63 /// ~~~~
64 /// # use chrono::{NaiveDate, Datelike, Weekday};
65 /// # let d = NaiveDate::from_isoywd(2015, 1, Weekday::Mon);
66 /// assert_eq!(d.year(), 2014);
67 /// assert_eq!(d, NaiveDate::from_ymd(2014, 12, 29));
68 /// ~~~~
69 #[inline]
year(&self) -> i3270 pub fn year(&self) -> i32 {
71 self.ywf >> 10
72 }
73
74 /// Returns the ISO week number starting from 1.
75 ///
76 /// The return value ranges from 1 to 53. (The last week of year differs by years.)
77 ///
78 /// # Example
79 ///
80 /// ~~~~
81 /// use chrono::{NaiveDate, Datelike, Weekday};
82 ///
83 /// let d = NaiveDate::from_isoywd(2015, 15, Weekday::Mon);
84 /// assert_eq!(d.iso_week().week(), 15);
85 /// ~~~~
86 #[inline]
week(&self) -> u3287 pub fn week(&self) -> u32 {
88 ((self.ywf >> 4) & 0x3f) as u32
89 }
90
91 /// Returns the ISO week number starting from 0.
92 ///
93 /// The return value ranges from 0 to 52. (The last week of year differs by years.)
94 ///
95 /// # Example
96 ///
97 /// ~~~~
98 /// use chrono::{NaiveDate, Datelike, Weekday};
99 ///
100 /// let d = NaiveDate::from_isoywd(2015, 15, Weekday::Mon);
101 /// assert_eq!(d.iso_week().week0(), 14);
102 /// ~~~~
103 #[inline]
week0(&self) -> u32104 pub fn week0(&self) -> u32 {
105 ((self.ywf >> 4) & 0x3f) as u32 - 1
106 }
107 }
108
109 /// The `Debug` output of the ISO week `w` is the same as
110 /// [`d.format("%G-W%V")`](../format/strftime/index.html)
111 /// where `d` is any `NaiveDate` value in that week.
112 ///
113 /// # Example
114 ///
115 /// ~~~~
116 /// use chrono::{NaiveDate, Datelike};
117 ///
118 /// assert_eq!(format!("{:?}", NaiveDate::from_ymd(2015, 9, 5).iso_week()), "2015-W36");
119 /// assert_eq!(format!("{:?}", NaiveDate::from_ymd( 0, 1, 3).iso_week()), "0000-W01");
120 /// assert_eq!(format!("{:?}", NaiveDate::from_ymd(9999, 12, 31).iso_week()), "9999-W52");
121 /// ~~~~
122 ///
123 /// ISO 8601 requires an explicit sign for years before 1 BCE or after 9999 CE.
124 ///
125 /// ~~~~
126 /// # use chrono::{NaiveDate, Datelike};
127 /// assert_eq!(format!("{:?}", NaiveDate::from_ymd( 0, 1, 2).iso_week()), "-0001-W52");
128 /// assert_eq!(format!("{:?}", NaiveDate::from_ymd(10000, 12, 31).iso_week()), "+10000-W52");
129 /// ~~~~
130 impl fmt::Debug for IsoWeek {
fmt(&self, f: &mut fmt::Formatter) -> fmt::Result131 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
132 let year = self.year();
133 let week = self.week();
134 if 0 <= year && year <= 9999 {
135 write!(f, "{:04}-W{:02}", year, week)
136 } else {
137 // ISO 8601 requires the explicit sign for out-of-range years
138 write!(f, "{:+05}-W{:02}", year, week)
139 }
140 }
141 }
142
143 #[cfg(test)]
144 mod tests {
145 use naive::{internals, MAX_DATE, MIN_DATE};
146 use Datelike;
147
148 #[test]
test_iso_week_extremes()149 fn test_iso_week_extremes() {
150 let minweek = MIN_DATE.iso_week();
151 let maxweek = MAX_DATE.iso_week();
152
153 assert_eq!(minweek.year(), internals::MIN_YEAR);
154 assert_eq!(minweek.week(), 1);
155 assert_eq!(minweek.week0(), 0);
156 assert_eq!(format!("{:?}", minweek), MIN_DATE.format("%G-W%V").to_string());
157
158 assert_eq!(maxweek.year(), internals::MAX_YEAR + 1);
159 assert_eq!(maxweek.week(), 1);
160 assert_eq!(maxweek.week0(), 0);
161 assert_eq!(format!("{:?}", maxweek), MAX_DATE.format("%G-W%V").to_string());
162 }
163 }
164