added `format_with_items` methods to every types.

This commit is contained in:
Kang Seonghoon 2015-02-14 12:33:12 +09:00
parent 292faa0b23
commit c7f132cca2
6 changed files with 61 additions and 23 deletions

View File

@ -17,7 +17,7 @@ use naive;
use naive::date::NaiveDate; use naive::date::NaiveDate;
use naive::time::NaiveTime; use naive::time::NaiveTime;
use datetime::DateTime; use datetime::DateTime;
use format::{DelayedFormat, StrftimeItems}; use format::{Item, DelayedFormat, StrftimeItems};
/// ISO 8601 calendar date with timezone. /// ISO 8601 calendar date with timezone.
#[derive(Clone)] #[derive(Clone)]
@ -200,12 +200,18 @@ impl<Off:Offset> Date<Off> {
} }
impl<Off: Offset + fmt::Display> Date<Off> { impl<Off: Offset + fmt::Display> Date<Off> {
/// Formats the date in the specified format string. /// Formats the date with the specified formatting items.
#[inline]
pub fn format_with_items<'a, I>(&'a self, items: I) -> DelayedFormat<'a, I>
where I: Iterator<Item=Item<'a>> + Clone {
DelayedFormat::new_with_offset(Some(self.local()), None, &self.offset, items)
}
/// Formats the date with the specified format string.
/// See the `format::strftime` module on the supported escape sequences. /// See the `format::strftime` module on the supported escape sequences.
#[inline] #[inline]
pub fn format<'a>(&'a self, fmt: &'a str) -> DelayedFormat<'a, StrftimeItems<'a>> { pub fn format<'a>(&'a self, fmt: &'a str) -> DelayedFormat<'a, StrftimeItems<'a>> {
DelayedFormat::new_with_offset(Some(self.local()), None, &self.offset, self.format_with_items(StrftimeItems::new(fmt))
StrftimeItems::new(fmt))
} }
} }

View File

@ -16,7 +16,7 @@ use duration::Duration;
use naive::datetime::NaiveDateTime; use naive::datetime::NaiveDateTime;
use time::Time; use time::Time;
use date::Date; use date::Date;
use format::{parse, Parsed, ParseResult, DelayedFormat, StrftimeItems}; use format::{parse, Item, Parsed, ParseResult, DelayedFormat, StrftimeItems};
/// ISO 8601 combined date and time with timezone. /// ISO 8601 combined date and time with timezone.
#[derive(Clone)] #[derive(Clone)]
@ -102,13 +102,19 @@ impl DateTime<FixedOffset> {
} }
impl<Off: Offset + fmt::Display> DateTime<Off> { impl<Off: Offset + fmt::Display> DateTime<Off> {
/// Formats the combined date and time in the specified format string. /// Formats the combined date and time with the specified formatting items.
#[inline]
pub fn format_with_items<'a, I>(&'a self, items: I) -> DelayedFormat<'a, I>
where I: Iterator<Item=Item<'a>> + Clone {
let local = self.local();
DelayedFormat::new_with_offset(Some(local.date()), Some(local.time()), &self.offset, items)
}
/// Formats the combined date and time with the specified format string.
/// See the `format::strftime` module on the supported escape sequences. /// See the `format::strftime` module on the supported escape sequences.
#[inline] #[inline]
pub fn format<'a>(&'a self, fmt: &'a str) -> DelayedFormat<'a, StrftimeItems<'a>> { pub fn format<'a>(&'a self, fmt: &'a str) -> DelayedFormat<'a, StrftimeItems<'a>> {
let local = self.local(); self.format_with_items(StrftimeItems::new(fmt))
DelayedFormat::new_with_offset(Some(local.date()), Some(local.time()), &self.offset,
StrftimeItems::new(fmt))
} }
} }

View File

@ -15,7 +15,7 @@ use div::div_mod_floor;
use duration::Duration; use duration::Duration;
use naive::time::NaiveTime; use naive::time::NaiveTime;
use naive::datetime::NaiveDateTime; use naive::datetime::NaiveDateTime;
use format::{parse, Parsed, ParseResult, DelayedFormat, StrftimeItems}; use format::{parse, Item, Parsed, ParseResult, DelayedFormat, StrftimeItems};
use self::internals::{DateImpl, Of, Mdf, YearFlags}; use self::internals::{DateImpl, Of, Mdf, YearFlags};
@ -366,11 +366,18 @@ impl NaiveDate {
Of::new(ordinal, flags)) Of::new(ordinal, flags))
} }
/// Formats the date in the specified format string. /// Formats the date with the specified formatting items.
#[inline]
pub fn format_with_items<'a, I>(&'a self, items: I) -> DelayedFormat<'a, I>
where I: Iterator<Item=Item<'a>> + Clone {
DelayedFormat::new(Some(self.clone()), None, items)
}
/// Formats the date with the specified format string.
/// See the `format::strftime` module on the supported escape sequences. /// See the `format::strftime` module on the supported escape sequences.
#[inline] #[inline]
pub fn format<'a>(&'a self, fmt: &'a str) -> DelayedFormat<'a, StrftimeItems<'a>> { pub fn format<'a>(&'a self, fmt: &'a str) -> DelayedFormat<'a, StrftimeItems<'a>> {
DelayedFormat::new(Some(self.clone()), None, StrftimeItems::new(fmt)) self.format_with_items(StrftimeItems::new(fmt))
} }
} }

