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::time::NaiveTime;
use datetime::DateTime;
use format::{DelayedFormat, StrftimeItems};
use format::{Item, DelayedFormat, StrftimeItems};
/// ISO 8601 calendar date with timezone.
#[derive(Clone)]
@ -200,12 +200,18 @@ impl<Off:Offset> 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.
#[inline]
pub fn format<'a>(&'a self, fmt: &'a str) -> DelayedFormat<'a, StrftimeItems<'a>> {
DelayedFormat::new_with_offset(Some(self.local()), None, &self.offset,
StrftimeItems::new(fmt))
self.format_with_items(StrftimeItems::new(fmt))
}
}

View File

@ -16,7 +16,7 @@ use duration::Duration;
use naive::datetime::NaiveDateTime;
use time::Time;
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.
#[derive(Clone)]
@ -102,13 +102,19 @@ impl DateTime<FixedOffset> {
}
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.
#[inline]
pub fn format<'a>(&'a self, fmt: &'a str) -> DelayedFormat<'a, StrftimeItems<'a>> {
let local = self.local();
DelayedFormat::new_with_offset(Some(local.date()), Some(local.time()), &self.offset,
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 naive::time::NaiveTime;
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};
@ -366,11 +366,18 @@ impl NaiveDate {
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.
#[inline]
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 naive::time::NaiveTime;
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.
#[derive(PartialEq, Eq, PartialOrd, Ord, Copy, Clone)]
@ -133,12 +133,18 @@ impl NaiveDateTime {
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.
#[inline]
pub fn format<'a>(&'a self, fmt: &'a str) -> DelayedFormat<'a, StrftimeItems<'a>> {
DelayedFormat::new(Some(self.date.clone()), Some(self.time.clone()),
StrftimeItems::new(fmt))
self.format_with_items(StrftimeItems::new(fmt))
}
}

View File

@ -14,7 +14,7 @@ use Timelike;
use div::div_mod_floor;
use offset::Offset;
use duration::Duration;
use format::{parse, Parsed, ParseResult, DelayedFormat, StrftimeItems};
use format::{parse, Item, Parsed, ParseResult, DelayedFormat, StrftimeItems};
/// ISO 8601 time without timezone.
/// Allows for the nanosecond precision and optional leap second representation.
@ -126,11 +126,18 @@ impl NaiveTime {
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.
#[inline]
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.

View File

@ -14,7 +14,7 @@ use Timelike;
use offset::Offset;
use duration::Duration;
use naive::time::NaiveTime;
use format::{DelayedFormat, StrftimeItems};
use format::{Item, DelayedFormat, StrftimeItems};
/// ISO 8601 time with timezone.
#[derive(Clone)]
@ -51,12 +51,18 @@ impl<Off:Offset> 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.
#[inline]
pub fn format<'a>(&'a self, fmt: &'a str) -> DelayedFormat<'a, StrftimeItems<'a>> {
DelayedFormat::new_with_offset(None, Some(self.local()), &self.offset,
StrftimeItems::new(fmt))
self.format_with_items(StrftimeItems::new(fmt))
}
}