GithubHelp home page GithubHelp logo

go-timeparser's Introduction

go-timeparser

Flexible Time Parser for Golang

Installation

Download timeparser with the following command:

$ go get -u github.com/kaz-yamam0t0/go-timeparser/timeparser

Then import this library in your source code:

import "github.com/kaz-yamam0t0/go-timeparser/timeparser"

Usage

Simply Parse or Format time.Time variable

// 
// time.Time vars to formatted strings
// 
tm := time.Now()
fmt.Println(timeparser.FormatTime("r", &tm))                     // Wed, 29 Dec 2021 18:24:00 +0900
fmt.Println(timeparser.FormatTime("l jS \\of F Y h:i:s A", &tm)) // Wednesday 29th of December 2021 06:24:00 PM
fmt.Println(timeparser.FormatTime("Y-m-d H:i:s", &tm))           // 2021-12-29 18:24:00

// 
// parse strings into time.Time vars
// 
tm, err := timeparser.ParseFormat(" l jS \\of F Y  h:i:s A", "Wednesday 29th of December  2021 06:24:12 PM ")
if err != nil {
	panic(err)
}
fmt.Println(tm)

// 
// parse strings more flexibly
// 
tm, err := timeparser.ParseTimeStr("Wed, 29 Dec 2021 18:24:00 +0900", nil)
tm, err := timeparser.ParseTimeStr("2021-12-29T18:24:00+09:00", nil)
tm, err := timeparser.ParseTimeStr("2021-12-29T18:24:00Z", nil)
tm, err := timeparser.ParseTimeStr("Wednesday 29th December 2021 06:24:00 PM", nil)

// 
// relative format
// 
tm := time.Now()
tm, err := timeparser.ParseTimeStr("1 year ago", &tm)
tm, err := timeparser.ParseTimeStr("+1 day 2 week 3 months -4 years 5 hours -6 minutes 7 seconds", &tm)
tm, err := timeparser.ParseTimeStr("P1Y2M3DT4H5M6.123456S", &tm) // ISO 8601 Interval Format

tm, err := timeparser.ParseTimeStr("next Thursday", &tm)
tm, err := timeparser.ParseTimeStr("last year", &tm)

TimeData

TimeData is a simple and flexible struct used in timeparser.

// tdata, _ := timeparser.Now()
// tdata, _ := timeparser.New("2022-01-31 11:22:33.123456789")
tdata, _ := timeparser.NewAsUTC("2022-01-31 11:22:33.123456789")

// 
// If you want to adjust the timezone offset difference from your local env, 
// first create a variable with `timeparser.New()` and then convert it to UTC with `SetUTC()`
// 
// tdata, _ := timeparser.New("2022-01-31 11:22:33.123456789")
// tdata.SetUTC()

fmt.Println(tdata.GetYear())        // 2022
fmt.Println(tdata.GetMonth())       // 1
fmt.Println(tdata.GetDay())         // 31
fmt.Println(tdata.GetHour())        // 11
fmt.Println(tdata.GetMinute())      // 22
fmt.Println(tdata.GetSecond())      // 33
fmt.Println(tdata.GetMillisecond()) // 123
fmt.Println(tdata.GetMicrosecond()) // 123456
fmt.Println(tdata.GetNanosecond())  // 123456789

// 
// Format
// 
fmt.Println(tdata.String()) // 2022-01-31T11:22:33+00:00
fmt.Println(tdata.Format("l jS \\of F Y h:i:s A")) // Monday 31st of January 2022 06:22:33 PM

// 
// Setter methods
// 
tdata.SetYear(2022)
tdata.SetMonth(1)
tdata.SetDay(31)
tdata.SetHour(11)
tdata.SetMinute(22)
tdata.SetSecond(33)

// 
// Difference 
// 
tm, _ := timeparser.NewAsUTC("2020-12-31 11:22:33.123456789")
fmt.Println(tdata.DiffYears(tm))   // 1
fmt.Println(tdata.DiffMonths(tm))  // 13
fmt.Println(tdata.DiffDays(tm))    // 396
fmt.Println(tdata.DiffHours(tm))   // 9504
fmt.Println(tdata.DiffMinutes(tm)) // 570240
fmt.Println(tdata.DiffSeconds(tm)) // 34214400

Documentation

go doc style documentation:

https://pkg.go.dev/github.com/kaz-yamam0t0/go-timeparser/timeparser

Most format characters are based on PHP datetime related functions.

https://www.php.net/manual/en/datetime.createfromformat.php

go-timeparser's People

Contributors

kaz-yamam0t0 avatar

Stargazers

Xie Zhenye avatar  avatar

Watchers

 avatar

go-timeparser's Issues

RFC3339 format not handled

Nice library! I found that datetimes formatted like this are not recognized:
2006-01-02T15:04:05Z
2006-01-02T15:04:05Z07:00

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.