tparse

package module
v2.1.1+incompatible Latest Latest
Warning

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

Go to latest
Published: Mar 16, 2016 License: MIT Imports: 6 Imported by: 12

README

tparse

Parse will return the time corresponding to the layout and value. It also parses floating point epoch values, and values of "now", "now+DURATION", and "now-DURATION".

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

Documentation

In addition to this handy README.md file, documentation is available in godoc format at GoDoc.

Examples

ParseNow can parse time values that are relative to the current time, by specifying a string starting with "now", a '+' or '-' byte, followed by a time duration.

    package main

    import (
        "fmt"
        "os"
        "time"
        tparse "gopkg.in/karrick/tparse.v2"
    )

    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)
    }

ParseWithMap can parse time values that use a base time other than "now".

    package main

    import (
        "fmt"
        "os"
        "time"
        tparse "gopkg.in/karrick/tparse.v2"
    )

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

        m := make(map[string]time.Time)
        m["start"] = start

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

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

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

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"

	tparse "gopkg.in/karrick/tparse.v2"
)

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"

	tparse "gopkg.in/karrick/tparse.v2"
)

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

	end, err := tparse.ParseWithMap(time.RFC3339, "start+8h", 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