Skip to content

Commit 4a5d44c

Browse files
committed
added description to methods in readme
1 parent 17c94bd commit 4a5d44c

10 files changed

+205
-2
lines changed

README.md

+95-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,95 @@
1-
# timerange
2-
working with time ranges simpler
1+
# Time Ranges
2+
The library adds the `Range` type with methods to perform complex operations
3+
over time ranges.
4+
5+
## Install and update
6+
`go get -u github.com/cappuccinotm/trn`
7+
8+
## Usage
9+
```go
10+
rng := trn.New(time.Now(), 3 * time.Hour, trn.In(time.UTC))
11+
```
12+
13+
For more examples see [test file](examples_test.go).
14+
15+
## Methods
16+
- `func (r Range) Stratify(duration time.Duration, interval time.Duration) []Range`
17+
18+
Slices the range into smaller ones with fixed `duration` and fixed `interval`
19+
between their **starts**.
20+
In case if the last interval doesn't fit into the given duration, `Stratify`
21+
won't return it.
22+
23+
<details><summary>Illustration</summary>
24+
25+
![stratify illustration](_img/stratify.svg)
26+
27+
</details>
28+
29+
- `func (r Range) Split(duration time.Duration, interval time.Duration) []Range`
30+
31+
Slices the range into smaller ones with fixed `duration` and fixed `interval`
32+
the **end** of the one range and **start** of next range.
33+
In case if the last interval doesn't fit into the given duration, `Split`
34+
won't return it.
35+
36+
<details><summary>Illustration</summary>
37+
38+
![split illustration](_img/split.svg)
39+
40+
</details>
41+
42+
- `func (r Range) Truncate(bounds Range) Range`
43+
44+
Cuts the start and the end of the range to fit the given `bounds`.
45+
46+
<details><summary>Illustration</summary>
47+
48+
![truncate illustration](_img/truncate.svg)
49+
50+
</details>
51+
52+
- `MergeOverlappingRanges(ranges []Range) []Range`
53+
54+
<details><summary>Illustration</summary>
55+
56+
![merge illustration](_img/merge_overlapping_ranges.svg)
57+
58+
</details>
59+
60+
- `func (r Range) Flip(ranges []Range) []Range`
61+
62+
Flips the given `ranges` within the given period (`r`).
63+
64+
The boundaries of the given ranges are considered to be inclusive, which means
65+
that the flipped ranges will start or end at the exact nanosecond where
66+
the boundary from the input starts or ends.
67+
68+
Note: for the sake of safety, ranges are being merged before flip to ensure
69+
the correct working of method.
70+
71+
<details><summary>Illustration</summary>
72+
73+
![flip illustration](_img/flip.svg)
74+
75+
</details>
76+
77+
- `func Intersection(ranges []Range) Range`
78+
79+
Returns the range, which is common for all the given ranges.
80+
81+
<details><summary>Illustration</summary>
82+
83+
![intersection illustration](_img/intersection.svg)
84+
85+
</details>
86+
87+
There are some other non-algorithmic methods, which you can see in the [source code](range.go).
88+
89+
## Details
90+
91+
`String` method formats the range in format `[start time, end time]`, where the
92+
times are formatted with the next template:
93+
```go
94+
const defaultRangeFmt = "2006-01-02 15:04:05.999999999 -0700 MST"
95+
```

_img/flip.svg

+4
Loading

_img/intersection.svg

+4
Loading

0 commit comments

Comments
 (0)