datetime

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Nov 14, 2025 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package datetime provides date parsing, validation, and formatting utilities for gitcommit.

Index

Constants

View Source
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

View Source
var (
	// ErrInvalidCalendarDate is returned when a date doesn't exist in the calendar.
	ErrInvalidCalendarDate = errors.New("invalid calendar date")
)

Functions

func FormatForGit

func FormatForGit(t time.Time) string

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

func ParseDate(dateStr string) (time.Time, error)

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

func ValidateChronology(commitDate time.Time, lastCommitDate *time.Time) (bool, string)

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

func ValidateDateValue

func ValidateDateValue(t time.Time, originalStr string) bool

ValidateDateValue checks if a parsed date is a valid calendar date. This is a secondary check after parsing to catch edge cases.

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.

Jump to

Keyboard shortcuts

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