forked from chronotope/chrono
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix "can't leak crate-private type in stub.rs" chronotope#662 (chrono…
…tope#684) First attempt to fix this issue for wasm32-unknown-* targets by changing visibility of functions not to leak crate-private types. Ensure the default toolchain is used for the wasm_unknown CI test.
- Loading branch information
1 parent
d63fd7b
commit d0a2916
Showing
3 changed files
with
110 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
// Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT | ||
// file at the top-level directory of this distribution and at | ||
// http://rust-lang.org/COPYRIGHT. | ||
// | ||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | ||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | ||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | ||
// option. This file may not be copied, modified, or distributed | ||
// except according to those terms. | ||
|
||
use super::Tm; | ||
|
||
fn time_to_tm(ts: i64, tm: &mut Tm) { | ||
let leapyear = |year| -> bool { year % 4 == 0 && (year % 100 != 0 || year % 400 == 0) }; | ||
|
||
static YTAB: [[i64; 12]; 2] = [ | ||
[31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31], | ||
[31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31], | ||
]; | ||
|
||
let mut year = 1970; | ||
|
||
let dayclock = ts % 86400; | ||
let mut dayno = ts / 86400; | ||
|
||
tm.tm_sec = (dayclock % 60) as i32; | ||
tm.tm_min = ((dayclock % 3600) / 60) as i32; | ||
tm.tm_hour = (dayclock / 3600) as i32; | ||
tm.tm_wday = ((dayno + 4) % 7) as i32; | ||
loop { | ||
let yearsize = if leapyear(year) { 366 } else { 365 }; | ||
if dayno >= yearsize { | ||
dayno -= yearsize; | ||
year += 1; | ||
} else { | ||
break; | ||
} | ||
} | ||
tm.tm_year = (year - 1900) as i32; | ||
tm.tm_yday = dayno as i32; | ||
let mut mon = 0; | ||
while dayno >= YTAB[if leapyear(year) { 1 } else { 0 }][mon] { | ||
dayno -= YTAB[if leapyear(year) { 1 } else { 0 }][mon]; | ||
mon += 1; | ||
} | ||
tm.tm_mon = mon as i32; | ||
tm.tm_mday = dayno as i32 + 1; | ||
tm.tm_isdst = 0; | ||
} | ||
|
||
fn tm_to_time(tm: &Tm) -> i64 { | ||
let mut y = tm.tm_year as i64 + 1900; | ||
let mut m = tm.tm_mon as i64 + 1; | ||
if m <= 2 { | ||
y -= 1; | ||
m += 12; | ||
} | ||
let d = tm.tm_mday as i64; | ||
let h = tm.tm_hour as i64; | ||
let mi = tm.tm_min as i64; | ||
let s = tm.tm_sec as i64; | ||
(365 * y + y / 4 - y / 100 + y / 400 + 3 * (m + 1) / 5 + 30 * m + d - 719561) * 86400 | ||
+ 3600 * h | ||
+ 60 * mi | ||
+ s | ||
} | ||
|
||
pub(super) fn time_to_local_tm(sec: i64, tm: &mut Tm) { | ||
// FIXME: Add timezone logic | ||
time_to_tm(sec, tm); | ||
} | ||
|
||
pub(super) fn utc_tm_to_time(tm: &Tm) -> i64 { | ||
tm_to_time(tm) | ||
} | ||
|
||
pub(super) fn local_tm_to_time(tm: &Tm) -> i64 { | ||
// FIXME: Add timezone logic | ||
tm_to_time(tm) | ||
} |