diff options
author | 4censord <49623362+4censord@users.noreply.github.com> | 2023-11-27 23:26:55 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-11-27 23:26:55 +0100 |
commit | e78f96c54bb781edd57c8af88c3061d937051d86 (patch) | |
tree | 67347fc79d9c613bebb2dacd48ed33a1f438c5c3 | |
parent | 0c808bcbef5537effe24229174fd749ef00dea90 (diff) |
Add format_time function (#143)
This takes a duration in seconds and formats it like "2min 26s", "1d 4h 20min".
It does this using the "human-time" crate.
-rw-r--r-- | Cargo.lock | 45 | ||||
-rw-r--r-- | Cargo.toml | 1 | ||||
-rw-r--r-- | docs/usage.md | 1 | ||||
-rw-r--r-- | src/function.rs | 14 |
4 files changed, 54 insertions, 7 deletions
@@ -491,6 +491,7 @@ dependencies = [ "chrono-english", "csv", "directories", + "human-time", "humansize", "imagesize", "kamadak-exif", @@ -549,7 +550,7 @@ checksum = "89ca545a94061b6365f2c7355b4b32bd20df3ff95f02da9329b34ccc3bd6ee72" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 2.0.38", ] [[package]] @@ -706,6 +707,25 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "df3b46402a9d5adb4c86a0cf463f42e19994e3ee891101b1841f30a545cb49a9" [[package]] +name = "human-time" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "259822ea527bd0d5ebf3108d84e98ac4f20769e2e8b2f3ab76e1dd6e21de7f3c" +dependencies = [ + "human-time-macros", +] + +[[package]] +name = "human-time-macros" +version = "0.1.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a04129f85bfd960234ed3fa53212a4904881e024322e804ac5a48da239e44a09" +dependencies = [ + "quote", + "syn 1.0.109", +] + +[[package]] name = "humansize" version = "2.1.3" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -1166,7 +1186,7 @@ dependencies = [ "phf_shared", "proc-macro2", "quote", - "syn", + "syn 2.0.38", ] [[package]] @@ -1523,7 +1543,7 @@ checksum = "1e48d1f918009ce3145511378cf68d613e3b3d9137d67272562080d68a2b32d5" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 2.0.38", ] [[package]] @@ -1674,6 +1694,17 @@ checksum = "0d703a3635418d4e4d0e410009ddbfb65047ef9468b1d29afd3b057a5bc4c217" [[package]] name = "syn" +version = "1.0.109" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "72b64191b275b66ffe2469e8af2c1cfe3bafa67b529ead792a6d0160888b4237" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "syn" version = "2.0.38" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e96b79aaa137db8f61e26363a0c9b47d8b4ec75da28b7d1d614c2303e232408b" @@ -1721,7 +1752,7 @@ checksum = "266b2e40bc00e5a6c09c3584011e08b06f123c00362c92b975ba9843aaaa14b8" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 2.0.38", ] [[package]] @@ -2040,7 +2071,7 @@ dependencies = [ "once_cell", "proc-macro2", "quote", - "syn", + "syn 2.0.38", "wasm-bindgen-shared", ] @@ -2074,7 +2105,7 @@ checksum = "54681b18a46765f095758388f2d0cf16eb8d4169b639ab575a8f5693af210c7b" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 2.0.38", "wasm-bindgen-backend", "wasm-bindgen-shared", ] @@ -2243,7 +2274,7 @@ checksum = "772666c41fb6dceaf520b564b962d738a8e1a83b41bd48945f50837aed78bb1d" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 2.0.38", ] [[package]] @@ -22,6 +22,7 @@ chrono = "0.4" chrono-english = "0.1" csv = "1.0" directories = "5.0" +human-time = "0.1.6" humansize = "2.0" imagesize = "0.12" kamadak-exif = "0.5" diff --git a/docs/usage.md b/docs/usage.md index 4bb1443..eed63fb 100644 --- a/docs/usage.md +++ b/docs/usage.md @@ -235,6 +235,7 @@ Used for detecting Japanese symbols in file names and such. | CONCAT | Returns concatenated string of expression values | `select CONCAT('Name is ', name, ' size is ', fsize, '!!!') from /home/user/Downloads` | | CONCAT_WS | Returns concatenated string of expression values with specified delimiter | `select name, fsize, CONCAT_WS('x', width, height) from /home/user/Images` | | RANDOM or RAND | Returns random integer (from zero to max int, from zero to *arg*, or from *arg1* to *arg2*) | `select path from /home/user/Music order by RAND()` | +| FORMAT_TIME or PRETTY_TIME | Returns human-redable durations like "2min 26s". | `select format_time(duration) from /home/user/Music` | | FORMAT_SIZE | Returns formatted size of a file | `select name, FORMAT_SIZE(size, '%.0') from /home/user/Downloads order by size desc limit 10` | Let's try `FORMAT_SIZE` with different format specifiers: diff --git a/src/function.rs b/src/function.rs index 1ca8c9c..560e2a0 100644 --- a/src/function.rs +++ b/src/function.rs @@ -6,6 +6,7 @@ use std::fs::DirEntry; use std::fs::File; use std::io::prelude::*; use std::str::FromStr; +use std::time::Duration; use chrono::Datelike; use chrono::Local; @@ -16,6 +17,8 @@ use serde::ser::{Serialize, Serializer}; #[cfg(unix)] use xattr::FileExt; +use human_time::ToHumanTimeString; + use crate::fileinfo::FileInfo; use crate::util::{capitalize, error_exit, format_date}; use crate::util::format_datetime; @@ -249,6 +252,7 @@ pub enum Function { RTrim, Coalesce, FormatSize, + FormatTime, Min, Max, @@ -324,6 +328,7 @@ impl FromStr for Function { "rtrim" => Ok(Function::RTrim), "coalesce" => Ok(Function::Coalesce), "format_size" | "format_filesize" => Ok(Function::FormatSize), + "format_time" | "pretty_time" => Ok(Function::FormatTime), "current_date" | "cur_date" | "curdate" => Ok(Function::CurrentDate), "day" => Ok(Function::Day), @@ -609,6 +614,15 @@ pub fn get_value(function: &Option<Function>, return Variant::empty(VariantType::String); } + Some(Function::FormatTime) => { + if function_arg.is_empty() { + return Variant::empty(VariantType::String); + } + + let seconds = function_arg.parse::<u64>().unwrap(); + let formated = Duration::from_secs(seconds).to_human_time_string(); + return Variant::from_string(&formated); + } Some(Function::CurrentDate) => { let now = Local::now().date_naive(); return Variant::from_string(&format_date(&now)); |