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
/*
 * This project is licensed under the MIT license.
 * See the LICENSE file in the project root for more information.
 */
/// IsoDate is an extension to Chrono's IsoWeek (ISO 8601) that provides additional
/// formatting and functions, such as the "W" prefix to weeks and the ISO ordinal.
use chrono::{DateTime, Datelike, FixedOffset, IsoWeek, Local, NaiveDate, Utc};


pub trait IsoDate {
    fn week_fancy(self) -> String;
    fn week0_fancy(self) -> String;
    fn date(self) -> String;
    fn date0(self) -> String;
}

pub trait IsoCal {
    fn iso_ordinal(self) -> u32;
    fn iso_ordinal0(self) -> u32;
}

impl IsoCal for NaiveDate {
    /// Returns the day of the day year.
    ///
    /// The return value ranges from 1 to 371. (The last day of year differs by years.)
    fn iso_ordinal(self) -> u32 {
        if self.year() % 4 == 0 {
            self.ordinal() + 5
        } else {
            self.ordinal() - 1
        }
    }

    /// Returns the day of the day year.
    ///
    /// The return value ranges from 0 to 370. (The last day of year differs by years.)
    fn iso_ordinal0(self) -> u32 {
        if self.year() % 4 == 0 {
            self.ordinal0() + 6
        } else {
            self.ordinal0()
        }
    }
}

impl IsoCal for DateTime<Utc> {
    /// Returns the day of the day year.
    ///
    /// The return value ranges from 1 to 371. (The last day of year differs by years.)
    fn iso_ordinal(self) -> u32 {
        if self.year() % 4 == 0 {
            self.ordinal() + 5
        } else {
            self.ordinal() - 1
        }
    }

    /// Returns the day of the day year.
    ///
    /// The return value ranges from 0 to 370. (The last day of year differs by years.)
    fn iso_ordinal0(self) -> u32 {
        if self.year() % 4 == 0 {
            self.ordinal0() + 5
        } else {
            self.ordinal0()
        }
    }
}

impl IsoCal for DateTime<Local> {
    /// Returns the day of the day year.
    ///
    /// The return value ranges from 1 to 371. (The last day of year differs by years.)
    fn iso_ordinal(self) -> u32 {
        if self.year() % 4 == 0 {
            self.ordinal() + 5
        } else {
            self.ordinal() - 1
        }
    }

    /// Returns the day of the day year.
    ///
    /// The return value ranges from 0 to 370. (The last day of year differs by years.)
    fn iso_ordinal0(self) -> u32 {
        if self.year() % 4 == 0 {
            self.ordinal0() + 5
        } else {
            self.ordinal0()
        }
    }
}

impl IsoCal for DateTime<FixedOffset> {
    /// Returns the day of the day year.
    ///
    /// The return value ranges from 1 to 371. (The last day of year differs by years.)
    fn iso_ordinal(self) -> u32 {
        if self.year() % 4 == 0 {
            self.ordinal() + 5
        } else {
            self.ordinal() - 1
        }
    }

    /// Returns the day of the day year.
    ///
    /// The return value ranges from 0 to 370. (The last day of year differs by years.)
    fn iso_ordinal0(self) -> u32 {
        if self.year() % 4 == 0 {
            self.ordinal0() + 5
        } else {
            self.ordinal0()
        }
    }
}

impl IsoDate for IsoWeek {
    /// Returns the ISO week with a W prefix.
    /// The return value ranges from 0 to 53. (The last week of year differs by years.)
    /// ```
    /// extern crate isocal; // Rust 2015
    /// use isocal::IsoDate;
    /// use chrono::{Local, IsoWeek, Datelike};
    ///
    /// fn main() {
    ///     let now = Local::now();
    ///     let isow = now.iso_week();
    ///
    ///     println!("{}", isow.week_fancy());
    /// }
    /// ```
    fn week_fancy(self) -> String {
        format!("W{:02}", self.week())
    }

    /// Returns the ISO week with a W prefix.
    /// The return value ranges from 0 to 52. (The last week of year differs by years.)
    fn week0_fancy(self) -> String {
        format!("W{:02}", self.week0())
    }

    /// Returns the ISO year and week.
    /// The week ranges from 0 to 53. (The last week of year differs by years.)
    /// ```
    /// extern crate isocal; // Rust 2015
    /// use isocal::IsoDate;
    /// use chrono::{Local, IsoWeek, Datelike};
    ///
    /// fn main() {
    ///     let now = Local::now();
    ///     let isow = now.iso_week();
    ///
    ///     println!("{}", isow.date());
    /// }
    /// ```
    fn date(self) -> String {
        format!("{}-{}", self.year(), self.week_fancy())
    }

    /// Returns the ISO year and week.
    /// The week ranges from 0 to 52. (The last week of year differs by years.)
    fn date0(self) -> String {
        format!("{}-{}", self.year(), self.week0_fancy())
    }
}