Skip to content

Commit d999588

Browse files
committed
Made the time to string functions pure as well as empty_tm.
This closes rust-lang#3919.
1 parent 7c87ff3 commit d999588

File tree

1 file changed

+14
-10
lines changed

1 file changed

+14
-10
lines changed

src/libstd/time.rs

+14-10
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ impl Tm : Eq {
107107
pure fn ne(other: &Tm) -> bool { *self != *(*other) }
108108
}
109109

110-
pub fn empty_tm() -> Tm {
110+
pub pure fn empty_tm() -> Tm {
111111
Tm_({
112112
tm_sec: 0_i32,
113113
tm_min: 0_i32,
@@ -151,13 +151,17 @@ pub fn now() -> Tm {
151151
}
152152

153153
/// Parses the time from the string according to the format string.
154-
pub fn strptime(s: &str, format: &str) -> Result<Tm, ~str> {
155-
do_strptime(s, format)
154+
pub pure fn strptime(s: &str, format: &str) -> Result<Tm, ~str> {
155+
// unsafe only because do_strptime is annoying to make pure
156+
// (it does IO with a str_reader)
157+
unsafe {do_strptime(s, format)}
156158
}
157159

158160
/// Formats the time according to the format string.
159-
pub fn strftime(format: &str, tm: Tm) -> ~str {
160-
do_strftime(format, tm)
161+
pub pure fn strftime(format: &str, tm: Tm) -> ~str {
162+
// unsafe only because do_strftime is annoying to make pure
163+
// (it does IO with a str_reader)
164+
unsafe {do_strftime(format, tm)}
161165
}
162166

163167
impl Tm {
@@ -186,18 +190,18 @@ impl Tm {
186190
* Return a string of the current time in the form
187191
* "Thu Jan 1 00:00:00 1970".
188192
*/
189-
fn ctime() -> ~str { self.strftime(~"%c") }
193+
pure fn ctime() -> ~str { self.strftime(~"%c") }
190194
191195
/// Formats the time according to the format string.
192-
fn strftime(format: &str) -> ~str { strftime(format, self) }
196+
pure fn strftime(format: &str) -> ~str { strftime(format, self) }
193197
194198
/**
195199
* Returns a time string formatted according to RFC 822.
196200
*
197201
* local: "Thu, 22 Mar 2012 07:53:18 PST"
198202
* utc: "Thu, 22 Mar 2012 14:53:18 UTC"
199203
*/
200-
fn rfc822() -> ~str {
204+
pure fn rfc822() -> ~str {
201205
if self.tm_gmtoff == 0_i32 {
202206
self.strftime(~"%a, %d %b %Y %T GMT")
203207
} else {
@@ -211,7 +215,7 @@ impl Tm {
211215
* local: "Thu, 22 Mar 2012 07:53:18 -0700"
212216
* utc: "Thu, 22 Mar 2012 14:53:18 -0000"
213217
*/
214-
fn rfc822z() -> ~str {
218+
pure fn rfc822z() -> ~str {
215219
self.strftime(~"%a, %d %b %Y %T %z")
216220
}
217221

@@ -221,7 +225,7 @@ impl Tm {
221225
* local: "2012-02-22T07:53:18-07:00"
222226
* utc: "2012-02-22T14:53:18Z"
223227
*/
224-
fn rfc3339() -> ~str {
228+
pure fn rfc3339() -> ~str {
225229
if self.tm_gmtoff == 0_i32 {
226230
self.strftime(~"%Y-%m-%dT%H:%M:%SZ")
227231
} else {

0 commit comments

Comments
 (0)