Coder Social home page Coder Social logo

sonirico / datetoken.go Goto Github PK

View Code? Open in Web Editor NEW
20.0 1.0 0.0 3.28 MB

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

License: MIT License

Go 93.71% Makefile 6.29%
golang-library golang-package timezone relative-time time relative hacktoberfest

datetoken.go's Introduction

Build ReportCard Docs Contributions Deps: 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 where dates need to be represented in a relative fashion, like background periodic tasks, datetime range pickers... in a compact and stringified format. This enables the programmer to persist these tokens during the lifetime of a process or even longer, since calculations are performed in the moment of evaluation. Theses tokens are also useful when caching URLs as replacement of timestamps, which would break caching given their mutability nature.

Some common examples of relative tokens:

From To
Today now/d now
Yesterday now-d/d now-d@d
Last 24 hours now-24h now
Last business week now-w/bw now-w@bw
This business week now/bw now@bw
Last month now-1M/M now-1M@M
Next week now+w/w now+w@w
Custom range now+w-2d/h now+2M-10h
Last month first business week now-M/M+w/bw now-M/+w@bw
This year now/Y now@Y
This quarter now/Q now@Q
This first quarter (Q1) now/Q1 now@Q1
This second quarter (Q2) now/Q2 now@Q2
This third quarter (Q3) now/Q3 now@Q3
This fourth quarter (Q4) now/Q4 now@Q4

As you may have noticed, token follow a pattern:

  • The word now. It means the point in the future timeline when tokens are parsed to their datetime form.

  • Optionally, modifiers to add and/or subtract the future value of now can be used. Unsurprisingly, additions are set via +, while - mean subtractions. These modifiers can be chained as many times as needed. E.g: now-1M+3d+2h. Along with the arithmetical sign and the amount, the unit of time the amount refers to must be specified. Currently, the supported units 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 the end 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, plus bw, meaning business week. With this, we achieve a simple way to define canonical relative date ranges, such as Today or Last month. As an example of the later:

    • String representation: now-1M/M, now-1M@M
    • Being today 15 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 lib to work under lower versions too.

Installing

go get github.com/sonirico/datetoken.go

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

Examples

Most probably you will be dealing with simple presets such as yesterday or the last 24 hours.

package main

import (
    "fmt"
    "time"

    "github.com/sonirico/datetoken.go"
)

func main() {
    Madrid, _ := time.LoadLocation("Europe/Madrid")
    time, err := datetoken.Eval("now/d", Madrid, time.Monday)
    if err != nil {
        panic(err)
    }
    fmt.Println(time.String())
}

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

Other versions

datetoken.go's People

Contributors

dependabot-preview[bot] avatar sonirico avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.