Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

[Feature Request] String representation always in ms #2

Open
chris-zen opened this issue Aug 4, 2020 · 2 comments
Open

[Feature Request] String representation always in ms #2

chris-zen opened this issue Aug 4, 2020 · 2 comments
Labels
help wanted Extra attention is needed invalid This doesn't seem right

Comments

@chris-zen
Copy link

Hi, I realized that if the number of miliseconds is not an exact multiple of seconds, minutes, ..., then it will end generating an string with the default ms. For example, the following test would fail:

    #[test]
    fn test_display_trait_2() {
        let d = DurationString::try_from(Duration::from_millis(1100));
        assert_eq!("1s", format!("{}", d.unwrap()));
    }

The fix for that would pass through replacing these lines:

        if ms % DAY_IN_MILLI == 0 {
            return (ms / DAY_IN_MILLI).to_string() + "d";
        }

by

        if ms >= DAY_IN_MILLI {
            return (ms / DAY_IN_MILLI).to_string() + "d";
        }
@RonniSkansing
Copy link
Owner

RonniSkansing commented May 25, 2022

Hi, sorry about the 2 years later reply.

The test

   #[test]
    fn test_display_trait_2() {
        let d = DurationString::try_from(Duration::from_millis(1100));
        assert_eq!("1s", format!("{}", d.unwrap()));
    }

Should return 1100ms and not 1s. Did you expect it to round to the nearest unit or perhaps 1s 100ms?

The current behavior is:
1000ms => 1s
1100ms => 1100ms
11000ms => 11s
11100ms => 11100ms

@RonniSkansing RonniSkansing added the help wanted Extra attention is needed label Mar 28, 2023
@RonniSkansing
Copy link
Owner

RonniSkansing commented Mar 28, 2023

This issue, is actually not a issue. But

I think it would be nice to implement a feature that would reverse the units in the following way.

From a duration to string

1000ms => 1s
1100ms => 1s 100ms
62m => 1h 2m
x => 1h 2m 3s 4ms

It would be nice to somehow be able to say what the smallest unit should be like, minutes or seconds and having it automatically round to the nearest or even more config that allows the consumer to decide if its round up, down or discard lesser units.

@RonniSkansing RonniSkansing changed the title String representation always in ms [Feature Request] String representation always in ms May 8, 2023
@RonniSkansing RonniSkansing added the invalid This doesn't seem right label May 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed invalid This doesn't seem right
Projects
None yet
Development

No branches or pull requests

2 participants