12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364 |
- package util
- import (
- "fmt"
- "strconv"
- "strings"
- "time"
- "app.yhyue.com/moapp/jybase/mongodb"
- "go.mongodb.org/mongo-driver/bson/primitive"
- "gopkg.in/mgo.v2/bson"
- )
- func MongoId(t int64) primitive.ObjectID {
- id, _ := primitive.ObjectIDFromHex(fmt.Sprintf("%x0000000000000000", t))
- return id
- }
- func SpitObjectId(s string) string {
- sArr := strings.Split(s, `("`)
- s = strings.Split(sArr[1], `")`)[0]
- return s
- }
- // return ObjectID("6424fefc0000000000000000")
- func GetObjectId(timestamp int64) primitive.ObjectID {
- t := time.Unix(timestamp, 0)
- tid := bson.NewObjectIdWithTime(t)
- return mongodb.StringTOBsonId(SpitObjectId(tid.String()))
- }
- //
- func FormatDate(dateInt int64) string {
- // 将 int 类型的日期数字转换为时间对象
- dateStr := fmt.Sprintf("%d", dateInt)
- if len(dateStr) < 8 {
- return ""
- }
- year, err := strconv.Atoi(dateStr[:4])
- if err != nil {
- return ""
- }
- month, err := strconv.Atoi(dateStr[4:6])
- if month == 0 {
- month = 1
- }
- if err != nil {
- return ""
- }
- day, err := strconv.Atoi(dateStr[6:8])
- if err != nil {
- return ""
- }
- if day == 0 {
- day = 1
- }
- loc, _ := time.LoadLocation("Local")
- t := time.Date(year, time.Month(month), day, 0, 0, 0, 0, loc)
- // 将时间对象格式化为指定的字符串格式
- formattedLayout := "2006-01-02 15:04:05"
- return t.Format(formattedLayout)
- }
|