parsetime

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Dec 3, 2023 License: MIT Imports: 2 Imported by: 0

README

Overview

ParseTime is a Go package dedicated to quickly parse RFC3339 or similar time strings.

The time.Parse in Go stdlib is fast enough in most cases, but not when processing massive time series data. The time string generated in many languages is not the standard RFC3339, but a variant of it. When it is not a standard RFC3339 string (e.g. 2006-01-02T15:04:05.999), or multiple uncertain time formats need to be tried, the resources consumed by the parsing time are very considerable.

// parsetime
BenchmarkRFC3339NanoBytes    	257910897	        23.21 ns/op
BenchmarkMultiFormat         	219147885	        27.09 ns/op
BenchmarkRFC3339Nano         	222247737	        27.01 ns/op
BenchmarkRFC3339             	253692507	        23.56 ns/op
BenchmarkDateTime            	346593026	        17.33 ns/op
BenchmarkDateOnly            	470903756	        12.57 ns/op
BenchmarkNonStandardFormat   	221962552	        27.08 ns/op
BenchmarkRFC3339InLocation   	223855030	        26.70 ns/op

// stdlib
BenchmarkGoMultiFormat       	 7471774	       799.0 ns/op
BenchmarkGoRFC3339Nano       	83486947	        69.18 ns/op
BenchmarkGoRFC3339           	100000000	        51.62 ns/op
BenchmarkGoDateTime          	42266613	       140.2 ns/op
BenchmarkGoDateOnly          	70607433	        83.01 ns/op
BenchmarkGoNonStandardFormat 	25277586	       240.5 ns/op
BenchmarkGoRFC3339InLocation 	100000000	        51.40 ns/op

Getting Started

Installation

go get github.com/richardliao/parsetime

Usage

package main

import (
	"github.com/richardliao/parsetime"
	"time"
)

func main() {
	// Parse the time to local.
	parsetime.Parse("2006-01-02T15:04:05.999999999+08:00")

	// Parse the time to the specified location.
	loc := time.UTC
	_, locOffset := time.Now().In(loc).Zone()
	parsetime.ParseInLocation("2006-01-02 15:04:05.999", loc, locOffset)
}

Supported Format

ParseTime supports the following formats, as well as their various variants.

2006-01-02T15:04:05.999999999+08:00
2006-01-02T15:04:05.999999+08:00
2006-01-02T15:04:05.999+08:00
2006-01-02T15:04:05,999+08:00
2006-01-02T15:04:05+08:00
2006-01-02T15:04:05+0800
2006-01-02T15:04:05+08
2006-01-02T15:04:05Z
2006-01-02T15:04:05z
2006-01-02T15:04:05
2006-01-02 15:04:05
2006-01-02

Documentation

Overview

Example
package main

import (
	"fmt"
	"github.com/richardliao/parsetime"
)

func main() {
	fmt.Println(parsetime.Parse("2006-01-02T15:04:05.999999999+08:00"))
	fmt.Println(parsetime.Parse("2006-01-02T15:04:05.999999+08:00"))
	fmt.Println(parsetime.Parse("2006-01-02T15:04:05.999+08:00"))
	fmt.Println(parsetime.Parse("2006-01-02T15:04:05+08:00"))
	fmt.Println(parsetime.Parse("2006-01-02T15:04:05+0800"))
	fmt.Println(parsetime.Parse("2006-01-02T15:04:05Z"))
	fmt.Println(parsetime.Parse("2006-01-02T15:04:05"))
	fmt.Println(parsetime.Parse("2006-01-02 15:04:05"))
	fmt.Println(parsetime.Parse("2006-01-02"))

}
Output:

2006-01-02 15:04:05.999999999 +0800 CST <nil>
2006-01-02 15:04:05.999999 +0800 CST <nil>
2006-01-02 15:04:05.999 +0800 CST <nil>
2006-01-02 15:04:05 +0800 CST <nil>
2006-01-02 15:04:05 +0800 CST <nil>
2006-01-02 23:04:05 +0800 CST <nil>
2006-01-02 23:04:05 +0800 CST <nil>
2006-01-02 23:04:05 +0800 CST <nil>
2006-01-02 08:00:00 +0800 CST <nil>

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Parse

func Parse(s string) (time.Time, error)

Parse is like time.Parse.

The result is the Local location. In the absence of a time zone information, Parse interprets the time as in UTC.

func ParseBytes

func ParseBytes(s []byte) (time.Time, error)

ParseBytes is like time.Parse but accepting bytes with better performance of about 4 ns.

func ParseBytesInLocation

func ParseBytesInLocation(s []byte, loc *time.Location, locOffset int) (time.Time, error)

ParseBytesInLocation is like time.ParseInLocation but accepting bytes with better performance of about 4 ns.

func ParseInLocation

func ParseInLocation(s string, loc *time.Location, locOffset int) (time.Time, error)

ParseInLocation is like time.ParseInLocation.

The result is the given location. In the absence of time zone information, ParseInLocation interprets the time as in the given location.

The parameter locOffset is added for performance, and its value is the offset corresponding to loc.

For example.

_, locOffset := time.Now().In(loc).Zone()

Types

This section is empty.

Jump to

Keyboard shortcuts

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