$ show jinzhu/now
jinzhu/now is a lightweight, zero-dependency Go library for time manipulation. It provides comprehensive functions to compute the beginning and end of various time units (minute, hour, day, week, month, quarter, year, half-year), parse flexible date/time strings into Go `time.Time` values, and handle configurable week start days (e.g., Monday vs Sunday) and time zones. The core pattern is a `Now` struct wrapping `time.Time` with a `Config` for customization, enabling fluent calls like `now.With(t).BeginningOfMonth()`. The library is widely used (4.7k stars) and thoroughly tested, with careful DST handling and a clever partial-date parser that merges string components with contextual current time. It is a pure library without external dependencies, targeting Go 1.12+ and suitable for any Go project needing robust time calculations or flexible date parsing.
The LLM identified 2 files worth highlighting for reuse:
The `Parse` method implements a flexible string-to-time parser that accepts partial dates (e.g., '10-12', '18:20', '2002-10', '18:20:39') and merges missing fields with current time. It uses a regex (`hasTimeRegexp`) to detect time components and an iterative format-try loop. A key insight is the `parseTime` array with nanos, seconds, minutes, hours, day, month, year; zero values are replaced with current time to fill gaps. This is a self-contained, reusable pattern for any Go project needing to parse vaguely formatted date/time strings without requiring a full layout specification.
The `Config` struct and `With` method provide a clean builder pattern for time calculations with configurable `WeekStartDay`, `TimeLocation`, and `TimeFormats`. The package-level functions (e.g., `BeginningOfMinute()`) create a default config, while users can instantiate a config and chain `.With(t).BeginningOf...()`. This isolates state and allows multiple configurations, making it reusable as a general pattern for wrapping standard library types with context-specific defaults.