datetime

package
v0.6.2 Latest Latest
Warning

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

Go to latest
Published: Oct 6, 2021 License: MIT Imports: 1 Imported by: 0

Documentation

Overview

Package datetime provides a way to working with datetime.

Example Usage

The following is a complete example using datetime package

import (
	"time"

	"github.com/phamtai97/go-utils/utils/datetime"
	"github.com/phamtai97/go-utils/utils/logger"
	"go.uber.org/zap"
)

func main() {
	logger.InitProduction("")

	// Convert current milliseconds to different formats
	logger.Info("Convert current milliseconds to format YYYY-MM-DD", zap.String("value", datetime.ConvertCurrentLocalTimeToString(datetime.YYYY_MM_DD)))
	logger.Info("Convert current milliseconds to format YYYY-MM-DD HH:mm:ss", zap.String("value", datetime.ConvertCurrentLocalTimeToString(datetime.YYYY_MM_DD_HH_MM_SS)))
	logger.Info("Convert current milliseconds to format YYYY-MM-DD HH:mm:ss.SSS", zap.String("value", datetime.ConvertCurrentLocalTimeToString(datetime.YYYY_MM_DD_HH_MM_SS_SSS)))
	logger.Info("Convert current milliseconds to format DD-MM-YYYY", zap.String("value", datetime.ConvertCurrentLocalTimeToString(datetime.DD_MM_YYYY)))
	logger.Info("Convert current milliseconds to format DD-MM-YYYY HH:mm:ss", zap.String("value", datetime.ConvertCurrentLocalTimeToString(datetime.DD_MM_YYYY_HH_MM_SS)))
	logger.Info("Convert current milliseconds to format DD-MM-YYYY HH:mm:ss.SSS", zap.String("value", datetime.ConvertCurrentLocalTimeToString(datetime.DD_MM_YYYY_HH_MM_SS_SSS)))

	// Get current millisenconds
	millis := datetime.GetCurrentMiliseconds()
	logger.Info("Current milliseconds", zap.Int64("value", millis))

	// Convert milliseconds to different formats
	yyyymmdd := datetime.ConvertMillisecondsToString(millis, datetime.YYYY_MM_DD)
	logger.Info("Convert milliseconds to format YYYY-MM-DD", zap.String("value", yyyymmdd))

	yyyymmdd_hhmmss := datetime.ConvertMillisecondsToString(millis, datetime.YYYY_MM_DD_HH_MM_SS)
	logger.Info("Convert milliseconds to format YYYY-MM-DD HH:mm:ss", zap.String("value", yyyymmdd_hhmmss))

	yyyymmdd_hhmmss_sss := datetime.ConvertMillisecondsToString(millis, datetime.YYYY_MM_DD_HH_MM_SS_SSS)
	logger.Info("Convert milliseconds to format YYYY-MM-DD HH:mm:ss.SSS", zap.String("value", yyyymmdd_hhmmss_sss))

	ddmmyyyy := datetime.ConvertMillisecondsToString(millis, datetime.DD_MM_YYYY)
	logger.Info("Convert milliseconds to format DD-MM-YYYY", zap.String("value", ddmmyyyy))

	ddmmyyyy_hhmmss := datetime.ConvertMillisecondsToString(millis, datetime.DD_MM_YYYY_HH_MM_SS)
	logger.Info("Convert milliseconds to format DD-MM-YYYY HH:mm:ss", zap.String("value", ddmmyyyy_hhmmss))

	ddmmyyyy_hhmmss_sss := datetime.ConvertMillisecondsToString(millis, datetime.DD_MM_YYYY_HH_MM_SS_SSS)
	logger.Info("Convert milliseconds to format DD-MM-YYYY HH:mm:ss.SSS", zap.String("value", ddmmyyyy_hhmmss_sss))

	// Convert string format to millisecond
	millis, _ = datetime.ConvertStringToMilliseconds(yyyymmdd, datetime.YYYY_MM_DD)
	logger.Info("Convert string format YYYY-MM-DD to millisecond", zap.Int64("value", millis))

	millis, _ = datetime.ConvertStringToMilliseconds(yyyymmdd_hhmmss, datetime.YYYY_MM_DD_HH_MM_SS)
	logger.Info("Convert string format YYYY-MM-DD HH:mm:ss to millisecond", zap.Int64("value", millis))

	millis, _ = datetime.ConvertStringToMilliseconds(yyyymmdd_hhmmss_sss, datetime.YYYY_MM_DD_HH_MM_SS_SSS)
	logger.Info("Convert string format YYYY-MM-DD HH:mm:ss.SSS to millisecond", zap.Int64("value", millis))

	millis, _ = datetime.ConvertStringToMilliseconds(ddmmyyyy, datetime.DD_MM_YYYY)
	logger.Info("Convert string format DD-MM-YYYY to millisecond", zap.Int64("value", millis))

	millis, _ = datetime.ConvertStringToMilliseconds(ddmmyyyy_hhmmss, datetime.DD_MM_YYYY_HH_MM_SS)
	logger.Info("Convert string format DD-MM-YYYY HH:mm:ss to millisecond", zap.Int64("value", millis))

	millis, _ = datetime.ConvertStringToMilliseconds(ddmmyyyy_hhmmss_sss, datetime.DD_MM_YYYY_HH_MM_SS_SSS)
	logger.Info("Convert string format DD-MM-YYYY HH:mm:ss.SSS to millisecond", zap.Int64("value", millis))

	// Other functions
	logger.Info("Year", zap.Int("value", datetime.GetYear()))
	logger.Info("Day of year", zap.Int("value", datetime.GetDayOfYear()))
	logger.Info("Day of month", zap.Int("value", datetime.GetDayOfMonth()))
	logger.Info("Month of year", zap.Int("value", datetime.GetMonthOfYear()))
	logger.Info("Start local time of year", zap.Int64("value", datetime.ConvertLocalTimeToMilliseconds(datetime.GetStartLocalTimeOfYear())))
	logger.Info("End local time of year", zap.Int64("value", datetime.ConvertLocalTimeToMilliseconds(datetime.GetEndLocalTimeOfYear())))
	logger.Info("Start local time of month", zap.Int64("value", datetime.ConvertLocalTimeToMilliseconds(datetime.GetStartLocalTimeOfMonth())))
	logger.Info("End local time of month", zap.Int64("value", datetime.ConvertLocalTimeToMilliseconds(datetime.GetEndLocalTimeOfMonth())))
	logger.Info("Start local time of day", zap.Int64("value", datetime.ConvertLocalTimeToMilliseconds(datetime.GetStartLocalTimeOfDay())))
	logger.Info("End local time of day", zap.Int64("value", datetime.ConvertLocalTimeToMilliseconds(datetime.GetEndLocalTimeOfDay())))
	logger.Info("Start local time of time", zap.Int64("value", datetime.ConvertLocalTimeToMilliseconds(datetime.GetStartLocalTimeOfTime(time.Now()))))
	logger.Info("Start local time of time", zap.Int64("value", datetime.ConvertLocalTimeToMilliseconds(datetime.GetEndLocalTimeOfTime(time.Now()))))
	logger.Info("Get before local time of time", zap.Int64("value", datetime.ConvertLocalTimeToMilliseconds(datetime.GetBeforeLocalTimeOfTime(time.Now(), 9, true))))
	logger.Info("Get after local time of time", zap.Int64("value", datetime.ConvertLocalTimeToMilliseconds(datetime.GetAfterLocalTimeOfTime(time.Now(), 9, false))))
}

