Categorygithub.com/uniplaces/carbon
modulepackage
0.2.2
Repository: https://github.com/uniplaces/carbon.git
Documentation: pkg.go.dev

# README

Carbon

Build Status Go Report Card codecov GoDoc License

A simple extension for Time based on PHP's Carbon library.

Features:

  • Time is embedded into Carbon (provides access to all of Time's functionality)
  • Supports addition and subtraction of dates
  • Provides methods to compare dates
  • Supports date formatting in common formats
  • Easily calculate difference between dates
  • Ease testing with dates by using carbon.Freeze() and carbon.Now()

To do:

  • Implement all localization features as in Carbon
  • Improve the code style to be more idiomatic Go

Getting started

Install Carbon:

go get github.com/uniplaces/carbon

Add to your imports to start using Carbon

import "github.com/uniplaces/carbon"

Examples

A simple example to get you started:

package main

import (
	"fmt"
	"time"

	"github.com/uniplaces/carbon"
)

func main() {
	fmt.Printf("Right now is %s\n", carbon.Now().DateTimeString())

	today, _ := carbon.NowInLocation("America/Vancouver")
	fmt.Printf("Right now in Vancouver is %s\n", today)

	fmt.Printf("Tomorrow is %s\n", carbon.Now().AddDay())
	fmt.Printf("Last week is %s\n", carbon.Now().SubWeek())

	nextOlympics, _ := carbon.CreateFromDate(2016, time.August, 5, "Europe/London")
	nextOlympics = nextOlympics.AddYears(4)
	fmt.Printf("Next olympics are in %d\n", nextOlympics.Year())

	if carbon.Now().IsWeekend() {
		fmt.Printf("Party time!")
	}
}

You can also check the examples/ folder for more examples.

Contributing

Please feel free to make suggestions, create issues, fork the repository and send pull requests!

Licence

Copyright 2016 UNIPLACES

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

# Packages

# Functions

After will be behave like time.After unless time has been frozen If time is frozen it will add the expected delay and immediately send the frozen time on the returned channel.
Create returns a new pointer to Carbon instance from a specific date and time.
CreateFromDate returns a new pointer to a Carbon instance from just a date.
CreateFromFormat returns a new pointer to a Carbon instance from a specific format.
CreateFromMonthAndYear returns a new pointer to a Carbon instance from a specific month and year.
CreateFromTime returns a new pointer to a Carbon instance from just a date.
CreateFromTimestamp returns a new pointer to a Carbon instance from a timestamp.
CreateFromTimestampUTC returns a new pointer to a Carbon instance from an UTC timestamp.
Freeze allows time to be frozen to facilitate testing.
IsTimeFrozen allows checking if time has been frozen.
MaxValue returns a pointer to a new carbon instance for greatest supported date.
MinValue returns a pointer to a new carbon instance for lowest supported date.
NewCarbon returns a pointer to a new Carbon instance.
NewCarbonInterval returns a pointer to a new CarbonInterval instance.
NewTranslator returns a initialized instance of Translator.
Now returns a new Carbon instance for right now in current localtime.
NowInLocation returns a new Carbon instance for right now in given location.
Parse returns a pointer to a new carbon instance from a string If the location is invalid, it returns an error instead.
Period returns an array of Carbon dates by accepting start date, number of days, and end date.
Sleep will be behave like time.Sleep unless time has been frozen If time is frozen it will add the expected sleep delay and return immediately.
Tick will be behave like time.Tick unless time has been frozen If time is frozen it will tick normally but the date will be based on the frozen date.
Today returns a pointer to a new carbon instance for today If the location is invalid, it returns an error instead.
Tomorrow returns a pointer to a new carbon instance for tomorrow If the location is invalid, it returns an error instead.
UnFreeze returns time to normal operation.
Yesterday returns a pointer to a new carbon instance for yesterday If the location is invalid, it returns an error instead.

# Constants

Represents the different string formats for dates.
Represents the different string formats for dates.
Represents the different string formats for dates.
Represents the different string formats for dates.
Represents the different string formats for dates.
Represents the different string formats for dates.
Represents the different string formats for dates.
Represents the different string formats for dates.
Represents the different string formats for dates.
Represents the different string formats for dates.
Represents the different string formats for dates.
Represents the different string formats for dates.
Represents the different string formats for dates.

# Variables

# Structs

The Carbon type represents a Time instance.
CarbonInterval represents an interval between two carbons.
Translator helps to translate time based on locale.

# Type aliases

Filter represents a predicate used for filtering diffs.