tparse

package module
v2.8.2 Latest Latest
Warning

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

Go to latest
Published: Jun 26, 2020 License: MIT Imports: 6 Imported by: 34

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func AbsoluteDuration added in v2.8.0

func AbsoluteDuration(base time.Time, duration string) (time.Duration, error)

AbsoluteDuration returns the time.Duration between the base time and the result of adding the duration string. This takes into account the number of days in the intervening months and years.

Example
t1 := time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC)

d1, err := AbsoluteDuration(t1, "1.5month")
if err != nil {
	fmt.Println(err)
	return
}

fmt.Println(d1)

t2 := time.Date(2020, time.February, 10, 23, 0, 0, 0, time.UTC)

d2, err := AbsoluteDuration(t2, "1.5month")
if err != nil {
	fmt.Println(err)
	return
}

fmt.Println(d2)
Output:

1080h0m0s
1056h0m0s

func AddDuration

func AddDuration(base time.Time, s string) (time.Time, error)

AddDuration parses the duration string, and adds the calculated duration value to the provided base time. On error, it returns the base time and the error.

Like `time.ParseDuration`, this accepts multiple fractional scalars, so "now+1.5days-3.21hours" is evaluated properly.

The following tokens may be used to specify the respective unit of time:

* Nanosecond: ns * Microsecond: us, µs (U+00B5 = micro symbol), μs (U+03BC = Greek letter mu) * Millisecond: ms * Second: s, sec, second, seconds * Minute: m, min, minute, minutes * Hour: h, hr, hour, hours * Day: d, day, days * Week: w, wk, week, weeks * Month: mo, mon, month, months * Year: y, yr, year, years

	package main

	import (
		"fmt"
		"os"
		"time"

		"github.com/karrick/tparse"
	)

	func main() {
             now := time.Now()
		another, err := tparse.AddDuration(now, "now+1d3w4mo-7y6h4m")
		if err != nil {
			fmt.Fprintf(os.Stderr, "error: %s\n", err)
			os.Exit(1)
		}

		fmt.Printf("time is: %s\n", another)
	}

func Parse

func Parse(layout, value string) (time.Time, error)

Parse will return the time value corresponding to the specified layout and value. It also parses floating point and integer epoch values.

func ParseNow

func ParseNow(layout, value string) (time.Time, error)

ParseNow will return the time value corresponding to the specified layout and value. It also parses floating point and integer epoch values. It recognizes the special string `now` and replaces that with the time ParseNow is called. This allows a suffix adding or subtracting various values from the base time. For instance, ParseNow(time.ANSIC, "now+1d") will return a time corresponding to 24 hours from the moment the function is invoked.

In addition to the duration abbreviations recognized by time.ParseDuration, it recognizes various tokens for days, weeks, months, and years.

package main

import (
	"fmt"
	"os"
	"time"

	"github.com/karrick/tparse"
)

func main() {
	actual, err := tparse.ParseNow(time.RFC3339, "now+1d3w4mo7y6h4m")
	if err != nil {
		fmt.Fprintf(os.Stderr, "error: %s\n", err)
		os.Exit(1)
	}

	fmt.Printf("time is: %s\n", actual)
}

func ParseWithMap

func ParseWithMap(layout, value string, dict map[string]time.Time) (time.Time, error)

ParseWithMap will return the time value corresponding to the specified layout and value. It also parses floating point and integer epoch values. It accepts a map of strings to time.Time values, and if the value string starts with one of the keys in the map, it replaces the string with the corresponding time.Time value.

package main

import (
    "fmt"
    "os"
    "time"
    "github.com/karrick/tparse"
)

func main() {
    m := make(map[string]time.Time)
    m["end"] = time.Now()

    start, err := tparse.ParseWithMap(time.RFC3339, "end-12h", m)
    if err != nil {
        fmt.Fprintf(os.Stderr, "error: %s\n", err)
        os.Exit(1)
    }

    fmt.Printf("start: %s; end: %s\n", start, end)
}

Types

This section is empty.

Jump to

Keyboard shortcuts

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