Logo

0x3d.Site

is designed for aggregating information.
Welcome
check repository here

timeutil - useful extensions to the golang's time package

Build Status Coverage Status GoDoc

timeutil provides useful extensions (Timedelta, Strftime, ...) to the golang's time package.

Quick Start

go get github.com/leekchan/timeutil

example.go

package main

import (
    "fmt"
    "time"

    "github.com/leekchan/timeutil"
)

func main() {
    // Timedelta
    // A basic usage.
    base := time.Date(2015, 2, 3, 0, 0, 0, 0, time.UTC)
    td := timeutil.Timedelta{Days: 10, Minutes: 17, Seconds: 56}

    result := base.Add(td.Duration())
    fmt.Println(result) // "2015-02-13 00:17:56 +0000 UTC"

    // Operation : Add
    base = time.Date(2015, 2, 3, 0, 0, 0, 0, time.UTC)

    td = timeutil.Timedelta{Days: 1, Minutes: 1, Seconds: 1}
    td2 := timeutil.Timedelta{Days: 2, Minutes: 2, Seconds: 2}
    td = td.Add(&td2) // td = td + td2

    result = base.Add(td.Duration())
    fmt.Println(result) // "2015-02-06 00:03:03 +0000 UTC"

    // Operation : Subtract
    base = time.Date(2015, 2, 3, 0, 0, 0, 0, time.UTC)

    td = timeutil.Timedelta{Days: 2, Minutes: 2, Seconds: 2}
    td2 = timeutil.Timedelta{Days: 1, Minutes: 1, Seconds: 1}
    td = td.Subtract(&td2) // td = td - td2

    result = base.Add(td.Duration())
    fmt.Println(result) // "2015-02-04 00:01:01 +0000 UTC"

    // Operation : Abs
    base = time.Date(2015, 2, 3, 0, 0, 0, 0, time.UTC)

    td = timeutil.Timedelta{Days: 1, Minutes: 1, Seconds: 1}
    td2 = timeutil.Timedelta{Days: 2, Minutes: 2, Seconds: 2}
    td = td.Subtract(&td2) // td = td - td2
    td = td.Abs()          // td = |td|

    result = base.Add(td.Duration())
    fmt.Println(result) // "2015-02-04 00:01:01 +0000 UTC"
    
    
    // Strftime
    date := time.Date(2015, 7, 2, 15, 24, 30, 35, time.UTC)
    str := timeutil.Strftime(&date, "%a %b %d %I:%M:%S %p %Y")
    fmt.Println(str) // "Thu Jul 02 03:24:30 PM 2015"
    
    // Unicode support
    str = timeutil.Strftime(&date, "작성일 : %a %b %d %I:%M:%S %p %Y")
    fmt.Println(str) // "작성일 : Thu Jul 02 03:24:30 PM 2015"
}

Timedelta

