Skip to content

Commit 9ed3d8b

Browse files
committed
Move conversion functions in libcc2rs
1 parent fd0a55e commit 9ed3d8b

3 files changed

Lines changed: 34 additions & 25 deletions

File tree

‎libcc2rs/Cargo.toml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ edition = "2024"
77
libcc2rs-macros = { path = "../libcc2rs-macros", version = "0.1.0" }
88
libc = "0.2"
99
nix = { version = "0.30", features = ["socket", "net", "fs", "poll", "time", "user", "dir", "term", "event", "hostname"] }
10+
jiff = "0.2"

‎libcc2rs/src/libc_shims/time.rs‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,36 @@ pub struct Tm {
2020
pub tm_zone: Value<Ptr<u8>>,
2121
}
2222

23+
impl Tm {
24+
pub fn from_zoned(dt: &jiff::Zoned) -> Self {
25+
let tm = Tm::default();
26+
*tm.tm_sec.borrow_mut() = dt.second() as i32;
27+
*tm.tm_min.borrow_mut() = dt.minute() as i32;
28+
*tm.tm_hour.borrow_mut() = dt.hour() as i32;
29+
*tm.tm_mday.borrow_mut() = dt.day() as i32;
30+
*tm.tm_mon.borrow_mut() = dt.month() as i32 - 1;
31+
*tm.tm_year.borrow_mut() = dt.year() as i32 - 1900;
32+
*tm.tm_wday.borrow_mut() = dt.weekday().to_sunday_zero_offset() as i32;
33+
*tm.tm_yday.borrow_mut() = dt.day_of_year() as i32 - 1;
34+
*tm.tm_isdst.borrow_mut() = 0;
35+
*tm.tm_gmtoff.borrow_mut() = dt.offset().seconds() as i64;
36+
*tm.tm_zone.borrow_mut() = Ptr::from_string_literal(b"GMT");
37+
tm
38+
}
39+
40+
pub fn to_civil(&self) -> Result<jiff::civil::DateTime, jiff::Error> {
41+
jiff::civil::DateTime::new(
42+
(*self.tm_year.borrow() + 1900) as i16,
43+
(*self.tm_mon.borrow() + 1) as i8,
44+
*self.tm_mday.borrow() as i8,
45+
*self.tm_hour.borrow() as i8,
46+
*self.tm_min.borrow() as i8,
47+
*self.tm_sec.borrow() as i8,
48+
0,
49+
)
50+
}
51+
}
52+
2353
impl Clone for Tm {
2454
fn clone(&self) -> Self {
2555
Self {

‎rules/time/tgt_refcount.rs‎

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -49,23 +49,11 @@ fn f2(a0: ::libc::clockid_t, a1: Ptr<Timespec>) -> i32 {
4949
}
5050

5151
fn f4(a0: Ptr<::libc::time_t>, a1: Ptr<Tm>) -> Ptr<Tm> {
52-
let __res = a1;
52+
let __res = a1.clone();
5353
match jiff::Timestamp::from_second(a0.read()) {
5454
Ok(__ts) => {
5555
let __dt = __ts.to_zoned(jiff::tz::TimeZone::UTC);
56-
__res.with_mut(|__tm| {
57-
*__tm.tm_sec.borrow_mut() = __dt.second() as i32;
58-
*__tm.tm_min.borrow_mut() = __dt.minute() as i32;
59-
*__tm.tm_hour.borrow_mut() = __dt.hour() as i32;
60-
*__tm.tm_mday.borrow_mut() = __dt.day() as i32;
61-
*__tm.tm_mon.borrow_mut() = __dt.month() as i32 - 1;
62-
*__tm.tm_year.borrow_mut() = __dt.year() as i32 - 1900;
63-
*__tm.tm_wday.borrow_mut() = __dt.weekday().to_sunday_zero_offset() as i32;
64-
*__tm.tm_yday.borrow_mut() = __dt.day_of_year() as i32 - 1;
65-
*__tm.tm_isdst.borrow_mut() = 0;
66-
*__tm.tm_gmtoff.borrow_mut() = 0;
67-
*__tm.tm_zone.borrow_mut() = Ptr::from_string_literal(b"GMT");
68-
});
56+
__res.with_mut(|__tm| *__tm = Tm::from_zoned(&__dt));
6957
__res
7058
}
7159
Err(_) => {
@@ -76,17 +64,7 @@ fn f4(a0: Ptr<::libc::time_t>, a1: Ptr<Tm>) -> Ptr<Tm> {
7664
}
7765

7866
fn f6(a0: Ptr<u8>, a1: usize, a2: Ptr<u8>, a3: Ptr<Tm>) -> usize {
79-
let __dt = a3.with(|__tm| {
80-
jiff::civil::DateTime::new(
81-
(*__tm.tm_year.borrow() + 1900) as i16,
82-
(*__tm.tm_mon.borrow() + 1) as i8,
83-
*__tm.tm_mday.borrow() as i8,
84-
*__tm.tm_hour.borrow() as i8,
85-
*__tm.tm_min.borrow() as i8,
86-
*__tm.tm_sec.borrow() as i8,
87-
0,
88-
)
89-
});
67+
let __dt = a3.with(|__tm| __tm.to_civil());
9068
let __text = match __dt {
9169
Ok(__d) => {
9270
jiff::fmt::strtime::format(a2.to_rust_string().as_str(), __d).unwrap_or_default()

0 commit comments

Comments
 (0)