View File

@ -15,7 +15,7 @@ use div::div_mod_floor;
use duration::Duration; use duration::Duration;
use naive::time::NaiveTime; use naive::time::NaiveTime;
use naive::date::NaiveDate; use naive::date::NaiveDate;
use format::{parse, Parsed, ParseResult, DelayedFormat, StrftimeItems}; use format::{parse, Item, Parsed, ParseResult, DelayedFormat, StrftimeItems};
/// ISO 8601 combined date and time without timezone. /// ISO 8601 combined date and time without timezone.
#[derive(PartialEq, Eq, PartialOrd, Ord, Copy, Clone)] #[derive(PartialEq, Eq, PartialOrd, Ord, Copy, Clone)]
@ -133,12 +133,18 @@ impl NaiveDateTime {
Some(NaiveDateTime { date: date, time: time }) Some(NaiveDateTime { date: date, time: time })
} }
/// Formats the combined date and time in the specified format string. /// Formats the combined date and time with the specified formatting items.
#[inline]
pub fn format_with_items<'a, I>(&'a self, items: I) -> DelayedFormat<'a, I>
where I: Iterator<Item=Item<'a>> + Clone {
DelayedFormat::new(Some(self.date.clone()), Some(self.time.clone()), items)
}
/// Formats the combined date and time with the specified format string.
/// See the `format::strftime` module on the supported escape sequences. /// See the `format::strftime` module on the supported escape sequences.
#[inline] #[inline]
pub fn format<'a>(&'a self, fmt: &'a str) -> DelayedFormat<'a, StrftimeItems<'a>> { pub fn format<'a>(&'a self, fmt: &'a str) -> DelayedFormat<'a, StrftimeItems<'a>> {
DelayedFormat::new(Some(self.date.clone()), Some(self.time.clone()), self.format_with_items(StrftimeItems::new(fmt))
StrftimeItems::new(fmt))
} }
} }

View File

@ -14,7 +14,7 @@ use Timelike;
use div::div_mod_floor; use div::div_mod_floor;
use offset::Offset; use offset::Offset;
use duration::Duration; use duration::Duration;
use format::{parse, Parsed, ParseResult, DelayedFormat, StrftimeItems}; use format::{parse, Item, Parsed, ParseResult, DelayedFormat, StrftimeItems};
/// ISO 8601 time without timezone. /// ISO 8601 time without timezone.
/// Allows for the nanosecond precision and optional leap second representation. /// Allows for the nanosecond precision and optional leap second representation.
@ -126,11 +126,18 @@ impl NaiveTime {
parsed.to_naive_time() parsed.to_naive_time()
} }
/// Formats the time in the specified format string. /// Formats the time with the specified formatting items.
#[inline]
pub fn format_with_items<'a, I>(&'a self, items: I) -> DelayedFormat<'a, I>
where I: Iterator<Item=Item<'a>> + Clone {
DelayedFormat::new(None, Some(self.clone()), items)
}
/// Formats the time with the specified format string.
/// See the `format::strftime` module on the supported escape sequences. /// See the `format::strftime` module on the supported escape sequences.
#[inline] #[inline]
pub fn format<'a>(&'a self, fmt: &'a str) -> DelayedFormat<'a, StrftimeItems<'a>> { pub fn format<'a>(&'a self, fmt: &'a str) -> DelayedFormat<'a, StrftimeItems<'a>> {
DelayedFormat::new(None, Some(self.clone()), StrftimeItems::new(fmt)) self.format_with_items(StrftimeItems::new(fmt))
} }
/// Returns a triple of the hour, minute and second numbers. /// Returns a triple of the hour, minute and second numbers.

View File

@ -14,7 +14,7 @@ use Timelike;
use offset::Offset; use offset::Offset;
use duration::Duration; use duration::Duration;
use naive::time::NaiveTime; use naive::time::NaiveTime;
use format::{DelayedFormat, StrftimeItems}; use format::{Item, DelayedFormat, StrftimeItems};
/// ISO 8601 time with timezone. /// ISO 8601 time with timezone.
#[derive(Clone)] #[derive(Clone)]
@ -51,12 +51,18 @@ impl<Off:Offset> Time<Off> {
} }
impl<Off: Offset + fmt::Display> Time<Off> { impl<Off: Offset + fmt::Display> Time<Off> {
/// Formats the time in the specified format string. /// Formats the time with the specified formatting items.
#[inline]
pub fn format_with_items<'a, I>(&'a self, items: I) -> DelayedFormat<'a, I>
where I: Iterator<Item=Item<'a>> + Clone {
DelayedFormat::new_with_offset(None, Some(self.local()), &self.offset, items)
}
/// Formats the time with the specified format string.
/// See the `format::strftime` module on the supported escape sequences. /// See the `format::strftime` module on the supported escape sequences.
#[inline] #[inline]
pub fn format<'a>(&'a self, fmt: &'a str) -> DelayedFormat<'a, StrftimeItems<'a>> { pub fn format<'a>(&'a self, fmt: &'a str) -> DelayedFormat<'a, StrftimeItems<'a>> {
DelayedFormat::new_with_offset(None, Some(self.local()), &self.offset, self.format_with_items(StrftimeItems::new(fmt))
StrftimeItems::new(fmt))
} }
} }