Skip to content

Commit c415f77

Browse files
committed
rust: remove util.c util_format_datetime, do it in Rust
1 parent bc9e3e1 commit c415f77

File tree

4 files changed

+29
-56
lines changed

4 files changed

+29
-56
lines changed

src/rust/bitbox02/src/lib.rs

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -131,20 +131,19 @@ pub fn strftime(timestamp: u32, format: &str) -> String {
131131
.into()
132132
}
133133

134+
/// Formats the timestamp in the local timezone.
135+
/// timestamp is the unix timestamp in seconds.
136+
/// timezone_offset is added to the timestamp, timezone part.
137+
/// date_only: if true, only the date is formatted. If false, both date and time are.
134138
pub fn format_datetime(timestamp: u32, timezone_offset: i32, date_only: bool) -> String {
135-
let mut out = [0u8; 100];
136-
unsafe {
137-
bitbox02_sys::util_format_datetime(
138-
timestamp,
139-
timezone_offset,
140-
date_only,
141-
out.as_mut_ptr(),
142-
out.len() as _,
143-
)
144-
}
145-
crate::util::str_from_null_terminated(&out[..])
146-
.unwrap()
147-
.into()
139+
strftime(
140+
((timestamp as i64) + (timezone_offset as i64)) as u32,
141+
if date_only {
142+
"%a %Y-%m-%d"
143+
} else {
144+
"%a %Y-%m-%d\n%H:%M"
145+
},
146+
)
148147
}
149148

150149
#[cfg(not(feature = "testing"))]
@@ -197,4 +196,21 @@ mod tests {
197196
"Mon 2020-09-28\n08:30",
198197
);
199198
}
199+
200+
#[test]
201+
fn test_format_datetime() {
202+
assert_eq!(format_datetime(1601281809, 0, true), "Mon 2020-09-28");
203+
assert_eq!(
204+
format_datetime(1601281809, 0, false),
205+
"Mon 2020-09-28\n08:30"
206+
);
207+
assert_eq!(
208+
format_datetime(1601281809, 18000, false),
209+
"Mon 2020-09-28\n13:30"
210+
);
211+
assert_eq!(
212+
format_datetime(1601281809, -32400, false),
213+
"Sun 2020-09-27\n23:30"
214+
);
215+
}
200216
}

src/util.c

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -68,15 +68,3 @@ void util_cleanup_64(uint8_t** buf)
6868
{
6969
util_zero(*buf, 64);
7070
}
71-
72-
void util_format_datetime(
73-
uint32_t timestamp,
74-
int32_t timezone_offset,
75-
bool date_only,
76-
char* out,
77-
size_t out_size)
78-
{
79-
time_t local_timestamp = timestamp + timezone_offset;
80-
struct tm* local_time = localtime(&local_timestamp);
81-
strftime(out, out_size, date_only ? "%a %Y-%m-%d" : "%a %Y-%m-%d\n%H:%M", local_time);
82-
}

src/util.h

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -145,18 +145,4 @@ typedef struct {
145145
*/
146146
typedef enum { ASYNC_OP_TRUE, ASYNC_OP_FALSE, ASYNC_OP_NOT_READY } async_op_result_t;
147147

148-
/**
149-
* Formats the timestamp in the local timezone.
150-
* @param[in] timestamp unix timestamp in seconds.
151-
* @param[in] timezone_offset is added to the timestamp, timezone part.
152-
* @param[in] date_only if true, only the date is formatted. If false, both date and time are
153-
* formatted.
154-
*/
155-
void util_format_datetime(
156-
uint32_t timestamp,
157-
int32_t timezone_offset,
158-
bool date_only,
159-
char* out,
160-
size_t out_size);
161-
162148
#endif

test/unit-test/test_util.c

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -50,27 +50,10 @@ static void test_minmax(void** state)
5050
assert_int_not_equal(res, 5);
5151
}
5252

53-
static void test_util_format_datetime(void** state)
54-
{
55-
char out[100];
56-
util_format_datetime(1601281809, 0, true, out, sizeof(out));
57-
assert_string_equal(out, "Mon 2020-09-28");
58-
59-
util_format_datetime(1601281809, 0, false, out, sizeof(out));
60-
assert_string_equal(out, "Mon 2020-09-28\n08:30");
61-
62-
util_format_datetime(1601281809, 18000, false, out, sizeof(out));
63-
assert_string_equal(out, "Mon 2020-09-28\n13:30");
64-
65-
util_format_datetime(1601281809, -32400, false, out, sizeof(out));
66-
assert_string_equal(out, "Sun 2020-09-27\n23:30");
67-
}
68-
6953
int main(void)
7054
{
7155
const struct CMUnitTest tests[] = {
7256
cmocka_unit_test(test_minmax),
73-
cmocka_unit_test(test_util_format_datetime),
7457
};
7558
return cmocka_run_group_tests(tests, NULL, NULL);
7659
}

0 commit comments

Comments
 (0)