1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-08-01 13:37:48 +00:00

date: use UResult

This commit is contained in:
Thomas Queiroz 2021-11-16 17:53:22 -03:00
parent 2dc4cba64a
commit abc93d6f17
No known key found for this signature in database
GPG key ID: 229D2DDF7ECA5F8F

View file

@ -18,6 +18,9 @@ use std::fs::File;
use std::io::{BufRead, BufReader}; use std::io::{BufRead, BufReader};
use std::path::PathBuf; use std::path::PathBuf;
use uucore::display::Quotable; use uucore::display::Quotable;
#[cfg(not(any(target_os = "macos", target_os = "redox")))]
use uucore::error::FromIo;
use uucore::error::{UResult, USimpleError};
use uucore::show_error; use uucore::show_error;
#[cfg(windows)] #[cfg(windows)]
use winapi::{ use winapi::{
@ -137,7 +140,8 @@ impl<'a> From<&'a str> for Rfc3339Format {
} }
} }
pub fn uumain(args: impl uucore::Args) -> i32 { #[uucore_procs::gen_uumain]
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
let syntax = format!( let syntax = format!(
"{0} [OPTION]... [+FORMAT]... "{0} [OPTION]... [+FORMAT]...
{0} [OPTION]... [MMDDhhmm[[CC]YY][.ss]]", {0} [OPTION]... [MMDDhhmm[[CC]YY][.ss]]",
@ -147,8 +151,10 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
let format = if let Some(form) = matches.value_of(OPT_FORMAT) { let format = if let Some(form) = matches.value_of(OPT_FORMAT) {
if !form.starts_with('+') { if !form.starts_with('+') {
show_error!("invalid date {}", form.quote()); return Err(USimpleError::new(
return 1; 1,
format!("invalid date {}", form.quote()),
));
} }
let form = form[1..].to_string(); let form = form[1..].to_string();
Format::Custom(form) Format::Custom(form)
@ -176,8 +182,10 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
let set_to = match matches.value_of(OPT_SET).map(parse_date) { let set_to = match matches.value_of(OPT_SET).map(parse_date) {
None => None, None => None,
Some(Err((input, _err))) => { Some(Err((input, _err))) => {
show_error!("invalid date {}", input.quote()); return Err(USimpleError::new(
return 1; 1,
format!("invalid date {}", input.quote()),
));
} }
Some(Ok(date)) => Some(date), Some(Ok(date)) => Some(date),
}; };
@ -241,14 +249,12 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
let formatted = date.format(format_string).to_string().replace("%f", "%N"); let formatted = date.format(format_string).to_string().replace("%f", "%N");
println!("{}", formatted); println!("{}", formatted);
} }
Err((input, _err)) => { Err((input, _err)) => show_error!("invalid date {}", input.quote()),
show_error!("invalid date {}", input.quote());
}
} }
} }
} }
0 Ok(())
} }
pub fn uu_app() -> App<'static, 'static> { pub fn uu_app() -> App<'static, 'static> {
@ -348,20 +354,24 @@ fn parse_date<S: AsRef<str> + Clone>(
} }
#[cfg(not(any(unix, windows)))] #[cfg(not(any(unix, windows)))]
fn set_system_datetime(_date: DateTime<Utc>) -> i32 { fn set_system_datetime(_date: DateTime<Utc>) -> UResult<()> {
unimplemented!("setting date not implemented (unsupported target)"); unimplemented!("setting date not implemented (unsupported target)");
} }
#[cfg(target_os = "macos")] #[cfg(target_os = "macos")]
fn set_system_datetime(_date: DateTime<Utc>) -> i32 { fn set_system_datetime(_date: DateTime<Utc>) -> UResult<()> {
show_error!("setting the date is not supported by macOS"); Err(USimpleError::new(
1 1,
"setting the date is not supported by macOS".to_string(),
))
} }
#[cfg(target_os = "redox")] #[cfg(target_os = "redox")]
fn set_system_datetime(_date: DateTime<Utc>) -> i32 { fn set_system_datetime(_date: DateTime<Utc>) -> UResult<()> {
show_error!("setting the date is not supported by Redox"); Err(USimpleError::new(
1 1,
"setting the date is not supported by Redox".to_string(),
))
} }
#[cfg(all(unix, not(target_os = "macos"), not(target_os = "redox")))] #[cfg(all(unix, not(target_os = "macos"), not(target_os = "redox")))]
@ -370,7 +380,7 @@ fn set_system_datetime(_date: DateTime<Utc>) -> i32 {
/// https://doc.rust-lang.org/libc/i686-unknown-linux-gnu/libc/fn.clock_settime.html /// https://doc.rust-lang.org/libc/i686-unknown-linux-gnu/libc/fn.clock_settime.html
/// https://linux.die.net/man/3/clock_settime /// https://linux.die.net/man/3/clock_settime
/// https://www.gnu.org/software/libc/manual/html_node/Time-Types.html /// https://www.gnu.org/software/libc/manual/html_node/Time-Types.html
fn set_system_datetime(date: DateTime<Utc>) -> i32 { fn set_system_datetime(date: DateTime<Utc>) -> UResult<()> {
let timespec = timespec { let timespec = timespec {
tv_sec: date.timestamp() as _, tv_sec: date.timestamp() as _,
tv_nsec: date.timestamp_subsec_nanos() as _, tv_nsec: date.timestamp_subsec_nanos() as _,
@ -379,11 +389,9 @@ fn set_system_datetime(date: DateTime<Utc>) -> i32 {
let result = unsafe { clock_settime(CLOCK_REALTIME, &timespec) }; let result = unsafe { clock_settime(CLOCK_REALTIME, &timespec) };
if result != 0 { if result != 0 {
let error = std::io::Error::last_os_error(); Err(std::io::Error::last_os_error().map_err_context(|| "cannot set date".to_string()))
show_error!("cannot set date: {}", error);
error.raw_os_error().unwrap()
} else { } else {
0 Ok(())
} }
} }
@ -392,7 +400,7 @@ fn set_system_datetime(date: DateTime<Utc>) -> i32 {
/// See here for more: /// See here for more:
/// https://docs.microsoft.com/en-us/windows/win32/api/sysinfoapi/nf-sysinfoapi-setsystemtime /// https://docs.microsoft.com/en-us/windows/win32/api/sysinfoapi/nf-sysinfoapi-setsystemtime
/// https://docs.microsoft.com/en-us/windows/win32/api/minwinbase/ns-minwinbase-systemtime /// https://docs.microsoft.com/en-us/windows/win32/api/minwinbase/ns-minwinbase-systemtime
fn set_system_datetime(date: DateTime<Utc>) -> i32 { fn set_system_datetime(date: DateTime<Utc>) -> UResult<()> {
let system_time = SYSTEMTIME { let system_time = SYSTEMTIME {
wYear: date.year() as WORD, wYear: date.year() as WORD,
wMonth: date.month() as WORD, wMonth: date.month() as WORD,
@ -409,10 +417,8 @@ fn set_system_datetime(date: DateTime<Utc>) -> i32 {
let result = unsafe { SetSystemTime(&system_time) }; let result = unsafe { SetSystemTime(&system_time) };
if result == 0 { if result == 0 {
let error = std::io::Error::last_os_error(); Err(std::io::Error::last_os_error().map_err_context(|| "cannot set date".to_string()))
show_error!("cannot set date: {}", error);
error.raw_os_error().unwrap()
} else { } else {
0 Ok(())
} }
} }