timex

package
v0.0.0-...-45da7de Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 25, 2024 License: MIT Imports: 11 Imported by: 2

README

jsontime

Parsing time in JSON compatible with some common format.

import (
	"encoding/json"
	"github.com/bingoohuang/gg/pkg/timex"
)

type Msg struct {
	O jsontime.Time
	A jsontime.Time
	B jsontime.Time
	C jsontime.Time
	E jsontime.Time
	F jsontime.Time
	D jsontime.Time `json:"d"`
}

func main() {
	j := `{
		"O": "",
		"A": "1604022846",
		"F": 1604022846000,
		"B": "2020-03-18 10:51:54.198",
		"C": "2020-03-18 10:51:54,198",
		"E": "2020-03-18T10:51:54,198",
		"d": "2020-03-18T10:51:54.198000Z",
		"G": "XYZ"
	}`

    var msg Msg
    err := json.Unmarshal([]byte(j), &msg)
}

supporting time format:

  1. direct milliseconds
  2. string format of millis seconds
  3. string format of yyyy-MM-dd HH:mm:ss.SSS
  4. string format of yyyy-MM-dd HH:mm:ss,SSS
  5. string format of yyyy-MM-ddTHH:mm:ss.SSS
  6. string format of yyyy-MM-ddTHH:mm:ss,SSS
  7. string format of yyyy-MM-dd HH:mm:ss.SSSSSSZ
  8. string format of yyyy-MM-dd HH:mm:ss,SSSSSSZ
  9. 完整年月日时分秒的字符串

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrClosed   = errors.New("TimingWheel is closed already")
	ErrArgument = errors.New("incorrect task argument")
)
View Source
var ErrUnknownTimeFormat = errors.New("unknown errors time format")

ErrUnknownTimeFormat defines the error type for unknown time format.

Functions

func ConvertFormat

func ConvertFormat(s string) string

ConvertFormat converts Java style layout to golang.

func FormatTime

func FormatTime(t time.Time, s string) string

FormatTime format time with Java style layout.

func GlobName

func GlobName(s string) string

GlobName format time with Java style layout.

func GoSafe

func GoSafe(fn func())

GoSafe runs the given fn using another goroutine, recovers if fn panics.

func ParseDuration

func ParseDuration(s string) (time.Duration, error)

ParseDuration parses a duration string. A duration string is a possibly signed sequence of decimal numbers, each with optional fraction and a unit suffix, such as "300ms", "-1.5h" or "2h45m". Valid time units are "ns", "us" (or "µs"), "ms", "s", "m", "h".

func Recover

func Recover(cleanups ...func())

Recover is used with defer to do cleanup on panics. Use it like:

defer Recover(func() {})

func RunSafe

func RunSafe(fn func())

RunSafe runs the given fn, recovers if fn panics.

func TryUnQuoted

func TryUnQuoted(v string) (string, bool)

TryUnQuoted tries to unquote string.

Types

type Execute

type Execute func(key, value interface{})

Execute defines the method to execute the task.

type JSONTime

type JSONTime time.Time

JSONTime defines a time.Time that can be used in struct tag for JSON unmarshalling.

func (*JSONTime) UnmarshalJSON

func (t *JSONTime) UnmarshalJSON(b []byte) error

UnmarshalJSON unmarshals bytes to JSONTime.

type TaskRunner

type TaskRunner struct {
	// contains filtered or unexported fields
}

A TaskRunner is used to control the concurrency of goroutines.

func NewTaskRunner

func NewTaskRunner(concurrency int) *TaskRunner

NewTaskRunner returns a TaskRunner.

func (*TaskRunner) Schedule

func (rp *TaskRunner) Schedule(task func())

Schedule schedules a task to run under concurrency control.

type TimingWheel

type TimingWheel struct {
	// contains filtered or unexported fields
}

A TimingWheel is a timing wheel object to schedule tasks.

func NewTimingWheel

func NewTimingWheel(interval time.Duration, numSlots int, execute Execute) (*TimingWheel, error)

NewTimingWheel returns a TimingWheel.

func (*TimingWheel) Drain

func (tw *TimingWheel) Drain(fn func(key, value interface{})) error

Drain drains all items and executes them.

func (*TimingWheel) MoveTimer

func (tw *TimingWheel) MoveTimer(key interface{}, delay time.Duration) error

MoveTimer moves the task with the given key to the given delay.

func (*TimingWheel) RemoveTimer

func (tw *TimingWheel) RemoveTimer(key interface{}) error

RemoveTimer removes the task with the given key.

func (*TimingWheel) SetTimer

func (tw *TimingWheel) SetTimer(key, value interface{}, delay time.Duration) error

SetTimer sets the task value with the given key to the delay.

func (*TimingWheel) Stop

func (tw *TimingWheel) Stop()

Stop stops tw. No more actions after stopping a TimingWheel.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL