remove ginext/mongoext (no-dep lib)
This commit is contained in:
55
timeext/duration.go
Normal file
55
timeext/duration.go
Normal file
@@ -0,0 +1,55 @@
|
||||
package timeext
|
||||
|
||||
import "time"
|
||||
|
||||
func FromSeconds(v int) time.Duration {
|
||||
return time.Duration(int64(v) * int64(time.Second))
|
||||
}
|
||||
|
||||
func FromSecondsInt32(v int32) time.Duration {
|
||||
return time.Duration(int64(v) * int64(time.Second))
|
||||
}
|
||||
|
||||
func FromSecondsInt64(v int64) time.Duration {
|
||||
return time.Duration(v * int64(time.Second))
|
||||
}
|
||||
|
||||
func FromSecondsFloat32(v float32) time.Duration {
|
||||
return time.Duration(int64(v * float32(time.Second)))
|
||||
}
|
||||
|
||||
func FromSecondsFloat64(v float64) time.Duration {
|
||||
return time.Duration(int64(v * float64(time.Second)))
|
||||
}
|
||||
|
||||
func FromSecondsFloat(v float64) time.Duration {
|
||||
return time.Duration(int64(v * float64(time.Second)))
|
||||
}
|
||||
|
||||
func FromMinutes(v int) time.Duration {
|
||||
return time.Duration(int64(v) * int64(time.Minute))
|
||||
}
|
||||
|
||||
func FromMinutesFloat(v float64) time.Duration {
|
||||
return time.Duration(int64(v * float64(time.Minute)))
|
||||
}
|
||||
|
||||
func FromMinutesFloat64(v float64) time.Duration {
|
||||
return time.Duration(int64(v * float64(time.Minute)))
|
||||
}
|
||||
|
||||
func FromHoursFloat64(v float64) time.Duration {
|
||||
return time.Duration(int64(v * float64(time.Hour)))
|
||||
}
|
||||
|
||||
func FromDays(v int) time.Duration {
|
||||
return time.Duration(int64(v) * int64(24) * int64(time.Hour))
|
||||
}
|
||||
|
||||
func FromMilliseconds(v int) time.Duration {
|
||||
return time.Duration(int64(v) * int64(time.Millisecond))
|
||||
}
|
||||
|
||||
func FromMillisecondsFloat(v float64) time.Duration {
|
||||
return time.Duration(int64(v * float64(time.Millisecond)))
|
||||
}
|
77
timeext/range.go
Normal file
77
timeext/range.go
Normal file
@@ -0,0 +1,77 @@
|
||||
package timeext
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
type OpenTimeRange struct {
|
||||
From *time.Time
|
||||
To *time.Time
|
||||
}
|
||||
|
||||
func (r OpenTimeRange) String() string {
|
||||
if r.From == nil && r.To == nil {
|
||||
return "[]"
|
||||
} else if r.From != nil && r.To != nil {
|
||||
return fmt.Sprintf("[%v - %v]", r.From, r.To)
|
||||
} else if r.From != nil {
|
||||
return fmt.Sprintf("[%v - *]", r.From)
|
||||
} else if r.To != nil {
|
||||
return fmt.Sprintf("[* - %v]", r.To)
|
||||
} else {
|
||||
return "[??]"
|
||||
}
|
||||
}
|
||||
|
||||
func (r OpenTimeRange) Contains(v time.Time) bool {
|
||||
if r.From != nil && v.Before(*r.From) {
|
||||
return false
|
||||
}
|
||||
if r.To != nil && !v.Before(*r.To) {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func NewOpenTimeRange(from *time.Time, to *time.Time) *OpenTimeRange {
|
||||
if from == nil && to == nil {
|
||||
return nil
|
||||
}
|
||||
return &OpenTimeRange{
|
||||
From: from,
|
||||
To: to,
|
||||
}
|
||||
}
|
||||
|
||||
func (r OpenTimeRange) ToMongoPipeline(key string) []interface{} {
|
||||
type bsonM map[string]interface{}
|
||||
type bsonE struct {
|
||||
Key string
|
||||
Value interface{}
|
||||
}
|
||||
type bsonD []bsonE
|
||||
|
||||
pipeline := make([]interface{}, 0)
|
||||
|
||||
if r.From != nil {
|
||||
pipeline = append(pipeline, bsonD{{Key: "$match", Value: bsonM{key: bsonM{"$ne": nil, "$gt": r.From}}}})
|
||||
}
|
||||
if r.To != nil {
|
||||
pipeline = append(pipeline, bsonD{{Key: "$match", Value: bsonM{key: bsonM{"$ne": nil, "$lt": r.To}}}})
|
||||
}
|
||||
|
||||
return pipeline
|
||||
}
|
||||
|
||||
func (r *OpenTimeRange) AppendToMongoPipeline(pipeline []interface{}, key string) []interface{} {
|
||||
if r == nil {
|
||||
return pipeline
|
||||
}
|
||||
|
||||
for _, v := range r.ToMongoPipeline(key) {
|
||||
pipeline = append(pipeline, v)
|
||||
}
|
||||
|
||||
return pipeline
|
||||
}
|
133
timeext/time.go
Normal file
133
timeext/time.go
Normal file
@@ -0,0 +1,133 @@
|
||||
package timeext
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math"
|
||||
"time"
|
||||
)
|
||||
|
||||
var TimezoneBerlin *time.Location
|
||||
|
||||
func init() {
|
||||
var err error
|
||||
TimezoneBerlin, err = time.LoadLocation("Europe/Berlin")
|
||||
if err != nil {
|
||||
panic(fmt.Sprintf("Could not load Timezone: %v", err))
|
||||
}
|
||||
}
|
||||
|
||||
// TimeToDatePart returns a timestamp at the start of the day which contains t (= 00:00:00)
|
||||
func TimeToDatePart(t time.Time) time.Time {
|
||||
t = t.In(TimezoneBerlin)
|
||||
return time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, t.Location())
|
||||
}
|
||||
|
||||
// TimeToWeekStart returns a timestamp at the start of the week which contains t (= Monday 00:00:00)
|
||||
func TimeToWeekStart(t time.Time) time.Time {
|
||||
t = TimeToDatePart(t)
|
||||
|
||||
delta := time.Duration(((int64(t.Weekday()) + 6) % 7) * 24 * int64(time.Hour))
|
||||
t = t.Add(-1 * delta)
|
||||
|
||||
return t
|
||||
}
|
||||
|
||||
// TimeToMonthStart returns a timestamp at the start of the month which contains t (= yyyy-MM-00 00:00:00)
|
||||
func TimeToMonthStart(t time.Time) time.Time {
|
||||
t = t.In(TimezoneBerlin)
|
||||
return time.Date(t.Year(), t.Month(), 1, 0, 0, 0, 0, t.Location())
|
||||
}
|
||||
|
||||
// TimeToMonthEnd returns a timestamp at the end of the month which contains t (= yyyy-MM-31 23:59:59.999999999)
|
||||
func TimeToMonthEnd(t time.Time) time.Time {
|
||||
return TimeToMonthStart(t).AddDate(0, 1, 0).Add(-1)
|
||||
}
|
||||
|
||||
// TimeToYearStart returns a timestamp at the start of the year which contains t (= yyyy-01-01 00:00:00)
|
||||
func TimeToYearStart(t time.Time) time.Time {
|
||||
t = t.In(TimezoneBerlin)
|
||||
return time.Date(t.Year(), 1, 1, 0, 0, 0, 0, t.Location())
|
||||
}
|
||||
|
||||
// TimeToYearEnd returns a timestamp at the end of the month which contains t (= yyyy-12-31 23:59:59.999999999)
|
||||
func TimeToYearEnd(t time.Time) time.Time {
|
||||
return TimeToYearStart(t).AddDate(1, 0, 0).Add(-1)
|
||||
}
|
||||
|
||||
// IsSameDayIncludingDateBoundaries returns true if t1 and t2 are part of the same day (TZ/Berlin), the boundaries of the day are
|
||||
// inclusive, this means 2021-09-15T00:00:00 is still part of the day 2021-09-14
|
||||
func IsSameDayIncludingDateBoundaries(t1 time.Time, t2 time.Time) bool {
|
||||
dp1 := TimeToDatePart(t1)
|
||||
dp2 := TimeToDatePart(t2)
|
||||
|
||||
if dp1.Equal(dp2) {
|
||||
return true
|
||||
}
|
||||
|
||||
if dp1.AddDate(0, 0, 1).Equal(dp2) && dp2.Equal(t2) {
|
||||
return true
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
// IsDatePartEqual returns true if a and b have the same date part (`yyyy`, `MM` and `dd`)
|
||||
func IsDatePartEqual(a time.Time, b time.Time) bool {
|
||||
yy1, mm1, dd1 := a.In(TimezoneBerlin).Date()
|
||||
yy2, mm2, dd2 := b.In(TimezoneBerlin).Date()
|
||||
|
||||
return yy1 == yy2 && mm1 == mm2 && dd1 == dd2
|
||||
}
|
||||
|
||||
// WithTimePart returns a timestamp with the date-part (`yyyy`, `MM`, `dd`) from base
|
||||
// and the time (`HH`, `mm`, `ss`) from the parameter
|
||||
func WithTimePart(base time.Time, hour, minute, second int) time.Time {
|
||||
datepart := TimeToDatePart(base)
|
||||
|
||||
delta := time.Duration(hour*int(time.Hour) + minute*int(time.Minute) + second*int(time.Second))
|
||||
|
||||
return datepart.Add(delta)
|
||||
}
|
||||
|
||||
// CombineDateAndTime returns a timestamp with the date-part (`yyyy`, `MM`, `dd`) from the d parameter
|
||||
// and the time (`HH`, `mm`, `ss`) from the t parameter
|
||||
func CombineDateAndTime(d time.Time, t time.Time) time.Time {
|
||||
datepart := TimeToDatePart(d)
|
||||
|
||||
delta := time.Duration(t.Hour()*int(time.Hour) + t.Minute()*int(time.Minute) + t.Second()*int(time.Second) + t.Nanosecond()*int(time.Nanosecond))
|
||||
|
||||
return datepart.Add(delta)
|
||||
}
|
||||
|
||||
// IsSunday returns true if t is a sunday (in TZ/Berlin)
|
||||
func IsSunday(t time.Time) bool {
|
||||
if t.In(TimezoneBerlin).Weekday() == time.Sunday {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func DurationFromTime(hours int, minutes int, seconds int) time.Duration {
|
||||
return time.Duration(hours*int(time.Hour) + minutes*int(time.Minute) + seconds*int(time.Second))
|
||||
}
|
||||
|
||||
func Min(a time.Time, b time.Time) time.Time {
|
||||
if a.UnixNano() < b.UnixNano() {
|
||||
return a
|
||||
} else {
|
||||
return b
|
||||
}
|
||||
}
|
||||
|
||||
func Max(a time.Time, b time.Time) time.Time {
|
||||
if a.UnixNano() > b.UnixNano() {
|
||||
return a
|
||||
} else {
|
||||
return b
|
||||
}
|
||||
}
|
||||
|
||||
func UnixFloatSeconds(v float64) time.Time {
|
||||
sec, dec := math.Modf(v)
|
||||
return time.Unix(int64(sec), int64(dec*(1e9)))
|
||||
}
|
23
timeext/translation.go
Normal file
23
timeext/translation.go
Normal file
@@ -0,0 +1,23 @@
|
||||
package timeext
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
var longDayNames = []string{
|
||||
"Sonntag",
|
||||
"Montag",
|
||||
"Dienstag",
|
||||
"Mittwoch", // meine Kerle
|
||||
"Donnerstag",
|
||||
"Freitag",
|
||||
"Samstag",
|
||||
}
|
||||
|
||||
func WeekdayNameGerman(d time.Weekday) string {
|
||||
if time.Sunday <= d && d <= time.Saturday {
|
||||
return longDayNames[d]
|
||||
}
|
||||
return "%!Weekday(" + strconv.Itoa(int(d)) + ")"
|
||||
}
|
72
timeext/weekcount.go
Normal file
72
timeext/weekcount.go
Normal file
@@ -0,0 +1,72 @@
|
||||
package timeext
|
||||
|
||||
import (
|
||||
"time"
|
||||
)
|
||||
|
||||
var isoWeekCountAggregate map[int]int
|
||||
|
||||
func init() {
|
||||
isoWeekCountAggregate = make(map[int]int)
|
||||
for y := 1900; y <= time.Now().Year(); y++ {
|
||||
GetAggregateIsoWeekCount(y)
|
||||
}
|
||||
}
|
||||
|
||||
func GetAggregateIsoWeekCount(year int) int {
|
||||
if v, ok := isoWeekCountAggregate[year]; ok {
|
||||
return v
|
||||
}
|
||||
|
||||
if year == 1900 {
|
||||
isoWeekCountAggregate[year] = 0
|
||||
return 0
|
||||
}
|
||||
|
||||
if year < 1900 {
|
||||
s := 0
|
||||
for yy := year; yy < 1900; yy++ {
|
||||
s += GetIsoWeekCount(yy)
|
||||
}
|
||||
w := -s
|
||||
isoWeekCountAggregate[year] = w
|
||||
return w
|
||||
}
|
||||
|
||||
w := GetIsoWeekCount(year)
|
||||
|
||||
w += GetAggregateIsoWeekCount(year - 1)
|
||||
|
||||
isoWeekCountAggregate[year] = w
|
||||
|
||||
return w
|
||||
}
|
||||
|
||||
func GetIsoWeekCount(year int) int {
|
||||
_, w1 := time.Date(year+0, 12, 27, 0, 0, 0, 0, TimezoneBerlin).ISOWeek()
|
||||
_, w2 := time.Date(year+0, 12, 31, 0, 0, 0, 0, TimezoneBerlin).ISOWeek()
|
||||
_, w3 := time.Date(year+1, 1, 4, 0, 0, 0, 0, TimezoneBerlin).ISOWeek()
|
||||
|
||||
w1 -= 1
|
||||
w2 -= 1
|
||||
w3 -= 1
|
||||
|
||||
w := w1
|
||||
if w2 > w {
|
||||
w = w2
|
||||
}
|
||||
if w3 > w {
|
||||
w = w3
|
||||
}
|
||||
|
||||
return w
|
||||
}
|
||||
|
||||
func GetGlobalWeeknumber(t time.Time) int {
|
||||
y, w := t.ISOWeek()
|
||||
w -= 1
|
||||
if y <= 1900 {
|
||||
w -= 1
|
||||
}
|
||||
return GetAggregateIsoWeekCount(y-1) + w
|
||||
}
|
Reference in New Issue
Block a user