1 Star 0 Fork 70

vdebug/carbon

forked from dromara/carbon 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
creator.go 2.04 KB
一键复制 编辑 原始数据 按行查看 历史
package carbon
import (
"strconv"
"time"
)
// CreateFromTimestamp 从时间戳创建 Carbon 实例
func (c Carbon) CreateFromTimestamp(timestamp int64) Carbon {
ts := timestamp
if ts == 0 {
return c
}
switch len(strconv.FormatInt(timestamp, 10)) {
case 10:
ts = timestamp
case 13:
ts = timestamp / 1e3
case 16:
ts = timestamp / 1e6
case 19:
ts = timestamp / 1e9
default:
ts = 0
}
c.Time = time.Unix(ts, 0)
return c
}
// CreateFromTimestamp 从时间戳创建 Carbon 实例(默认时区)
func CreateFromTimestamp(timestamp int64) Carbon {
return NewCarbon().CreateFromTimestamp(timestamp)
}
// 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, time.Now().Nanosecond(), c.Loc)
return c
}
// CreateFromDateTime 从年月日时分秒创建 Carbon 实例(默认时区)
func CreateFromDateTime(year int, month int, day int, hour int, minute int, second int) Carbon {
return NewCarbon().CreateFromDateTime(year, month, day, hour, minute, second)
}
// 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, time.Now().Nanosecond(), c.Loc)
return c
}
// CreateFromDate 从年月日创建 Carbon 实例(默认时区)
func CreateFromDate(year int, month int, day int) Carbon {
return NewCarbon().CreateFromDate(year, month, day)
}
// 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, time.Now().Nanosecond(), c.Loc)
return c
}
// CreateFromTime 从时分秒创建 Carbon 实例(默认时区)
func CreateFromTime(hour int, minute int, second int) Carbon {
return NewCarbon().CreateFromTime(hour, minute, second)
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/vdebug/carbon.git
git@gitee.com:vdebug/carbon.git
vdebug
carbon
carbon
master

搜索帮助