From c7f132cca2295df80e839c0404c6c4186c2faa64 Mon Sep 17 00:00:00 2001 From: Kang Seonghoon Date: Sat, 14 Feb 2015 12:33:12 +0900 Subject: [PATCH] added `format_with_items` methods to every types. --- src/date.rs | 14 ++++++++++---- src/datetime.rs | 16 +++++++++++----- src/naive/date.rs | 13 ++++++++++--- src/naive/datetime.rs | 14 ++++++++++---- src/naive/time.rs | 13 ++++++++++--- src/time.rs | 14 ++++++++++---- 6 files changed, 61 insertions(+), 23 deletions(-) diff --git a/src/date.rs b/src/date.rs index 7498f86..6a86989 100644 --- a/src/date.rs +++ b/src/date.rs @@ -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 Date { } impl Date { - /// 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> + 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)) } } diff --git a/src/datetime.rs b/src/datetime.rs index d784f44..67165ec 100644 --- a/src/datetime.rs +++ b/src/datetime.rs @@ -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 { } impl DateTime { - /// 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> + 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)) } } diff --git a/src/naive/date.rs b/src/naive/date.rs index a2efa5d..80ea536 100644 --- a/src/naive/date.rs +++ b/src/naive/date.rs @@ -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> + 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)) } } diff --git a/src/naive/datetime.rs b/src/naive/datetime.rs index 2e9a5d9..235ae29 100644 --- a/src/naive/datetime.rs +++ b/src/naive/datetime.rs @@ -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> + 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)) } } diff --git a/src/naive/time.rs b/src/naive/time.rs index f20319c..8e43a63 100644 --- a/src/naive/time.rs +++ b/src/naive/time.rs @@ -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> + 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. diff --git a/src/time.rs b/src/time.rs index 84df8e1..0f5b5ba 100644 --- a/src/time.rs +++ b/src/time.rs @@ -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 Time { } impl 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> + 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)) } }