diff --git a/README.en.md b/README.en.md index 219aa903..1e769284 100755 --- a/README.en.md +++ b/README.en.md @@ -48,7 +48,7 @@ carbon.Now().ToTimestampWithMicrosecond() // 1596604455000000 // Timestamp with nanosecond of today carbon.Now().ToTimestampWithNanosecond() // 1596604455000000000 -// Datetime of yesterday +// Datetime of yesterday carbon.Yesterday().ToDateTimeString() // 2020-08-04 13:14:15 // Date of yesterday carbon.Yesterday().ToDateString() // 2020-08-04 @@ -63,6 +63,8 @@ carbon.Yesterday().ToTimestampWithMillisecond() // 1596518055000 carbon.Yesterday().ToTimestampWithMicrosecond() // 1596518055000000 // Timestamp with nanosecond of yesterday carbon.Yesterday().ToTimestampWithNanosecond() // 1596518055000000000 +// Datetime of yesterday in other day +carbon.Parse("2021-01-28 13:14:15").Yesterday().ToDateTimeString() // 2021-01-27 13:14:15 // Datetime of tomorrow carbon.Tomorrow().ToDateTimeString() // 2020-08-06 13:14:15 @@ -79,6 +81,8 @@ carbon.Tomorrow().ToTimestampWithMillisecond() // 1596690855000 carbon.Tomorrow().ToTimestampWithMicrosecond() // 1596690855000000 // Timestamp with nanosecond of tomorrow carbon.Tomorrow().ToTimestampWithNanosecond() // 1596690855000000000 +// Datetime of tomorrow in other day +carbon.Parse("2021-01-28 13:14:15").Tomorrow().ToDateTimeString() // 2021-01-29 13:14:15 ``` ##### Create carbon instance @@ -107,7 +111,7 @@ carbon.CreateFromGoTime(time.Now()).ToTimestamp() // 1596604455 // Time.time convert to Carbon carbon.CreateFromGoTime(time.Now()) // Carbon convert to Time.time -carbon.Now().ToGoTime() +carbon.Now().ToGoTime() 或 carbon.Now().Time ``` ##### Parse standard time format string @@ -131,22 +135,6 @@ carbon.ParseByFormat("2020年08月05日", "Y年m月d日").ToDateTimeString() // carbon.ParseByFormat("13时14分15秒", "H时i分s秒").ToDateTimeString() // 2020-08-05 13:14:15 ``` -##### Parse duration time string (base on now) -```go -// Ten hours later -carbon.ParseByDuration("10h").ToDateTimeString() // 2020-08-06 23:14:15 -// Ten and a half hours ago -carbon.ParseByDuration("-10.5h").ToDateTimeString // 2020-08-05 02:44:15 -// Ten minutes later -carbon.ParseByDuration("10m").ToDateTimeString // 2020-08-05 13:24:15 -// Ten and a half minutes ago -carbon.ParseByDuration("-10.5m").ToDateTimeString // 2020-08-05 13:03:45 -// Ten seconds later -carbon.ParseByDuration("10s").ToDateTimeString // 2020-08-05 13:14:25 -// Ten seconds ago -carbon.ParseByDuration("-10.5s").ToDateTimeString // 2020-08-05 13:14:04 -``` - ##### Time setter ```go // Set timezone @@ -319,6 +307,7 @@ carbon.Parse("2020-08-05 13:14:15").SubDay().ToDateTimeString() // 2020-08-04 13 carbon.Parse("2020-08-05 13:14:15").AddHours(3).ToDateTimeString() // 2020-08-05 16:14:15 // Add two and a half hours carbon.Parse("2020-08-05 13:14:15").AddDuration("2.5h").ToDateTimeString() // 2020-08-05 15:44:15 +carbon.Parse("2020-08-05 13:14:15").AddDuration("2h30m").ToDateTimeString() // 2020-08-05 15:44:15 // Add one hour carbon.Parse("2020-08-05 13:14:15").AddHour().ToDateTimeString() // 2020-08-05 14:14:15 @@ -326,6 +315,7 @@ carbon.Parse("2020-08-05 13:14:15").AddHour().ToDateTimeString() // 2020-08-05 1 carbon.Parse("2020-08-05 13:14:15").SubHours(3).ToDateTimeString() // 2020-08-05 10:14:15 // Subtract two and a half hours carbon.Parse("2020-08-05 13:14:15").SubDuration("2.5h").ToDateTimeString() // 2020-08-05 10:44:15 +carbon.Parse("2020-08-05 13:14:15").SubDuration("2h30m").ToDateTimeString() // 2020-08-05 10:44:15 // Subtract one hour carbon.Parse("2020-08-05 13:14:15").SubHour().ToDateTimeString() // 2020-08-05 12:14:15 @@ -333,6 +323,7 @@ carbon.Parse("2020-08-05 13:14:15").SubHour().ToDateTimeString() // 2020-08-05 1 carbon.Parse("2020-08-05 13:14:15").AddMinutes(3).ToDateTimeString() // 2020-08-05 13:17:15 // Add two and a half minutes carbon.Parse("2020-08-05 13:14:15").AddDuration("2.5m").ToDateTimeString() // 2020-08-05 13:16:45 +carbon.Parse("2020-08-05 13:14:15").AddDuration("2m30s").ToDateTimeString() // 2020-08-05 13:16:45 // Add one minute carbon.Parse("2020-08-05 13:14:15").AddMinute().ToDateTimeString() // 2020-08-05 13:15:15 @@ -620,6 +611,12 @@ carbon.Parse("2020-08-05 13:14:15").Hour() // 13 carbon.Parse("2020-08-05 13:14:15").Minute() // 14 // Get current second carbon.Parse("2020-08-05 13:14:15").Second() // 15 +// Get current millisecond +carbon.Parse("2020-08-05 13:14:15").Millisecond() // 1596604455000 +// Get current microsecond +carbon.Parse("2020-08-05 13:14:15").Microsecond() // 1596604455000000 +// Get current nanosecond +carbon.Parse("2020-08-05 13:14:15").Nanosecond() // 1596604455000000000 // Get timezone name carbon.SetTimezone(carbon.PRC).Timezone() // PRC @@ -803,27 +800,43 @@ invalid timezone "XXXX", please see the $GOROOT/lib/time/zoneinfo.zip file for a #### Appendix ##### Format sign table -| sign | desc | type | length | range | example | -| ------------ | ------------ | ------------ | ------------ | ------------ | ------------ | -| Y | year | number | 4 | 0000-9999 | 2020 | -| y | year | number | 2 | 00-99 | 20 | -| M | month | letter | 3 | Jan-Dec | Aug | -| m | month | number | 2 | 01-12 | 08 | -| F | month | letter | - | January-December | August | -| n | month | number | 1/2 | 1-12 | 8 | -| l | weekday | letter | - | Monday-Sunday | Wednesday | -| D | weekday | letter | 3 | Mon-Sun | Wed | -| d | day | number | 2 | 01-31 | 05 | -| j | day | number | 1/2 |1-31 | 5 | -| H | hour | number | 2 | 00-23 | 15 | -| h | hour | number | 2 | 00-11 | 03 | -| i | minute | number | 2 | 01-59 | 14 | -| s | second | number | 2 | 01-59 | 15 | -| P | Ante Meridiem/Post Meridiem | letter | 2 | AM/PM | PM | -| p | ante meridiem/post meridiem | letter | 2 | am/pm | pm | +| sign | desc | length | range | example | +| :------------: | :------------: | :------------: | :------------: | :------------: | +| d | Day of the month, 2 digits with leading zeros | 2 | 01-31 | 05 | +| D | A textual representation of a day, three letters | 3 | Mon-Sun | Wed | +| j | Day of the month without leading zeros | 1/2 |1-31 | 5 | +| l | A full textual representation of the day of the week | - | Monday-Sunday | Wednesday | +| F | A full textual representation of a month | - | January-December | August | +| m | Numeric representation of a month, with leading zeros | 2 | 01-12 | 08 | +| M | A short textual representation of a month, three letters | 3 | Jan-Dec | Aug | +| n | Numeric representation of a month, without leading zeros | 1/2 | 1-12 | 8 | +| y | A two digit representation of a year | 2 | 00-99 | 20 | +| Y | A full numeric representation of a year, 4 digits | 4 | 0000-9999 | 2020 | +| a | A full numeric representation of a year, 4 digits | 2 | am/pm | pm | +| A | Uppercase Ante meridiem and Post meridiem | 2 | AM/PM | PM | +| g | 12-hour format of an hour without leading zeros | 1/2 | 1-12 | 1 | +| G | 24-hour format of an hour without leading zeros | 1/2 | 0-23 | 15 | +| h | 12-hour format of an hour with leading zeros | 2 | 00-11 | 03 | +| H | 24-hour format of an hour with leading zeros | 2 | 00-23 | 15 | +| i | Minutes with leading zeros | 2 | 01-59 | 14 | +| s | Seconds with leading zeros | 2 | 01-59 | 15 | +| c | ISO 8601 date | - | - | 2020-08-05T15:19:21+00:00 | +| r | RFC 2822 date | - | - | Thu, 21 Dec 2020 16:01:07 +0200 | +| O | Difference to Greenwich time (GMT) without colon between hours and minutes | - | - | +0200 | +| P | Difference to Greenwich time (GMT) with colon between hours and minutes | - | - | +02:00 | +| T | Timezone abbreviation | - | - | EST | +| N | ISO-8601 numeric representation of the day of the week | 1 | 1-7 | 6 | +| L | Whether it's a leap year | 1 | 0-1 | 1 | +| U | Seconds since the Unix Epoch (January 1 1970 00:00:00 GMT) | 10 | - | 1611818268 | +| u | Microseconds| - | - | 999 | +| w | Numeric representation of the day of the week | 1 | 0-6 | 6 | +| t | Number of days in the given month | 2 | 28-31 | 30 | +| z | The day of the year (starting from 0) | 1/2/3 | 0-365 | 15 | +| e | Timezone identifier | - | - | UTC | #### Reference * [briannesbitt/carbon](https://github.com/briannesbitt/Carbon) * [uniplaces/carbon](https://github.com/uniplaces/carbon) * [jinzhu/now](https://github.com/jinzhu/now/) * [araddon/dateparse](https://github.com/araddon/dateparse) +* [goframe/gtime](https://github.com/gogf/gf/tree/master/os/gtime) diff --git a/README.md b/README.md index 1fcea007..4f12288e 100755 --- a/README.md +++ b/README.md @@ -62,6 +62,8 @@ carbon.Yesterday().ToTimestampWithMillisecond() // 1596518055000 carbon.Yesterday().ToTimestampWithMicrosecond() // 1596518055000000 // 明天纳秒级时间戳 carbon.Yesterday().ToTimestampWithNanosecond() // 1596518055000000000 +// 指定日期的昨天此刻 +carbon.Parse("2021-01-28 13:14:15").Yesterday().ToDateTimeString() // 2021-01-27 13:14:15 // 明天此刻 carbon.Tomorrow().ToDateTimeString() // 2020-08-06 13:14:15 @@ -78,6 +80,8 @@ carbon.Tomorrow().ToTimestampWithMillisecond() // 1596690855000 carbon.Tomorrow().ToTimestampWithMicrosecond() // 1596690855000000 // 明天纳秒级时间戳 carbon.Tomorrow().ToTimestampWithNanosecond() // 1596690855000000000 +// 指定日期的明天此刻 +carbon.Parse("2021-01-28 13:14:15").Tomorrow().ToDateTimeString() // 2021-01-29 13:14:15 ``` ##### 创建 Carbon 实例 @@ -106,7 +110,7 @@ carbon.CreateFromGoTime(time.Now()).ToTimestamp() // 1596604455 // Go 内置 Time.time 转 Carbon carbon.CreateFromGoTime(time.Now()) // Carbon 转 Go 内置 Time.time -carbon.Now().ToGoTime() +carbon.Now().ToGoTime() 或 carbon.Now().Time ``` ##### 解析标准格式时间字符串 @@ -130,25 +134,6 @@ carbon.ParseByFormat("2020年08月05日", "Y年m月d日").ToDateTimeString() // carbon.ParseByFormat("13时14分15秒", "H时i分s秒").ToDateTimeString() // 2020-08-05 13:14:15 ``` -##### 解析持续时间字符串(基于当前时间) - -支持正负整数/浮点数和符号ns(纳秒)、us(微妙)、ms(毫秒)、s(秒)、m(分钟)、h(小时)的组合 - -```go -// 十小时后 -carbon.ParseByDuration("10h").ToDateTimeString() // 2020-08-06 23:14:15 -// 十小时半前 -carbon.ParseByDuration("-10.5h").ToDateTimeString() // 2020-08-05 02:44:15 -// 十分钟后 -carbon.ParseByDuration("10m").ToDateTimeString() // 2020-08-05 13:24:15 -// 十分钟半前 -carbon.ParseByDuration("-10.5m").ToDateTimeString() // 2020-08-05 13:03:45 -// 十秒后 -carbon.ParseByDuration("10s").ToDateTimeString() // 2020-08-05 13:14:25 -// 十秒半前 -carbon.ParseByDuration("-10.5s").ToDateTimeString() // 2020-08-05 13:14:04 -``` - ##### 时间设置 ```go // 设置时区 @@ -305,6 +290,7 @@ carbon.Parse("2020-08-05 13:14:15").SubDay().ToDateTimeString() // 2020-08-04 13 carbon.Parse("2020-08-05 13:14:15").AddHours(3).ToDateTimeString() // 2020-08-05 16:14:15 // 二小时半后 carbon.Parse("2020-08-05 13:14:15").AddDuration("2.5h").ToDateTimeString() // 2020-08-05 15:44:15 +carbon.Parse("2020-08-05 13:14:15").AddDuration("2h30m").ToDateTimeString() // 2020-08-05 15:44:15 // 一小时后 carbon.Parse("2020-08-05 13:14:15").AddHour().ToDateTimeString() // 2020-08-05 14:14:15 @@ -312,6 +298,7 @@ carbon.Parse("2020-08-05 13:14:15").AddHour().ToDateTimeString() // 2020-08-05 1 carbon.Parse("2020-08-05 13:14:15").SubHours(3).ToDateTimeString() // 2020-08-05 10:14:15 // 二小时半前 carbon.Parse("2020-08-05 13:14:15").SubDuration("2.5h").ToDateTimeString() // 2020-08-05 10:44:15 +carbon.Parse("2020-08-05 13:14:15").SubDuration("2h30m").ToDateTimeString() // 2020-08-05 10:44:15 // 一小时前 carbon.Parse("2020-08-05 13:14:15").SubHour().ToDateTimeString() // 2020-08-05 12:14:15 @@ -319,6 +306,7 @@ carbon.Parse("2020-08-05 13:14:15").SubHour().ToDateTimeString() // 2020-08-05 1 carbon.Parse("2020-08-05 13:14:15").AddMinutes(3).ToDateTimeString() // 2020-08-05 13:17:15 // 二分钟半后 carbon.Parse("2020-08-05 13:14:15").AddDuration("2.5m").ToDateTimeString() // 2020-08-05 13:16:45 +carbon.Parse("2020-08-05 13:14:15").AddDuration("2m30s").ToDateTimeString() // 2020-08-05 13:16:45 // 一分钟后 carbon.Parse("2020-08-05 13:14:15").AddMinute().ToDateTimeString() // 2020-08-05 13:15:15 @@ -326,6 +314,7 @@ carbon.Parse("2020-08-05 13:14:15").AddMinute().ToDateTimeString() // 2020-08-05 carbon.Parse("2020-08-05 13:14:15").SubMinutes(3).ToDateTimeString() // 2020-08-05 13:11:15 // 二分钟半前 carbon.Parse("2020-08-05 13:14:15").SubDuration("2.5m").ToDateTimeString() // 2020-08-05 13:11:45 +carbon.Parse("2020-08-05 13:14:15").SubDuration("2m30s").ToDateTimeString() // 2020-08-05 13:11:45 // 一分钟前 carbon.Parse("2020-08-05 13:14:15").SubMinute().ToDateTimeString() // 2020-08-05 13:13:15 @@ -611,6 +600,12 @@ carbon.Parse("2020-08-05 13:14:15").Hour() // 13 carbon.Parse("2020-08-05 13:14:15").Minute() // 14 // 获取当前秒 carbon.Parse("2020-08-05 13:14:15").Second() // 15 +// 获取当前毫秒 +carbon.Parse("2020-08-05 13:14:15").Millisecond() // 1596604455000 +// 获取当前微秒 +carbon.Parse("2020-08-05 13:14:15").Microsecond() // 1596604455000000 +// 获取当前纳秒 +carbon.Parse("2020-08-05 13:14:15").Nanosecond() // 1596604455000000000 // 获取时区 carbon.SetTimezone(carbon.PRC).Timezone() // PRC @@ -621,7 +616,7 @@ carbon.Parse("2002-01-01 13:14:15").Age() // 17 carbon.Parse("2002-12-31 13:14:15").Age() // 18 ``` -> 关于第几周的计算请查看 [ISO8601标准](https://baike.baidu.com/item/ISO%208601/3910715) +> 关于第几周的计算如有疑惑请查看 [ISO8601标准](https://baike.baidu.com/item/ISO%208601/3910715) ##### 农历支持 ```go @@ -791,35 +786,61 @@ fmt.Println(c.ToDateTimeString()) // 输出 invalid timezone "XXXX", please see the $GOROOT/lib/time/zoneinfo.zip file for all valid timezone ``` -> 建议使用SetTimezone()、Parse()、ParseByFormat()、ParseByDuration()、AddDuration()、SubDuration()等方法时先进行错误处理判断,除非你能确保传入参数无误 +> 建议使用SetTimezone()、Parse()、ParseByFormat()、AddDuration()、SubDuration()等方法时先进行错误处理判断,除非你能确保传入参数无误 #### 附录 ##### 格式化符号表 -| 符号 | 描述 | 类型 | 长度 | 范围 | 示例 | -| :------------: | :------------: | :------------: | :------------: | :------------: | :------------: | -| Y | 年份 | 数字 | 4 | 0000-9999 | 2020 | -| y | 年份 | 数字 | 2 | 00-99 | 20 | -| M | 月份 | 字母 | 3 | Jan-Dec | Aug | -| m | 月份 | 数字 | 2 | 01-12 | 08 | -| F | 月份 | 字母 | - | January-December | August | -| n | 月份 | 数字 | 1/2 | 1-12 | 8 | -| l | 周几 | 字母 | - | Monday-Sunday | Wednesday | -| D | 周几 | 字母 | 3 | Mon-Sun | Wed | -| d | 天数 | 数字 | 2 | 01-31 | 05 | -| j | 天数 | 数字 | 1/2 |1-31 | 5 | -| H | 小时 | 数字 | 2 | 00-23 | 15 | -| h | 小时 | 数字 | 2 | 00-11 | 03 | -| i | 分钟 | 数字 | 2 | 01-59 | 14 | -| s | 秒钟 | 数字 | 2 | 01-59 | 15 | -| P | 上下午 | 字母 | 2 | AM/PM | PM | -| p | 上下午 | 字母 | 2 | am/pm | pm | +| 符号 | 描述 | 长度 | 范围 | 示例 | +| :------------: | :------------: | :------------: | :------------: | :------------: | +| d | 月份中的第几天,有前导零 | 2 | 01-31 | 05 | +| D | 星期中的第几天 | 3 | Mon-Sun | Wed | +| j | 月份中的第几天,没有前导零 | 1/2 |1-31 | 5 | +| l | 完整单词表示的星期几 | - | Monday-Sunday | Wednesday | +| F | 完整单词表示的月份 | - | January-December | August | +| m | 数字表示的月份,有前导零 | 2 | 01-12 | 08 | +| M | 缩写单词表示的月份 | 3 | Jan-Dec | Aug | +| n | 数字表示的月份,没有前导零 | 1/2 | 1-12 | 8 | +| y | 数字表示的简写年份,有前导零 | 2 | 00-99 | 20 | +| Y | 数字表示的完整年份 | 4 | 0000-9999 | 2020 | +| a | 小写的上午和下午缩写字母 | 2 | am/pm | pm | +| A | 大写的上午和下午缩写字母 | 2 | AM/PM | PM | +| g | 数字表示的小时,12 小时格式,没有前导零 | 1/2 | 1-12 | 1 | +| G | 数字表示的小时,24 小时格式,没有前导零 | 1/2 | 0-23 | 15 | +| h | 数字表示的小时,12 小时格式,有前导零 | 2 | 00-11 | 03 | +| H | 数字表示的小时,24 小时格式,有前导零 | 2 | 00-23 | 15 | +| i | 数字表示的分钟,有前导零 | 2 | 01-59 | 14 | +| s | 数字表示的秒数,有前导零 | 2 | 01-59 | 15 | +| c | ISO8601 格式的日期 | - | - | 2020-08-05T15:19:21+00:00 | +| r | RFC822 格式的日期 | - | - | Thu, 21 Dec 2020 16:01:07 +0200 | +| O | 与格林威治时间相差的小时数 | - | - | +0200 | +| P | 与格林威治时间相差的小时数,小时和分钟之间有冒号分隔 | - | - | +02:00 | +| T | 本机所在的时区 | - | - | EST | +| N | ISO-8601 格式数字表示的星期中的第几天 | 1 | 1-7 | 6 | +| L | 是否为闰年,如果是闰年为 1,否则为 0 | 1 | 0-1 | 1 | +| U | 秒级时间戳 | 10 | - | 1611818268 | +| u | 数字表示的毫秒 | - | - | 999 | +| w | 数字表示的星期中的第几天 | 1 | 0-6 | 6 | +| t | 指定的月份有几天 | 2 | 28-31 | 30 | +| z | 年份中的第几天 | 1/2/3 | 0-365 | 15 | +| e | 时区标识 | - | - | UTC | #### 参考项目 * [briannesbitt/carbon](https://github.com/briannesbitt/Carbon) * [uniplaces/carbon](https://github.com/uniplaces/carbon) * [jinzhu/now](https://github.com/jinzhu/now) * [araddon/dateparse](https://github.com/araddon/dateparse) +* [goframe/gtime](https://github.com/gogf/gf/tree/master/os/gtime) #### 更新日志 +##### 2021-01-29 +* 修复readme.md文档部分描述错误 +* 修复ToDayDateTimeString()方法缺少In(c.Loc)的错误 +* 修复IsYesterday()方法某些情况下的判断错误 +* 修复IsTomorrow()方法某些情况下的判断错误 +* 移除ParseByDuration()方法 +* 优化Yesterday()方法,支持指定时间的昨天,未指定时间则默认为当前时间的昨天 +* 优化Tomorrow()方法,支持指定时间的明天,未指定时间则默认为当前时间的明天 +* 优化Format()方法,增加对N、L、G、U、O、P、T等格式化符号支持,具体含义请查看格式化符号表 + ##### 2021-01-26 * 更新readme.md文档 * 完善单元测试代码覆盖率 @@ -830,10 +851,10 @@ invalid timezone "XXXX", please see the $GOROOT/lib/time/zoneinfo.zip file for a * 新增AddCentury()方法获取一世纪后时间 * 新增SubCenturies()方法获取N世纪前时间 * 新增SubCentury()方法获取一世纪前时间 -* 新增NextCenturies()方法获取N世纪后时间(不跨月) -* 新增NextCentury()方法获取一世纪后时间(不跨月) -* 新增PreCenturies()方法获取N世纪前时间(不跨月) -* 新增PreCentury()方法获取一世纪前时间(不跨月) +* 新增NextCenturies()方法获取N世纪后时间(月份不溢出) +* 新增NextCentury()方法获取一世纪后时间(月份不溢出) +* 新增PreCenturies()方法获取N世纪前时间(月份不溢出) +* 新增PreCentury()方法获取一世纪前时间(月份不溢出) ##### 2021-01-18 * 更新readme.md文档 diff --git a/carbon.go b/carbon.go index 5229e577..190e90bd 100755 --- a/carbon.go +++ b/carbon.go @@ -12,7 +12,7 @@ type Carbon struct { Error error } -// Now 当前(指定时区) +// Now 当前 func (c Carbon) Now() Carbon { c.Time = time.Now() return c @@ -23,9 +23,13 @@ func Now() Carbon { return SetTimezone(Local).Now() } -// Tomorrow 明天(指定时区) +// Tomorrow 明天 func (c Carbon) Tomorrow() Carbon { - c.Time = time.Now().AddDate(0, 0, 1) + if c.Time.IsZero() { + c.Time = time.Now().AddDate(0, 0, 1) + } else { + c.Time = c.Time.AddDate(0, 0, 1) + } return c } @@ -34,9 +38,13 @@ func Tomorrow() Carbon { return SetTimezone(Local).Tomorrow() } -// Yesterday 昨天(指定时区) +// Yesterday 昨天 func (c Carbon) Yesterday() Carbon { - c.Time = time.Now().AddDate(0, 0, -1) + if c.Time.IsZero() { + c.Time = time.Now().AddDate(0, 0, -1) + } else { + c.Time = c.Time.AddDate(0, 0, -1) + } return c } @@ -45,7 +53,7 @@ func Yesterday() Carbon { return SetTimezone(Local).Yesterday() } -// CreateFromTimestamp 从时间戳创建Carbon实例(指定时区) +// CreateFromTimestamp 从时间戳创建Carbon实例 func (c Carbon) CreateFromTimestamp(timestamp int64) Carbon { ts := timestamp switch len(strconv.FormatInt(timestamp, 10)) { @@ -69,7 +77,7 @@ func CreateFromTimestamp(timestamp int64) Carbon { return SetTimezone(Local).CreateFromTimestamp(timestamp) } -// CreateFromDateTime 从年月日时分秒创建Carbon实例(指定时区) +// CreateFromDateTime 从年月日时分秒创建Carbon实例 func (c Carbon) CreateFromDateTime(year int, month int, day int, hour int, minute int, second int) Carbon { c.Time = time.Date(year, time.Month(month), day, hour, minute, second, 0, c.Loc) return c @@ -80,7 +88,7 @@ func CreateFromDateTime(year int, month int, day int, hour int, minute int, seco return SetTimezone(Local).CreateFromDateTime(year, month, day, hour, minute, second) } -// CreateFromDate 从年月日创建Carbon实例(指定时区) +// CreateFromDate 从年月日创建Carbon实例 func (c Carbon) CreateFromDate(year int, month int, day int) Carbon { hour, minute, second := time.Now().Clock() c.Time = time.Date(year, time.Month(month), day, hour, minute, second, 0, c.Loc) @@ -92,7 +100,7 @@ func CreateFromDate(year int, month int, day int) Carbon { return SetTimezone(Local).CreateFromDate(year, month, day) } -// CreateFromTime 从时分秒创建Carbon实例(指定时区) +// CreateFromTime 从时分秒创建Carbon实例 func (c Carbon) CreateFromTime(hour int, minute int, second int) Carbon { year, month, day := time.Now().Date() c.Time = time.Date(year, month, day, hour, minute, second, 0, c.Loc) @@ -104,7 +112,7 @@ func CreateFromTime(hour int, minute int, second int) Carbon { return SetTimezone(Local).CreateFromTime(hour, minute, second) } -// CreateFromGoTime 从原生time.Time创建Carbon实例(指定时区) +// CreateFromGoTime 从原生time.Time创建Carbon实例 func (c Carbon) CreateFromGoTime(tt time.Time) Carbon { c.Time = tt return c @@ -115,34 +123,35 @@ func CreateFromGoTime(tt time.Time) Carbon { return SetTimezone(Local).CreateFromGoTime(tt) } -// Parse 解析标准格式时间字符串(指定时区) +// Parse 解析标准格式时间字符串 func (c Carbon) Parse(value string) Carbon { if c.Error != nil { return c } + layout := DateTimeFormat + if value == "" || value == "0" || value == "0000-00-00 00:00:00" || value == "0000-00-00" || value == "00:00:00" { return c } - layout := DateTimeFormat - if len(value) == 10 && strings.Count(value, "-") == 2 { layout = DateFormat } - if len(value) == 14 { - layout = ShortDateTimeFormat - } - - if len(value) == 8 { - layout = ShortDateFormat - } - if strings.Index(value, "T") == 10 { layout = RFC3339Format } + if _, err := strconv.ParseInt(value, 10, 64); err == nil { + switch len(value) { + case 8: + layout = ShortDateFormat + case 14: + layout = ShortDateTimeFormat + } + } + tt, err := parseByLayout(value, layout) c.Time = tt c.Error = err @@ -154,7 +163,7 @@ func Parse(value string) Carbon { return SetTimezone(Local).Parse(value) } -// ParseByFormat 解析指定格式时间字符串(指定时区) +// ParseByFormat 解析指定格式时间字符串 func (c Carbon) ParseByFormat(value string, format string) Carbon { if c.Error != nil { return c @@ -171,24 +180,6 @@ func ParseByFormat(value string, format string) Carbon { return SetTimezone(Local).ParseByFormat(value, format) } -// ParseByDuration 解析持续时间字符串(指定时区) -// 支持正负整数/浮点数和符号ns(纳秒)、us(微妙)、ms(毫秒)、s(秒)、m(分钟)、h(小时)的组合 -func (c Carbon) ParseByDuration(duration string) Carbon { - if c.Error != nil { - return c - } - td, err := parseByDuration(duration) - c.Time = time.Now().Add(td) - c.Error = err - return c -} - -// ParseByDuration 解析持续时间字符串(默认时区) -// 支持正负整数/浮点数和符号ns(纳秒)、us(微妙)、ms(毫秒)、s(秒)、m(分钟)、h(小时)的组合 -func ParseByDuration(duration string) Carbon { - return SetTimezone(Local).ParseByDuration(duration) -} - // AddDurations 按照持续时间字符串增加时间 // 支持整数/浮点数和符号ns(纳秒)、us(微妙)、ms(毫秒)、s(秒)、m(分钟)、h(小时)的组合 func (c Carbon) AddDuration(duration string) Carbon { @@ -413,27 +404,27 @@ func (c Carbon) SubSecond() Carbon { return c.SubSeconds(1) } -// NextCenturies N世纪后 +// NextCenturies N世纪后(不会出现月份溢出) func (c Carbon) NextCenturies(centuries int) Carbon { return c.NextYears(centuries * YearsPerCentury) } -// NextCentury 1世纪后 +// NextCentury 1世纪后(不会出现月份溢出) func (c Carbon) NextCentury() Carbon { return c.NextYears(YearsPerCentury) } -// PreCenturies N世纪前 +// PreCenturies N世纪前(不会出现月份溢出) func (c Carbon) PreCenturies(centuries int) Carbon { return c.PreYears(centuries * YearsPerCentury) } -// PreYears 1世纪前 +// PreYears 1世纪前(不会出现月份溢出) func (c Carbon) PreCentury() Carbon { return c.PreYears(YearsPerCentury) } -// NextYears N年后 +// NextYears N年后(不会出现月份溢出) func (c Carbon) NextYears(years int) Carbon { year := c.Time.Year() + years month := c.Time.Month() @@ -450,42 +441,42 @@ func (c Carbon) NextYears(years int) Carbon { return c } -// NextYear 1年后 +// NextYear 1年后(不会出现月份溢出) func (c Carbon) NextYear() Carbon { return c.NextYears(1) } -// PreYears N年前 +// PreYears N年前(不会出现月份溢出) func (c Carbon) PreYears(years int) Carbon { return c.NextYears(-years) } -// PreYear 1年前 +// PreYear 1年前(不会出现月份溢出) func (c Carbon) PreYear() Carbon { return c.NextYears(-1) } -// NextQuarters N季度后 +// NextQuarters N季度后(不会出现月份溢出) func (c Carbon) NextQuarters(quarters int) Carbon { return c.NextMonths(quarters * MonthsPerQuarter) } -// NextQuarters 1季度后 +// NextQuarters 1季度后(不会出现月份溢出) func (c Carbon) NextQuarter() Carbon { return c.NextQuarters(1) } -// PreQuarters N季度前 +// PreQuarters N季度前(不会出现月份溢出) func (c Carbon) PreQuarters(quarters int) Carbon { return c.NextMonths(-quarters * MonthsPerQuarter) } -// PreQuarter 1季度前 +// PreQuarter 1季度前(不会出现月份溢出) func (c Carbon) PreQuarter() Carbon { return c.PreQuarters(1) } -// NextMonths N月后 +// NextMonths N月后(不会出现月份溢出) func (c Carbon) NextMonths(months int) Carbon { year := c.Time.Year() month := c.Time.Month() + time.Month(months) @@ -502,17 +493,17 @@ func (c Carbon) NextMonths(months int) Carbon { return c } -// NextMonth 1月后 +// NextMonth 1月后(不会出现月份溢出) func (c Carbon) NextMonth() Carbon { return c.NextMonths(1) } -// PreMonths N月前 +// PreMonths N月前(不会出现月份溢出) func (c Carbon) PreMonths(months int) Carbon { return c.NextMonths(-months) } -// PreMonth 1月前 +// PreMonth 1月前(不会出现月份溢出) func (c Carbon) PreMonth() Carbon { return c.PreMonths(1) } diff --git a/carbon_test.go b/carbon_test.go index 33ea14c2..314de2f8 100755 --- a/carbon_test.go +++ b/carbon_test.go @@ -193,9 +193,9 @@ func TestCarbon_Yesterday(t *testing.T) { t.Fatalf("Expected %s, but got %s", expected, output) } - output = SetTimezone(PRC).Yesterday().Time.Format(DateTimeFormat) + output = SetTimezone(PRC).Parse("2020-08-05").Yesterday().Time.Format(DateFormat) - if expected != output { + if "2020-08-04" != output { t.Fatalf("Expected %s, but got %s", expected, output) } } @@ -209,9 +209,9 @@ func TestCarbon_Tomorrow(t *testing.T) { t.Fatalf("Expected %s, but got %s", expected, output) } - output = SetTimezone(PRC).Tomorrow().Time.Format(DateTimeFormat) + output = SetTimezone(PRC).Parse("2020-08-05").Tomorrow().Time.Format(DateFormat) - if expected != output { + if "2020-08-06" != output { t.Fatalf("Expected %s, but got %s", expected, output) } } @@ -698,59 +698,6 @@ func TestCarbon_ParseByFormat2(t *testing.T) { } } -var ParseByDurationTests = []struct { - input string // 输入值 - duration string // 输入参数 - output string // 期望输出值 -}{ - {Now().ToDateTimeString(), "10h", ParseByDuration("10h").Format(DateTimeFormat)}, - {Now().ToDateTimeString(), "-10h", ParseByDuration("-10h").Format(DateTimeFormat)}, - {Now().ToDateTimeString(), "10.5h", ParseByDuration("10.5h").Format(DateTimeFormat)}, - {Now().ToDateTimeString(), "-10.5h", ParseByDuration("-10.5h").Format(DateTimeFormat)}, - - {Now().ToDateTimeString(), "10m", ParseByDuration("10m").Format(DateTimeFormat)}, - {Now().ToDateTimeString(), "-10m", ParseByDuration("-10m").Format(DateTimeFormat)}, - {Now().ToDateTimeString(), "10.5m", ParseByDuration("10.5m").Format(DateTimeFormat)}, - {Now().ToDateTimeString(), "-10.5m", ParseByDuration("-10.5m").Format(DateTimeFormat)}, - - {Now().ToDateTimeString(), "10s", ParseByDuration("10s").Format(DateTimeFormat)}, - {Now().ToDateTimeString(), "-10s", ParseByDuration("-10s").Format(DateTimeFormat)}, - {Now().ToDateTimeString(), "10.5s", ParseByDuration("10.5s").Format(DateTimeFormat)}, - {Now().ToDateTimeString(), "-10.5s", ParseByDuration("-10.5s").Format(DateTimeFormat)}, - - {Now().ToDateTimeString(), "XXXX", ""}, // 异常情况 -} - -func TestCarbon_ParseByDuration1(t *testing.T) { - for _, v := range ParseByDurationTests { - output := ParseByDuration(v.duration) - - if output.Error != nil { - fmt.Println("catch an exception in ParseByDuration():", output.Error) - return - } - - if output.ToDateTimeString() != v.output { - t.Fatalf("Input %s, expected %s, but got %s\n", v.input, v.output, output.ToDateTimeString()) - } - } -} - -func TestCarbon_ParseByDuration2(t *testing.T) { - for _, v := range ParseByDurationTests { - output := SetTimezone("XXXX").ParseByDuration(v.duration) - - if output.Error != nil { - fmt.Println("catch an exception in ParseByDuration():", output.Error) - return - } - - if output.ToDateTimeString() != v.output { - t.Fatalf("Input %s, expected %s, but got %s\n", v.input, v.output, output.ToDateTimeString()) - } - } -} - func TestCarbon_AddDuration(t *testing.T) { Tests := []struct { input string // 输入值 diff --git a/constant.go b/constant.go index a6d74f37..62e5f1fa 100644 --- a/constant.go +++ b/constant.go @@ -4,7 +4,7 @@ import "time" // 版权信息 const ( - Version = "v1.2.6" + Version = "v1.2.7" Author = "gouguoyin" Email = "mail@gouguoyin.cn" Blog = "www.gouguoyin.cn" diff --git a/coverage.html b/coverage.html index 41caea76..625cdb88 100644 --- a/coverage.html +++ b/coverage.html @@ -1,5 +1,5 @@ - +