Documentation
¶
Overview ¶
Package datetime provides date parsing, validation, and formatting utilities for gitcommit.
Index ¶
Constants ¶
const ( // InputDateLayout is the format users provide for commit dates. // Example: "2025-02-05 20:19:19". InputDateLayout = "2006-01-02 15:04:05" // GitDateLayout is the format Git expects for GIT_AUTHOR_DATE and GIT_COMMITTER_DATE environment variables. // Example: "Wed 5 Feb 2025 20:19:19 CEST". GitDateLayout = "Mon 2 Jan 2006 15:04:05 MST" // GitLogDateLayout is the format git log returns when using --format=%cI (ISO 8601). // This is used to parse the last commit date from git log output. GitLogDateLayout = time.RFC3339 )
Variables ¶
var ( // ErrInvalidCalendarDate is returned when a date doesn't exist in the calendar. ErrInvalidCalendarDate = errors.New("invalid calendar date") )
Functions ¶
func FormatForGit ¶
FormatForGit formats a time.Time into the format Git expects for GIT_AUTHOR_DATE and GIT_COMMITTER_DATE environment variables.
Format: "Dow DD Mon YYYY HH:MM:SS TZ" Example: "Wed 5 Feb 2025 20:19:19 CEST"
The timezone used is the local timezone of the system.
func ParseDate ¶
ParseDate parses a date string in the format "YYYY-MM-DD HH:MM:SS" and returns a time.Time. The date is parsed in the local timezone.
Example input: "2025-02-05 20:19:19"
Returns an error if the date format is invalid or the date doesn't exist (e.g., Feb 30).
func ValidateChronology ¶
ValidateChronology validates that a commit date is chronologically after the last commit. Returns true if valid, and an error type string if invalid.
Parameters:
- commitDate: The proposed commit date
- lastCommitDate: The date of the last commit (nil if no previous commits)
Returns:
- bool: true if the date is valid, false otherwise
- string: empty string if valid, or one of:
- "chronology_violation": date is before last commit
- "chronology_violation_equal": date is equal to last commit
Types ¶
type ValidationResult ¶
type ValidationResult struct {
// Valid indicates whether the date is valid.
Valid bool
// ErrorType describes the type of validation error, if any.
// Possible values: "chronology_violation", "chronology_violation_equal", "invalid_format", "invalid_value"
ErrorType string
// ErrorMessage provides a human-readable error message.
ErrorMessage string
// ProvidedDate is the date that was validated.
ProvidedDate time.Time
// LastCommitDate is the date of the last commit, if any.
LastCommitDate *time.Time
}
ValidationResult represents the result of a date validation operation.
func ValidateDate ¶
func ValidateDate(dateStr string, lastCommitDate *time.Time) *ValidationResult
ValidateDate performs comprehensive validation of a commit date. It checks format, value, and chronology against the last commit.
Parameters:
- dateStr: The date string to validate
- lastCommitDate: The date of the last commit (nil if no previous commits)
Returns a ValidationResult with details about the validation.