datetime

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Nov 21, 2024 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package datetime provides utility functions for handling date and time operations. This package simplifies parsing, formatting, and computing date and time values.

Package datetime provides utility functions for handling time intervals and durations. This package simplifies measuring the execution time of code.

Index

Constants

View Source
const (
	ISO8601        = "2006-01-02T15:04:05Z07:00"           // ISO 8601 combined date and time with timezone
	ISO8601Date    = "2006-01-02"                          // ISO 8601 date only
	ISO8601Time    = "15:04:05"                            // ISO 8601 time only
	RFC1123        = "Mon, 02 Jan 2006 15:04:05 MST"       // RFC 1123 date and time
	RFC1123Z       = "Mon, 02 Jan 2006 15:04:05 -0700"     // RFC 1123 with numeric timezone
	RFC3339        = "2006-01-02T15:04:05Z07:00"           // RFC 3339 combined date and time with timezone
	RFC3339Nano    = "2006-01-02T15:04:05.999999999Z07:00" // RFC 3339 with nanosecond precision
	ANSIC          = "Mon Jan _2 15:04:05 2006"            // ANSI C's asctime() format
	UnixDate       = "Mon Jan _2 15:04:05 MST 2006"        // Unix date format
	RubyDate       = "Mon Jan 02 15:04:05 -0700 2006"      // Ruby's date format
	Kitchen        = "3:04PM"                              // The time in 12-hour AM/PM format
	Stamp          = "Jan _2 15:04:05"                     // Date and time without year
	StampMilli     = "Jan _2 15:04:05.000"                 // Date and time with millisecond precision
	StampMicro     = "Jan _2 15:04:05.000000"              // Date and time with microsecond precision
	StampNano      = "Jan _2 15:04:05.000000000"           // Date and time with nanosecond precision
	CustomDateTime = "02-01-2006 15:04:05"                 // Custom date and time format (DD-MM-YYYY HH:MM:SS)
	CustomDate     = "02-01-2006"                          // Custom date format (DD-MM-YYYY)
	CustomTime     = "15:04:05"                            // Custom time format (HH:MM:SS)
)

Variables

This section is empty.

Functions

func AddMonths

func AddMonths(date time.Time, months int) time.Time

AddMonths adds a given number of months to a date and returns the new date.

Parameters: - date: the time.Time object to add months to - months: the number of months to add

Returns: - time.Time: the new date with the added months

func AddYears

func AddYears(date time.Time, years int) time.Time

AddYears adds a given number of years to a date and returns the new date.

Parameters: - date: the time.Time object to add years to - years: the number of years to add

Returns: - time.Time: the new date with the added years

func ChineseZodiac

func ChineseZodiac(year int) string

ChineseZodiac returns the Chinese zodiac sign for the given year.

Parameters: - year: the year to get the Chinese zodiac sign for

Returns: - string: the Chinese zodiac sign

func DateAdd

func DateAdd(date time.Time, duration time.Duration) time.Time

DateAdd adds a duration to the given date and returns the new date.

Parameters: - date: the time.Time object to add the duration to - duration: the duration to add

Returns: - time.Time: the new date with the added duration

func DateDiff

func DateDiff(date1, date2 time.Time) time.Duration

DateDiff calculates the difference between two dates and returns the duration.

Parameters: - date1: the first date - date2: the second date

Returns: - time.Duration: the duration between the two dates

func DateRange

func DateRange(start, end time.Time) ([]time.Time, error)

DateRange generates a slice of dates between the start and end dates, inclusive.

Parameters: - start: the start date - end: the end date

Returns: - []time.Time: a slice of dates between the start and end dates - error: if the end date is before the start date

func DaysBetween

func DaysBetween(start, end time.Time) int

DaysBetween calculates the number of days between two dates.

Parameters: - start: the start date - end: the end date

Returns: - int: the number of days between the two dates

func DaysInMonth

func DaysInMonth(year int, month time.Month) int

DaysInMonth returns the number of days in a given month of a specific year.

Parameters: - year: the year - month: the month

Returns: - int: the number of days in the month

func FormatDate

func FormatDate(date time.Time, layout string) string

FormatDate formats a time.Time object into a string based on the provided layout.

Parameters: - date: the time.Time object to format - layout: the layout string to use for formatting

