All Projects → smartwalle → time4go

smartwalle / time4go

Licence: MIT license
No description or website provided.

Programming Languages

go
31211 projects - #10 most used programming language

Projects that are alternatives of or similar to time4go

faketime
Fake currentTimeMillis() without class loader hacks
Stars: ✭ 100 (+284.62%)
Mutual labels:  time
dt
Go's missing DateTime package
Stars: ✭ 34 (+30.77%)
Mutual labels:  time
popoPicker
popoPicker是一个移动端3D滚轮日期时间和单项的选择器,支持无限循环滚动,不依赖第三方库
Stars: ✭ 26 (+0%)
Mutual labels:  time
rutimeparser
Recognize date and time in russian text and return datetime.datetime.
Stars: ✭ 17 (-34.62%)
Mutual labels:  time
duration-humanizer
361000 becomes "6 minutes, 1 second"
Stars: ✭ 61 (+134.62%)
Mutual labels:  time
pest-plugin-test-time
A Pest plugin to control the flow of time
Stars: ✭ 31 (+19.23%)
Mutual labels:  time
Sorting-Algorithms
sorting algorithms in python
Stars: ✭ 15 (-42.31%)
Mutual labels:  time
time-formater
在javascript中显示日期。
Stars: ✭ 44 (+69.23%)
Mutual labels:  time
vue-translated
Internationalization (i18n) and localization (l10n) library for Vue.js v2.
Stars: ✭ 19 (-26.92%)
Mutual labels:  time
about-time
A cool helper for tracking time and throughput of code blocks, with beautiful human friendly renditions.
Stars: ✭ 36 (+38.46%)
Mutual labels:  time
go-systemd-time
📅 Go implementation of systemd relative time adjustments
Stars: ✭ 21 (-19.23%)
Mutual labels:  time
hs-hourglass
efficient and simpler time API for haskell
Stars: ✭ 43 (+65.38%)
Mutual labels:  time
timeskew
Override time reporting in Linux processes (accelerate/slowdown games, test code involving timers/delays)
Stars: ✭ 36 (+38.46%)
Mutual labels:  time
qrono
🕥 Just right date time library
Stars: ✭ 111 (+326.92%)
Mutual labels:  time
jinja2-time
📆 Jinja2 Extension for Dates and Times
Stars: ✭ 64 (+146.15%)
Mutual labels:  time
ElevenClock
ElevenClock: Customize Windows 11 taskbar clock
Stars: ✭ 1,494 (+5646.15%)
Mutual labels:  time
shamsi date
A Flutter and Dart package for using Jalali (Shamsi, Solar, Persian or Jalaali) calendar. You can convert, format and manipulate Jalali and Gregorian (Miladi) date and times.
Stars: ✭ 59 (+126.92%)
Mutual labels:  time
chronos
One library to rule the time
Stars: ✭ 17 (-34.62%)
Mutual labels:  time
ios-application
A native, lightweight and secure one-time-password (OTP) client built for iOS; Raivo OTP!
Stars: ✭ 581 (+2134.62%)
Mutual labels:  time
relative.time.parser
Moment.js Plugin for parsing Relative Time Strings
Stars: ✭ 13 (-50%)
Mutual labels:  time

Time4Go

Go 语言的时间工具库。

帮助

在集成的过程中有遇到问题,欢迎加 QQ 群 564704807 讨论。

安装

$ go get github.com/smartwalle/time4go

开始

package main

import (
	"fmt"
	"github.com/smartwalle/time4go"
	"time"
)

func main() {
	var now = time4go.Now()
	fmt.Println(now)

	var d = time4go.Date(2018, time.May, 20, 13, 14, 0, 0, time.Local)
	fmt.Println(d)
}

获取当前日期所在周的第一天和最后一天

var now = time4go.Now()
now.BeginningDateOfWeek()
now.EndDateOfWeek()

获取当前日期所在月的第一天和最后一天

var now = time4go.Now()
now.BeginningDateOfMonth()
now.EndDateOfMonth()

JSON

  • 设置序列化成 JSON 字符串时的格式
time4go.JSONFormatter = time4go.DefaultFormatter{Layout: "2006-01-02 15:04:05"}

例子:

package main

import (
	"encoding/json"
	"fmt"
	"github.com/smartwalle/time4go"
)

func main() {
	time4go.JSONFormatter = time4go.DefaultFormatter{Layout: "2006-01-02 15:04:05"}

	var s = &Schedule{}
	s.Begin = time4go.Now()
	s.End = s.Begin.AddDate(0, 1, 0)

	sBytes, _ := json.Marshal(s)
	fmt.Println(string(sBytes))

	var ts = `{"begin":"2019-11-10 09:59:21","end":"2019-12-10 09:59:21"}`
	var s2 *Schedule
	json.Unmarshal([]byte(ts), &s2)
	fmt.Println(s2.Begin, s2.End)
}

type Schedule struct {
	Begin *time4go.Time `json:"begin"`
	End   *time4go.Time `json:"end"`
}
  • 自定义 Formatter

当然你也可以自定义 Formatter,只需要实现以下接口即可:

type TimeFormatter interface {
	Format(t time.Time) ([]byte, error)
	Parse(data []byte) (time.Time, error)
}

比如:

type MyFormatter struct {
}

func (this MyFormatter) Format(t time.Time) ([]byte, error) {
	...
}

func (this MyFormatter) Parse(data []byte) (time.Time, error) {
	...
}

time4go.JSONFormatter = MyFormatter{}

支持 SQL 类数据库

db, err := sql.Open("mysql", "xxx")
if err != nil {
	fmt.Println("连接数据库出错:", err)
	return
}
defer db.Close()
db.Exec("INSERT INTO `user` (`name`, `age`, `created_on`) VALUES (?, ?, ?)", "test", 18, time4go.Now())

写入 SQL 类数据的时候,会将时间转换为 UTC 时区的时间,从 SQL 类数据库读取的时候,会将时间转换为 UTC 时区的时间。从而避免了在使用 github.com/lib/pq 库的时候,当数据库服务器和业务服务器时区不同引发的操作 timestamp 类型字段数据会不一致的问题。

License

This project is licensed under the MIT License.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].