Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
/ carbon Public

A simple, semantic and developer-friendly time package for golang

License

Notifications You must be signed in to change notification settings

dromara/carbon

Folders and files

NameName
Last commit message
Last commit date

Latest commit

History

2,381 Commits

Repository files navigation

carbon

Carbon Release Go Test Go Report Card Go Coverage Carbon Doc Awesome HelloGitHub License

English | 简体中文 | 日本語 | 한국어

Introduction

Carbon is a lightweight, semantic, and developer-friendly golang time package that doesn't depend on any third-party package, has 100% unit test coverage, and has been used by docker organization as well as included by awesome-go and hello-github.

docker

Repository

github.com/dromara/carbon

gitee.com/dromara/carbon

gitcode.com/dromara/carbon

Quick Start

Installation

go version >= 1.18

// Via github
go get -u github.com/dromara/carbon/v2
import "github.com/dromara/carbon/v2"
// Via gitee
go get -u gitee.com/dromara/carbon/v2
import "gitee.com/dromara/carbon/v2"
// Via gitcode
go get -u gitcode.com/dromara/carbon/v2
import "gitcode.com/dromara/gitcode/v2"

Carbon was donated to the dromara organization, the repository URL has changed. If the previous repository used was golang-module/carbon, please replace the original repository with the new repository in go.mod, or execute the following command:

go mod edit -replace github.com/golang-module/carbon/v2 = github.com/dromara/carbon/v2

Example Usage

Default timezone is UTC, language locale is English, start day of the week is Monday and weekend days of the week are Saturday and Sunday.

carbon.SetTestNow(carbon.Parse("2020-08-05 13:14:15.999999999"))
carbon.IsTestNow() // true
carbon.Now().ToString() // 2020年08月05日 13:14:15.999999999 +0000 UTC
carbon.Yesterday().ToString() // 2020年08月04日 13:14:15.999999999 +0000 UTC
carbon.Tomorrow().ToString() // 2020年08月06日 13:14:15.999999999 +0000 UTC
carbon.Parse("2020-08-05 13:14:15").ToString() // 2020年08月05日 13:14:15 +0000 UTC
carbon.Parse("2022-03-08T03:01:14-07:00").ToString() // 2022年03月08日 10:01:14 +0000 UTC
carbon.ParseByLayout("It is 2020年08月05日 13:14:15", "It is 2006年01月02日 15:04:05").ToString() // 2020年08月05日 13:14:15 +0000 UTC
carbon.ParseByFormat("It is 2020年08月05日 13:14:15", "\\I\\t \\i\\s Y-m-d H:i:s").ToString() // 2020年08月05日 13:14:15 +0000 UTC
carbon.CreateFromDate(2020, 8, 5).ToString() // 2020年08月05日 00:00:00 +0000 UTC
carbon.CreateFromTime(13, 14, 15).ToString() // 2020年08月05日 13:14:15 +0000 UTC
carbon.CreateFromDateTime(2020, 8, 5, 13, 14, 15).ToString() // 2020年08月05日 13:14:15 +0000 UTC
carbon.CreateFromTimestamp(1596633255).ToString() // 2020年08月05日 13:14:15 +0000 UTC
carbon.Parse("2020-07-05 13:14:15").DiffForHumans() // 1 month before
carbon.Parse("2020-07-05 13:14:15").SetLocale("zh-CN").DiffForHumans() // 1 月前
carbon.ClearTestNow()
carbon.IsTestNow() // false

For more usage examples, please refer to official document.

For performance test reports, please refer to benchmark report

References

Contributors

Thanks to all the following who contributed to Carbon:

Translators

Ask for help to translate Carbon in other localized languages

How to add new localized language support to carbon

Sponsors

Carbon is a non-commercial open source project. If you want to support Carbon, you can buy a cup of coffee for developer.

Thanks

Carbon had been being developed with GoLand under the free JetBrains Open Source license, I would like to express my thanks here.

JetBrains

License

Carbon is licensed under the MIT License, see the LICENSE file for details.

Sponsor this project

Contributors 42

Languages

AltStyle によって変換されたページ (->オリジナル) /