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
- func AddMonths(date time.Time, months int) time.Time
- func AddYears(date time.Time, years int) time.Time
- func ChineseZodiac(year int) string
- func DateAdd(date time.Time, duration time.Duration) time.Time
- func DateDiff(date1, date2 time.Time) time.Duration
- func DateRange(start, end time.Time) ([]time.Time, error)
- func DaysBetween(start, end time.Time) int
- func DaysInMonth(year int, month time.Month) int
- func FormatDate(date time.Time, layout string) string
- func FormatDuration(duration time.Duration) string
- func GetCurrentTime() time.Time
- func GetCurrentUTCTime() time.Time
- func GetDatePart(date time.Time, part string) (int, error)
- func GetEndOfDay(date time.Time) time.Time
- func GetStartOfDay(date time.Time) time.Time
- func IsLeapYear(year int) bool
- func IsWeekday(date time.Time) bool
- func IsWeekend(date time.Time) bool
- func MonthsBetween(start, end time.Time) int
- func ParseDateString(dateStr, layout string) (time.Time, error)
- func WeeksBetween(start, end time.Time) int
- func YearsBetween(start, end time.Time) int
- func ZodiacSign(date time.Time) string
- type DatePattern
- type DateTime
- type DateUnit
- type DayOfWeek
- type Month
- type Season
- type TimeInterval
- func (ti *TimeInterval) Elapsed(unit DateUnit) int64
- func (ti *TimeInterval) ElapsedDays() int64
- func (ti *TimeInterval) ElapsedHours() int64
- func (ti *TimeInterval) ElapsedMillis() int64
- func (ti *TimeInterval) ElapsedMinutes() int64
- func (ti *TimeInterval) ElapsedSeconds() int64
- func (ti *TimeInterval) ElapsedWeeks() int64
Constants ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
GetCurrentTime returns the current local time.
Returns: - time.Time: the current local time
func GetCurrentUTCTime ¶
GetCurrentUTCTime returns the current UTC time.
Returns: - time.Time: the current UTC time
func GetDatePart ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 DateTime ¶
DateTime is a wrapper around time.Time to provide more utility methods
func NewDateTime ¶
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 ¶
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 ¶
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 ¶
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
type DayOfWeek ¶
type DayOfWeek int
DayOfWeek represents days of the week
func DayOfWeekFromTime ¶
DayOfWeekFromTime converts time.Weekday to DayOfWeek
type Month ¶
type Month int
Month represents months of the year
func (Month) GetLastDay ¶
GetLastDay returns the last day of the month
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