Index

Constants

View Source
const (
	// YYYY_MM_DD_HH_MM_SS_SSS is the format "2006-01-02 15:04:05.000".
	YYYY_MM_DD_HH_MM_SS_SSS = "2006-01-02 15:04:05.000"
	// YYYY_MM_DD_HH_MM_SS is the format "2006-01-02 15:04:05".
	YYYY_MM_DD_HH_MM_SS = "2006-01-02 15:04:05"
	// YYYY_MM_DD is the format "2006-01-02".
	YYYY_MM_DD = "2006-01-02"
	// DD_MM_YYYY is the format "02-01-2006".
	DD_MM_YYYY = "02-01-2006"
	// DD_MM_YYYY_HH_MM_SS is the format "02-01-2006 15:04:05".
	DD_MM_YYYY_HH_MM_SS = "02-01-2006 15:04:05"
	// DD_MM_YYYY_HH_MM_SS_SSS is the format "02-01-2006 15:04:05.000".
	DD_MM_YYYY_HH_MM_SS_SSS = "02-01-2006 15:04:05.000"
)

Variables

This section is empty.

Functions

func ConvertCurrentLocalTimeToString

func ConvertCurrentLocalTimeToString(format string) string

ConvertCurrentLocalTimeToString converts the current local time to string with the specific format.

