GithubHelp home page GithubHelp logo

trn's Introduction

Time Ranges Go Reference Go codecov

Package trn introduces a Range type with useful methods to perform complex operations over time ranges.

Install and update

go get -u github.com/cappuccinotm/trn

Usage

rng := trn.New(time.Now(), 3 * time.Hour, trn.In(time.UTC))

betweenRng := trn.Between(time.Now(), time.Now().Add(3 * time.Hour), trn.In(time.UTC))

For more examples see test file.

Todo

  • Make UTC and opts tests work in UTC location

Methods

  • func New(start time.Time, duration time.Duration, opts ...Option) Range

    Creates a new Range with start at the given time and with the given duration.

  • func Between(start, end time.Time, opts ...Option) (Range, error)

    Creates a new Range within the given time range. Between uses the location of the start time for the range. Returns ErrStartAfterEnd if the start time is later than the end.

  • func (r Range) Stratify(duration time.Duration, interval time.Duration) ([]Range, error)

    Slices the range into smaller ones with fixed duration and fixed interval between their starts. In case if the last interval doesn't fit into the given duration, Stratify won't return it. Returns ErrZeroDurationInterval if the provided duration or interval is less or equal to zero.

Illustration

stratify illustration

  • func (r Range) Split(duration time.Duration, interval time.Duration) ([]Range, error)

    Slices the range into smaller ones with fixed duration and fixed interval between the end of the one range and start of next range. In case if the last interval doesn't fit into the given duration, Split won't return it. Returns ErrZeroDurationInterval if the provided duration is less or equal to zero.

Illustration

split illustration

  • func (r Range) Truncate(bounds Range) Range

    Cuts the start and the end of the range to fit the given bounds.

Illustration

truncate illustration

  • MergeOverlappingRanges(ranges []Range) []Range
Illustration

merge illustration

  • func (r Range) Flip(ranges []Range) []Range

    Flips the given ranges within the given period (r).

    The boundaries of the given ranges are considered to be inclusive, which means that the flipped ranges will start or end at the exact nanosecond where the boundary from the input starts or ends.

    Note: for the sake of safety, ranges are being merged before flip to ensure the correct working of method.

Illustration

flip illustration

  • func Intersection(ranges []Range) Range

    Returns the range, which is common for all the given ranges.

Illustration

intersection illustration

There are some other non-algorithmic methods, which you can see in the reference.

Details

String method formats the range in format [start time, end time], where the times are formatted with the next template:

const defaultRangeFmt = "2006-01-02 15:04:05.999999999 -0700 MST"

Status

The code was extracted from existing project and still under development. Until v1.x released the API may change.

trn's People

Contributors

semior001 avatar

Stargazers

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

Watchers

 avatar

trn's Issues

Add support of unbounded, half-open and open intervals

There is a need in intervals, which are not bounded to concrete times (e.g. "from the beginning of the universe to <timestamp>," "anywhen," etc.) and intervals, which may not include the exact point of time itself, (e.g. "before <timestamp>").

json marshal/unmarshal

Right now these types don't support marshal/unmarshal from/to json, but implementing this might be pretty handy in the future.

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.