Timedelta represents a duration between two dates. (inspired by python's timedelta)

Timedelta struct

type Timedelta struct {
    Days, Seconds, Microseconds, Milliseconds, Minutes, Hours, Weeks time.Duration
}

Initialization

All fields are optional and default to 0. You can initialize any type of timedelta by specifying field values which you want to use.

Examples:

td := timeutil.Timedelta{Days: 10}
td = timeutil.Timedelta{Minutes: 17}
td = timeutil.Timedelta{Seconds: 56}
td = timeutil.Timedelta{Days: 10, Minutes: 17, Seconds: 56}
td = timeutil.Timedelta{Days: 1, Seconds: 1, Microseconds: 1, Milliseconds: 1, Minutes: 1, Hours: 1, Weeks: 1}

func (t *Timedelta) Duration() time.Duration

Duration() returns time.Duration. time.Duration can be added to time.Date.

Examples:

base := time.Date(2015, 2, 3, 0, 0, 0, 0, time.UTC)
td := timeutil.Timedelta{Days: 10, Minutes: 17, Seconds: 56}

result := base.Add(td.Duration())
fmt.Println(result) // "2015-02-13 00:17:56 +0000 UTC"

Operations

func (t *Timedelta) Add(t2 *Timedelta)

Add returns the Timedelta t+t2.

Examples:

base := time.Date(2015, 2, 3, 0, 0, 0, 0, time.UTC)
td := timeutil.Timedelta{Days: 1, Minutes: 1, Seconds: 1}
td2 := timeutil.Timedelta{Days: 2, Minutes: 2, Seconds: 2}
td = td.Add(&td2) // td = td + td2

result = base.Add(td.Duration())
fmt.Println(result) // "2015-02-06 00:03:03 +0000 UTC"

func (t *Timedelta) Subtract(t2 *Timedelta) Timedelta

Subtract returns the Timedelta t-t2.

Examples:

base := time.Date(2015, 2, 3, 0, 0, 0, 0, time.UTC)

td := timeutil.Timedelta{Days: 2, Minutes: 2, Seconds: 2}
td2 := timeutil.Timedelta{Days: 1, Minutes: 1, Seconds: 1}
td = td.Subtract(&td2) // td = td - td2

result = base.Add(td.Duration())
fmt.Println(result) // "2015-02-04 00:01:01 +0000 UTC"

func (t *Timedelta) Abs() Timedelta

Abs returns the absolute value of t

Examples:

base := time.Date(2015, 2, 3, 0, 0, 0, 0, time.UTC)

td := timeutil.Timedelta{Days: 1, Minutes: 1, Seconds: 1}
td2 := timeutil.Timedelta{Days: 2, Minutes: 2, Seconds: 2}
td = td.Subtract(&td2) // td = td - td2
td = td.Abs() // td = |td|

result = base.Add(td.Duration())
fmt.Println(result) // "2015-02-04 00:01:01 +0000 UTC"

Strftime

Strftime formats time.Date according to the directives in the given format string. The directives begins with a percent (%) character.

(Strftime supports unicode format string.)

DirectiveMeaningExample
%aWeekday as locale’s abbreviated name.Sun, Mon, ..., Sat
%AWeekday as locale’s full name.Sunday, Monday, ..., Saturday
%wWeekday as a decimal number, where 0 is Sunday and 6 is Saturday0, 1, ..., 6
%dDay of the month as a zero-padded decimal number.01, 02, ..., 31
%bMonth as locale’s abbreviated name.Jan, Feb, ..., Dec
%BMonth as locale’s full name.January, February, ..., December
%mMonth as a zero-padded decimal number.01, 02, ..., 12
%yYear without century as a zero-padded decimal number.00, 01, ..., 99
%YYear with century as a decimal number.1970, 1988, 2001, 2013
%HHour (24-hour clock) as a zero-padded decimal number.00, 01, ..., 23
%IHour (12-hour clock) as a zero-padded decimal number.01, 02, ..., 12
%pMeridian indicator. (AM or PM.)AM, PM
%MMinute as a zero-padded decimal number.00, 01, ..., 59
%SSecond as a zero-padded decimal number.00, 01, ..., 59
%fMicrosecond as a decimal number, zero-padded on the left.000000, 000001, ..., 999999
%zUTC offset in the form +HHMM or -HHMM+0000
%ZTime zone nameUTC
%jDay of the year as a zero-padded decimal number001, 002, ..., 366
%UWeek number of the year (Sunday as the first day of the week) as a zero padded decimal number. All days in a new year preceding the first Sunday are considered to be in week 0.00, 01, ..., 53
%WWeek number of the year (Monday as the first day of the week) as a decimal number. All days in a new year preceding the first Monday are considered to be in week 0.00, 01, ..., 53
%cDate and time representation.Tue Aug 16 21:30:00 1988
%xDate representation.08/16/88
%XTime representation.21:30:00
%%A literal '%' character.%

Examples:

date := time.Date(2015, 7, 2, 15, 24, 30, 35, time.UTC)
str := timeutil.Strftime(&date, "%a %b %d %I:%M:%S %p %Y")
fmt.Println(str) // "Thu Jul 02 03:24:30 PM 2015"

// Unicode support
str = timeutil.Strftime(&date, "작성일 : %a %b %d %I:%M:%S %p %Y")
fmt.Println(str) // "작성일 : Thu Jul 02 03:24:30 PM 2015"

TODO

  • Locale support
  • Strptime - a function which returns a time.Date parsed according to a format string
  • Auto date parser - a generic string parser which is able to parse most known formats to represent a date
  • And other useful features...
Golang
Golang
Golang, or Go, is a statically typed programming language developed by Google. It’s known for its simplicity, performance, and support for concurrent programming. Go is widely used in cloud computing, server-side applications, and microservices.
GitHub - goyek/goyek: Task automation Go library
GitHub - goyek/goyek: Task automation Go library
gommon/color at master · labstack/gommon
gommon/color at master · labstack/gommon
GitHub - blevesearch/bleve: A modern text/numeric/geo-spatial/vector indexing library for go
GitHub - blevesearch/bleve: A modern text/numeric/geo-spatial/vector indexing library for go
GitHub - schollz/progressbar: A really basic thread-safe progress bar for Golang applications
GitHub - schollz/progressbar: A really basic thread-safe progress bar for Golang applications
GitHub - free/concurrent-writer: Highly concurrent drop-in replacement for bufio.Writer
GitHub - free/concurrent-writer: Highly concurrent drop-in replacement for bufio.Writer
GitHub - mattn/go-colorable
GitHub - mattn/go-colorable
GitHub - StudioSol/set: A simple Set data structure implementation in Go (Golang) using LinkedHashMap.
GitHub - StudioSol/set: A simple Set data structure implementation in Go (Golang) using LinkedHashMap.
GitHub - essentialkaos/branca: Authenticated encrypted API tokens (IETF XChaCha20-Poly1305 AEAD) for Golang
GitHub - essentialkaos/branca: Authenticated encrypted API tokens (IETF XChaCha20-Poly1305 AEAD) for Golang
Cloud Native Computing Foundation
Cloud Native Computing Foundation
GitHub - sakirsensoy/genv: Genv is a library for Go (golang) that makes it easy to read and use environment variables in your projects. It also allows environment variables to be loaded from the .env file.
GitHub - sakirsensoy/genv: Genv is a library for Go (golang) that makes it easy to read and use environment variables in your projects. It also allows environment variables to be loaded from the .env file.
GitHub - heetch/confita: Load configuration in cascade from multiple backends into a struct
GitHub - heetch/confita: Load configuration in cascade from multiple backends into a struct
GitHub - brianvoe/sjwt: Simple JWT Golang
GitHub - brianvoe/sjwt: Simple JWT Golang
GitHub - subpop/go-ini: Go package that encodes and decodes INI-files
GitHub - subpop/go-ini: Go package that encodes and decodes INI-files
GitHub - DylanMeeus/GoAudio: Go tools for audio processing & creation 🎶
GitHub - DylanMeeus/GoAudio: Go tools for audio processing & creation 🎶
GitHub - daviddengcn/go-colortext: Change the color of console text.
GitHub - daviddengcn/go-colortext: Change the color of console text.
GitHub - andOTP/andOTP: [Unmaintained] Open source two-factor authentication for Android
GitHub - andOTP/andOTP: [Unmaintained] Open source two-factor authentication for Android
GitHub - alfiankan/crab-config-files-templating: Dynamic configuration file templating tool for kubernetes manifest or general configuration files
GitHub - alfiankan/crab-config-files-templating: Dynamic configuration file templating tool for kubernetes manifest or general configuration files
GitHub - viney-shih/go-cache: A flexible multi-layer Go caching library to deal with in-memory and shared cache by adopting Cache-Aside pattern.
GitHub - viney-shih/go-cache: A flexible multi-layer Go caching library to deal with in-memory and shared cache by adopting Cache-Aside pattern.
GitHub - TreyBastian/colourize: An ANSI colour terminal package for Go
GitHub - TreyBastian/colourize: An ANSI colour terminal package for Go
GitHub - Evertras/bubble-table: A customizable, interactive table component for the Bubble Tea framework
GitHub - Evertras/bubble-table: A customizable, interactive table component for the Bubble Tea framework
GitHub - deatil/go-array: A Go package that read or set data from map, slice or json
GitHub - deatil/go-array: A Go package that read or set data from map, slice or json
GitHub - cometbft/cometbft: CometBFT (fork of Tendermint Core): A distributed, Byzantine fault-tolerant, deterministic state machine replication engine
GitHub - cometbft/cometbft: CometBFT (fork of Tendermint Core): A distributed, Byzantine fault-tolerant, deterministic state machine replication engine
GitHub - icza/session: Go session management for web servers (including support for Google App Engine - GAE).
GitHub - icza/session: Go session management for web servers (including support for Google App Engine - GAE).
GitHub - cristalhq/jwt: Safe, simple and fast JSON Web Tokens for Go
GitHub - cristalhq/jwt: Safe, simple and fast JSON Web Tokens for Go
GitHub - lrita/cmap: a thread-safe concurrent map for go
GitHub - lrita/cmap: a thread-safe concurrent map for go
GitHub - wzshiming/ctc: Console Text Colors - The non-invasive cross-platform terminal color library does not need to modify the Print method
GitHub - wzshiming/ctc: Console Text Colors - The non-invasive cross-platform terminal color library does not need to modify the Print method
GitHub - goradd/maps: map library using Go generics that offers a standard interface, go routine synchronization, and sorting
GitHub - goradd/maps: map library using Go generics that offers a standard interface, go routine synchronization, and sorting
GitHub - alfiankan/teleterm: Telegram Bot Exec Terminal Command
GitHub - alfiankan/teleterm: Telegram Bot Exec Terminal Command
GitHub - JeremyLoy/config: 12 factor configuration as a typesafe struct in as little as two function calls
GitHub - JeremyLoy/config: 12 factor configuration as a typesafe struct in as little as two function calls
GitHub - goraz/onion: Layer based configuration for golang
GitHub - goraz/onion: Layer based configuration for golang
Golang
More on Golang

Programming Tips & Tricks

Code smarter, not harder—insider tips and tricks for developers.

Error Solutions

Turn frustration into progress—fix errors faster than ever.

Shortcuts

The art of speed—shortcuts to supercharge your workflow.
  1. Collections 😎
  2. Frequently Asked Question's 🤯

Tools

available to use.

Made with ❤️

to provide resources in various ares.