Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement strftime #330

Merged
merged 2 commits into from
Apr 5, 2019
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 51 additions & 6 deletions lib/emscripten/src/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -298,17 +298,62 @@ pub fn _time(ctx: &mut Ctx, time_p: u32) -> i32 {

/// emscripten: _strftime
pub fn _strftime(
_ctx: &mut Ctx,
_s_ptr: c_int,
_maxsize: u32,
_format_ptr: c_int,
_tm_ptr: c_int,
ctx: &mut Ctx,
s_ptr: c_int,
maxsize: u32,
format_ptr: c_int,
tm_ptr: c_int,
) -> i32 {
debug!(
"emscripten::_strftime {} {} {} {}",
_s_ptr, _maxsize, _format_ptr, _tm_ptr
);
0

#[allow(clippy::cast_ptr_alignment)]
let s = emscripten_memory_pointer!(ctx.memory(0), s_ptr) as *mut c_char;
#[allow(clippy::cast_ptr_alignment)]
let format = emscripten_memory_pointer!(ctx.memory(0), format_ptr) as *const c_char;
#[allow(clippy::cast_ptr_alignment)]
let tm = emscripten_memory_pointer!(ctx.memory(0), tm_ptr) as *const guest_tm;

let format_string = unsafe { std::ffi::CStr::from_ptr(format).to_str().unwrap() };

debug!("=> format_string: {:?}", format_string);

let tm = unsafe { &*tm };

let rust_tm = ::time::Tm {
tm_sec: tm.tm_sec,
tm_min: tm.tm_min,
tm_hour: tm.tm_hour,
tm_mday: tm.tm_mday,
tm_mon: tm.tm_mon,
tm_year: tm.tm_year,
tm_wday: tm.tm_wday,
tm_yday: tm.tm_yday,
tm_isdst: tm.tm_isdst,
tm_utcoff: tm.tm_gmtoff,
tm_nsec: 0,
};

let result_str = match ::time::strftime(format_string, &rust_tm) {
Ok(res_string) => res_string,
// TODO: maybe match on e in Err(e) and return different values if required
_ => return 0,
};

// pad for null?
let bytes = result_str.chars().count();
if bytes as u32 > maxsize {
return 0;
} else {
// write output string
for (i, c) in result_str.chars().enumerate() {
unsafe { *s.add(i) = c as c_char };
}
// null terminate?
bytes as i32
}
}

/// emscripten: _strftime_l
Expand Down