go-carbon1.2.0 is released! Improve and optimize multi-scenario support for ORM

藏色散人
Release: 2020-10-13 10:30:47
forward
2216 people have browsed it

The following is the tutorial column of golang to introduce the go-carbon 1.2.0 version to you, I hope it will be helpful to friends in need!

go-carbon1.2.0 is released! Improve and optimize multi-scenario support for ORM

carbon is a lightweight, semantic, developer-friendly Golang time processing library that supports chain calls and mainstream ORMs such as gorm and xrom

If you think it is good, please give it a star

github:github.com/golang-module/carbon

gitee:gitee.com/go-package/carbon

Installation and use

// 使用github库
go get -u github.com/golang-module/carbon

import (
 "github.com/golang-module/carbon")

// 使用gitee库
go get -u gitee.com/go-package/carbon

import (
 "gitee.com/go-package/carbon")
Copy after login
Database support

Assume that the data table is users, and the fields are id(int), name(varchar), age(int), graduated_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"`}
Copy after login
Instantiate the model
user := UserModel {
 Name: "勾国印", 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()},}
Copy after login
Output model fields
user.ID // 18
user.Name // 勾国印
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
Copy after login
JSON output model
data, _ := json.Marshal(&user)
fmt.Print(string(data))
// 输出
{
 "id": 42, "name": "勾国印", "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",}
Copy after login

Update log

  • Perfect unit testing, code coverage 100%
  • Improve and optimize multi-scenario support for ORM
  • Added English version of README.MD documentation
  • Unified exception handling
  • Unified output function naming rules, Format() changed to ToFormatString()
  • Added CreateFromGoTime(t time.Time) method to create Carbon instance from native time.Time
  • Optimize the code organization structure and unify the final methods that are not inheritable Put it in the final.go file
  • Abandon the New() initialization function and use it directly without initialization
  • Add multiple time format outputs, such as Cookie, W3C, RSS, RFC7231
  • New ParseByDuration() method parses duration string (relative to today), supports positive and negative integers/floating point numbers and symbols ns (nanoseconds), us (microseconds), ms (milliseconds), s (seconds) , m (minutes), h (hours) combination
  • Added NextYears(), NextYear(), PreYears(), PreYear() methods to prevent the phenomenon of crossing months when adding/subtracting the specified year
  • Added NextMonths(), NextMonth(), PreMonths(), and PreMonth() methods to prevent the phenomenon of crossing months after adding/reducing the specified month
  • Added DaysInYear() method to obtain this The total number of days in the year
  • Add the DaysInMonth() method to get the total number of days in this month
  • Add the MonthOfYear() method to get the month of the year
  • Add the DayOfYear () method to get the day of the year
  • New DayOfMonth() method to get the day of the month
  • New DayOfWeek() method to get the day of the week
  • Added WeekOfYear() method to get the week of this year
  • Added WeekOfMonth() method to get the week of this month
  • Added IsZero() method to determine whether It is a zero value time
  • Added IsFuture() method to determine whether it is future time
  • Added IsPast() method to determine whether it is past time
  • Added IsNow() method Determine whether it is the current time

The above is the detailed content of go-carbon1.2.0 is released! Improve and optimize multi-scenario support for ORM. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:learnku.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template