chrono-humanize-0.2.2/.cargo_vcs_info.json0000644000000001360000000000100141430ustar { "git": { "sha1": "d98cac49d8b2be29a64d13f9cca8da25dc4f6744" }, "path_in_vcs": "" }chrono-humanize-0.2.2/.gitignore000064400000000000000000000000221046102023000147150ustar 00000000000000target Cargo.lock chrono-humanize-0.2.2/.gitlab-ci.yml000064400000000000000000000010731046102023000153700ustar 00000000000000stages: - test - deploy stable: image: rustdocker/rust:stable stage: test script: - cargo test --verbose --jobs 1 --release beta: image: rustdocker/rust:beta stage: test script: - cargo test --verbose --jobs 1 --release nightly: image: rustdocker/rust:nightly stage: test script: - cargo test --verbose --jobs 1 --release .pages: image: rustdocker/rust:stable stage: deploy only: - master script: - cargo doc - rm -rf public - mkdir public - cp -R target/doc/* public artifacts: paths: - public chrono-humanize-0.2.2/.travis.yml000064400000000000000000000001411046102023000150400ustar 00000000000000language: rust sudo: false rust: - stable - beta - nightly os: - osx - linux chrono-humanize-0.2.2/Cargo.lock0000644000000145620000000000100121260ustar # This file is automatically @generated by Cargo. # It is not intended for manual editing. version = 3 [[package]] name = "android_system_properties" version = "0.1.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d7ed72e1635e121ca3e79420540282af22da58be50de153d36f81ddc6b83aa9e" dependencies = [ "libc", ] [[package]] name = "autocfg" version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" [[package]] name = "bumpalo" version = "3.11.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c1ad822118d20d2c234f427000d5acc36eabe1e29a348c89b63dd60b13f28e5d" [[package]] name = "cfg-if" version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" [[package]] name = "chrono" version = "0.4.22" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bfd4d1b31faaa3a89d7934dbded3111da0d2ef28e3ebccdb4f0179f5929d1ef1" dependencies = [ "iana-time-zone", "js-sys", "num-integer", "num-traits", "time", "wasm-bindgen", "winapi", ] [[package]] name = "chrono-humanize" version = "0.2.2" dependencies = [ "chrono", ] [[package]] name = "core-foundation-sys" version = "0.8.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5827cebf4670468b8772dd191856768aedcb1b0278a04f989f7766351917b9dc" [[package]] name = "iana-time-zone" version = "0.1.45" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ef5528d9c2817db4e10cc78f8d4c8228906e5854f389ff6b076cee3572a09d35" dependencies = [ "android_system_properties", "core-foundation-sys", "js-sys", "wasm-bindgen", "winapi", ] [[package]] name = "js-sys" version = "0.3.59" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "258451ab10b34f8af53416d1fdab72c22e805f0c92a1136d59470ec0b11138b2" dependencies = [ "wasm-bindgen", ] [[package]] name = "libc" version = "0.2.132" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8371e4e5341c3a96db127eb2465ac681ced4c433e01dd0e938adbef26ba93ba5" [[package]] name = "log" version = "0.4.17" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "abb12e687cfb44aa40f41fc3978ef76448f9b6038cad6aef4259d3c095a2382e" dependencies = [ "cfg-if", ] [[package]] name = "num-integer" version = "0.1.45" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "225d3389fb3509a24c93f5c29eb6bde2586b98d9f016636dff58d7c6f7569cd9" dependencies = [ "autocfg", "num-traits", ] [[package]] name = "num-traits" version = "0.2.15" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "578ede34cf02f8924ab9447f50c28075b4d3e5b269972345e7e0372b38c6cdcd" dependencies = [ "autocfg", ] [[package]] name = "once_cell" version = "1.13.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "074864da206b4973b84eb91683020dbefd6a8c3f0f38e054d93954e891935e4e" [[package]] name = "proc-macro2" version = "1.0.43" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0a2ca2c61bc9f3d74d2886294ab7b9853abd9c1ad903a3ac7815c58989bb7bab" dependencies = [ "unicode-ident", ] [[package]] name = "quote" version = "1.0.21" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bbe448f377a7d6961e30f5955f9b8d106c3f5e449d493ee1b125c1d43c2b5179" dependencies = [ "proc-macro2", ] [[package]] name = "syn" version = "1.0.99" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "58dbef6ec655055e20b86b15a8cc6d439cca19b667537ac6a1369572d151ab13" dependencies = [ "proc-macro2", "quote", "unicode-ident", ] [[package]] name = "time" version = "0.1.44" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6db9e6914ab8b1ae1c260a4ae7a49b6c5611b40328a735b21862567685e73255" dependencies = [ "libc", "wasi", "winapi", ] [[package]] name = "unicode-ident" version = "1.0.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c4f5b37a154999a8f3f98cc23a628d850e154479cd94decf3414696e12e31aaf" [[package]] name = "wasi" version = "0.10.0+wasi-snapshot-preview1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1a143597ca7c7793eff794def352d41792a93c481eb1042423ff7ff72ba2c31f" [[package]] name = "wasm-bindgen" version = "0.2.82" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fc7652e3f6c4706c8d9cd54832c4a4ccb9b5336e2c3bd154d5cccfbf1c1f5f7d" dependencies = [ "cfg-if", "wasm-bindgen-macro", ] [[package]] name = "wasm-bindgen-backend" version = "0.2.82" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "662cd44805586bd52971b9586b1df85cdbbd9112e4ef4d8f41559c334dc6ac3f" dependencies = [ "bumpalo", "log", "once_cell", "proc-macro2", "quote", "syn", "wasm-bindgen-shared", ] [[package]] name = "wasm-bindgen-macro" version = "0.2.82" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b260f13d3012071dfb1512849c033b1925038373aea48ced3012c09df952c602" dependencies = [ "quote", "wasm-bindgen-macro-support", ] [[package]] name = "wasm-bindgen-macro-support" version = "0.2.82" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5be8e654bdd9b79216c2929ab90721aa82faf65c48cdf08bdc4e7f51357b80da" dependencies = [ "proc-macro2", "quote", "syn", "wasm-bindgen-backend", "wasm-bindgen-shared", ] [[package]] name = "wasm-bindgen-shared" version = "0.2.82" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6598dd0bd3c7d51095ff6531a5b23e02acdc81804e30d8f07afb77b7215a140a" [[package]] name = "winapi" version = "0.3.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" dependencies = [ "winapi-i686-pc-windows-gnu", "winapi-x86_64-pc-windows-gnu", ] [[package]] name = "winapi-i686-pc-windows-gnu" version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" [[package]] name = "winapi-x86_64-pc-windows-gnu" version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" chrono-humanize-0.2.2/Cargo.toml0000644000000024100000000000100121360ustar # 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 = "chrono-humanize" version = "0.2.2" authors = ["Cyril Plisko "] description = "Human-friendly time expressions - similar to Python arrow.humanize" homepage = "https://gitlab.com/imp/chrono-humanize-rs" documentation = "https://docs.rs/chrono-humanize" readme = "README.md" keywords = [ "humanize", "chrono", "time", "date", "arrow", ] categories = [ "date-and-time", "value-formatting", ] license = "MIT/Apache-2.0" repository = "https://gitlab.com/imp/chrono-humanize-rs.git" [dependencies.chrono] version = "0.4" [features] pedantic = [] wasmbind = ["chrono/wasmbind"] [badges.gitlab] repository = "imp/chrono-humanize-rs" [badges.maintenance] status = "experimental" [badges.travis-ci] repository = "imp/chrono-humanize-rs" chrono-humanize-0.2.2/Cargo.toml.orig000064400000000000000000000014541046102023000156260ustar 00000000000000[package] authors = ["Cyril Plisko "] categories = ["date-and-time", "value-formatting"] description = "Human-friendly time expressions - similar to Python arrow.humanize" documentation = "https://docs.rs/chrono-humanize" homepage = "https://gitlab.com/imp/chrono-humanize-rs" keywords = ["humanize", "chrono", "time", "date", "arrow"] license = "MIT/Apache-2.0" name = "chrono-humanize" readme = "README.md" repository = "https://gitlab.com/imp/chrono-humanize-rs.git" version = "0.2.2" # remember to update html_root_url edition = "2018" [badges] gitlab = { repository = "imp/chrono-humanize-rs" } travis-ci = { repository = "imp/chrono-humanize-rs" } maintenance = { status = "experimental" } [dependencies] chrono = "0.4" [features] pedantic = [] wasmbind = ["chrono/wasmbind"] chrono-humanize-0.2.2/LICENSE-APACHE000064400000000000000000000261161046102023000146650ustar 00000000000000 Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 1. Definitions. "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work. 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form. 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed. 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: (a) You must give any other recipients of the Work or Derivative Works a copy of this License; and (b) You must cause any modified files to carry prominent notices stating that You changed the files; and (c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and (d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions. 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file. 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. END OF TERMS AND CONDITIONS APPENDIX: How to apply the Apache License to your work. To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "{}" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives. Copyright 2016 Cyril Plisko Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. chrono-humanize-0.2.2/LICENSE-MIT000064400000000000000000000020671046102023000143740ustar 00000000000000The MIT License (MIT) Copyright (c) 2016 Cyril Plisko Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. chrono-humanize-0.2.2/README.md000064400000000000000000000011721046102023000142130ustar 00000000000000## Chrono Humanize - Make your text time representation human appealing Build status branch | status :-------|:------: master| [![Build Status](https://travis-ci.org/imp/chrono-humanize-rs.svg?branch=master)](https://travis-ci.org/imp/chrono-humanize-rs) develop| [![Build Status](https://travis-ci.org/imp/chrono-humanize-rs.svg?branch=develop)](https://travis-ci.org/imp/chrono-humanize-rs) ## Quick Start ```rust use chrono::{Local, Duration}; use chrono_humanize::HumanTime; let dt = Local::now() + Duration::days(35); let ht = HumanTime::from(dt); let english = format!("{}", ht); assert_eq!("in a month", english); ``` chrono-humanize-0.2.2/benches/benches.rs000064400000000000000000000015101046102023000163140ustar 00000000000000#![feature(test)] extern crate chrono; extern crate chrono_humanize; extern crate test; #[cfg(test)] mod tests { use chrono_humanize::HumanTime; use chrono::Duration; use test::Bencher; #[bench] fn one_week(b: &mut Bencher) { let ht = HumanTime::from(Duration::weeks(1)); b.iter(|| format!("{}", ht)); } #[bench] fn two_weeks(b: &mut Bencher) { let ht = HumanTime::from(Duration::weeks(2)); b.iter(|| format!("{}", ht)); } #[bench] fn long_string(b: &mut Bencher) { let ht = HumanTime::from(Duration::seconds(1_234_567_890)); b.iter(|| format!("{:#}", ht)); // let english = format!("{:#}", ht); // assert_eq!("in 39 years, 1 month, 3 weeks, 2 days, 23 hours, 31 minutes and 30 seconds", // english); } } chrono-humanize-0.2.2/examples/humanize_systemtime.rs000064400000000000000000000010611046102023000212200ustar 00000000000000use std::time::Duration as StdDuration; use std::time::SystemTime; // use chrono::Duration; use chrono_humanize::{HumanTime, Humanize}; fn humanize_systemtime(src_time: Option) -> Option { src_time.map(|time| time.humanize()) } fn main() { let time = SystemTime::now() - StdDuration::from_secs(46); let humantime = HumanTime::from(time); println!("precise : '{:#}'", humantime); println!("rough : '{}'", humantime); if let Some(text) = humanize_systemtime(Some(time)) { println!("{:#}", text); } } chrono-humanize-0.2.2/src/humantime.rs000064400000000000000000000312361046102023000160640ustar 00000000000000use std::borrow::Cow; use std::cmp::max; use std::fmt; use std::time::SystemTime; use chrono::{DateTime, Duration, TimeZone, Utc}; use crate::Humanize; /// Indicates the time of the period in relation to the time of the utterance #[derive(Clone, Copy, Debug, Hash, PartialEq, PartialOrd)] pub enum Tense { Past, Present, Future, } /// The accuracy of the representation #[derive(Clone, Copy, Debug, Hash, PartialEq, PartialOrd)] pub enum Accuracy { /// Rough approximation, easy to grasp, but not necessarily accurate Rough, /// Concise expression, accurate, but not necessarily easy to grasp Precise, } impl Accuracy { /// Returns whether this accuracy is precise #[must_use] pub fn is_precise(self) -> bool { self == Self::Precise } /// Returns whether this accuracy is rough #[must_use] pub fn is_rough(self) -> bool { self == Self::Rough } } // Number of seconds in various time periods const S_MINUTE: i64 = 60; const S_HOUR: i64 = S_MINUTE * 60; const S_DAY: i64 = S_HOUR * 24; const S_WEEK: i64 = S_DAY * 7; const S_MONTH: i64 = S_DAY * 30; const S_YEAR: i64 = S_DAY * 365; #[derive(Clone, Copy, Debug)] enum TimePeriod { Now, Nanos(i64), Micros(i64), Millis(i64), Seconds(i64), Minutes(i64), Hours(i64), Days(i64), Weeks(i64), Months(i64), Years(i64), Eternity, } impl TimePeriod { fn to_text_precise(self) -> Cow<'static, str> { match self { Self::Now => "now".into(), Self::Nanos(n) => format!("{} ns", n).into(), Self::Micros(n) => format!("{} µs", n).into(), Self::Millis(n) => format!("{} ms", n).into(), Self::Seconds(1) => "1 second".into(), Self::Seconds(n) => format!("{} seconds", n).into(), Self::Minutes(1) => "1 minute".into(), Self::Minutes(n) => format!("{} minutes", n).into(), Self::Hours(1) => "1 hour".into(), Self::Hours(n) => format!("{} hours", n).into(), Self::Days(1) => "1 day".into(), Self::Days(n) => format!("{} days", n).into(), Self::Weeks(1) => "1 week".into(), Self::Weeks(n) => format!("{} weeks", n).into(), Self::Months(1) => "1 month".into(), Self::Months(n) => format!("{} months", n).into(), Self::Years(1) => "1 year".into(), Self::Years(n) => format!("{} years", n).into(), Self::Eternity => "eternity".into(), } } fn to_text_rough(self) -> Cow<'static, str> { match self { Self::Now => "now".into(), Self::Nanos(n) => format!("{} ns", n).into(), Self::Micros(n) => format!("{} µs", n).into(), Self::Millis(n) => format!("{} ms", n).into(), Self::Seconds(n) => format!("{} seconds", n).into(), Self::Minutes(1) => "a minute".into(), Self::Minutes(n) => format!("{} minutes", n).into(), Self::Hours(1) => "an hour".into(), Self::Hours(n) => format!("{} hours", n).into(), Self::Days(1) => "a day".into(), Self::Days(n) => format!("{} days", n).into(), Self::Weeks(1) => "a week".into(), Self::Weeks(n) => format!("{} weeks", n).into(), Self::Months(1) => "a month".into(), Self::Months(n) => format!("{} months", n).into(), Self::Years(1) => "a year".into(), Self::Years(n) => format!("{} years", n).into(), Self::Eternity => "eternity".into(), } } fn to_text(self, accuracy: Accuracy) -> Cow<'static, str> { match accuracy { Accuracy::Rough => self.to_text_rough(), Accuracy::Precise => self.to_text_precise(), } } } /// `Duration` wrapper that helps expressing the duration in human languages #[derive(Clone, Copy, Debug, PartialEq, PartialOrd)] pub struct HumanTime(Duration); impl HumanTime { const DAYS_IN_YEAR: i64 = 365; const DAYS_IN_MONTH: i64 = 30; /// Create `HumanTime` object that corresponds to the current point in time. ///. Similar to `chrono::Utc::now()` pub fn now() -> Self { Self(Duration::zero()) } /// Gives English text representation of the `HumanTime` with given `accuracy` and 'tense` #[must_use] pub fn to_text_en(self, accuracy: Accuracy, tense: Tense) -> String { let mut periods = match accuracy { Accuracy::Rough => self.rough_period(), Accuracy::Precise => self.precise_period(), }; let first = periods.remove(0).to_text(accuracy); let last = periods.pop().map(|last| last.to_text(accuracy)); let mut text = periods.into_iter().fold(first, |acc, p| { format!("{}, {}", acc, p.to_text(accuracy)).into() }); if let Some(last) = last { text = format!("{} and {}", text, last).into(); } match tense { Tense::Past => format!("{} ago", text), Tense::Future => format!("in {}", text), Tense::Present => text.into_owned(), } } fn tense(self, accuracy: Accuracy) -> Tense { if accuracy.is_rough() && self.0.num_seconds().abs() < 11 { Tense::Present } else if self.0 > Duration::zero() { Tense::Future } else if self.0 < Duration::zero() { Tense::Past } else { Tense::Present } } fn rough_period(self) -> Vec { let period = match self.0.num_seconds().abs() { n if n > 547 * S_DAY => TimePeriod::Years(max(n / S_YEAR, 2)), n if n > 345 * S_DAY => TimePeriod::Years(1), n if n > 45 * S_DAY => TimePeriod::Months(max(n / S_MONTH, 2)), n if n > 29 * S_DAY => TimePeriod::Months(1), n if n > 10 * S_DAY + 12 * S_HOUR => TimePeriod::Weeks(max(n / S_WEEK, 2)), n if n > 6 * S_DAY + 12 * S_HOUR => TimePeriod::Weeks(1), n if n > 36 * S_HOUR => TimePeriod::Days(max(n / S_DAY, 2)), n if n > 22 * S_HOUR => TimePeriod::Days(1), n if n > 90 * S_MINUTE => TimePeriod::Hours(max(n / S_HOUR, 2)), n if n > 45 * S_MINUTE => TimePeriod::Hours(1), n if n > 90 => TimePeriod::Minutes(max(n / S_MINUTE, 2)), n if n > 45 => TimePeriod::Minutes(1), n if n > 10 => TimePeriod::Seconds(n), 0..=10 => TimePeriod::Now, _ => TimePeriod::Eternity, }; vec![period] } fn precise_period(self) -> Vec { let mut periods = vec![]; let (years, reminder) = self.split_years(); if let Some(years) = years { periods.push(TimePeriod::Years(years)); } let (months, reminder) = reminder.split_months(); if let Some(months) = months { periods.push(TimePeriod::Months(months)); } let (weeks, reminder) = reminder.split_weeks(); if let Some(weeks) = weeks { periods.push(TimePeriod::Weeks(weeks)); } let (days, reminder) = reminder.split_days(); if let Some(days) = days { periods.push(TimePeriod::Days(days)); } let (hours, reminder) = reminder.split_hours(); if let Some(hours) = hours { periods.push(TimePeriod::Hours(hours)); } let (minutes, reminder) = reminder.split_minutes(); if let Some(minutes) = minutes { periods.push(TimePeriod::Minutes(minutes)); } let (seconds, reminder) = reminder.split_seconds(); if let Some(seconds) = seconds { periods.push(TimePeriod::Seconds(seconds)); } let (millis, reminder) = reminder.split_milliseconds(); if let Some(millis) = millis { periods.push(TimePeriod::Millis(millis)); } let (micros, reminder) = reminder.split_microseconds(); if let Some(micros) = micros { periods.push(TimePeriod::Micros(micros)); } let (nanos, reminder) = reminder.split_nanoseconds(); if let Some(nanos) = nanos { periods.push(TimePeriod::Nanos(nanos)); } debug_assert!(reminder.is_zero()); if periods.is_empty() { periods.push(TimePeriod::Seconds(0)); } periods } /// Split this `HumanTime` into number of whole years and the reminder fn split_years(self) -> (Option, Self) { let years = self.0.num_days() / Self::DAYS_IN_YEAR; let reminder = self.0 - Duration::days(years * Self::DAYS_IN_YEAR); Self::normalize_split(years, reminder) } /// Split this `HumanTime` into number of whole months and the reminder fn split_months(self) -> (Option, Self) { let months = self.0.num_days() / Self::DAYS_IN_MONTH; let reminder = self.0 - Duration::days(months * Self::DAYS_IN_MONTH); Self::normalize_split(months, reminder) } /// Split this `HumanTime` into number of whole weeks and the reminder fn split_weeks(self) -> (Option, Self) { let weeks = self.0.num_weeks(); let reminder = self.0 - Duration::weeks(weeks); Self::normalize_split(weeks, reminder) } /// Split this `HumanTime` into number of whole days and the reminder fn split_days(self) -> (Option, Self) { let days = self.0.num_days(); let reminder = self.0 - Duration::days(days); Self::normalize_split(days, reminder) } /// Split this `HumanTime` into number of whole hours and the reminder fn split_hours(self) -> (Option, Self) { let hours = self.0.num_hours(); let reminder = self.0 - Duration::hours(hours); Self::normalize_split(hours, reminder) } /// Split this `HumanTime` into number of whole minutes and the reminder fn split_minutes(self) -> (Option, Self) { let minutes = self.0.num_minutes(); let reminder = self.0 - Duration::minutes(minutes); Self::normalize_split(minutes, reminder) } /// Split this `HumanTime` into number of whole seconds and the reminder fn split_seconds(self) -> (Option, Self) { let seconds = self.0.num_seconds(); let reminder = self.0 - Duration::seconds(seconds); Self::normalize_split(seconds, reminder) } /// Split this `HumanTime` into number of whole milliseconds and the reminder fn split_milliseconds(self) -> (Option, Self) { let millis = self.0.num_milliseconds(); let reminder = self.0 - Duration::milliseconds(millis); Self::normalize_split(millis, reminder) } /// Split this `HumanTime` into number of whole seconds and the reminder fn split_microseconds(self) -> (Option, Self) { let micros = self.0.num_microseconds().unwrap_or_default(); let reminder = self.0 - Duration::microseconds(micros); Self::normalize_split(micros, reminder) } /// Split this `HumanTime` into number of whole seconds and the reminder fn split_nanoseconds(self) -> (Option, Self) { let nanos = self.0.num_nanoseconds().unwrap_or_default(); let reminder = self.0 - Duration::nanoseconds(nanos); Self::normalize_split(nanos, reminder) } fn normalize_split(wholes: impl Into>, reminder: Duration) -> (Option, Self) { let wholes = wholes.into().map(i64::abs).filter(|x| *x > 0); (wholes, Self(reminder)) } pub fn is_zero(self) -> bool { self.0.is_zero() } fn locale_en(&self, accuracy: Accuracy) -> String { let tense = self.tense(accuracy); self.to_text_en(accuracy, tense) } } impl fmt::Display for HumanTime { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { let accuracy = if f.alternate() { Accuracy::Precise } else { Accuracy::Rough }; f.pad(&self.locale_en(accuracy)) } } impl From for HumanTime { fn from(duration: Duration) -> Self { Self(duration) } } impl From> for HumanTime where TZ: TimeZone, { fn from(dt: DateTime) -> Self { dt.signed_duration_since(Utc::now()).into() } } impl From for HumanTime { fn from(st: SystemTime) -> Self { DateTime::::from(st).into() } } impl Humanize for Duration { fn humanize(&self) -> String { format!("{}", HumanTime::from(*self)) } } impl Humanize for DateTime where TZ: TimeZone, { fn humanize(&self) -> String { format!("{}", HumanTime::from(self.clone())) } } impl Humanize for SystemTime { fn humanize(&self) -> String { HumanTime::from(*self).to_string() } } chrono-humanize-0.2.2/src/lib.rs000064400000000000000000000032711046102023000146410ustar 00000000000000//! Representation for chrono objects in human languages //! //! # Quick Start //! //! `HumanTime` objects are created from chrono objects, such as `chrono::DateTime` //! and `chrono::Duration` //! //! # Examples //! //! Convert current time taken as `Local::now()` to `HumanTime` //! //! ``` //! let dt = chrono::Local::now(); //! let ht = chrono_humanize::HumanTime::from(dt); //! //! assert_eq!("now", format!("{}", ht)); //! ``` //! //! //! ``` //! let dt = chrono::Local::now() - chrono::Duration::minutes(58); //! let ht = chrono_humanize::HumanTime::from(dt); //! //! assert_eq!("an hour ago", format!("{}", ht)); //! ``` //! //! For full control over the text representation use `HumanTime::to_text_en()` //! //! ``` //! use chrono::Duration; //! use chrono_humanize::{Accuracy, HumanTime, Tense}; //! //! # fn main() { //! let dt = Duration::days(45); //! let ht = HumanTime::from(dt); //! //! assert_eq!("a month", ht.to_text_en(Accuracy::Rough, Tense::Present)); //! assert_eq!("1 month, 2 weeks and 1 day", ht.to_text_en(Accuracy::Precise, Tense::Present)); //! # } //! ``` #![cfg_attr(feature = "pedantic", warn(clippy::pedantic))] #![warn(clippy::use_self)] #![warn(deprecated_in_future)] #![warn(future_incompatible)] #![warn(unreachable_pub)] #![warn(missing_debug_implementations)] #![warn(rust_2018_compatibility)] #![warn(rust_2018_idioms)] #![warn(unused)] #![deny(warnings)] #![doc(html_root_url = "https://docs.rs/chrono-humanize/0.2.2")] pub use crate::humantime::{Accuracy, HumanTime, Tense}; mod humantime; /// Present the object in human friendly text form pub trait Humanize { /// Emits `String` that represents current object in human friendly form fn humanize(&self) -> String; } chrono-humanize-0.2.2/tests/humanize.rs000064400000000000000000000204751046102023000162730ustar 00000000000000extern crate chrono; extern crate chrono_humanize; #[cfg(test)] mod duration { use chrono::Duration; use chrono_humanize::Humanize; #[test] fn now() { let english = Duration::zero().humanize(); assert_eq!("now", english); } #[test] fn plus_5s() { let english = Duration::seconds(5).humanize(); assert_eq!("now", english); } #[test] fn minus_5s() { let english = Duration::seconds(-5).humanize(); assert_eq!("now", english); } #[test] fn plus_15s() { let english = Duration::seconds(15).humanize(); assert_eq!("in 15 seconds", english); } #[test] fn minus_15s() { let english = Duration::seconds(-15).humanize(); assert_eq!("15 seconds ago", english); } #[test] fn plus_95s() { let english = Duration::seconds(95).humanize(); assert_eq!("in 2 minutes", english); } #[test] fn minus_95s() { let english = Duration::seconds(-95).humanize(); assert_eq!("2 minutes ago", english); } #[test] fn plus_125s() { let english = Duration::seconds(125).humanize(); assert_eq!("in 2 minutes", english); } #[test] fn minus_125s() { let english = Duration::seconds(-125).humanize(); assert_eq!("2 minutes ago", english); } #[test] fn plus_31m() { let english = Duration::minutes(31).humanize(); assert_eq!("in 31 minutes", english); } #[test] fn minus_31m() { let english = Duration::minutes(-31).humanize(); assert_eq!("31 minutes ago", english); } #[test] fn plus_45m() { let english = Duration::minutes(45).humanize(); assert_eq!("in 45 minutes", english); } #[test] fn minus_45m() { let english = Duration::minutes(-45).humanize(); assert_eq!("45 minutes ago", english); } #[test] fn plus_46m() { let english = Duration::minutes(46).humanize(); assert_eq!("in an hour", english); } #[test] fn minus_46m() { let english = Duration::minutes(-46).humanize(); assert_eq!("an hour ago", english); } #[test] fn plus_1h() { let english = Duration::hours(1).humanize(); assert_eq!("in an hour", english); } #[test] fn minus_1h() { let english = Duration::hours(-1).humanize(); assert_eq!("an hour ago", english); } #[test] fn plus_12h() { let english = Duration::hours(12).humanize(); assert_eq!("in 12 hours", english); } #[test] fn minus_12h() { let english = Duration::hours(-12).humanize(); assert_eq!("12 hours ago", english); } #[test] fn plus_23h() { let english = Duration::hours(23).humanize(); assert_eq!("in a day", english); } #[test] fn minus_23h() { let english = Duration::hours(-23).humanize(); assert_eq!("a day ago", english); } #[test] fn plus_26h() { let english = Duration::hours(26).humanize(); assert_eq!("in a day", english); } #[test] fn minus_26h() { let english = Duration::hours(-26).humanize(); assert_eq!("a day ago", english); } #[test] fn plus_1d() { let english = Duration::days(1).humanize(); assert_eq!("in a day", english); } #[test] fn minus_1d() { let english = Duration::days(-1).humanize(); assert_eq!("a day ago", english); } #[test] fn plus_2d() { let english = Duration::days(2).humanize(); assert_eq!("in 2 days", english); } #[test] fn minus_2d() { let english = Duration::days(-2).humanize(); assert_eq!("2 days ago", english); } #[test] fn plus_6d_13h() { let english = (Duration::days(6) + Duration::hours(13)).humanize(); assert_eq!("in a week", english); } #[test] fn minus_6d_13h() { let english = (Duration::days(-6) + Duration::hours(-13)).humanize(); assert_eq!("a week ago", english); } #[test] fn plus_7d() { let english = Duration::days(7).humanize(); assert_eq!("in a week", english); } #[test] fn minus_7d() { let english = Duration::days(-7).humanize(); assert_eq!("a week ago", english); } #[test] fn plus_10d() { let english = Duration::days(10).humanize(); assert_eq!("in a week", english); } #[test] fn minus_10d() { let english = Duration::days(-10).humanize(); assert_eq!("a week ago", english); } #[test] fn plus_11d() { let english = Duration::days(11).humanize(); assert_eq!("in 2 weeks", english); } #[test] fn minus_11d() { let english = Duration::days(-11).humanize(); assert_eq!("2 weeks ago", english); } #[test] fn plus_4w() { let english = Duration::weeks(4).humanize(); assert_eq!("in 4 weeks", english); } #[test] fn minus_4w() { let english = Duration::weeks(-4).humanize(); assert_eq!("4 weeks ago", english); } #[test] fn plus_30d() { let english = Duration::days(30).humanize(); assert_eq!("in a month", english); } #[test] fn minus_30d() { let english = Duration::days(-30).humanize(); assert_eq!("a month ago", english); } #[test] fn plus_45d() { let english = Duration::days(45).humanize(); assert_eq!("in a month", english); } #[test] fn minus_45d() { let english = Duration::days(-45).humanize(); assert_eq!("a month ago", english); } #[test] fn plus_46d() { let english = Duration::days(46).humanize(); assert_eq!("in 2 months", english); } #[test] fn minus_46d() { let english = Duration::days(-46).humanize(); assert_eq!("2 months ago", english); } #[test] fn plus_24w() { let english = Duration::weeks(24).humanize(); assert_eq!("in 5 months", english); } #[test] fn minus_24w() { let english = Duration::weeks(-24).humanize(); assert_eq!("5 months ago", english); } #[test] fn plus_26w() { let english = Duration::weeks(26).humanize(); assert_eq!("in 6 months", english); } #[test] fn minus_26w() { let english = Duration::weeks(-26).humanize(); assert_eq!("6 months ago", english); } #[test] fn plus_50w() { let english = Duration::weeks(50).humanize(); assert_eq!("in a year", english); } #[test] fn minus_50w() { let english = Duration::weeks(-50).humanize(); assert_eq!("a year ago", english); } #[test] fn plus_100w() { let english = Duration::weeks(100).humanize(); assert_eq!("in 2 years", english); } #[test] fn minus_100w() { let english = Duration::weeks(-100).humanize(); assert_eq!("2 years ago", english); } #[test] fn plus_120w() { let english = Duration::weeks(120).humanize(); assert_eq!("in 2 years", english); } #[test] fn minus_120w() { let english = Duration::weeks(-120).humanize(); assert_eq!("2 years ago", english); } #[test] fn plus_200w() { let english = Duration::weeks(200).humanize(); assert_eq!("in 3 years", english); } #[test] fn minus_200w() { let english = Duration::weeks(-200).humanize(); assert_eq!("3 years ago", english); } } #[cfg(test)] mod utc { use chrono::Utc; use chrono_humanize::Humanize; #[test] fn now() { let english = Utc::now().humanize(); assert_eq!("now", english); } } #[cfg(test)] mod local { use chrono::{Duration, Local}; use chrono_humanize::Humanize; #[test] fn now() { let english = Local::now().humanize(); assert_eq!("now", english); } #[test] fn minus_35d() { let past = Local::now() - Duration::days(35); let english = past.humanize(); assert_eq!("a month ago", english); } #[test] fn plus_35d() { let future = Local::now() + Duration::days(35); let english = future.humanize(); assert_eq!("in a month", english); } } chrono-humanize-0.2.2/tests/precise.rs000064400000000000000000000306041046102023000161000ustar 00000000000000#[cfg(test)] mod duration { use chrono::Duration; use chrono_humanize::HumanTime; #[test] fn zero() { let ht = HumanTime::from(Duration::zero()); let english = format!("{:#}", ht); assert_eq!("0 seconds", english); } #[test] fn plus_1ms() { let ht = HumanTime::from(Duration::milliseconds(1)); let english = format!("{:#}", ht); assert_eq!("in 1 ms", english); } #[test] fn minus_1ms() { let ht = HumanTime::from(Duration::milliseconds(-1)); let english = format!("{:#}", ht); assert_eq!("1 ms ago", english); } #[test] fn plus_1s() { let ht = HumanTime::from(Duration::seconds(1)); let english = format!("{:#}", ht); assert_eq!("in 1 second", english); } #[test] fn minus_1s() { let ht = HumanTime::from(Duration::seconds(-1)); let english = format!("{:#}", ht); assert_eq!("1 second ago", english); } #[test] fn plus_5s() { let ht = HumanTime::from(Duration::seconds(5)); let english = format!("{:#}", ht); assert_eq!("in 5 seconds", english); } #[test] fn minus_5s() { let ht = HumanTime::from(Duration::seconds(-5)); let english = format!("{:#}", ht); assert_eq!("5 seconds ago", english); } #[test] fn plus_15s() { let ht = HumanTime::from(Duration::seconds(15)); let english = format!("{:#}", ht); assert_eq!("in 15 seconds", english); } #[test] fn minus_15s() { let ht = HumanTime::from(Duration::seconds(-15)); let english = format!("{:#}", ht); assert_eq!("15 seconds ago", english); } #[test] fn plus_95s() { let ht = HumanTime::from(Duration::seconds(95)); let english = format!("{:#}", ht); assert_eq!("in 1 minute and 35 seconds", english); } #[test] fn minus_95s() { let ht = HumanTime::from(Duration::seconds(-95)); let english = format!("{:#}", ht); assert_eq!("1 minute and 35 seconds ago", english); } #[test] fn plus_125s() { let ht = HumanTime::from(Duration::seconds(125)); let english = format!("{:#}", ht); assert_eq!("in 2 minutes and 5 seconds", english); } #[test] fn minus_125s() { let ht = HumanTime::from(Duration::seconds(-125)); let english = format!("{:#}", ht); assert_eq!("2 minutes and 5 seconds ago", english); } #[test] fn plus_31m() { let ht = HumanTime::from(Duration::minutes(31)); let english = format!("{:#}", ht); assert_eq!("in 31 minutes", english); } #[test] fn minus_31m() { let ht = HumanTime::from(Duration::minutes(-31)); let english = format!("{:#}", ht); assert_eq!("31 minutes ago", english); } #[test] fn plus_45m() { let ht = HumanTime::from(Duration::minutes(45)); let english = format!("{:#}", ht); assert_eq!("in 45 minutes", english); } #[test] fn minus_45m() { let ht = HumanTime::from(Duration::minutes(-45)); let english = format!("{:#}", ht); assert_eq!("45 minutes ago", english); } #[test] fn plus_46m() { let ht = HumanTime::from(Duration::minutes(46)); let english = format!("{:#}", ht); assert_eq!("in 46 minutes", english); } #[test] fn minus_46m() { let ht = HumanTime::from(Duration::minutes(-46)); let english = format!("{:#}", ht); assert_eq!("46 minutes ago", english); } #[test] fn plus_1h() { let ht = HumanTime::from(Duration::hours(1)); let english = format!("{:#}", ht); assert_eq!("in 1 hour", english); } #[test] fn minus_1h() { let ht = HumanTime::from(Duration::hours(-1)); let english = format!("{:#}", ht); assert_eq!("1 hour ago", english); } #[test] fn plus_72m() { let ht = HumanTime::from(Duration::minutes(72)); let english = format!("{:#}", ht); assert_eq!("in 1 hour and 12 minutes", english); } #[test] fn minus_72m() { let ht = HumanTime::from(Duration::minutes(-72)); let english = format!("{:#}", ht); assert_eq!("1 hour and 12 minutes ago", english); } #[test] fn plus_12h() { let ht = HumanTime::from(Duration::hours(12)); let english = format!("{:#}", ht); assert_eq!("in 12 hours", english); } #[test] fn minus_12h() { let ht = HumanTime::from(Duration::hours(-12)); let english = format!("{:#}", ht); assert_eq!("12 hours ago", english); } #[test] fn plus_23h() { let ht = HumanTime::from(Duration::hours(23)); let english = format!("{:#}", ht); assert_eq!("in 23 hours", english); } #[test] fn minus_23h() { let ht = HumanTime::from(Duration::hours(-23)); let english = format!("{:#}", ht); assert_eq!("23 hours ago", english); } #[test] fn plus_26h() { let ht = HumanTime::from(Duration::hours(26)); let english = format!("{:#}", ht); assert_eq!("in 1 day and 2 hours", english); } #[test] fn minus_26h() { let ht = HumanTime::from(Duration::hours(-26)); let english = format!("{:#}", ht); assert_eq!("1 day and 2 hours ago", english); } #[test] fn plus_1d() { let ht = HumanTime::from(Duration::days(1)); let english = format!("{:#}", ht); assert_eq!("in 1 day", english); } #[test] fn minus_1d() { let ht = HumanTime::from(Duration::days(-1)); let english = format!("{:#}", ht); assert_eq!("1 day ago", english); } #[test] fn plus_2d() { let ht = HumanTime::from(Duration::days(2)); let english = format!("{:#}", ht); assert_eq!("in 2 days", english); } #[test] fn minus_2d() { let ht = HumanTime::from(Duration::days(-2)); let english = format!("{:#}", ht); assert_eq!("2 days ago", english); } #[test] fn plus_6d_13h() { let ht = HumanTime::from(Duration::days(6) + Duration::hours(13)); let english = format!("{:#}", ht); assert_eq!("in 6 days and 13 hours", english); } #[test] fn minus_6d_13h() { let ht = HumanTime::from(Duration::days(-6) + Duration::hours(-13)); let english = format!("{:#}", ht); assert_eq!("6 days and 13 hours ago", english); } #[test] fn plus_7d() { let ht = HumanTime::from(Duration::days(7)); let english = format!("{:#}", ht); assert_eq!("in 1 week", english); } #[test] fn minus_7d() { let ht = HumanTime::from(Duration::days(-7)); let english = format!("{:#}", ht); assert_eq!("1 week ago", english); } #[test] fn plus_10d() { let ht = HumanTime::from(Duration::days(10)); let english = format!("{:#}", ht); assert_eq!("in 1 week and 3 days", english); } #[test] fn minus_10d() { let ht = HumanTime::from(Duration::days(-10)); let english = format!("{:#}", ht); assert_eq!("1 week and 3 days ago", english); } #[test] fn plus_11d() { let ht = HumanTime::from(Duration::days(11)); let english = format!("{:#}", ht); assert_eq!("in 1 week and 4 days", english); } #[test] fn minus_11d() { let ht = HumanTime::from(Duration::days(-11)); let english = format!("{:#}", ht); assert_eq!("1 week and 4 days ago", english); } #[test] fn plus_4w() { let ht = HumanTime::from(Duration::weeks(4)); let english = format!("{:#}", ht); assert_eq!("in 4 weeks", english); } #[test] fn minus_4w() { let ht = HumanTime::from(Duration::weeks(-4)); let english = format!("{:#}", ht); assert_eq!("4 weeks ago", english); } #[test] fn plus_30d() { let ht = HumanTime::from(Duration::days(30)); let english = format!("{:#}", ht); assert_eq!("in 1 month", english); } #[test] fn minus_30d() { let ht = HumanTime::from(Duration::days(-30)); let english = format!("{:#}", ht); assert_eq!("1 month ago", english); } #[test] fn plus_45d() { let ht = HumanTime::from(Duration::days(45)); let english = format!("{:#}", ht); assert_eq!("in 1 month, 2 weeks and 1 day", english); } #[test] fn minus_45d() { let ht = HumanTime::from(Duration::days(-45)); let english = format!("{:#}", ht); assert_eq!("1 month, 2 weeks and 1 day ago", english); } #[test] fn plus_46d() { let ht = HumanTime::from(Duration::days(46)); let english = format!("{:#}", ht); assert_eq!("in 1 month, 2 weeks and 2 days", english); } #[test] fn minus_46d() { let ht = HumanTime::from(Duration::days(-46)); let english = format!("{:#}", ht); assert_eq!("1 month, 2 weeks and 2 days ago", english); } #[test] fn plus_24w() { let ht = HumanTime::from(Duration::weeks(24)); let english = format!("{:#}", ht); assert_eq!("in 5 months, 2 weeks and 4 days", english); } #[test] fn minus_24w() { let ht = HumanTime::from(Duration::weeks(-24)); let english = format!("{:#}", ht); assert_eq!("5 months, 2 weeks and 4 days ago", english); } #[test] fn plus_26w() { let ht = HumanTime::from(Duration::weeks(26)); let english = format!("{:#}", ht); assert_eq!("in 6 months and 2 days", english); } #[test] fn minus_26w() { let ht = HumanTime::from(Duration::weeks(-26)); let english = format!("{:#}", ht); assert_eq!("6 months and 2 days ago", english); } #[test] fn plus_50w() { let ht = HumanTime::from(Duration::weeks(50)); let english = format!("{:#}", ht); assert_eq!("in 11 months, 2 weeks and 6 days", english); } #[test] fn minus_50w() { let ht = HumanTime::from(Duration::weeks(-50)); let english = format!("{:#}", ht); assert_eq!("11 months, 2 weeks and 6 days ago", english); } #[test] fn plus_100w() { let ht = HumanTime::from(Duration::weeks(100)); let english = format!("{:#}", ht); assert_eq!("in 1 year, 11 months and 5 days", english); } #[test] fn minus_100w() { let ht = HumanTime::from(Duration::weeks(-100)); let english = format!("{:#}", ht); assert_eq!("1 year, 11 months and 5 days ago", english); } #[test] fn plus_120w() { let ht = HumanTime::from(Duration::weeks(120)); let english = format!("{:#}", ht); assert_eq!("in 2 years, 3 months, 2 weeks and 6 days", english); } #[test] fn minus_120w() { let ht = HumanTime::from(Duration::weeks(-120)); let english = format!("{:#}", ht); assert_eq!("2 years, 3 months, 2 weeks and 6 days ago", english); } #[test] fn plus_200w() { let ht = HumanTime::from(Duration::weeks(200)); let english = format!("{:#}", ht); assert_eq!("in 3 years, 10 months and 5 days", english); } #[test] fn minus_200w() { let ht = HumanTime::from(Duration::weeks(-200)); let english = format!("{:#}", ht); assert_eq!("3 years, 10 months and 5 days ago", english); } } #[cfg(test)] mod utc { use chrono_humanize::HumanTime; #[test] fn now() { let ht = HumanTime::now(); let english = format!("{:#}", ht); assert_eq!("0 seconds", english); } } #[cfg(test1)] mod local { use chrono::{Duration, Local}; use chrono_humanize::HumanTime; #[test] fn now() { let ht = HumanTime::from(Local::now()); let english = format!("{:#}", ht); assert_eq!("0 seconds", english); } #[test] fn minus_35d() { let past = Local::now() - Duration::days(35); let ht = HumanTime::from(past); let english = format!("{:#}", ht); assert_eq!("1 month ago", english); } #[test] fn plus_35d() { let future = Local::now() + Duration::days(35); let ht = HumanTime::from(future); let english = format!("{:#}", ht); assert_eq!("in 1 month", english); } } chrono-humanize-0.2.2/tests/present.rs000064400000000000000000000135321046102023000161270ustar 00000000000000macro_rules! duration_test { ($($name:ident: $duration:expr, $rough:expr, $precise:expr,)+) => { $(#[test] fn $name() { let ht = HumanTime::from($duration); let rough = ht.to_text_en(Accuracy::Rough, Tense::Present); let precise = ht.to_text_en(Accuracy::Precise, Tense::Present); assert_eq!($rough, rough); assert_eq!($precise, precise); })+ } } #[cfg(test)] mod duration { use chrono::Duration; use chrono_humanize::{Accuracy, HumanTime, Tense}; // test_name: Duration expression, "Rough text", "Precise text" duration_test! { now: Duration::zero(), "now", "0 seconds", plus_1s: Duration::seconds(1), "now", "1 second", minus_1s: Duration::seconds(-1), "now", "1 second", plus_5s: Duration::seconds(5), "now", "5 seconds", minus_5s: Duration::seconds(-5), "now", "5 seconds", plus_15s: Duration::seconds(15), "15 seconds", "15 seconds", minus_15s: Duration::seconds(-15), "15 seconds", "15 seconds", plus_95s: Duration::seconds(95), "2 minutes", "1 minute and 35 seconds", minus_95s: Duration::seconds(-95), "2 minutes", "1 minute and 35 seconds", plus_125s: Duration::seconds(125), "2 minutes", "2 minutes and 5 seconds", minus_125s: Duration::seconds(-125), "2 minutes", "2 minutes and 5 seconds", plus_31m: Duration::minutes(31), "31 minutes", "31 minutes", minus_31m: Duration::minutes(-31), "31 minutes", "31 minutes", plus_45m: Duration::minutes(45), "45 minutes", "45 minutes", minus_45m: Duration::minutes(-45), "45 minutes", "45 minutes", plus_46m: Duration::minutes(46), "an hour", "46 minutes", minus_46m: Duration::minutes(-46), "an hour", "46 minutes", plus_1h: Duration::hours(1), "an hour", "1 hour", minus_1h: Duration::hours(-1), "an hour", "1 hour", plus_12h: Duration::hours(12), "12 hours", "12 hours", minus_12h: Duration::hours(-12), "12 hours", "12 hours", plus_23h: Duration::hours(23), "a day", "23 hours", minus_23h: Duration::hours(-23), "a day", "23 hours", plus_26h: Duration::hours(26), "a day", "1 day and 2 hours", minus_26h: Duration::hours(-26), "a day", "1 day and 2 hours", plus_1d: Duration::days(1), "a day", "1 day", minus_1d: Duration::days(-1), "a day", "1 day", plus_2d: Duration::days(2), "2 days", "2 days", minus_2d: Duration::days(-2), "2 days", "2 days", plus_6d_13h: Duration::days(6) + Duration::hours(13), "a week", "6 days and 13 hours", minus_6d_13h: Duration::days(-6) + Duration::hours(-13), "a week", "6 days and 13 hours", plus_7d: Duration::days(7), "a week", "1 week", minus_7d: Duration::days(-7), "a week", "1 week", plus_10d: Duration::days(10), "a week", "1 week and 3 days", minus_10d: Duration::days(-10), "a week", "1 week and 3 days", plus_11d: Duration::days(11), "2 weeks", "1 week and 4 days", minus_11d: Duration::days(-11), "2 weeks", "1 week and 4 days", plus_4w: Duration::weeks(4), "4 weeks", "4 weeks", minus_4w: Duration::weeks(-4), "4 weeks", "4 weeks", plus_30d: Duration::days(30), "a month", "1 month", minus_30d: Duration::days(-30), "a month", "1 month", plus_45d: Duration::days(45), "a month", "1 month, 2 weeks and 1 day", minus_45d: Duration::days(-45), "a month", "1 month, 2 weeks and 1 day", plus_46d: Duration::days(46), "2 months", "1 month, 2 weeks and 2 days", minus_46d: Duration::days(-46), "2 months", "1 month, 2 weeks and 2 days", plus_24w: Duration::weeks(24), "5 months", "5 months, 2 weeks and 4 days", minus_24w: Duration::weeks(-24), "5 months", "5 months, 2 weeks and 4 days", plus_26w: Duration::weeks(26), "6 months", "6 months and 2 days", minus_26w: Duration::weeks(-26), "6 months", "6 months and 2 days", plus_50w: Duration::weeks(50), "a year", "11 months, 2 weeks and 6 days", minus_50w: Duration::weeks(-50), "a year", "11 months, 2 weeks and 6 days", plus_100w: Duration::weeks(100), "2 years", "1 year, 11 months and 5 days", minus_100w: Duration::weeks(-100), "2 years", "1 year, 11 months and 5 days", plus_101w: Duration::weeks(101), "2 years", "1 year, 11 months, 1 week and 5 days", minus_101w: Duration::weeks(-101), "2 years", "1 year, 11 months, 1 week and 5 days", plus_120w: Duration::weeks(120), "2 years", "2 years, 3 months, 2 weeks and 6 days", minus_120w: Duration::weeks(-120), "2 years", "2 years, 3 months, 2 weeks and 6 days", plus_200w: Duration::weeks(200), "3 years", "3 years, 10 months and 5 days", minus_200w: Duration::weeks(-200), "3 years", "3 years, 10 months and 5 days", } } #[cfg(test)] mod utc { use chrono::Utc; use chrono_humanize::{Accuracy, HumanTime, Tense}; #[test] fn now() { let ht = HumanTime::from(Utc::now()); let rough = ht.to_text_en(Accuracy::Rough, Tense::Present); assert_eq!("now", rough); } } #[cfg(test)] mod local { use chrono::{Duration, Local}; use chrono_humanize::{Accuracy, HumanTime, Tense}; #[test] fn now() { let ht = HumanTime::from(Local::now()); let rough = ht.to_text_en(Accuracy::Rough, Tense::Present); assert_eq!("now", rough); } #[test] fn minus_35d() { let past = Local::now() - Duration::days(35); let ht = HumanTime::from(past); let rough = ht.to_text_en(Accuracy::Rough, Tense::Present); assert_eq!("a month", rough); } #[test] fn plus_35d() { let future = Local::now() + Duration::days(35); let ht = HumanTime::from(future); let rough = ht.to_text_en(Accuracy::Rough, Tense::Present); assert_eq!("a month", rough); } } chrono-humanize-0.2.2/tests/rough.rs000064400000000000000000000265311046102023000155760ustar 00000000000000#[cfg(test)] mod duration { use chrono::Duration; use chrono_humanize::HumanTime; #[test] fn now() { let ht = HumanTime::now(); let english = format!("{}", ht); assert_eq!("now", english); } #[test] fn plus_1s() { let ht = HumanTime::from(Duration::seconds(1)); let english = format!("{}", ht); assert_eq!("now", english); } #[test] fn minus_1s() { let ht = HumanTime::from(Duration::seconds(-1)); let english = format!("{}", ht); assert_eq!("now", english); } #[test] fn plus_5s() { let ht = HumanTime::from(Duration::seconds(5)); let english = format!("{}", ht); assert_eq!("now", english); } #[test] fn minus_5s() { let ht = HumanTime::from(Duration::seconds(-5)); let english = format!("{}", ht); assert_eq!("now", english); } #[test] fn plus_10s() { let ht = HumanTime::from(Duration::seconds(10)); let english = format!("{}", ht); assert_eq!("now", english); } #[test] fn minus_10s() { let ht = HumanTime::from(Duration::seconds(-10)); let english = format!("{}", ht); assert_eq!("now", english); } #[test] fn plus_15s() { let ht = HumanTime::from(Duration::seconds(15)); let english = format!("{}", ht); assert_eq!("in 15 seconds", english); } #[test] fn minus_15s() { let ht = HumanTime::from(Duration::seconds(-15)); let english = format!("{}", ht); assert_eq!("15 seconds ago", english); } #[test] fn plus_95s() { let ht = HumanTime::from(Duration::seconds(95)); let english = format!("{}", ht); assert_eq!("in 2 minutes", english); } #[test] fn minus_95s() { let ht = HumanTime::from(Duration::seconds(-95)); let english = format!("{}", ht); assert_eq!("2 minutes ago", english); } #[test] fn plus_125s() { let ht = HumanTime::from(Duration::seconds(125)); let english = format!("{}", ht); assert_eq!("in 2 minutes", english); } #[test] fn minus_125s() { let ht = HumanTime::from(Duration::seconds(-125)); let english = format!("{}", ht); assert_eq!("2 minutes ago", english); } #[test] fn plus_31m() { let ht = HumanTime::from(Duration::minutes(31)); let english = format!("{}", ht); assert_eq!("in 31 minutes", english); } #[test] fn minus_31m() { let ht = HumanTime::from(Duration::minutes(-31)); let english = format!("{}", ht); assert_eq!("31 minutes ago", english); } #[test] fn plus_45m() { let ht = HumanTime::from(Duration::minutes(45)); let english = format!("{}", ht); assert_eq!("in 45 minutes", english); } #[test] fn minus_45m() { let ht = HumanTime::from(Duration::minutes(-45)); let english = format!("{}", ht); assert_eq!("45 minutes ago", english); } #[test] fn plus_46m() { let ht = HumanTime::from(Duration::minutes(46)); let english = format!("{}", ht); assert_eq!("in an hour", english); } #[test] fn minus_46m() { let ht = HumanTime::from(Duration::minutes(-46)); let english = format!("{}", ht); assert_eq!("an hour ago", english); } #[test] fn plus_1h() { let ht = HumanTime::from(Duration::hours(1)); let english = format!("{}", ht); assert_eq!("in an hour", english); } #[test] fn minus_1h() { let ht = HumanTime::from(Duration::hours(-1)); let english = format!("{}", ht); assert_eq!("an hour ago", english); } #[test] fn plus_12h() { let ht = HumanTime::from(Duration::hours(12)); let english = format!("{}", ht); assert_eq!("in 12 hours", english); } #[test] fn minus_12h() { let ht = HumanTime::from(Duration::hours(-12)); let english = format!("{}", ht); assert_eq!("12 hours ago", english); } #[test] fn plus_23h() { let ht = HumanTime::from(Duration::hours(23)); let english = format!("{}", ht); assert_eq!("in a day", english); } #[test] fn minus_23h() { let ht = HumanTime::from(Duration::hours(-23)); let english = format!("{}", ht); assert_eq!("a day ago", english); } #[test] fn plus_26h() { let ht = HumanTime::from(Duration::hours(26)); let english = format!("{}", ht); assert_eq!("in a day", english); } #[test] fn minus_26h() { let ht = HumanTime::from(Duration::hours(-26)); let english = format!("{}", ht); assert_eq!("a day ago", english); } #[test] fn plus_1d() { let ht = HumanTime::from(Duration::days(1)); let english = format!("{}", ht); assert_eq!("in a day", english); } #[test] fn minus_1d() { let ht = HumanTime::from(Duration::days(-1)); let english = format!("{}", ht); assert_eq!("a day ago", english); } #[test] fn plus_2d() { let ht = HumanTime::from(Duration::days(2)); let english = format!("{}", ht); assert_eq!("in 2 days", english); } #[test] fn minus_2d() { let ht = HumanTime::from(Duration::days(-2)); let english = format!("{}", ht); assert_eq!("2 days ago", english); } #[test] fn plus_6d_13h() { let ht = HumanTime::from(Duration::days(6) + Duration::hours(13)); let english = format!("{}", ht); assert_eq!("in a week", english); } #[test] fn minus_6d_13h() { let ht = HumanTime::from(Duration::days(-6) + Duration::hours(-13)); let english = format!("{}", ht); assert_eq!("a week ago", english); } #[test] fn plus_7d() { let ht = HumanTime::from(Duration::days(7)); let english = format!("{}", ht); assert_eq!("in a week", english); } #[test] fn minus_7d() { let ht = HumanTime::from(Duration::days(-7)); let english = format!("{}", ht); assert_eq!("a week ago", english); } #[test] fn plus_10d() { let ht = HumanTime::from(Duration::days(10)); let english = format!("{}", ht); assert_eq!("in a week", english); } #[test] fn minus_10d() { let ht = HumanTime::from(Duration::days(-10)); let english = format!("{}", ht); assert_eq!("a week ago", english); } #[test] fn plus_11d() { let ht = HumanTime::from(Duration::days(11)); let english = format!("{}", ht); assert_eq!("in 2 weeks", english); } #[test] fn minus_11d() { let ht = HumanTime::from(Duration::days(-11)); let english = format!("{}", ht); assert_eq!("2 weeks ago", english); } #[test] fn plus_4w() { let ht = HumanTime::from(Duration::weeks(4)); let english = format!("{}", ht); assert_eq!("in 4 weeks", english); } #[test] fn minus_4w() { let ht = HumanTime::from(Duration::weeks(-4)); let english = format!("{}", ht); assert_eq!("4 weeks ago", english); } #[test] fn plus_30d() { let ht = HumanTime::from(Duration::days(30)); let english = format!("{}", ht); assert_eq!("in a month", english); } #[test] fn minus_30d() { let ht = HumanTime::from(Duration::days(-30)); let english = format!("{}", ht); assert_eq!("a month ago", english); } #[test] fn plus_45d() { let ht = HumanTime::from(Duration::days(45)); let english = format!("{}", ht); assert_eq!("in a month", english); } #[test] fn minus_45d() { let ht = HumanTime::from(Duration::days(-45)); let english = format!("{}", ht); assert_eq!("a month ago", english); } #[test] fn plus_46d() { let ht = HumanTime::from(Duration::days(46)); let english = format!("{}", ht); assert_eq!("in 2 months", english); } #[test] fn minus_46d() { let ht = HumanTime::from(Duration::days(-46)); let english = format!("{}", ht); assert_eq!("2 months ago", english); } #[test] fn plus_24w() { let ht = HumanTime::from(Duration::weeks(24)); let english = format!("{}", ht); assert_eq!("in 5 months", english); } #[test] fn minus_24w() { let ht = HumanTime::from(Duration::weeks(-24)); let english = format!("{}", ht); assert_eq!("5 months ago", english); } #[test] fn plus_26w() { let ht = HumanTime::from(Duration::weeks(26)); let english = format!("{}", ht); assert_eq!("in 6 months", english); } #[test] fn minus_26w() { let ht = HumanTime::from(Duration::weeks(-26)); let english = format!("{}", ht); assert_eq!("6 months ago", english); } #[test] fn plus_50w() { let ht = HumanTime::from(Duration::weeks(50)); let english = format!("{}", ht); assert_eq!("in a year", english); } #[test] fn minus_50w() { let ht = HumanTime::from(Duration::weeks(-50)); let english = format!("{}", ht); assert_eq!("a year ago", english); } #[test] fn plus_100w() { let ht = HumanTime::from(Duration::weeks(100)); let english = format!("{}", ht); assert_eq!("in 2 years", english); } #[test] fn minus_100w() { let ht = HumanTime::from(Duration::weeks(-100)); let english = format!("{}", ht); assert_eq!("2 years ago", english); } #[test] fn plus_120w() { let ht = HumanTime::from(Duration::weeks(120)); let english = format!("{}", ht); assert_eq!("in 2 years", english); } #[test] fn minus_120w() { let ht = HumanTime::from(Duration::weeks(-120)); let english = format!("{}", ht); assert_eq!("2 years ago", english); } #[test] fn plus_200w() { let ht = HumanTime::from(Duration::weeks(200)); let english = format!("{}", ht); assert_eq!("in 3 years", english); } #[test] fn minus_200w() { let ht = HumanTime::from(Duration::weeks(-200)); let english = format!("{}", ht); assert_eq!("3 years ago", english); } } #[cfg(test)] mod utc { use chrono::Utc; use chrono_humanize::HumanTime; #[test] fn now() { let ht = HumanTime::from(Utc::now()); let english = format!("{}", ht); assert_eq!("now", english); } } #[cfg(test)] mod local { use chrono::{Duration, Local}; use chrono_humanize::HumanTime; #[test] fn now() { let ht = HumanTime::from(Local::now()); let english = format!("{}", ht); assert_eq!("now", english); } #[test] fn minus_35d() { let past = Local::now() - Duration::days(35); let ht = HumanTime::from(past); let english = format!("{}", ht); assert_eq!("a month ago", english); } #[test] fn plus_35d() { let future = Local::now() + Duration::days(35); let ht = HumanTime::from(future); let english = format!("{}", ht); assert_eq!("in a month", english); } }