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

Overview

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"
Issues
  • Add support of unbounded, half-open and open intervals

    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>").

    enhancement help wanted 
    opened by Semior001 0
Releases(v0.2.0)
Owner
CappuccinoTeam
CappuccinoTeam
A tool for managing complex enterprise Kubernetes environments as code.

kubecfg A tool for managing Kubernetes resources as code. kubecfg allows you to express the patterns across your infrastructure and reuse these powerf

null 65 Jun 30, 2022
for Prometheus, complex settings

Remo Manager Nature Remo Cloud API を用いて、Prometheus上にデータを展開するプログラム。 Nature Remo Cloud APIのアクセスリミットに対応し、1分間に1度だけデータを更新する。 使い方 make build -> docker-compo

null 0 Dec 29, 2021
Open Source runtime scanner for OpenShift cluster and perform security audit checks based on CIS RedHat OpenShift Benchmark specification

OpenShift-Ordeal Scan your Openshift cluster !! OpenShift-Ordeal is an open source audit scanner who perform audit check on OpenShift Cluster and outp

chenk 4 Apr 17, 2022
Cloud-Z gathers information and perform benchmarks on cloud instances in multiple cloud providers.

Cloud-Z Cloud-Z gathers information and perform benchmarks on cloud instances in multiple cloud providers. Cloud type, instance id, and type CPU infor

CloudSnorkel 16 Jun 8, 2022
Savoir - A tool to perform tasks during internal security assessment

Savoir Savoir is a tool to perform tasks during internal security assessment. Th

Vincent D. 7 Jun 5, 2022
S3 Reverse Proxy with GET, PUT and DELETE methods and authentication (OpenID Connect and Basic Auth)

Menu Why ? Features Configuration Templates Open Policy Agent (OPA) API GET PUT DELETE AWS IAM Policy Grafana Dashboard Prometheus metrics Deployment

Havrileck Alexandre 106 Jun 28, 2022
Package create provides a generic option pattern for creating new values of any type

create Package create provides a generic option pattern for creating new values

null 1 Dec 30, 2021
A Go package providing a generic data type to track maximum and minimum peak values.

go-peak Overview go-peak is a Go package providing a generic data type that tracks the maximum and minimum peak values within a specific period of tim

tunabay 0 Mar 26, 2022
CPU usage percentage is the ratio of the total time the CPU was active, to the elapsed time of the clock on your wall.

Docker-Kubernetes-Container-CPU-Utilization Implementing CPU Load goroutine requires the user to call the goroutine from the main file. go CPULoadCalc

Ishank Jain 1 Dec 15, 2021
A Kubernetes Network Fabric for Enterprises that is Rich in Functions and Easy in Operations

中文教程 Kube-OVN, a CNCF Sandbox Level Project, integrates the OVN-based Network Virtualization with Kubernetes. It offers an advanced Container Network

null 1.3k Jun 21, 2022
Planet Scale Robotics - Offload computation-heavy robotic operations to GPU powered world's first cloud-native robotics platform.

robolaunch ?? Planet Scale Robotics - Offload computation-heavy robotic operations to GPU powered world's first cloud-native robotics platform. robola

robolaunch 5 Feb 28, 2022
Go-Mongodb API - A sample REST API ( CRUD operations ) created using Golang

Go-Mongodb_API This is a sample REST API ( CRUD operations ) created using the G

Aman Sharma 1 May 31, 2022
Kubelet-bench - Example Go-based e2e benchmark for various Kubelet operations without spinning up whole K8s cluster

kubelet-bench An example of Go based e2e benchmark for various Kubelet operation

Bartlomiej Plotka 3 Mar 17, 2022
Cli tool to save useful links to read it later

lnk - Cli tool to save useful links still in development Usage lnk <new|list> [url] - new: creates a new link url: required params, which i

Wassim 8 Jun 23, 2022
go-ima is a tool that checks if a file has been tampered with. It is useful in ensuring integrity in CI systems

go-ima Tool that checks the ima-log to see if a file has been tampered with. How to use Set the IMA policy to tcb by configuring GRUB GRUB_CMDLINE_LIN

TestifySec 9 Apr 26, 2022
Automatically capture all potentially useful information about each executed command (as well as its output) and get powerful querying mechanism

nhi is a revolutionary tool which automatically captures all potentially useful information about each executed command and everything around, and delivers powerful querying mechanism.

Karol Ołtarzewski 230 Jun 17, 2022
simple web app that print its environment variable, useful for testing on k8s

Index Sample web app that pints environment variables. Start it: docker-compose up Use it: > curl localhost:8080 PATH=/usr/local/sbin:/usr/local/bin:/

francesco palazzini 0 Jan 10, 2022
A library containing useful functions for working with Go types.

Go Type Tools A library containing useful functions for working with Go types. Table of Contents Reasoning Examples Array Map Int String Usage License

eloonstra 6 Feb 18, 2022