compound_duration-1.2.1/.cargo_vcs_info.json0000644000000001360000000000100145660ustar { "git": { "sha1": "fea2d9dea9195ce37005bc2997554334dc55fce2" }, "path_in_vcs": "" }compound_duration-1.2.1/.circleci/config.yml000064400000000000000000000007411046102023000172030ustar 00000000000000version: 2 workflows: version: 2 test: jobs: - test-latest jobs: test-latest: &test-template docker: - image: circleci/rust:latest steps: - checkout - run: name: Version information command: rustc --version; cargo --version; rustup --version - run: name: Build all targets command: cargo build --all --all-targets - run: name: Run all tests command: cargo test --all compound_duration-1.2.1/.github/FUNDING.yml000064400000000000000000000000161046102023000165300ustar 00000000000000github: nbari compound_duration-1.2.1/.github/workflows/rust.yml000064400000000000000000000003301046102023000204670ustar 00000000000000name: Rust on: [push] jobs: build: runs-on: ubuntu-latest steps: - uses: actions/checkout@v1 - name: Build run: cargo build --verbose - name: Run tests run: cargo test --verbose compound_duration-1.2.1/.gitignore000064400000000000000000000005001046102023000153410ustar 00000000000000# Generated by Cargo # will have compiled files and executables /target/ # Remove Cargo.lock from gitignore if creating an executable, leave it for libraries # More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html Cargo.lock # These are backup files generated by rustfmt **/*.rs.bk compound_duration-1.2.1/.travis.yml000064400000000000000000000004141046102023000154660ustar 00000000000000language: rust rust: - stable os: - linux - osx before_script: - rustup component add clippy script: - cargo clippy --all-targets --all-features -- -D clippy::pedantic -D clippy::nursery - cargo test --all notifications: email: on_success: never compound_duration-1.2.1/Cargo.toml0000644000000021510000000000100125630ustar # THIS FILE IS AUTOMATICALLY GENERATED BY CARGO # # When uploading crates to the registry Cargo will automatically # "normalize" Cargo.toml files for maximal compatibility # with all versions of Cargo and also rewrite `path` dependencies # to registry (e.g., crates.io) dependencies. # # If you are reading this file be aware that the original Cargo.toml # will likely look very different (and much more reasonable). # See Cargo.toml.orig for the original contents. [package] edition = "2018" name = "compound_duration" version = "1.2.1" authors = ["Nicolas Embriz "] description = "Convert seconds to compound duration (week, days, hours, minutes, seconds)" homepage = "https://docs.rs/compound_duration/latest/compound_duration/" documentation = "https://docs.rs/compound_duration/latest/compound_duration/" readme = "README.md" keywords = [ "time", "format", "duration", "compound", "seconds", ] categories = ["date-and-time"] license-file = "LICENSE" repository = "https://github.com/nbari/compound_duration" [badges.travis-ci] branch = "master" repository = "nbari/compound_duration" compound_duration-1.2.1/Cargo.toml.orig000064400000000000000000000012061046102023000162440ustar 00000000000000[package] name = "compound_duration" version = "1.2.1" authors = ["Nicolas Embriz "] description = "Convert seconds to compound duration (week, days, hours, minutes, seconds)" documentation = "https://docs.rs/compound_duration/latest/compound_duration/" homepage = "https://docs.rs/compound_duration/latest/compound_duration/" repository = "https://github.com/nbari/compound_duration" readme = "README.md" keywords = ["time", "format", "duration", "compound", "seconds"] categories = ["date-and-time"] license-file = "LICENSE" edition = "2018" [badges] travis-ci = { repository = "nbari/compound_duration", branch = "master" } compound_duration-1.2.1/LICENSE000064400000000000000000000027621046102023000143720ustar 00000000000000BSD 3-Clause License Copyright (c) 2019, Nicolas Embriz All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. compound_duration-1.2.1/README.md000064400000000000000000000017471046102023000146460ustar 00000000000000# compound_duration [![crates.io](https://img.shields.io/crates/v/compound_duration.svg)](https://crates.io/crates/compound_duration) [![Build Status](https://travis-ci.org/nbari/compound_duration.svg?branch=master)](https://travis-ci.org/nbari/compound_duration) Convert seconds to compound duration (week, days, hours, minutes, seconds) | input number | output string | | ------------ | ------------- | | 7259 | 2h59s | | 86400 | 1d | | 6000000 | 9w6d10h40m | | 4294967295 | 7101w3d6h28m15s | `format_dhms` or `format_wdhms`, example: ```rust use compound_duration::format_dhms; use std::time::Instant; fn main() { let now = Instant::now(); // do something ... println!("{}", format_dhms(now.elapsed().as_secs())); } ``` If need nanoseconds use `format_ns`: ```rust use compound_duration::format_ns; use std::time::Instant; fn main() { let now = Instant::now(); // do something ... println!("{}", format_ns(now.elapsed().as_nanos())); } ``` compound_duration-1.2.1/src/lib.rs000064400000000000000000000203621046102023000152640ustar 00000000000000//! Convert seconds to compound duration (week, days, hours, minutes, seconds) use std::convert::{TryFrom, TryInto}; use std::fmt::Debug; use std::ops::BitAnd; pub const NS: usize = 1; pub const US: usize = 1_000; pub const MS: usize = 1_000_000; pub const NANOS: usize = 1_000_000_000; pub const SECOND: usize = 1; pub const MINUTE: usize = 60; pub const HOUR: usize = 3_600; pub const DAY: usize = 86_400; pub const WEEK: usize = 604_800; /// Convert seconds to compound duration (days, hours, minutes, seconds) /// /// Example: ///``` /// use compound_duration::format_dhms; /// use std::time::Instant; /// /// let now = Instant::now(); /// // do something ... /// println!("{}", format_dhms(now.elapsed().as_secs() as usize)); /// /// // 69d10h40m /// println!("{}", format_dhms(6000000)); ///``` #[must_use] pub fn format_dhms + TryFrom + BitAnd>(seconds: T) -> String where >::Error: Debug, >::Error: Debug, { let seconds: usize = if std::mem::size_of::() <= std::mem::size_of::() { seconds.try_into().unwrap() } else { (seconds & usize::MAX.try_into().unwrap()) .try_into() .unwrap() }; let mut compound_duration = String::new(); if seconds == 0 { compound_duration.push_str("0s"); return compound_duration; } let mut sec = seconds % DAY; let ds = seconds / DAY; // days if ds != 0 { compound_duration.push_str(format!("{ds}d").as_str()); } // hours let hs = sec / HOUR; sec %= HOUR; if hs != 0 { compound_duration.push_str(format!("{hs}h").as_str()); } // minutes let ms = sec / MINUTE; sec %= MINUTE; if ms != 0 { compound_duration.push_str(format!("{ms}m").as_str()); } // seconds if sec != 0 { compound_duration.push_str(format!("{sec}s").as_str()); } compound_duration } /// Convert seconds to compound duration (week, days, hours, minutes, seconds) /// /// Example: ///``` /// use compound_duration::format_wdhms; /// // 9w6d10h40m /// println!("{}", format_wdhms(6000000)); ///``` #[must_use] pub fn format_wdhms + TryFrom + BitAnd>(seconds: T) -> String where >::Error: Debug, >::Error: Debug, { let seconds: usize = if std::mem::size_of::() <= std::mem::size_of::() { seconds.try_into().unwrap() } else { (seconds & usize::MAX.try_into().unwrap()) .try_into() .unwrap() }; let mut compound_duration = String::new(); if seconds == 0 { compound_duration.push_str("0s"); return compound_duration; } let mut sec = seconds % WEEK; // weeks let ws = seconds / WEEK; if ws != 0 { compound_duration.push_str(format!("{ws}w").as_str()); } // days let ds = sec / DAY; sec %= DAY; if ds != 0 { compound_duration.push_str(format!("{ds}d").as_str()); } // hours let hs = sec / HOUR; sec %= HOUR; if hs != 0 { compound_duration.push_str(format!("{hs}h").as_str()); } // minutes let ms = sec / MINUTE; sec %= MINUTE; if ms != 0 { compound_duration.push_str(format!("{ms}m").as_str()); } // seconds if sec != 0 { compound_duration.push_str(format!("{sec}s").as_str()); } compound_duration } /// Convert seconds to compound duration (days, hours, minutes, seconds, ms, µs, ns) /// /// Example: ///``` /// use compound_duration::format_ns; /// use std::time::Instant; /// /// let now = Instant::now(); /// println!("{}", format_ns(now.elapsed().as_nanos() as usize)); ///``` #[must_use] pub fn format_ns + TryFrom + BitAnd>(nanos: T) -> String where >::Error: Debug, >::Error: Debug, { let nanos: usize = if std::mem::size_of::() <= std::mem::size_of::() { nanos.try_into().unwrap() } else { (nanos & usize::MAX.try_into().unwrap()).try_into().unwrap() }; let mut compound_duration = String::new(); if nanos == 0 { compound_duration.push_str("0ns"); return compound_duration; } let mut ns = nanos % (DAY * NANOS); let d_ns = nanos / (DAY * NANOS); // days if d_ns != 0 { compound_duration.push_str(format!("{d_ns}d").as_str()); } // hours let h_ns = ns / (HOUR * NANOS); ns %= HOUR * NANOS; if h_ns != 0 { compound_duration.push_str(format!("{h_ns}h").as_str()); } // minutes let minutes_ns = ns / (MINUTE * NANOS); ns %= MINUTE * NANOS; if minutes_ns != 0 { compound_duration.push_str(format!("{minutes_ns}m").as_str()); } // seconds let sec_ns = ns / (SECOND * NANOS); ns %= SECOND * NANOS; if sec_ns != 0 { compound_duration.push_str(format!("{sec_ns}s").as_str()); } // milliseconds let ms_ns = ns / MS; ns %= MS; if ms_ns != 0 { compound_duration.push_str(format!("{ms_ns}ms").as_str()); } // microseconds let micro_ns = ns / US; ns %= US; if micro_ns != 0 { compound_duration.push_str(format!("{micro_ns}\u{b5}s").as_str()); } // nanoseconds if ns != 0 { compound_duration.push_str(format!("{ns}ns").as_str()); } compound_duration } #[cfg(test)] mod tests { use super::{format_dhms, format_ns, format_wdhms}; #[test] fn test_format_dhms() { assert_eq!(format_dhms(0), "0s"); assert_eq!(format_dhms(30), "30s"); assert_eq!(format_dhms(61), "1m1s"); assert_eq!(format_dhms(3600), "1h"); assert_eq!(format_dhms(86400), "1d"); assert_eq!(format_dhms(86401), "1d1s"); assert_eq!(format_dhms(7259), "2h59s"); assert_eq!(format_dhms(604_800), "7d"); assert_eq!(format_dhms(6_000_000), "69d10h40m"); assert_eq!(format_dhms(4_294_967_295_usize), "49710d6h28m15s"); } #[test] fn test_format_wdhms() { assert_eq!(format_wdhms(0), "0s"); assert_eq!(format_wdhms(30), "30s"); assert_eq!(format_wdhms(61), "1m1s"); assert_eq!(format_wdhms(3600), "1h"); assert_eq!(format_wdhms(86400), "1d"); assert_eq!(format_wdhms(86401), "1d1s"); assert_eq!(format_wdhms(7259), "2h59s"); assert_eq!(format_wdhms(604_800), "1w"); assert_eq!(format_wdhms(6_000_000), "9w6d10h40m"); assert_eq!(format_wdhms(4_294_967_295_usize), "7101w3d6h28m15s"); } #[test] fn test_format_ns() { assert_eq!(format_ns(3_000_129_723_usize), "3s129\u{b5}s723ns"); assert_eq!(format_ns(100_000_000_000_000_000_usize), "1157d9h46m40s"); assert_eq!(format_ns(100_000_000_000_000_001_usize), "1157d9h46m40s1ns"); assert_eq!( format_ns(100_000_000_000_001_001_usize), "1157d9h46m40s1\u{b5}s1ns" ); assert_eq!( format_ns(100_000_000_000_100_001_usize), "1157d9h46m40s100\u{b5}s1ns" ); assert_eq!( format_ns(100_000_000_010_100_001_usize), "1157d9h46m40s10ms100\u{b5}s1ns" ); assert_eq!( format_ns(100_000_000_010_000_001_usize), "1157d9h46m40s10ms1ns" ); assert_eq!(format_ns(10_000_000_000_000_000_usize), "115d17h46m40s"); assert_eq!(format_ns(1_000_000_000_000_000_usize), "11d13h46m40s"); assert_eq!(format_ns(100_000_000_000_000_usize), "1d3h46m40s"); assert_eq!(format_ns(10_000_000_000_000_usize), "2h46m40s"); assert_eq!(format_ns(1_000_000_000_000_usize), "16m40s"); assert_eq!(format_ns(100_000_000_000_usize), "1m40s"); assert_eq!(format_ns(100_000_000_010_usize), "1m40s10ns"); assert_eq!(format_ns(1_000_000_000), "1s"); assert_eq!(format_ns(100_000_000), "100ms"); assert_eq!(format_ns(10_000_000), "10ms"); assert_eq!(format_ns(1_000_001), "1ms1ns"); assert_eq!(format_ns(1_000_000), "1ms"); assert_eq!(format_ns(10_000), "10\u{b5}s"); assert_eq!(format_ns(1000), "1\u{b5}s"); assert_eq!(format_ns(100), "100ns"); assert_eq!(format_ns(1), "1ns"); assert_eq!(format_ns(0), "0ns"); } }