Skip to content

Commit 4530f8b

Browse files
committed
Rename ctime to asctime and add *proper* ctime.
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. Signed-off-by: OGINO Masanori <[email protected]>
1 parent e6c54a1 commit 4530f8b

File tree

1 file changed

+35
-5
lines changed

1 file changed

+35
-5
lines changed

src/libtime/lib.rs

Lines changed: 35 additions & 5 deletions
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 {
@@ -1371,6 +1385,19 @@ mod tests {
13711385
assert_eq!(strptime("360", "%Y-%m-%d"), Err("Invalid year".to_string()))
13721386
}
13731387

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

@@ -1380,7 +1407,7 @@ mod tests {
13801407

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

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

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

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

1442-
assert_eq!(utc.ctime(), "Fri Feb 13 23:31:30 2009".to_string());
1470+
assert_eq!(utc.asctime(), "Fri Feb 13 23:31:30 2009".to_string());
1471+
assert_eq!(utc.ctime(), "Fri Feb 13 15:31:30 2009".to_string());
14431472
assert_eq!(utc.rfc822(), "Fri, 13 Feb 2009 23:31:30 GMT".to_string());
14441473
assert_eq!(utc.rfc822z(), "Fri, 13 Feb 2009 23:31:30 -0000".to_string());
14451474
assert_eq!(utc.rfc3339(), "2009-02-13T23:31:30Z".to_string());
@@ -1488,6 +1517,7 @@ mod tests {
14881517
test_to_timespec();
14891518
test_conversions();
14901519
test_strptime();
1520+
test_asctime();
14911521
test_ctime();
14921522
test_strftime();
14931523
test_timespec_eq_ord();

0 commit comments

Comments
 (0)