goats

package module
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Jan 19, 2023 License: MIT Imports: 7 Imported by: 0

README

Go Alternative Time Strings (goats)

Build Issues Pull Requests Go Doc License

A Goat eating string wrapped around a somewhat alternative clock

Go Alternative Time Strings provides mappings between other time formats, and the Go time format. The library will attempt to determine what format the time string is in based on the number of matching format patterns.

For example, consider the following Go and Java format patterns:

goats.Translate("2006-01-02T15:04:05")
goats.Translate("yyyy-MM-dd'T'HH:mm:ss"")

Both yield a format string of 2006-01-02T15:04:05.

New formatters can be registered with Go Alternative Time Strings allowing arbitrary time formatting strings to be used.

Attribution

Goats logo by ACID.

The goats image is Copyright (c) 2021 Dom Davis and may only be used in relation to this library.

Documentation

Overview

Package goats requires a description.

Example
package main

import (
	"fmt"
	"time"

	"bitbucket.org/idomdavis/goats"
)

func main() {
	t := time.Date(2015, time.March, 12, 15, 6, 0, 0, time.UTC)

	fmt.Println(t.Format(goats.Translate("2006-01-02T15:04:05")))
	fmt.Println(t.Format(goats.Translate("Mon, 2 Jan 2006 15:04:05 MST")))
	fmt.Println(t.Format(goats.Translate("yyyy-MM-dd'T'HH:mm:ss")))
	fmt.Println(t.Format(goats.Translate("EEE, d MMM yyyy HH:mm:ss z")))

}
Output:

2015-03-12T15:06:00
Thu, 12 Mar 2015 15:06:00 UTC
2015-03-12T15:06:00
Thu, 12 Mar 2015 15:06:00 UTC

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Parse added in v0.1.1

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

Parse a date using the given layout string. The layout string will be translated if possible and then the Go version of the string passed to time.Parse. If the layout is "days" or "excel" then Parse will take this as days since 1900.

Example
package main

import (
	"fmt"

	"bitbucket.org/idomdavis/goats"
)

func main() {
	fmt.Println(goats.Parse("1900", "44619"))
	fmt.Println(goats.Parse("excel", "40729"))
	fmt.Println(goats.Parse("excel", "40729.74653"))
	fmt.Println(goats.Parse("1904", "39267"))
	fmt.Println(goats.Parse("yyyy/MM/dd z", "2022/02/14 UTC"))

}
Output:

2022-02-27 00:00:00 +0000 UTC <nil>
2011-07-05 00:00:00 +0000 UTC <nil>
2011-07-05 00:00:00 +0000 UTC <nil>
2011-07-05 00:00:00 +0000 UTC <nil>
2022-02-14 00:00:00 +0000 UTC <nil>

func Register

func Register(translator Translator)

Register a translator with goats.

Example
package main

import (
	"fmt"
	"math"

	"bitbucket.org/idomdavis/goats"
)

func main() {
	fmt.Printf("%q\n", goats.Translate(""))

	goats.Register(func(input string) *goats.Translation {
		return &goats.Translation{Format: "translated", Confidence: math.MaxInt64}
	})

	fmt.Printf("%q\n", goats.Translate(""))

	goats.Reset()

}
Output:

""
"translated"

func Reset

func Reset()

Reset the translator list to the default.

func Translate

func Translate(input string) string

Translate a string using the highest matching translator. If no translators provide a match then the string is returned as is.

Types

type Translation

type Translation struct {
	// Confidence in the translation. Confidence is set to the number of
	// matching formatting patterns that were found.
	Confidence int

	// Format string for the translation.
	Format string
	// contains filtered or unexported fields
}

Translation of a time format.

func Go

func Go(source string) *Translation

Go translator does nothing to the source string, but set a confidence based on the number of matches to the Go time format string in the source.

func Java

func Java(source string) *Translation

Java will provide a Translation from Java date time format. Both DD and D will produce space padded day of year. Both HH and H will produce a zero padded 24 hour representation of the hour. All zero padded patterns are reduced to the shorted possible number of zeros, except for S which is 0 padded to the number of S's provided. z is a standard timezone (MST), Z returns a numeric timezone (-0700) and z returns a numeric timezone with a colon (-07:00).

func (*Translation) Match

func (t *Translation) Match(s string) bool

Match the given string and update the Confidence with the number of matches. Returns true if any matches were found, false otherwise.

Example
package main

import (
	"fmt"

	"bitbucket.org/idomdavis/goats"
)

func main() {
	format := "'Time' HH:mm"

	t := &goats.Translation{Format: format}
	t.Match("HH")

	fmt.Println(t.Confidence, t.Format)

}
Output:

1 'Time' HH:mm

func (*Translation) Rebuild

func (t *Translation) Rebuild(strip *regexp.Regexp)

Rebuild the format with the literals stripped with Strip. The literals have any string that matches string replaced with the empty string before being added back into the format.

Example
package main

import (
	"fmt"
	"regexp"

	"bitbucket.org/idomdavis/goats"
)

func main() {
	format := "'Time' HH:mm"

	t := &goats.Translation{Format: format}
	t.Strip(regexp.MustCompile(`'(?:[^']|'')*'`), "@")
	t.Rebuild(regexp.MustCompile(`^'|'$`))

	fmt.Println(t.Format)

}
Output:

Time HH:mm

func (*Translation) Replace

func (t *Translation) Replace(source, target string) bool

Replace source with target and update the Confidence with the number of matches. Returns true if any replacements were made, false otherwise.

Example
package main

import (
	"fmt"

	"bitbucket.org/idomdavis/goats"
)

func main() {
	format := "'Time' HH:mm"

	t := &goats.Translation{Format: format}
	t.Replace("'Time' ", "")

	fmt.Println(t.Confidence, t.Format)

}
Output:

1 HH:mm

func (*Translation) Strip

func (t *Translation) Strip(literal *regexp.Regexp, placeholder string)

Strip and store literals from the format string. This allows Update, Match Replace to ignore string literals during translation. Use Rebuild to return the literals to the Format.

Example
package main

import (
	"fmt"
	"regexp"

	"bitbucket.org/idomdavis/goats"
)

func main() {
	format := "'Time' HH:mm"

	t := &goats.Translation{Format: format}
	t.Strip(regexp.MustCompile(`'(?:[^']|'')*'`), "@")

	fmt.Println(t.Format)

}
Output:

@ HH:mm

func (*Translation) Update

func (t *Translation) Update(find *regexp.Regexp, replace string)

Update the Format by finding and replacing all instances of the find regexp with replace. The Confidence is not affected by Update.

Example
package main

import (
	"fmt"
	"regexp"

	"bitbucket.org/idomdavis/goats"
)

func main() {
	format := "'Time' HH:mm"

	t := &goats.Translation{Format: format}
	t.Update(regexp.MustCompile(`'.*' `), "")

	fmt.Println(t.Confidence, t.Format)

}
Output:

0 HH:mm

type Translator

type Translator func(source string) *Translation

Translator function that will take a source time format and return a Translation.

Jump to

Keyboard shortcuts

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