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

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

Directive Meaning Example
%a Weekday as locale’s abbreviated name. Sun, Mon, ..., Sat
%A Weekday as locale’s full name. Sunday, Monday, ..., Saturday
%w Weekday as a decimal number, where 0 is Sunday and 6 is Saturday 0, 1, ..., 6
%d Day of the month as a zero-padded decimal number. 01, 02, ..., 31
%b Month as locale’s abbreviated name. Jan, Feb, ..., Dec
%B Month as locale’s full name. January, February, ..., December
%m Month as a zero-padded decimal number. 01, 02, ..., 12
%y Year without century as a zero-padded decimal number. 00, 01, ..., 99
%Y Year with century as a decimal number. 1970, 1988, 2001, 2013
%H Hour (24-hour clock) as a zero-padded decimal number. 00, 01, ..., 23
%I Hour (12-hour clock) as a zero-padded decimal number. 01, 02, ..., 12
%p Meridian indicator. (AM or PM.) AM, PM
%M Minute as a zero-padded decimal number. 00, 01, ..., 59
%S Second as a zero-padded decimal number. 00, 01, ..., 59
%f Microsecond as a decimal number, zero-padded on the left. 000000, 000001, ..., 999999
%z UTC offset in the form +HHMM or -HHMM +0000
%Z Time zone name UTC
%j Day of the year as a zero-padded decimal number 001, 002, ..., 366
%U Week 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
%W Week 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
%c Date and time representation. Tue Aug 16 21:30:00 1988
%x Date representation. 08/16/88
%X Time 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...
Owner
Similar Resources

fasttime - fast time formatting for go

fasttime - fast time formatting for go

Dec 13, 2022

🌐 A time zone helper

🌐 A time zone helper

🌐 A time zone helper tz helps you schedule things across time zones. It is an interactive TUI program that displays time across a few time zones of y

Dec 29, 2022

Clock is a small library for mocking time in Go.

clock Clock is a small library for mocking time in Go. It provides an interface around the standard library's time package so that the application can

Dec 30, 2022

A natural language date/time parser with pluggable rules

when when is a natural language date/time parser with pluggable rules and merge strategies Examples tonight at 11:10 pm at Friday afternoon the deadli

Dec 26, 2022

Copy of stdlib's time.Duration, but ParseDuration accepts other bigger units such as days, weeks, months and years

duration Copy of stdlib's time.Duration, but ParseDuration accepts other units as well: d: days (7 * 24 * time.Hour) w: weeks (7 * Day) mo: months (30

Jun 21, 2022

time format golang

a simple plugin to change date and time format

Sep 29, 2021

Show time by timezone

Show time by timezone

Jan 22, 2022

Go-timeparser - Flexible Time Parser for Golang

go-timeparser Flexible Time Parser for Golang Installation Download timeparser w

Dec 29, 2022

A Go package for working with dates

date Package date provides functionality for working with dates. This package introduces a light-weight Date type that is storage-efficient and conven

Dec 13, 2022
Comments
  • No update since 7 years: mature or dead?

    No update since 7 years: mature or dead?

    No update since 7 years. Is this project mature or dead?

    Please provide a hyperlink to a better alternative in the README, if this package is no longer supported.

Fast strftime for Go

strftime Fast strftime for Go SYNOPSIS f := strftime.New(`.... pattern ...`) if err := f.Format(buf, time.Now()); err != nil { log.Println(err.Err

Nov 28, 2022
Time struct in Go that uses 4 bytes of memory vs the 24 bytes of time.Time

A tiny time object in Go. Tinytime uses 4 bytes of memory vs the 24 bytes of a standard time.Time{}

Oct 3, 2022
Golang package to manipulate time intervals.

timespan timespan is a Go library for interacting with intervals of time, defined as a start time and a duration. Documentation API Installation Insta

Sep 26, 2022
Structural time package for jalali calendar

Jalali Structural time package for jalali calendar. This package support parse from string, json and time. Structures There are three data structures

Mar 21, 2022
Timediff is a Go package for printing human readable, relative time differences 🕰️

timediff is a Go package for printing human readable, relative time differences. Output is based on ranges defined in the Day.js JavaScript library, and can be customized if needed.

Dec 25, 2022
Carbon for Golang, an extension for Time

Carbon A simple extension for Time based on PHP's Carbon library. Features: Time is embedded into Carbon (provides access to all of Time's functionali

Dec 20, 2022
:clock8: Better time duration formatting in Go!

durafmt durafmt is a tiny Go library that formats time.Duration strings (and types) into a human readable format. go get github.com/hako/durafmt Why

Dec 16, 2022
:clock1: Date and Time - Golang Formatting Library
:clock1: Date and Time - Golang Formatting Library

Kair Date and Time - Golang Formatting Library Setup To get Kair > Go CLI go get github.com/GuilhermeCaruso/kair > Go DEP dep ensure -add github.com/G

Sep 26, 2022
Now is a time toolkit for golang

Now Now is a time toolkit for golang Install go get -u github.com/jinzhu/now Usage Calculating time based on current time import "github.com/jinzhu/n

Dec 23, 2022
Go time library inspired by Moment.js

Goment Current Version: 1.4.0 Changelog Goment is a port of the popular Javascript datetime library Moment.js. It follows the Moment.js API closely, w

Dec 24, 2022