Returns: - string: the formatted date string

func FormatDuration

func FormatDuration(duration time.Duration) string

FormatDuration formats a duration into a human-readable string.

Parameters: - duration: the time.Duration object to format

Returns: - string: the formatted duration string

func GetCurrentTime

func GetCurrentTime() time.Time

GetCurrentTime returns the current local time.

Returns: - time.Time: the current local time

func GetCurrentUTCTime

func GetCurrentUTCTime() time.Time

GetCurrentUTCTime returns the current UTC time.

Returns: - time.Time: the current UTC time

func GetDatePart

func GetDatePart(date time.Time, part string) (int, error)

GetDatePart extracts a specific part of the date (year, month, day, hour, minute, second).

Parameters: - date: the time.Time object to extract from - part: the part of the date to extract ("year", "month", "day", "hour", "minute", "second")

Returns: - int: the extracted part of the date - error: if an invalid part is specified

func GetEndOfDay

func GetEndOfDay(date time.Time) time.Time

GetEndOfDay returns the end time of the day for the given date.

Parameters: - date: the time.Time object to get the end of the day for

Returns: - time.Time: the end time of the day

func GetStartOfDay

func GetStartOfDay(date time.Time) time.Time

GetStartOfDay returns the start time of the day for the given date.

Parameters: - date: the time.Time object to get the start of the day for

Returns: - time.Time: the start time of the day

func IsLeapYear

func IsLeapYear(year int) bool

IsLeapYear checks if a given year is a leap year.

Parameters: - year: the year to check

Returns: - bool: true if the year is a leap year, false otherwise

func IsWeekday

func IsWeekday(date time.Time) bool

IsWeekday checks if a given date falls on a weekday.

Parameters: - date: the time.Time object to check

Returns: - bool: true if the date is on a weekday, false otherwise

func IsWeekend

func IsWeekend(date time.Time) bool

IsWeekend checks if a given date falls on a weekend.

Parameters: - date: the time.Time object to check

Returns: - bool: true if the date is on a weekend, false otherwise

func MonthsBetween

func MonthsBetween(start, end time.Time) int

MonthsBetween calculates the number of months between two dates.

Parameters: - start: the start date - end: the end date

Returns: - int: the number of months between the two dates

func ParseDateString

func ParseDateString(dateStr, layout string) (time.Time, error)

ParseDateString parses a date string into a time.Time object. The layout parameter should be a Go time layout string.

Parameters: - dateStr: the date string to parse - layout: the layout string to use for parsing

Returns: - time.Time: the parsed time.Time object - error: if an error occurs during parsing

func WeeksBetween

func WeeksBetween(start, end time.Time) int

WeeksBetween calculates the number of weeks between two dates.

Parameters: - start: the start date - end: the end date

Returns: - int: the number of weeks between the two dates

func YearsBetween

func YearsBetween(start, end time.Time) int

YearsBetween calculates the number of years between two dates.

Parameters: - start: the start date - end: the end date

Returns: - int: the number of years between the two dates

func ZodiacSign

func ZodiacSign(date time.Time) string

ZodiacSign returns the zodiac sign for the given date.

Parameters: - date: the time.Time object to get the zodiac sign for

Returns: - string: the zodiac sign

Types

type DatePattern

type DatePattern struct{}

DatePattern provides common date formatting patterns

type DateTime

type DateTime struct {
	time.Time
}

DateTime is a wrapper around time.Time to provide more utility methods

func NewDateTime

func NewDateTime(t time.Time) *DateTime

NewDateTime creates a new DateTime instance

Parameters: - t: the time.Time object to wrap

Returns: - *DateTime: the new DateTime instance

func Now

func Now() *DateTime

Now returns the current DateTime

Returns: - *DateTime: the current DateTime

func (*DateTime) AddDays

func (dt *DateTime) AddDays(days int) *DateTime

AddDays adds the given number of days to the DateTime

Parameters: - days: the number of days to add

Returns: - *DateTime: the new DateTime with the added days

func (*DateTime) AddMonths

func (dt *DateTime) AddMonths(months int) *DateTime

AddMonths adds the given number of months to the DateTime

Parameters: - months: the number of months to add

Returns: - *DateTime: the new DateTime with the added months

