timeconv

package module
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Jun 1, 2021 License: BSD-3-Clause Imports: 3 Imported by: 2

README

timeconv

Package timeconv provides some simple function to convert go time.Time values.

GoDoc Build status Coverage Status Latest

The function AddDate in package time adds dates passed in format of years, months and days. In the biginning, I thought it did handle different date of month. However, it is NOT.

For example, in many case in real life, if a month is added to 2019-01-31, the result should be 2019-01-28, identifying last day of the month. But the result with AddDate in package time will be 2019-03-03 (Playground)

This simple package timeconv provides a simple alternate of AddDate function, it focus on month operation and corrects result day for return. Please refer to the demo below for usage:

package main

import (
	"fmt"
	"time"
	"github.com/Andrew-M-C/go.timeconv"
)

func main() {
	t := time.Date(2019, 1, 31, 0, 0, 0, 0, time.UTC)
	nt := t.AddDate(0, 1, 0)	// Add one month
	fmt.Printf("%v\n", nt)		// 2019-03-03 00:00:00 +0000 UTC, not expected

	nt = timeconv.AddDate(t, 0, 1, 0)
	fmt.Printf("%v\n", nt)		// 2019-02-28 00:00:00 +0000 UTC
}

Playground

Additionally, there is a pointer version: func AddDateP(t *time.Time, years, months, days int)

Documentation

Overview

Package timeconv provides some simple function to convert go time.Time values. Please refer to functions below.

Example
package main

import (
	"fmt"

	timeconv "github.com/Andrew-M-C/go.timeconv"
)

func main() {
	p := fmt.Println
	t := timeconv.Date(2020, 1, 31)

	p(t)
	p(t.AddDate(0, 1, 0))
	p(timeconv.AddDate(t, 0, 1, 0))

}
Output:

2020-01-31 00:00:00 +0000 UTC
2020-03-02 00:00:00 +0000 UTC
2020-02-29 00:00:00 +0000 UTC

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddDate

func AddDate(t time.Time, years, months, days int) time.Time

AddDate returns the time corresponding to adding the given number of years, months, and days to t. For example, AddDate(-1, 2, 3) applied to January 1, 2011 returns March 4, 2010.

However, this version of AddDate will examine the date of month. For example, AddDate(0, 1, 0) applied to January 31, 2011 returns Feburary 28, 2011 instead of March 3, 2011.

func AddDateP added in v0.2.0

func AddDateP(t *time.Time, years, months, days int)

AddDateP is the pointer version of AddDate()

func Date added in v0.2.0

func Date(year int, month time.Month, day int, loc ...*time.Location) time.Time

Date returns a time.Time value with year, month, day and location only. If not indecating loc, UTC will be used.

func HHMM added in v0.4.0

func HHMM(t time.Time, seperator string) string

HHMM is equivalent to t.Format(fmt.Sprintf("03%s04", seperator))

func HHMMSS added in v0.4.0

func HHMMSS(t time.Time, seperator string, decimal ...int) string

HHMMSS is equivalent to t.Format(fmt.Sprintf("03%s04%s05", seperator, seperator)). With decimal, additional decimal digits of seconds will be added after a dot.

func ProcUpDuration added in v0.3.0

func ProcUpDuration() time.Duration

ProcUpDuration returns a rough duration indecating how long current process has run.

func UnixMicro added in v0.2.0

func UnixMicro(t time.Time) int64

UnixMicro returns Unix timestamp in microseconds

func UnixMilli added in v0.2.0

func UnixMilli(t time.Time) int64

UnixMilli returns Unix timestamp in milliseconds

func YYMMDD added in v0.4.0

func YYMMDD(t time.Time, seperator string) string

YYMMDD is equivalent to t.Format(fmt.Sprintf("06%s01%s02", seperator, seperator))

func YYYYMMDD added in v0.4.0

func YYYYMMDD(t time.Time, seperator string) string

YYYYMMDD is equivalent to t.Format(fmt.Sprintf("2006%s01%s02", seperator, seperator))

Types

This section is empty.

Jump to

Keyboard shortcuts

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