Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

datetoken is a time toolkit for golang, with support for relative delta and string parsing

License

NotificationsYou must be signed in to change notification settings

sonirico/datetoken.go

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

30 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

BuildReportCardDocsContributionsDeps: none

datetoken

Featuring

  • Token representation of relative dates. (dates whose value depends upon when they are evaluated)
  • Time zone support. Time zones tend to be a major pending subject to many devs.
  • Configure when week start. Cause not every country cries on Mondays.
  • Business weeks. The previous point allows this.
  • Quarters

Motivation

This package aims to solve a set of needs present in applications wheredates need to be represented in a relative fashion, like background periodictasks, datetime range pickers... in a compact and stringified format. Thisenables the programmer to persist these tokens during the lifetime of aprocess or even longer, since calculations are performed in the moment ofevaluation. Theses tokens are also useful when caching URLs as replacementof timestamps, which would break caching given their mutability nature.

Some common examples of relative tokens:

FromTo
Todaynow/dnow
Yesterdaynow-d/dnow-d@d
Last 24 hoursnow-24hnow
Last business weeknow-w/bwnow-w@bw
This business weeknow/bwnow@bw
Last monthnow-1M/Mnow-1M@M
Next weeknow+w/wnow+w@w
Custom rangenow+w-2d/hnow+2M-10h
Last month first business weeknow-M/M+w/bwnow-M/+w@bw
This yearnow/Ynow@Y
This quarternow/Qnow@Q
This first quarter (Q1)now/Q1now@Q1
This second quarter (Q2)now/Q2now@Q2
This third quarter (Q3)now/Q3now@Q3
This fourth quarter (Q4)now/Q4now@Q4

As you may have noticed, token follow a pattern:

  • The wordnow. It means the point in the future timeline when tokens areparsed to their datetime form.

  • Optionally, modifiers to add and/or subtract the future value ofnow canbe used. Unsurprisingly, additions are set via+, while- meansubtractions. These modifiers can be chained as many times as needed.E.g:now-1M+3d+2h. Along with the arithmetical sign and the amount, theunit of time the amount refers to must be specified. Currently, the supportedunits are:

    • s seconds
    • m minutes
    • h hours
    • d days
    • w weeks
    • M months
    • Y years
    • Q quarters
  • Optionally, there exist two extra modifiers to snap dates to the start or theend of any given snapshot unit. Those are:

    • / Snap the date to the start of the snapshot unit.
    • @ Snap the date to the end of the snapshot unit.

    Snapshot units are the same as arithmetical modifiers, plusbw, meaningbusiness week. With this, we achieve a simple way to define canonicalrelative date ranges, such asToday orLast month. As an example ofthe later:

    • String representation:now-1M/M,now-1M@M
    • Being today15 Jan 2018, the result range should be:2018-01-01 00:00:00 / 2018-01-31 23:59:59

Compatibility

This library has been developed under version 1.14 but you can expect the libto work under lower versions too.

Installing

go get github.com/sonirico/datetoken.gogo mod install github.com/sonirico/datetoken.go

Examples

Most probably you will be dealing with simple presets such asyesterday orthelast 24 hours.

package mainimport ("fmt""time""github.com/sonirico/datetoken.go")funcmain() {Madrid,_:=time.LoadLocation("Europe/Madrid")time,err:=datetoken.Eval("now/d",Madrid,time.Monday)iferr!=nil {panic(err)    }fmt.Println(time.String())}

For more examples you can refer tohttps://github.com/sonirico/datetoken.go/tree/master/examples

Other versions

About

datetoken is a time toolkit for golang, with support for relative delta and string parsing

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

[8]ページ先頭

©2009-2025 Movatter.jp