All Projects → hako → Durafmt

hako / Durafmt

Licence: mit
🕗 Better time duration formatting in Go!

Programming Languages

go
31211 projects - #10 most used programming language
golang
3204 projects

Projects that are alternatives of or similar to Durafmt

Moment.php
Parse, validate, manipulate, and display dates in PHP w/ i18n support. Inspired by moment.js
Stars: ✭ 900 (+148.62%)
Mutual labels:  time, formatter
Humanizeduration.js
361000 becomes "6 minutes, 1 second"
Stars: ✭ 1,234 (+240.88%)
Mutual labels:  time, duration
Translatedjs
Internationalization and localization for JavaScript and Node.js
Stars: ✭ 17 (-95.3%)
Mutual labels:  time, formatter
Ruby Duration
Immutable type that represents some amount of time with accuracy in seconds.
Stars: ✭ 122 (-66.3%)
Mutual labels:  time, duration
Swift-ISO8601-DurationParser
Swift ISO8601 Parser
Stars: ✭ 24 (-93.37%)
Mutual labels:  duration, time
vue-translated
Internationalization (i18n) and localization (l10n) library for Vue.js v2.
Stars: ✭ 19 (-94.75%)
Mutual labels:  time, formatter
Time
Windows tool for measuring command/program execution speed
Stars: ✭ 21 (-94.2%)
Mutual labels:  time, duration
As Duration
Extraction of ActiveSupport::Duration from Rails
Stars: ✭ 126 (-65.19%)
Mutual labels:  time, duration
lit-date
Light-weight, faster datetime formatter for modern browsers.
Stars: ✭ 33 (-90.88%)
Mutual labels:  time, formatter
duration-humanizer
361000 becomes "6 minutes, 1 second"
Stars: ✭ 61 (-83.15%)
Mutual labels:  duration, time
Time.dart
⏰ Type-safe DateTime and Duration calculations, powered by extensions.
Stars: ✭ 363 (+0.28%)
Mutual labels:  time, duration
Ntp
a simple ntp client package for go
Stars: ✭ 339 (-6.35%)
Mutual labels:  time
Prettier Eslint
Code ➡️ prettier ➡️ eslint --fix ➡️ Formatted Code ✨
Stars: ✭ 3,435 (+848.9%)
Mutual labels:  formatter
M
Stars: ✭ 313 (-13.54%)
Mutual labels:  time
Anyformatkit
Simple text formatting in Swift
Stars: ✭ 296 (-18.23%)
Mutual labels:  formatter
Rustfmt
Format Rust code
Stars: ✭ 4,049 (+1018.51%)
Mutual labels:  formatter
Time
Simple time handling in Rust
Stars: ✭ 334 (-7.73%)
Mutual labels:  time
Timestamp
⏰ A better macOS menu bar clock.
Stars: ✭ 296 (-18.23%)
Mutual labels:  time
React Datetime Picker
A datetime picker for your React app.
Stars: ✭ 294 (-18.78%)
Mutual labels:  time
Timecop
A gem providing "time travel", "time freezing", and "time acceleration" capabilities, making it simple to test time-dependent code. It provides a unified method to mock Time.now, Date.today, and DateTime.now in a single call.
Stars: ✭ 3,110 (+759.12%)
Mutual labels:  time

durafmt

Build Status Go Report Card codecov GoDoc Open Source Helpers

durafmt is a tiny Go library that formats time.Duration strings (and types) into a human readable format.

go get github.com/hako/durafmt

Why

If you've worked with time.Duration in Go, you most likely have come across this:

53m28.587093086s // :)

The above seems very easy to read, unless your duration looks like this:

354h22m3.24s // :S

Usage

durafmt.ParseString()

package main

import (
	"fmt"
	"github.com/hako/durafmt"
)

func main() {
	duration, err := durafmt.ParseString("354h22m3.24s")
	if err != nil {
		fmt.Println(err)
	}
	fmt.Println(duration) // 2 weeks 18 hours 22 minutes 3 seconds
	// duration.String() // String representation. "2 weeks 18 hours 22 minutes 3 seconds"
}

durafmt.ParseStringShort()

Version of durafmt.ParseString() that only returns the first part of the duration string.

package main

import (
	"fmt"
	"github.com/hako/durafmt"
)

func main() {
	duration, err := durafmt.ParseStringShort("354h22m3.24s")
	if err != nil {
		fmt.Println(err)
	}
	fmt.Println(duration) // 2 weeks
	// duration.String() // String short representation. "2 weeks"
}

durafmt.Parse()

package main

import (
	"fmt"
	"time"
	"github.com/hako/durafmt"
)

func main() {
	timeduration := (354 * time.Hour) + (22 * time.Minute) + (3 * time.Second)
	duration := durafmt.Parse(timeduration).String()
	fmt.Println(duration) // 2 weeks 18 hours 22 minutes 3 seconds
}

LimitFirstN()

Like durafmt.ParseStringShort() but for limiting the first N parts of the duration string.

package main

import (
	"fmt"
	"time"
	"github.com/hako/durafmt"
)

func main() {
	timeduration := (354 * time.Hour) + (22 * time.Minute) + (3 * time.Second)
	duration := durafmt.Parse(timeduration).LimitFirstN(2) // // limit first two parts.
	fmt.Println(duration) // 2 weeks 18 hours
}

Custom Units

Like durafmt.Units{} and durafmt.Durafmt.Format(units) to stringify duration with custom units.

package main

import (
	"fmt"
	"time"
	"github.com/hako/durafmt"
)

func main() {
	timeduration := (354 * time.Hour) + (22 * time.Minute) + (1 * time.Second) + (100*time.Microsecond)
	duration := durafmt.Parse(timeduration)
	// units in portuguese
	units, err := durafmt.DefaultUnitsCoder.Decode("ano,semana,dia,hora,minuto,segundo,milissegundo,microssegundo")
	if err != nil {
		panic(err)
	}
	fmt.Println(duration.Format(units)) // 2 semanas 18 horas 22 minutos 1 segundo 100 microssegundos

    // custom plural (singular:plural)
	units, err = durafmt.DefaultUnitsCoder.Decode("ano,semana:SEMANAS,dia,hora,minuto,segundo,milissegundo,microssegundo")
	if err != nil {
		panic(err)
	}
	fmt.Println(duration.Format(units)) // 2 SEMANAS 18 horas 22 minutos 1 segundo 100 microssegundos
}

Contributing

Contributions are welcome! Fork this repo, add your changes and submit a PR.

If you would like to fix a bug, add a feature or provide feedback you can do so in the issues section.

durafmt is tested against golangci-lint and you can run tests with go test.

When contributing, running go test; go vet; golint or golangci-lint is recommended.

License

MIT

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].