func (*DateTime) AddYears

func (dt *DateTime) AddYears(years int) *DateTime

AddYears adds the given number of years to the DateTime

Parameters: - years: the number of years to add

Returns: - *DateTime: the new DateTime with the added years

func (*DateTime) Between

func (dt *DateTime) Between(other *DateTime) time.Duration

Between calculates the duration between two DateTime instances

Parameters: - other: the other DateTime instance to compare with

Returns: - time.Duration: the duration between the two DateTime instances

func (*DateTime) Format

func (dt *DateTime) Format(pattern string) string

Format formats the DateTime according to the given pattern

Parameters: - pattern: the format pattern

Returns: - string: the formatted DateTime string

type DateUnit

type DateUnit int

DateUnit represents a unit of time

const (
	Millisecond DateUnit = 1
	Second      DateUnit = 1000 * Millisecond
	Minute      DateUnit = 60 * Second
	Hour        DateUnit = 60 * Minute
	Day         DateUnit = 24 * Hour
	Week        DateUnit = 7 * Day
)

func (DateUnit) GetMillis

func (du DateUnit) GetMillis() int64

GetMillis returns the number of milliseconds in the DateUnit

type DayOfWeek

type DayOfWeek int

DayOfWeek represents days of the week

const (
	Sunday DayOfWeek = iota
	Monday
	Tuesday
	Wednesday
	Thursday
	Friday
	Saturday
)

func DayOfWeekFromTime

func DayOfWeekFromTime(weekday time.Weekday) DayOfWeek

DayOfWeekFromTime converts time.Weekday to DayOfWeek

type Month

type Month int

Month represents months of the year

const (
	January Month = iota + 1
	February
	March
	April
	May
	June
	July
	August
	September
	October
	November
	December
)

func Of

func Of(month time.Month) Month

Of converts time.Month to Month

func (Month) GetLastDay

func (m Month) GetLastDay(leapYear bool) int

GetLastDay returns the last day of the month

type Season

type Season int

Season represents the four seasons

const (
	Spring Season = iota + 1 // 1~3 months
	Summer                   // 4~6 months
	Autumn                   // 7~9 months
	Winter                   // 10~12 months
)

func (Season) GetMonths

func (s Season) GetMonths() []Month

GetMonths returns the months that belong to the season

type TimeInterval

type TimeInterval struct {
	// contains filtered or unexported fields
}

TimeInterval is a simple timer class used to calculate the duration of code execution

func NewTimeInterval

func NewTimeInterval() *TimeInterval

NewTimeInterval creates a new TimeInterval instance and starts the timer

Returns: - *TimeInterval: a new TimeInterval instance

func (*TimeInterval) Elapsed

func (ti *TimeInterval) Elapsed(unit DateUnit) int64

Elapsed returns the elapsed time in the specified unit

Parameters: - unit: the unit of time to return

Returns: - int64: the elapsed time in the specified unit

func (*TimeInterval) ElapsedDays

func (ti *TimeInterval) ElapsedDays() int64

ElapsedDays returns the elapsed time in days

ElapsedDays

Returns: - int64: the elapsed time in days

func (*TimeInterval) ElapsedHours

func (ti *TimeInterval) ElapsedHours() int64

ElapsedHours returns the elapsed time in hours

ElapsedHours

Returns: - int64: the elapsed time in hours

func (*TimeInterval) ElapsedMillis

func (ti *TimeInterval) ElapsedMillis() int64

ElapsedMillis returns the elapsed time in milliseconds

Returns: - int64: the elapsed time in milliseconds

func (*TimeInterval) ElapsedMinutes

func (ti *TimeInterval) ElapsedMinutes() int64

ElapsedMinutes returns the elapsed time in minutes

Returns: - int64: the elapsed time in minutes

func (*TimeInterval) ElapsedSeconds

func (ti *TimeInterval) ElapsedSeconds() int64

ElapsedSeconds returns the elapsed time in seconds

Returns: - int64: the elapsed time in seconds

func (*TimeInterval) ElapsedWeeks

func (ti *TimeInterval) ElapsedWeeks() int64

ElapsedWeeks returns the elapsed time in weeks

ElapsedWeeks

Returns: - int64: the elapsed time in weeks

Jump to

Keyboard shortcuts

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