func ConvertLocalTimeToMilliseconds

func ConvertLocalTimeToMilliseconds(t time.Time) int64

ConvertLocalTimeToMilliseconds converts the local time to milliseconds.

func ConvertLocalTimeToString

func ConvertLocalTimeToString(localTime time.Time, format string) string

ConvertLocalTimeToString converts the local time to string with specific format.

func ConvertMillisecondsToLocalTime

func ConvertMillisecondsToLocalTime(millis int64) time.Time

ConvertMillisecondsToLocalTime converts the milliseconds to local time.

func ConvertMillisecondsToString

func ConvertMillisecondsToString(millis int64, format string) string

ConvertMillisecondsToString converts the milliseconds to the string with the specific format.

func ConvertStringToLocalTime

func ConvertStringToLocalTime(value, format string) (time.Time, error)

ConvertStringToLocalTime converts the string with specific format to the local time.

func ConvertStringToMilliseconds

func ConvertStringToMilliseconds(value, format string) (int64, error)

ConvertStringToMilliseconds converts the string with specific format to milliseconds.

func GetAfterLocalTimeOfTime

func GetAfterLocalTimeOfTime(inputTime time.Time, numberDay int, isStartTime bool) time.Time

GetAfterLocalTimeOfTime returns after local time with the number of days compared to specific local time.

func GetBeforeLocalTimeOfTime

func GetBeforeLocalTimeOfTime(inputTime time.Time, numberDay int, isStartTime bool) time.Time

GetBeforeLocalTimeOfTime returns before local time with the number of days compared to specific local time.

func GetCurrentLocalTime

func GetCurrentLocalTime() time.Time

GetCurrentLocalTime returns the current local time.

func GetCurrentMiliseconds

func GetCurrentMiliseconds() int64

GetCurrentMiliseconds returns the current milliseconds.

func GetDayOfMonth

func GetDayOfMonth() int

GetDayOfMonth returns the day of month.

func GetDayOfYear

func GetDayOfYear() int

GetDayOfYear returns the day of the year.

func GetEndLocalTimeOfDay

func GetEndLocalTimeOfDay() time.Time

GetEndLocalTimeOfDay return the end local time of day.

func GetEndLocalTimeOfMonth

func GetEndLocalTimeOfMonth() time.Time

GetEndLocalTimeOfMonth returns the end local time of month.

func GetEndLocalTimeOfTime

func GetEndLocalTimeOfTime(inputTime time.Time) time.Time

GetEndLocalTimeOfTime return the end local time of specific local time.

func GetEndLocalTimeOfYear

func GetEndLocalTimeOfYear() time.Time

GetEndLocalTimeOfYear returns the end local time of year.

func GetMillisecondsBetween

func GetMillisecondsBetween(startDate time.Time, endTime time.Time) int64

GetMillisecondsBetween returns the number of milliseconds between 2 local time.

func GetMonthOfYear

func GetMonthOfYear() int

GetMonthOfYear returns the month of year. Value from 1 to 12.

func GetStartLocalTimeOfDay

func GetStartLocalTimeOfDay() time.Time

GetStartLocalTimeOfDay return the start local time of day.

func GetStartLocalTimeOfMonth

func GetStartLocalTimeOfMonth() time.Time

GetStartLocalTimeOfMonth returns the start local time of month.

func GetStartLocalTimeOfTime

func GetStartLocalTimeOfTime(inputTime time.Time) time.Time

GetStartLocalTimeOfTime return the start local time of specific local time.

func GetStartLocalTimeOfYear

func GetStartLocalTimeOfYear() time.Time

GetStartLocalTimeOfYear returns the start local time of year.

func GetYear

func GetYear() int

GetYear returns the current year.

Types

This section is empty.

Jump to

Keyboard shortcuts

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