|
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 | + |
| 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 | + |
| 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 | + |
| 49 | + |
| 50 | +</details> |
| 51 | + |
| 52 | +- `MergeOverlappingRanges(ranges []Range) []Range` |
| 53 | + |
| 54 | +<details><summary>Illustration</summary> |
| 55 | + |
| 56 | + |
| 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 | + |
| 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 | + |
| 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 | +``` |
0 commit comments