Go carbon version 1.2.0 has been released to improve and optimize the multi scenario support for orm


Carbon is a lightweight, semantic and developer friendly golang time processing library, which supports chain call, lunar calendar, Gorm, xorm and other mainstream orm

If you think it’s good, please give it to a star



Installation and use

//Using GitHub Library
go get -u github.com/golang-module/carbon

import (

//Using gitee Library
go get -u gitee.com/go-package/carbon

import (
Database support

Suppose the data table is users and the fields are ID (int), name (varchar), age (int) and graded_ at(date)、birthday(date)、created_ at(datetime)、updated_ at(datetime)、deleted_ at(datetime)

Define the model
type UserModel struct {
 ID  int64  `json:"id"` Name string `json:"name"` Age int `json:"age"` Birthday carbon.Carbon `json:"birthday"` CreatedAt carbon.ToDateTimeString `json:"created_at"` DeletedAt carbon.ToTimestamp `json:"deleted_at"` GraduatedAt carbon.ToDateString `json:"graduated_at"` UpdatedAt carbon.ToTimeString `json:"updated_at"`}
Instantiation model
user := UserModel {
 Name: "gouguoyin", age: 18, birthday: carbon.Now ().SubYears(18), CreatedAt:  carbon.ToDateTimeString { carbon.Now ()}, DeletedAt:  carbon.ToTimestamp { carbon.Parse ("2020-08-05 13:14:15")}, GraduatedAt:  carbon.ToDateString { carbon.Parse ("2012-09-09")}, UpdatedAt:  carbon.ToTimeString { carbon.Now ()},}
Output model fields
user.ID // 18
user.Name  //Gouguo seal
user.Birthday.ToDateString() // 2012-08-05
user.CreatedAt.ToTimestamp() // 2012-08-05 13:14:15
user.DeletedAt.ToDateTimeString() // 1596604455
user.GraduatedAt.AddDay().ToDateString() // 2012-09-10
user.UpdatedAt.ToDateString() // 2012-08-05
JSON output model
data, _ := json.Marshal(&user)
 "Id": 42, "name": "gouguoyin", "age": 18, "birthday": "2012-08-05 00:00:00", "created"_ at": "2020-08-05 13:14:15", "deleted_ at": 1596604455 "graduated_ at": "2012-09-09", "updated_ at": "13:14:15",}

Update log

  • Improve unit testing, code coverage of 100%
  • Improve and optimize the multi scenario support for ORM
  • New English version README.MD Documentation
  • Unified exception handling
  • Unify the naming rules of output function, change format() to toformatstring ()
  • New create from gotime (T) time.Time )Methods from the original time.Time Create carbon instance
  • Optimize the code organization structure, and put the non inheritable final method into the unified system final.go In the file
  • Discard the new() initialization function and use it directly without initialization
  • Add a variety of time format output, such as cookie, W3C, RSS, rfc7231
  • The parsebyduration() method is added to parse the duration string (compared with today’s), supporting the combination of positive and negative integers / floating-point numbers and symbols ns (nanosecond), US (subtle), MS (millisecond), s (second), m (minute), H (hour)
  • Add nextyears(), nextyear(), preyears(), preyear() methods to prevent the phenomenon of cross month when adding / reducing the specified year
  • Add nextmonths(), nextmonth(), premonths(), premonth() methods to prevent cross month phenomenon after adding / reducing the specified month
  • Add the daysinyear() method to get the total days of this year
  • Add the daysinmonth() method to get the total days of this month
  • Add the monthofyear() method to get the month of this year
  • Add dayofyear() method to get the day of the year
  • Add dayofmonth() method to get the day of the month
  • Add dayofweek() method to get the day of the week
  • Add the weekofyear() method to get the week of the year
  • Add the weekofmonth() method to get the week of the month
  • Add iszero() method to judge whether it is zero time
  • Add isfuture() method to judge whether it is the future time
  • Add ispast() method to judge whether it is the past time
  • Add isnow() method to judge whether it is the current time

This work adoptsCC agreementReprint must indicate the author and the link of this article