Documentation
¶
Overview ¶
Package date provides utilities for working with dates and times.
Package date provides utilities for working with dates and times in a consistent manner.
This package standardizes date and time handling across the application by providing a set of functions for parsing, formatting, and manipulating dates. It uses a standard date format (RFC3339) to ensure consistency in date representations.
The package addresses common challenges in date handling:
- Consistent date formatting across the application
- Handling of optional (nullable) dates
- Proper error handling for date parsing failures
Key components:
- StandardDateFormat: The standard date format constant (RFC3339)
- Parse functions: For converting strings to time.Time values
- Format functions: For converting time.Time values to strings
- Support for both required and optional (pointer-based) dates
Example usage:
// Parsing a date string
dateStr := "2023-04-15T14:30:00Z"
parsedDate, err := date.ParseDate(dateStr)
if err != nil {
log.Fatalf("Failed to parse date: %v", err)
}
// Working with the parsed date
if parsedDate.After(time.Now()) {
fmt.Println("The date is in the future")
}
// Formatting a date
formattedDate := date.FormatDate(parsedDate)
fmt.Println("Formatted date:", formattedDate)
// Working with optional dates
var optionalDateStr *string
// optionalDateStr is nil
optionalDate, _ := date.ParseOptionalDate(optionalDateStr)
// optionalDate will be nil
// Setting a value
dateValue := "2023-05-20T10:15:00Z"
optionalDateStr = &dateValue
optionalDate, _ = date.ParseOptionalDate(optionalDateStr)
// optionalDate will contain the parsed date
// Formatting an optional date
formattedOptional := date.FormatOptionalDate(optionalDate)
// formattedOptional will be a pointer to the formatted string
Index ¶
Constants ¶
const ( // StandardDateFormat is the standard date format used throughout the application. // This format follows the RFC3339 standard (2006-01-02T15:04:05Z07:00), // which is both human-readable and machine-parsable, and includes // date, time, and timezone information. StandardDateFormat = time.RFC3339 )
Variables ¶
This section is empty.
Functions ¶
func FormatDate ¶
FormatDate formats a time.Time as a string in the standard format (RFC3339). It converts a time.Time object into a standardized string representation that can be used for display, storage, or transmission.
This function ensures consistent date formatting throughout the application.
Parameters:
- date: The time.Time value to format
Returns:
- string: The formatted date string (e.g., "2023-04-15T14:30:00Z")
func FormatOptionalDate ¶
FormatOptionalDate formats an optional time.Time as a string in the standard format (RFC3339). This function is designed to handle nullable time values (represented as pointers). If the input pointer is nil, the function returns nil without attempting to format. Otherwise, it behaves similarly to FormatDate, converting the time.Time to a string.
This is particularly useful when working with optional date fields in APIs or databases.
Parameters:
- date: Pointer to the time.Time value to format, can be nil
Returns:
- *string: Pointer to the formatted date string (e.g., "2023-04-15T14:30:00Z") if input is not nil, nil if input is nil
func ParseDate ¶
ParseDate parses a date string in the standard format (RFC3339). It converts a string representation of a date and time into a time.Time object. If the string cannot be parsed according to the StandardDateFormat, it returns a validation error with details about the failure.
Parameters:
- dateStr: The date string to parse (e.g., "2023-04-15T14:30:00Z")
Returns:
- time.Time: The parsed time if successful
- error: A validation error if parsing fails, nil otherwise
func ParseOptionalDate ¶
ParseOptionalDate parses an optional date string in the standard format (RFC3339). This function is designed to handle nullable date strings (represented as pointers). If the input pointer is nil, the function returns nil without attempting to parse. Otherwise, it behaves similarly to ParseDate, converting the string to a time.Time.
This is particularly useful when working with optional date fields in APIs or databases.
Parameters:
- dateStr: Pointer to the date string to parse (e.g., "2023-04-15T14:30:00Z"), can be nil
Returns:
- *time.Time: Pointer to the parsed time if successful, nil if input is nil
- error: A validation error if parsing fails, nil otherwise
Types ¶
This section is empty.