Skip to content

Commit a9e1358

Browse files
jesse99brson
authored andcommitted
Moved strptime and strftime into private helper functions.
Makes the public API much easier to see and prepares the way for making them pure.
1 parent 68c852a commit a9e1358

File tree

1 file changed

+84
-76
lines changed

1 file changed

+84
-76
lines changed

src/libstd/time.rs

Lines changed: 84 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,89 @@ pub fn now() -> Tm {
152152

153153
/// Parses the time from the string according to the format string.
154154
pub fn strptime(s: &str, format: &str) -> Result<Tm, ~str> {
155+
do_strptime(s, format)
156+
}
157+
158+
pub fn strftime(format: &str, tm: Tm) -> ~str {
159+
do_strftime(format, tm)
160+
}
161+
162+
impl Tm {
163+
/// Convert time to the seconds from January 1, 1970
164+
fn to_timespec() -> Timespec {
165+
let mut sec = 0i64;
166+
if self.tm_gmtoff == 0_i32 {
167+
rustrt::rust_timegm(self, &mut sec);
168+
} else {
169+
rustrt::rust_mktime(self, &mut sec);
170+
}
171+
{ sec: sec, nsec: self.tm_nsec }
172+
}
173+
174+
/// Convert time to the local timezone
175+
fn to_local() -> Tm {
176+
at(self.to_timespec())
177+
}
178+
179+
/// Convert time to the UTC
180+
fn to_utc() -> Tm {
181+
at_utc(self.to_timespec())
182+
}
183+
184+
/**
185+
* Return a string of the current time in the form
186+
* "Thu Jan 1 00:00:00 1970".
187+
*/
188+
fn ctime() -> ~str { self.strftime(~"%c") }
189+
190+
/// Formats the time according to the format string.
191+
fn strftime(format: &str) -> ~str { strftime(format, self) }
192+
193+
/**
194+
* Returns a time string formatted according to RFC 822.
195+
*
196+
* local: "Thu, 22 Mar 2012 07:53:18 PST"
197+
* utc: "Thu, 22 Mar 2012 14:53:18 UTC"
198+
*/
199+
fn rfc822() -> ~str {
200+
if self.tm_gmtoff == 0_i32 {
201+
self.strftime(~"%a, %d %b %Y %T GMT")
202+
} else {
203+
self.strftime(~"%a, %d %b %Y %T %Z")
204+
}
205+
}
206+
207+
/**
208+
* Returns a time string formatted according to RFC 822 with Zulu time.
209+
*
210+
* local: "Thu, 22 Mar 2012 07:53:18 -0700"
211+
* utc: "Thu, 22 Mar 2012 14:53:18 -0000"
212+
*/
213+
fn rfc822z() -> ~str {
214+
self.strftime(~"%a, %d %b %Y %T %z")
215+
}
216+
217+
/**
218+
* Returns a time string formatted according to ISO 8601.
219+
*
220+
* local: "2012-02-22T07:53:18-07:00"
221+
* utc: "2012-02-22T14:53:18Z"
222+
*/
223+
fn rfc3339() -> ~str {
224+
if self.tm_gmtoff == 0_i32 {
225+
self.strftime(~"%Y-%m-%dT%H:%M:%SZ")
226+
} else {
227+
let s = self.strftime(~"%Y-%m-%dT%H:%M:%S");
228+
let sign = if self.tm_gmtoff > 0_i32 { '+' } else { '-' };
229+
let mut m = i32::abs(self.tm_gmtoff) / 60_i32;
230+
let h = m / 60_i32;
231+
m -= h * 60_i32;
232+
s + fmt!("%c%02d:%02d", sign, h as int, m as int)
233+
}
234+
}
235+
}
236+
237+
priv fn do_strptime(s: &str, format: &str) -> Result<Tm, ~str> {
155238
type TmMut = {
156239
mut tm_sec: i32,
157240
mut tm_min: i32,
@@ -592,7 +675,7 @@ pub fn strptime(s: &str, format: &str) -> Result<Tm, ~str> {
592675
}
593676
}
594677

595-
fn strftime(format: &str, tm: Tm) -> ~str {
678+
priv fn do_strftime(format: &str, tm: Tm) -> ~str {
596679
fn parse_type(ch: char, tm: &Tm) -> ~str {
597680
//FIXME (#2350): Implement missing types.
598681
let die = || fmt!("strftime: can't understand this format %c ", ch);
@@ -759,81 +842,6 @@ fn strftime(format: &str, tm: Tm) -> ~str {
759842
buf
760843
}
761844

762-
impl Tm {
763-
/// Convert time to the seconds from January 1, 1970
764-
fn to_timespec() -> Timespec {
765-
let mut sec = 0i64;
766-
if self.tm_gmtoff == 0_i32 {
767-
rustrt::rust_timegm(self, &mut sec);
768-
} else {
769-
rustrt::rust_mktime(self, &mut sec);
770-
}
771-
{ sec: sec, nsec: self.tm_nsec }
772-
}
773-
774-
/// Convert time to the local timezone
775-
fn to_local() -> Tm {
776-
at(self.to_timespec())
777-
}
778-
779-
/// Convert time to the UTC
780-
fn to_utc() -> Tm {
781-
at_utc(self.to_timespec())
782-
}
783-
784-
/**
785-
* Return a string of the current time in the form
786-
* "Thu Jan 1 00:00:00 1970".
787-
*/
788-
fn ctime() -> ~str { self.strftime(~"%c") }
789-
790-
/// Formats the time according to the format string.
791-
fn strftime(format: &str) -> ~str { strftime(format, self) }
792-
793-
/**
794-
* Returns a time string formatted according to RFC 822.
795-
*
796-
* local: "Thu, 22 Mar 2012 07:53:18 PST"
797-
* utc: "Thu, 22 Mar 2012 14:53:18 UTC"
798-
*/
799-
fn rfc822() -> ~str {
800-
if self.tm_gmtoff == 0_i32 {
801-
self.strftime(~"%a, %d %b %Y %T GMT")
802-
} else {
803-
self.strftime(~"%a, %d %b %Y %T %Z")
804-
}
805-
}
806-
807-
/**
808-
* Returns a time string formatted according to RFC 822 with Zulu time.
809-
*
810-
* local: "Thu, 22 Mar 2012 07:53:18 -0700"
811-
* utc: "Thu, 22 Mar 2012 14:53:18 -0000"
812-
*/
813-
fn rfc822z() -> ~str {
814-
self.strftime(~"%a, %d %b %Y %T %z")
815-
}
816-
817-
/**
818-
* Returns a time string formatted according to ISO 8601.
819-
*
820-
* local: "2012-02-22T07:53:18-07:00"
821-
* utc: "2012-02-22T14:53:18Z"
822-
*/
823-
fn rfc3339() -> ~str {
824-
if self.tm_gmtoff == 0_i32 {
825-
self.strftime(~"%Y-%m-%dT%H:%M:%SZ")
826-
} else {
827-
let s = self.strftime(~"%Y-%m-%dT%H:%M:%S");
828-
let sign = if self.tm_gmtoff > 0_i32 { '+' } else { '-' };
829-
let mut m = i32::abs(self.tm_gmtoff) / 60_i32;
830-
let h = m / 60_i32;
831-
m -= h * 60_i32;
832-
s + fmt!("%c%02d:%02d", sign, h as int, m as int)
833-
}
834-
}
835-
}
836-
837845
#[cfg(test)]
838846
mod tests {
839847
#[legacy_exports];

0 commit comments

Comments
 (0)