All Projects → karrick → tparse

karrick / tparse

Licence: MIT license
time parsing library for Go; supports more time units than standard library

Programming Languages

go
31211 projects - #10 most used programming language

Projects that are alternatives of or similar to tparse

mpq
Decoder/parser of Blizzard's MPQ archive file format
Stars: ✭ 28 (-33.33%)
Mutual labels:  parse
snapdragon-lexer
Converts a string into an array of tokens, with useful methods for looking ahead and behind, capturing, matching, et cetera.
Stars: ✭ 19 (-54.76%)
Mutual labels:  parse
postcss-styl
PostCSS parser plugin for converting Stylus syntax to PostCSS AST.
Stars: ✭ 15 (-64.29%)
Mutual labels:  parse
pixl-xml
A simple module for parsing and composing XML.
Stars: ✭ 78 (+85.71%)
Mutual labels:  parse
exjson
JSON parser and genarator in Elixir.
Stars: ✭ 71 (+69.05%)
Mutual labels:  parse
parse-author
Parse a person, author, contributor or maintainer string into an object with name, email and url properties following NPM conventions. Useful for the `authors` property in package.json or for parsing an AUTHORS file into an array of person objects.
Stars: ✭ 23 (-45.24%)
Mutual labels:  parse
lilt
LILT: noun, A characteristic rising and falling of the voice when speaking; a pleasant gentle accent.
Stars: ✭ 18 (-57.14%)
Mutual labels:  parse
es6-template-regex
Regular expression for matching es6 template delimiters in a string.
Stars: ✭ 15 (-64.29%)
Mutual labels:  parse
sitemapper
parses sitemaps for Node.JS
Stars: ✭ 70 (+66.67%)
Mutual labels:  parse
pixie
Tiny template functions.
Stars: ✭ 14 (-66.67%)
Mutual labels:  parse
parse-server-test-runner
A tool for programmatically starting Parse Server
Stars: ✭ 18 (-57.14%)
Mutual labels:  parse
read-env
🔧 Transform environment variables into JSON object with sanitized values.
Stars: ✭ 60 (+42.86%)
Mutual labels:  parse
parse-git-config
Parse `.git/config` into a JavaScript object. sync or async.
Stars: ✭ 55 (+30.95%)
Mutual labels:  parse
logparser
Easy parsing of Apache HTTPD and NGINX access logs with Java, Hadoop, Hive, Pig, Flink, Beam, Storm, Drill, ...
Stars: ✭ 139 (+230.95%)
Mutual labels:  parse
extract-domain
Extract domain name from an URL
Stars: ✭ 22 (-47.62%)
Mutual labels:  parse
PyScholar
A 'supervised' parser for Google Scholar
Stars: ✭ 74 (+76.19%)
Mutual labels:  parse
flex-bison-indentation
An example of how to correctly parse python-like indentation-scoped files using flex (and bison).
Stars: ✭ 32 (-23.81%)
Mutual labels:  parse
node-red-contrib-string
Provides a string manipulation node with a chainable UI based on the concise and lightweight stringjs.com.
Stars: ✭ 15 (-64.29%)
Mutual labels:  parse
parse-react
[EXPERIMENTAL] React, React Native, and React with SSR (e.g. Next.js) packages to interact with Parse Server backend
Stars: ✭ 58 (+38.1%)
Mutual labels:  parse
SwiftDomainParser
A Full Swift Lightweight Framework that uses the Public Suffix list to Parse URLs
Stars: ✭ 48 (+14.29%)
Mutual labels:  parse

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, tparse recognizes various tokens for days, weeks, months, and years, as well as tokens for seconds, minutes, and hours.

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

Documentation

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

Examples

ParseNow

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"
        "github.com/karrick/tparse"
    )

    func main() {
        actual, err := tparse.ParseNow(time.RFC3339, "now+1d-3w4mo+7y6h4m")
        if err != nil {
            fmt.Fprintf(os.Stderr, "error: %s\n", err)
            os.Exit(1)
        }
        fmt.Printf("time is: %s\n", actual)
    }

ParseWithMap

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

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

AddDuration

AddDuration is used to compute the value of a duration string and add it to a known time. This function is used by the other library functions to parse all duration strings.

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, "+1d3w4mo-7y6h4m")
        if err != nil {
            fmt.Fprintf(os.Stderr, "error: %s\n", err)
            os.Exit(1)
        }

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

AbsoluteDuration

When you would rather have the time.Duration representation of a duration string, there is a function for that, but with a caveat.

First, not every month has 30 days, and therefore Go does not have a time.Duration type constant to represent one month.

When I add one month to February 3, do I get March 3 or March 4? Depends on what the year is and whether or not that year is a leap year.

Is one month always 30 days? Is one month 31 days, or 28, or 29? I did not want to have to answer this question, so I defaulted to saying the length of one month depends on which month and year, and I allowed the Go standard library to add duration concretely to a given moment in time.

Consider the below two examples of calling AbsoluteDuration with the same duration string, but different base times.

func ExampleAbsoluteDuration() {
    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
}

Benchmark against goparsetime

GO111MODULE=on go test -bench=. -tags goparsetime
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].