go-carbon 日期时间处理库开源项目

我要开发同款
匿名用户2020年09月07日
100阅读
开发技术GO语言
所属分类Google Go、常用工具包、程序开发
授权协议MIT

作品详情

carbon是一个轻量级、语义化、对开发者友好的Golang时间处理库,支持链式调用和gorm、xorm等主流orm。

安装使用

//使用github库goget-ugithub.com/golang-module/carbonimport("github.com/golang-module/carbon")//使用gitee库goget-ugitee.com/golang-module/carbonimport("gitee.com/golang-module/carbon")

用法示例

默认时区为Local,即服务器所在时区,假设当前时间为2020-08-0513:14:15

昨天、今天、明天//今天此刻fmt.Sprintf("%s",carbon.Now())//2020-08-0513:14:15carbon.Now().ToString()//2020-08-0513:14:15+0800CSTcarbon.Now().ToDateTimeString()//2020-08-0513:14:15//今天日期carbon.Now().ToDateString()//2020-08-05//今天时间carbon.Now().ToTimeString()//13:14:15//指定时区的今天此刻carbon.Now(Carbon.NewYork).ToDateTimeString()//2020-08-0514:14:15//今天秒级时间戳carbon.Now().Timestamp()//1596604455//今天毫秒级时间戳carbon.Now().TimestampMilli()//1596604455000//今天微秒级时间戳carbon.Now().TimestampMicro()//1596604455000000//今天纳秒级时间戳carbon.Now().TimestampNano()//1596604455000000000//昨天此刻fmt.Sprintf("%s",carbon.Yesterday())//2020-08-0413:14:15carbon.Yesterday().ToString()//2020-08-0413:14:15+0800CSTcarbon.Yesterday().ToDateTimeString()//2020-08-0413:14:15//昨天日期carbon.Yesterday().ToDateString()//2020-08-04//昨天时间carbon.Yesterday().ToTimeString()//13:14:15//指定日期的昨天此刻carbon.Parse("2021-01-2813:14:15").Yesterday().ToDateTimeString()//2021-01-2713:14:15//指定时区的昨天此刻carbon.Yesterday(Carbon.NewYork).ToDateTimeString()//2020-08-0414:14:15//昨天秒级时间戳carbon.Yesterday().Timestamp()//1596518055//昨天毫秒级时间戳carbon.Yesterday().TimestampMilli()//1596518055000//昨天微秒级时间戳carbon.Yesterday().TimestampMicro()//1596518055000000//昨天纳秒级时间戳carbon.Yesterday().TimestampNano()//1596518055000000000//明天此刻fmt.Sprintf("%s",carbon.Tomorrow())//2020-08-0613:14:15carbon.Tomorrow().ToString()//2020-08-0613:14:15+0800CSTcarbon.Tomorrow().ToDateTimeString()//2020-08-0613:14:15//明天日期carbon.Tomorrow().ToDateString()//2020-08-06//明天时间carbon.Tomorrow().ToTimeString()//13:14:15//指定日期的明天此刻carbon.Parse("2021-01-2813:14:15").Tomorrow().ToDateTimeString()//2021-01-2913:14:15//指定时区的明天此刻carbon.Tomorrow(Carbon.NewYork).ToDateTimeString()//2020-08-0614:14:15//明天秒级时间戳carbon.Tomorrow().Timestamp()//1596690855//明天毫秒级时间戳carbon.Tomorrow().TimestampMilli()//1596690855000//明天微秒级时间戳carbon.Tomorrow().TimestampMicro()//1596690855000000//明天纳秒级时间戳carbon.Tomorrow().TimestampNano()//1596690855000000000创建carbon实例//从秒级时间戳创建Carbon实例carbon.CreateFromTimestamp(-1).ToString()//1970-01-0107:59:59+0800CSTcarbon.CreateFromTimestamp(0).ToString()//1970-01-0108:00:00+0800CSTcarbon.CreateFromTimestamp(1).ToString()//1970-01-0108:00:01+0800CSTcarbon.CreateFromTimestamp(1649735755).ToString()//2022-04-1211:55:55+0800CST//从毫秒级时间戳创建Carbon实例carbon.CreateFromTimestampMilli(1649735755981).ToString()//2022-04-1211:55:55.981+0800CST//从微秒级时间戳创建Carbon实例carbon.CreateFromTimestampMicro(1649735755981566).ToString()//2022-04-1211:55:55.981566+0800CST//从纳秒级时间戳创建Carbon实例carbon.CreateFromTimestampNano(1649735755981566000).ToString()//2022-04-1211:55:55.981566+0800CST//从年月日时分秒创建Carbon实例carbon.CreateFromDateTime(2020,8,5,13,14,15).ToString()//2020-08-0513:14:15+0800CST//从年月日时分秒创建Carbon实例,包含毫秒carbon.CreateFromDateTimeMilli(2020,8,5,13,14,15,999).ToString()//2020-08-0513:14:15.999+0800CST//从年月日时分秒创建Carbon实例,包含微秒carbon.CreateFromDateTimeMicro(2020,8,5,13,14,15,999999).ToString()//2020-08-0513:14:15.999999+0800CST//从年月日时分秒创建Carbon实例,包含纳秒carbon.CreateFromDateTimeNano(2020,8,5,13,14,15,999999999).ToString()//2020-08-0513:14:15.999999999+0800CST//从年月日创建Carbon实例(时分秒默认为当前时分秒)carbon.CreateFromDate(2020,8,5).ToString()////2020-08-0513:14:15+0800CST//从年月日创建Carbon实例(时分秒默认为当前时分秒),包含毫秒carbon.CreateFromDateMilli(2020,8,5,999).ToString()//2020-08-0513:14:15.999+0800CST//从年月日创建Carbon实例(时分秒默认为当前时分秒),包含微秒carbon.CreateFromDateMicro(2020,8,5,999999).ToString()//2020-08-0513:14:15.999999+0800CST//从年月日创建Carbon实例(时分秒默认为当前时分秒),包含纳秒carbon.CreateFromDateNano(2020,8,5,999999999).ToString()//2020-08-0513:14:15.999999999+0800CST//从时分秒创建Carbon实例(年月日默认为当前年月日)carbon.CreateFromTime(13,14,15).ToString()//2020-08-0513:14:15+0800CST//从时分秒创建Carbon实例(年月日默认为当前年月日),包含毫秒carbon.CreateFromTimeMilli(13,14,15,999).ToString()//2020-08-0513:14:15.999+0800CST//从时分秒创建Carbon实例(年月日默认为当前年月日),包含微秒carbon.CreateFromTimeMicro(13,14,15,999999).ToString()//2020-08-0513:14:15.999999+0800CST//从时分秒创建Carbon实例(年月日默认为当前年月日),包含纳秒carbon.CreateFromTimeNano(13,14,15,999999999).ToString()//2020-08-0513:14:15.999999999+0800CST将标准格式时间字符串解析成carbon实例carbon.Parse("").ToDateTimeString()//空字符串carbon.Parse("0").ToDateTimeString()//空字符串carbon.Parse("0000-00-0000:00:00").ToDateTimeString()//空字符串carbon.Parse("0000-00-00").ToDateTimeString()//空字符串carbon.Parse("00:00:00").ToDateTimeString()//空字符串carbon.Parse("2020-08-05").ToString()//2020-08-0500:00:00+0800CSTcarbon.Parse("2020-08-0513:14:15").ToString()//2020-08-0513:14:15+0800CSTcarbon.Parse("2020-08-0513:14:15.999").ToString()//2020-08-0513:14:15.999+0800CSTcarbon.Parse("2020-08-0513:14:15.999999").ToString()//2020-08-0513:14:15.999999+0800CSTcarbon.Parse("2020-08-0513:14:15.999999999").ToString()//2020-08-0513:14:15.999999999+0800CSTcarbon.Parse("2020-08-05T13:14:15+08:00").ToString()//2020-08-0513:14:15+0800CSTcarbon.Parse("2020-08-05T13:14:15.999+08:00").ToString()//2020-08-0513:14:15.999+0800CSTcarbon.Parse("2020-08-05T13:14:15.999999+08:00").ToString()//2020-08-0513:14:15.999999+0800CSTcarbon.Parse("2020-08-05T13:14:15.999999999+08:00").ToString()//2020-08-0513:14:15.999999999+0800CSTcarbon.Parse("20200805").ToString()//2020-08-0500:00:00+0800CSTcarbon.Parse("20200805131415").ToString()//2020-08-0513:14:15+0800CSTcarbon.Parse("20200805131415.999").ToString()//2020-08-0513:14:15.999+0800CSTcarbon.Parse("20200805131415.999999").ToString()//2020-08-0513:14:15.999999+0800CSTcarbon.Parse("20200805131415.999999999").ToString()//2020-08-0513:14:15.999999999+0800CST通过格式模板将字符串解析成carbon实例carbon.ParseByFormat("2020|08|0513|14|15","Y|m|dH|i|s").ToDateTimeString()//2020-08-0513:14:15carbon.ParseByFormat("Itis2020-08-0513:14:15","\\I\\t\\i\\sY-m-dH:i:s").ToDateTimeString()//2020-08-0513:14:15carbon.ParseByFormat("今天是2020年08月05日13时14分15秒","今天是Y年m月d日H时i分s秒").ToDateTimeString()//2020-08-0513:14:15carbon.ParseByFormat("2020-08-0513:14:15","Y-m-dH:i:s",carbon.Tokyo).ToDateTimeString()//2020-08-0514:14:15通过布局模板将字符串解析成carbon实例carbon.ParseByLayout("2020|08|0513|14|15","2006|01|0215|04|05").ToDateTimeString()//2020-08-0513:14:15carbon.ParseByLayout("Itis2020-08-0513:14:15","Itis2006-01-0215:04:05").ToDateTimeString()//2020-08-0513:14:15carbon.ParseByLayout("今天是2020年08月05日13时14分15秒","今天是2006年01月02日15时04分05秒").ToDateTimeString()//2020-08-0513:14:15carbon.ParseByLayout("2020-08-0513:14:15","2006-01-0215:04:05",carbon.Tokyo).ToDateTimeString()//2020-08-0514:14:15carbon和time.Time互转//将time.Time转换成Carboncarbon.Time2Carbon(time.Now())//将Carbon转换成time.Timecarbon.Now().Carbon2Time()

更多用法请查看下方软件文档

声明:本文仅代表作者观点,不代表本站立场。如果侵犯到您的合法权益,请联系我们删除侵权资源!如果遇到资源链接失效,请您通过评论或工单的方式通知管理员。未经允许,不得转载,本站所有资源文章禁止商业使用运营!
下载安装【程序员客栈】APP
实时对接需求、及时收发消息、丰富的开放项目需求、随时随地查看项目状态

评论