carbon

package
v0.18.0 Latest Latest
Warning

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

Go to latest
Published: Jun 3, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package carbon is a small ergonomic wrapper around time.Time modelled on Laravel's Carbon helper.

The standard library is already excellent — this package only adds the methods callers tend to wish stdlib had: human-readable diffs, boundary helpers (StartOfDay, EndOfMonth, …), Parse with multiple layouts, and chainable Add helpers.

A Carbon is just `time.Time` with methods, so callers can drop in any place a stdlib time is accepted:

t := carbon.Now()
t = t.AddDays(7).StartOfDay()
fmt.Println(t.DiffForHumans(carbon.Now())) // "1 week from now"

Index

Constants

This section is empty.

Variables

View Source
var ParseLayouts = []string{
	time.RFC3339Nano,
	time.RFC3339,
	"2006-01-02 15:04:05",
	"2006-01-02T15:04:05",
	"2006-01-02",
	"02/01/2006",
	"01/02/2006",
}

ParseLayouts is the list of layouts Parse tries in order. The default set covers the common shapes lagodev apps see: RFC3339, SQL datetime, plain date, and ISO date.

Functions

This section is empty.

Types

type Carbon

type Carbon struct {
	// contains filtered or unexported fields
}

Carbon wraps time.Time. The zero value is a zero Carbon (use IsZero to check).

func From

func From(t time.Time) Carbon

From wraps an existing time.Time.

func MustParse

func MustParse(s string) Carbon

MustParse panics on error. Use only with hard-coded inputs.

func Now

func Now() Carbon

Now returns the current instant in the local timezone.

func NowIn

func NowIn(loc *time.Location) Carbon

NowIn returns the current instant in the given location.

func Parse

func Parse(s string) (Carbon, error)

Parse returns the first layout in ParseLayouts that successfully decodes s.

func (Carbon) Add

func (c Carbon) Add(d time.Duration) Carbon

func (Carbon) AddDays

func (c Carbon) AddDays(n int) Carbon

func (Carbon) AddHours

func (c Carbon) AddHours(n int) Carbon

func (Carbon) AddMinutes

func (c Carbon) AddMinutes(n int) Carbon

func (Carbon) AddMonths

func (c Carbon) AddMonths(n int) Carbon

func (Carbon) AddSeconds

func (c Carbon) AddSeconds(n int) Carbon

func (Carbon) AddWeeks

func (c Carbon) AddWeeks(n int) Carbon

func (Carbon) AddYears

func (c Carbon) AddYears(n int) Carbon

func (Carbon) After

func (c Carbon) After(o Carbon) bool

func (Carbon) Before

func (c Carbon) Before(o Carbon) bool

Before / After delegate to time.Time.

func (Carbon) Date

func (c Carbon) Date() string

Date returns YYYY-MM-DD.

func (Carbon) DateTime

func (c Carbon) DateTime() string

DateTime returns YYYY-MM-DD HH:MM:SS.

func (Carbon) DiffForHumans

func (c Carbon) DiffForHumans(o Carbon) string

DiffForHumans renders a relative description of (c - o):

"5 seconds ago"
"3 minutes from now"
"1 hour ago"
"2 days from now"

func (Carbon) DiffInDays

func (c Carbon) DiffInDays(o Carbon) int

DiffInDays returns the difference in calendar days (rounded toward zero) between c and o (c - o).

func (Carbon) EndOfDay

func (c Carbon) EndOfDay() Carbon

EndOfDay sets the time to 23:59:59.999999999 on the same day.

func (Carbon) EndOfMonth

func (c Carbon) EndOfMonth() Carbon

EndOfMonth returns the last instant of the month.

func (Carbon) EndOfYear

func (c Carbon) EndOfYear() Carbon

EndOfYear returns the last instant of the year.

func (Carbon) Equal

func (c Carbon) Equal(o Carbon) bool

Equal compares two Carbons by wall-clock instant.

func (Carbon) Format

func (c Carbon) Format(layout string) string

Format delegates to time.Time.Format.

func (Carbon) ISO8601

func (c Carbon) ISO8601() string

ISO8601 is shorthand for Format(time.RFC3339).

func (Carbon) IsFuture

func (c Carbon) IsFuture() bool

func (Carbon) IsPast

func (c Carbon) IsPast() bool

func (Carbon) IsToday

func (c Carbon) IsToday() bool

func (Carbon) IsZero

func (c Carbon) IsZero() bool

IsZero reports whether the wrapped time is the zero value.

func (Carbon) StartOfDay

func (c Carbon) StartOfDay() Carbon

StartOfDay sets the time to 00:00:00 on the same day.

func (Carbon) StartOfMonth

func (c Carbon) StartOfMonth() Carbon

StartOfMonth sets the time to the first day of the month at 00:00.

func (Carbon) StartOfYear

func (c Carbon) StartOfYear() Carbon

StartOfYear sets the time to Jan 1 00:00 in the same year.

func (Carbon) Sub

func (c Carbon) Sub(o Carbon) time.Duration

func (Carbon) SubDays

func (c Carbon) SubDays(n int) Carbon

func (Carbon) SubMonths

func (c Carbon) SubMonths(n int) Carbon

func (Carbon) SubYears

func (c Carbon) SubYears(n int) Carbon

func (Carbon) Time

func (c Carbon) Time() time.Time

Time returns the underlying time.Time.

Jump to

Keyboard shortcuts

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