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 { // previous year
33         let prevlastweek = YearFlags::from_year(year - 1).nisoweeks();
34         (year - 1, prevlastweek)
35     } else {
36         let lastweek = of.flags().nisoweeks();
37         if rawweek > lastweek { // next year
38             (year + 1, 1)
39         } else {
40             (year, rawweek)
41         }
42     };
43     IsoWeek { ywf: (year << 10) | (week << 4) as DateImpl | DateImpl::from(of.flags().0) }
44 }
45 
46 impl IsoWeek {
47     /// Returns the year number for this ISO week.
48     ///
49     /// # Example
50     ///
51     /// ~~~~
52     /// use chrono::{NaiveDate, Datelike, Weekday};
53     ///
54     /// let d = NaiveDate::from_isoywd(2015, 1, Weekday::Mon);
55     /// assert_eq!(d.iso_week().year(), 2015);
56     /// ~~~~
57     ///
58     /// This year number might not match the calendar year number.
59     /// Continuing the example...
60     ///
61     /// ~~~~
62     /// # use chrono::{NaiveDate, Datelike, Weekday};
63     /// # let d = NaiveDate::from_isoywd(2015, 1, Weekday::Mon);
64     /// assert_eq!(d.year(), 2014);
65     /// assert_eq!(d, NaiveDate::from_ymd(2014, 12, 29));
66     /// ~~~~
67     #[inline]
year(&self) -> i3268     pub fn year(&self) -> i32 {
69         self.ywf >> 10
70     }
71 
72     /// Returns the ISO week number starting from 1.
73     ///
74     /// The return value ranges from 1 to 53. (The last week of year differs by years.)
75     ///
76     /// # Example
77     ///
78     /// ~~~~
79     /// use chrono::{NaiveDate, Datelike, Weekday};
80     ///
81     /// let d = NaiveDate::from_isoywd(2015, 15, Weekday::Mon);
82     /// assert_eq!(d.iso_week().week(), 15);
83     /// ~~~~
84     #[inline]
week(&self) -> u3285     pub fn week(&self) -> u32 {
86         ((self.ywf >> 4) & 0x3f) as u32
87     }
88 
89     /// Returns the ISO week number starting from 0.
90     ///
91     /// The return value ranges from 0 to 52. (The last week of year differs by years.)
92     ///
93     /// # Example
94     ///
95     /// ~~~~
96     /// use chrono::{NaiveDate, Datelike, Weekday};
97     ///
98     /// let d = NaiveDate::from_isoywd(2015, 15, Weekday::Mon);
99     /// assert_eq!(d.iso_week().week0(), 14);
100     /// ~~~~
101     #[inline]
week0(&self) -> u32102     pub fn week0(&self) -> u32 {
103         ((self.ywf >> 4) & 0x3f) as u32 - 1
104     }
105 }
106 
107 /// The `Debug` output of the ISO week `w` is the same as
108 /// [`d.format("%G-W%V")`](../format/strftime/index.html)
109 /// where `d` is any `NaiveDate` value in that week.
110 ///
111 /// # Example
112 ///
113 /// ~~~~
114 /// use chrono::{NaiveDate, Datelike};
115 ///
116 /// assert_eq!(format!("{:?}", NaiveDate::from_ymd(2015,  9,  5).iso_week()), "2015-W36");
117 /// assert_eq!(format!("{:?}", NaiveDate::from_ymd(   0,  1,  3).iso_week()), "0000-W01");
118 /// assert_eq!(format!("{:?}", NaiveDate::from_ymd(9999, 12, 31).iso_week()), "9999-W52");
119 /// ~~~~
120 ///
121 /// ISO 8601 requires an explicit sign for years before 1 BCE or after 9999 CE.
122 ///
123 /// ~~~~
124 /// # use chrono::{NaiveDate, Datelike};
125 /// assert_eq!(format!("{:?}", NaiveDate::from_ymd(    0,  1,  2).iso_week()),  "-0001-W52");
126 /// assert_eq!(format!("{:?}", NaiveDate::from_ymd(10000, 12, 31).iso_week()), "+10000-W52");
127 /// ~~~~
128 impl fmt::Debug for IsoWeek {
fmt(&self, f: &mut fmt::Formatter) -> fmt::Result129     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
130         let year = self.year();
131         let week = self.week();
132         if 0 <= year && year <= 9999 {
133             write!(f, "{:04}-W{:02}", year, week)
134         } else {
135             // ISO 8601 requires the explicit sign for out-of-range years
136             write!(f, "{:+05}-W{:02}", year, week)
137         }
138     }
139 }
140 
141 #[cfg(test)]
142 mod tests {
143     use naive::{internals, MIN_DATE, MAX_DATE};
144     use Datelike;
145 
146     #[test]
test_iso_week_extremes()147     fn test_iso_week_extremes() {
148         let minweek = MIN_DATE.iso_week();
149         let maxweek = MAX_DATE.iso_week();
150 
151         assert_eq!(minweek.year(), internals::MIN_YEAR);
152         assert_eq!(minweek.week(), 1);
153         assert_eq!(minweek.week0(), 0);
154         assert_eq!(format!("{:?}", minweek), MIN_DATE.format("%G-W%V").to_string());
155 
156         assert_eq!(maxweek.year(), internals::MAX_YEAR + 1);
157         assert_eq!(maxweek.week(), 1);
158         assert_eq!(maxweek.week0(), 0);
159         assert_eq!(format!("{:?}", maxweek), MAX_DATE.format("%G-W%V").to_string());
160     }
161 }
162