]>
git.proxmox.com Git - rustc.git/blob - vendor/time-0.1.43/src/display.rs
1 use std
::fmt
::{self, Write}
;
3 use super::{TmFmt, Tm, Fmt}
;
5 impl<'a
> fmt
::Display
for TmFmt
<'a
> {
6 fn fmt(&self, fmt
: &mut fmt
::Formatter
) -> fmt
::Result
{
9 let mut chars
= s
.chars();
10 while let Some(ch
) = chars
.next() {
12 // we've already validated that % always precedes
14 parse_type(fmt
, chars
.next().unwrap(), self.tm
)?
;
22 Fmt
::Ctime
=> self.tm
.to_local().asctime().fmt(fmt
),
24 if self.tm
.tm_utcoff
== 0 {
27 format
: Fmt
::Str("%Y-%m-%dT%H:%M:%SZ"),
32 format
: Fmt
::Str("%Y-%m-%dT%H:%M:%S"),
34 let sign
= if self.tm
.tm_utcoff
> 0 { '+' }
else { '-' }
;
35 let mut m
= abs(self.tm
.tm_utcoff
) / 60;
38 write
!(fmt
, "{}{}{:02}:{:02}", s
, sign
, h
, m
)
45 fn is_leap_year(year
: i32) -> bool
{
46 (year
% 4 == 0) && ((year
% 100 != 0) || (year
% 400 == 0))
49 fn days_in_year(year
: i32) -> i32 {
50 if is_leap_year(year
) { 366 }
54 fn iso_week_days(yday
: i32, wday
: i32) -> i32 {
55 /* The number of days from the first day of the first ISO week of this
56 * year to the year day YDAY with week day WDAY.
57 * ISO weeks start on Monday. The first ISO week has the year's first
59 * YDAY may be as small as yday_minimum.
61 let iso_week_start_wday
: i32 = 1; /* Monday */
62 let iso_week1_wday
: i32 = 4; /* Thursday */
63 let yday_minimum
: i32 = 366;
64 /* Add enough to the first operand of % to make it nonnegative. */
65 let big_enough_multiple_of_7
: i32 = (yday_minimum
/ 7 + 2) * 7;
67 yday
- (yday
- wday
+ iso_week1_wday
+ big_enough_multiple_of_7
) % 7
68 + iso_week1_wday
- iso_week_start_wday
71 fn iso_week(fmt
: &mut fmt
::Formatter
, ch
:char, tm
: &Tm
) -> fmt
::Result
{
72 let mut year
= tm
.tm_year
+ 1900;
73 let mut days
= iso_week_days(tm
.tm_yday
, tm
.tm_wday
);
76 /* This ISO week belongs to the previous year. */
78 days
= iso_week_days(tm
.tm_yday
+ (days_in_year(year
)), tm
.tm_wday
);
80 let d
= iso_week_days(tm
.tm_yday
- (days_in_year(year
)),
83 /* This ISO week belongs to the next year. */
90 'G'
=> write
!(fmt
, "{}", year
),
91 'g'
=> write
!(fmt
, "{:02}", (year
% 100 + 100) % 100),
92 'V'
=> write
!(fmt
, "{:02}", days
/ 7 + 1),
97 fn parse_type(fmt
: &mut fmt
::Formatter
, ch
: char, tm
: &Tm
) -> fmt
::Result
{
99 'A'
=> fmt
.write_str(match tm
.tm_wday
{
109 'a'
=> fmt
.write_str(match tm
.tm_wday
{
119 'B'
=> fmt
.write_str(match tm
.tm_mon
{
134 'b'
| 'h'
=> fmt
.write_str(match tm
.tm_mon
{
149 'C'
=> write
!(fmt
, "{:02}", (tm
.tm_year
+ 1900) / 100),
151 parse_type(fmt
, 'a'
, tm
)?
;
153 parse_type(fmt
, 'b'
, tm
)?
;
155 parse_type(fmt
, 'e'
, tm
)?
;
157 parse_type(fmt
, 'T'
, tm
)?
;
159 parse_type(fmt
, 'Y'
, tm
)
162 parse_type(fmt
, 'm'
, tm
)?
;
164 parse_type(fmt
, 'd'
, tm
)?
;
166 parse_type(fmt
, 'y'
, tm
)
168 'd'
=> write
!(fmt
, "{:02}", tm
.tm_mday
),
169 'e'
=> write
!(fmt
, "{:2}", tm
.tm_mday
),
170 'f'
=> write
!(fmt
, "{:09}", tm
.tm_nsec
),
172 parse_type(fmt
, 'Y'
, tm
)?
;
174 parse_type(fmt
, 'm'
, tm
)?
;
176 parse_type(fmt
, 'd'
, tm
)
178 'G'
=> iso_week(fmt
, 'G'
, tm
),
179 'g'
=> iso_week(fmt
, 'g'
, tm
),
180 'H'
=> write
!(fmt
, "{:02}", tm
.tm_hour
),
182 let mut h
= tm
.tm_hour
;
184 if h
> 12 { h -= 12 }
185 write
!(fmt
, "{:02}", h
)
187 'j'
=> write
!(fmt
, "{:03}", tm
.tm_yday
+ 1),
188 'k'
=> write
!(fmt
, "{:2}", tm
.tm_hour
),
190 let mut h
= tm
.tm_hour
;
192 if h
> 12 { h -= 12 }
193 write
!(fmt
, "{:2}", h
)
195 'M'
=> write
!(fmt
, "{:02}", tm
.tm_min
),
196 'm'
=> write
!(fmt
, "{:02}", tm
.tm_mon
+ 1),
197 'n'
=> fmt
.write_str("\n"),
198 'P'
=> fmt
.write_str(if tm
.tm_hour
< 12 { "am" }
else { "pm" }
),
199 'p'
=> fmt
.write_str(if (tm
.tm_hour
) < 12 { "AM" }
else { "PM" }
),
201 parse_type(fmt
, 'H'
, tm
)?
;
203 parse_type(fmt
, 'M'
, tm
)
206 parse_type(fmt
, 'I'
, tm
)?
;
208 parse_type(fmt
, 'M'
, tm
)?
;
210 parse_type(fmt
, 'S'
, tm
)?
;
212 parse_type(fmt
, 'p'
, tm
)
214 'S'
=> write
!(fmt
, "{:02}", tm
.tm_sec
),
215 's'
=> write
!(fmt
, "{}", tm
.to_timespec().sec
),
217 parse_type(fmt
, 'H'
, tm
)?
;
219 parse_type(fmt
, 'M'
, tm
)?
;
221 parse_type(fmt
, 'S'
, tm
)
223 't'
=> fmt
.write_str("\t"),
224 'U'
=> write
!(fmt
, "{:02}", (tm
.tm_yday
- tm
.tm_wday
+ 7) / 7),
227 write
!(fmt
, "{}", (if i
== 0 { 7 }
else { i }
))
229 'V'
=> iso_week(fmt
, 'V'
, tm
),
231 parse_type(fmt
, 'e'
, tm
)?
;
233 parse_type(fmt
, 'b'
, tm
)?
;
235 parse_type(fmt
, 'Y'
, tm
)
238 write
!(fmt
, "{:02}", (tm
.tm_yday
- (tm
.tm_wday
- 1 + 7) % 7 + 7) / 7)
240 'w'
=> write
!(fmt
, "{}", tm
.tm_wday
),
241 'Y'
=> write
!(fmt
, "{}", tm
.tm_year
+ 1900),
242 'y'
=> write
!(fmt
, "{:02}", (tm
.tm_year
+ 1900) % 100),
243 // FIXME (#2350): support locale
244 'Z'
=> fmt
.write_str(if tm
.tm_utcoff
== 0 { "UTC"}
else { "" }
),
246 let sign
= if tm
.tm_utcoff
> 0 { '+' }
else { '-' }
;
247 let mut m
= abs(tm
.tm_utcoff
) / 60;
250 write
!(fmt
, "{}{:02}{:02}", sign
, h
, m
)
252 '
+'
=> write
!(fmt
, "{}", tm
.rfc3339()),
253 '
%'
=> fmt
.write_str("%"),
258 fn abs(i
: i32) -> i32 {
259 if i
< 0 {-i}
else {i}