Help us shape the future of Go! Take the 2026 Go Developer Survey

gohumantime

package module
v0.0.0-...-eb30da7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 19, 2017 License: MIT Imports: 5 Imported by: 2

README

gohumantime

A human readable time translator in golang

GoDoc

Motivation

Use a simple human readable string representation of time Inspired by: https://github.com/rschmukler/human-interval

Usage:

import "github.com/urjitbhatia/gohumantime"
//...
millis, err := gohumantime.ToMilliseconds("1 day and 14 hours")

millis, err := gohumantime.ToMilliseconds("9981234314")
// If string is numeric, unit is assumed to be Milliseconds and a simple atoi conversion is returned.
//...

Benchmarks

  //3000 samples of ToMilliseconds("4 seconds, 2 minutes 1 hour and 3 days and 10weeks, 1 month and 1 year"):

  Ran 3000 samples:
  runtime:
    Fastest Time: 0.000s
    Slowest Time: 0.001s
    Average Time: 0.000s ± 0.000s

Documentation

Overview

Package gohumantime is a Human readable time interval parser for golang

import "github.com/urjitbhatia/gohumantime"

...

ToMilliseconds("ten minutes and four seconds")
ToMilliseconds('one minute')
ToMilliseconds('2.3 minutes')
ToMilliseconds('3 days, 4 hours and 36 seconds')

Supported Units

The following time unit words are supported

  • `seconds`
  • `minutes`
  • `hours`
  • `days`
  • `weeks`
  • `months` -- assumes 30 days
  • `years` -- assumes 365 days

Numeric word representations

gohumantime supports numbers up to ten being written out in English. Beyond then, it understands:

`fifteen`

and then every tens unit from

'twenty', 'thirty'... onto 'hundred'

Index

Constants

View Source
const (
	Second = 1000
	Minute = Second * 60
	Hour   = Minute * 60
	Day    = Hour * 24
	Week   = Day * 7
	Month  = Day * 30
	Year   = Day * 365
)

Export Unit times in in milliseconds (easier testing)

Variables

This section is empty.

Functions

func ToMilliseconds

func ToMilliseconds(humanReadableTime string) (int, error)

ToMilliseconds converts a human readable interval string into milliseconds. Example: ToMilliseconds("three minutes and five seconds") returns 3 * 60 * 1000 + 5 * 1000

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL