Giter Site home page Giter Site logo

marcelohdez / hrtime Goto Github PK

View Code? Open in Web Editor NEW
3.0 1.0 0.0 13 KB

A thin Rust library which converts seconds into a string of colon-separated time or vice versa.

Home Page: https://crates.io/crates/hrtime

License: MIT License

Rust 100.00%
time rust library human-readable human readable

hrtime's Introduction

hrtime

Human-Readable Time, stylized as hrtime, is a thin Rust library which converts seconds into either a colon-seperated time string and vice versa, or into the raw hour, minute, and second values.

Why?

Seemed like a simple library to make as I am learning Rust and thought it may prove useful to someone. As you can see by the versioning (0.2.0) it is currently in very early stages.

How?

From seconds

This crate only contains four functions, two "from" functions: from_sec and from_sec_padded, and two "to" functions: to_sec and to_time. The two "from" functions will convert a given u64 into a colon-separated time string, with from_sec_padded specifically introducing leading zeroes to reach the format "00:00:00" (HH:MM:SS). An example using from_sec:

let secs = 234;
println!("{secs} seconds is {}", hrtime::from_sec(secs));

Will print "234 seconds is 3:54",

and the same example but using from_sec_padded:

let secs = 234;
println!("{secs} seconds is {}", hrtime::from_sec_padded(secs));

Will print "234 seconds is 00:03:54"!

To seconds

The first "to" function is to_sec, which takes in a time string as an argument and attempts to convert it into the amount of seconds it represents. This string has some requirements to meet in order to work though, like having the values separated by colons (ex: "1:38" for a minute and 38 seconds) and there being no more than three colons (ex: "1:23:14:38" will panic). to_sec may be used like so:

let time = "10:50";
let secs = hrtime::to_sec(time);
println!("{time} is {secs} seconds");

Which prints "10:50 is 650 seconds"!

To time

The second "to" function is to_time, which returns the raw hour, minute, and second values represented by the given u64 respectively. This is returned as a tuple containing a u64 (hour), and two u8s (minute and seconds) more information can be seen in its documentation. to_time may be used like so:

let seconds = 650;
let (hrs, min, sec) = hrtime::to_time(seconds);
println!("{seconds} seconds is {hrs}h{min}m{sec}s");

Which prints "650 seconds is 0h10m50s"!

More

More examples can be seen in the examples folder.

License

hrtime is licensed under the MIT license, for more information please read the LICENSE file.

hrtime's People

Contributors

marcelohdez avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.