Skip to content

Commit 9766b49

Browse files
committed
auto merge of #15369 : omasanori/rust/asctime, r=alexcrichton
In C, `ctime(t)` is equivalent to `asctime(localtime(t))`, so the result should depend on the local timezone. Current `ctime` is compatible with `asctime` in C, not `ctime`. This commit renames `ctime` to `asctime` and adds `ctime` which converts the time to the local timezone before formatting it. This commit also fixes the documentation of them. Current documentation of `ctime` says it returns "a string of the current time." However, it actually returns a string of the time represented as `self`, not the time when it is called.
2 parents 1bff1ff + 4530f8b commit 9766b49

File tree

1 file changed

+35
-5
lines changed

1 file changed

+35
-5
lines changed

src/libtime/lib.rs

+35-5
Original file line numberDiff line numberDiff line change
@@ -316,10 +316,24 @@ impl Tm {
316316
}
317317

318318
/**
319-
* Return a string of the current time in the form
320-
* "Thu Jan 1 00:00:00 1970".
319+
* Returns a time string formatted according to the `asctime` format in ISO
320+
* C, in the local timezone.
321+
*
322+
* Example: "Thu Jan 1 00:00:00 1970"
323+
*/
324+
pub fn ctime(&self) -> String {
325+
self.to_local().asctime()
326+
}
327+
328+
/**
329+
* Returns a time string formatted according to the `asctime` format in ISO
330+
* C.
331+
*
332+
* Example: "Thu Jan 1 00:00:00 1970"
321333
*/
322-
pub fn ctime(&self) -> String { self.strftime("%c") }
334+
pub fn asctime(&self) -> String {
335+
self.strftime("%c")
336+
}
323337

324338
/// Formats the time according to the format string.
325339
pub fn strftime(&self, format: &str) -> String {
@@ -1372,6 +1386,19 @@ mod tests {
13721386
assert_eq!(strptime("360", "%Y-%m-%d"), Err("Invalid year".to_string()))
13731387
}
13741388

1389+
fn test_asctime() {
1390+
set_time_zone();
1391+
1392+
let time = Timespec::new(1234567890, 54321);
1393+
let utc = at_utc(time);
1394+
let local = at(time);
1395+
1396+
debug!("test_ctime: {:?} {:?}", utc.asctime(), local.asctime());
1397+
1398+
assert_eq!(utc.asctime(), "Fri Feb 13 23:31:30 2009".to_string());
1399+
assert_eq!(local.asctime(), "Fri Feb 13 15:31:30 2009".to_string());
1400+
}
1401+
13751402
fn test_ctime() {
13761403
set_time_zone();
13771404

@@ -1381,7 +1408,7 @@ mod tests {
13811408

13821409
debug!("test_ctime: {:?} {:?}", utc.ctime(), local.ctime());
13831410

1384-
assert_eq!(utc.ctime(), "Fri Feb 13 23:31:30 2009".to_string());
1411+
assert_eq!(utc.ctime(), "Fri Feb 13 15:31:30 2009".to_string());
13851412
assert_eq!(local.ctime(), "Fri Feb 13 15:31:30 2009".to_string());
13861413
}
13871414

@@ -1436,11 +1463,13 @@ mod tests {
14361463
assert_eq!(local.strftime("%z"), "-0800".to_string());
14371464
assert_eq!(local.strftime("%%"), "%".to_string());
14381465

1466+
assert_eq!(local.asctime(), "Fri Feb 13 15:31:30 2009".to_string());
14391467
assert_eq!(local.ctime(), "Fri Feb 13 15:31:30 2009".to_string());
14401468
assert_eq!(local.rfc822z(), "Fri, 13 Feb 2009 15:31:30 -0800".to_string());
14411469
assert_eq!(local.rfc3339(), "2009-02-13T15:31:30-08:00".to_string());
14421470

1443-
assert_eq!(utc.ctime(), "Fri Feb 13 23:31:30 2009".to_string());
1471+
assert_eq!(utc.asctime(), "Fri Feb 13 23:31:30 2009".to_string());
1472+
assert_eq!(utc.ctime(), "Fri Feb 13 15:31:30 2009".to_string());
14441473
assert_eq!(utc.rfc822(), "Fri, 13 Feb 2009 23:31:30 GMT".to_string());
14451474
assert_eq!(utc.rfc822z(), "Fri, 13 Feb 2009 23:31:30 -0000".to_string());
14461475
assert_eq!(utc.rfc3339(), "2009-02-13T23:31:30Z".to_string());
@@ -1489,6 +1518,7 @@ mod tests {
14891518
test_to_timespec();
14901519
test_conversions();
14911520
test_strptime();
1521+
test_asctime();
14921522
test_ctime();
14931523
test_strftime();
14941524
test_timespec_eq_ord();

0 commit comments

Comments
 (0)