date and gz helper methods
This commit is contained in:
89
date/date.go
Normal file
89
date/date.go
Normal file
@@ -0,0 +1,89 @@
|
||||
// Copyright 2025 Patial Tech (Ankit Patial).
|
||||
//
|
||||
// This file is part of code.patial.tech/go/appcore, which is MIT licensed.
|
||||
// See http://opensource.org/licenses/MIT
|
||||
|
||||
package date
|
||||
|
||||
import (
|
||||
"time"
|
||||
)
|
||||
|
||||
// ParseISODate ISO date format to utc
|
||||
func ParseISODate(dt string) (time.Time, error) {
|
||||
d, err := time.Parse(time.RFC3339, dt)
|
||||
if err != nil {
|
||||
return time.Time{}, err
|
||||
}
|
||||
|
||||
return d.UTC(), nil
|
||||
}
|
||||
|
||||
func CountWorkingDays(from, end time.Time) int {
|
||||
workingDays := 0
|
||||
start := from // from.AddDate(0, 0, 1)
|
||||
for d := start; d.Before(end) || d.Equal(end); d = d.AddDate(0, 0, 1) {
|
||||
weekday := d.Weekday()
|
||||
if weekday != time.Saturday && weekday != time.Sunday {
|
||||
workingDays++
|
||||
}
|
||||
}
|
||||
|
||||
return workingDays
|
||||
}
|
||||
|
||||
func StartOfDay(date time.Time) time.Time {
|
||||
year, month, day := date.Date()
|
||||
return time.Date(year, month, day, 0, 0, 0, 0, date.Location())
|
||||
}
|
||||
|
||||
func EndOfDay(date time.Time) time.Time {
|
||||
year, month, day := date.Date()
|
||||
return time.Date(year, month, day, 23, 59, 59, 0, date.Location())
|
||||
}
|
||||
|
||||
func StartOfMonth(date time.Time) time.Time {
|
||||
year, month, _ := date.Date()
|
||||
return time.Date(year, month, 1, 0, 0, 0, 0, date.Location())
|
||||
}
|
||||
|
||||
func EndOfMonth(date time.Time) time.Time {
|
||||
return EndOfDay(date.AddDate(0, 1, -date.Day()))
|
||||
}
|
||||
|
||||
func StartOfWeek(date time.Time) time.Time {
|
||||
return StartOfDay(date.AddDate(0, 0, int(time.Sunday)-int(date.Weekday())))
|
||||
}
|
||||
|
||||
func EndOfWeek(date time.Time) time.Time {
|
||||
return EndOfDay(date.AddDate(0, 0, int(time.Saturday-date.Weekday())))
|
||||
}
|
||||
|
||||
func StartOfYear(date time.Time) time.Time {
|
||||
year, _, _ := date.Date()
|
||||
return time.Date(year, 1, 1, 0, 0, 0, 0, date.Location())
|
||||
}
|
||||
|
||||
func EndOfYear(date time.Time) time.Time {
|
||||
return EndOfDay(date.AddDate(0, int(time.December-date.Month())+1, -date.Day()))
|
||||
}
|
||||
|
||||
func LastWeek(date time.Time) time.Time {
|
||||
return date.AddDate(0, 0, -7)
|
||||
}
|
||||
|
||||
func LastMonth(date time.Time) time.Time {
|
||||
return date.AddDate(0, -1, 0)
|
||||
}
|
||||
|
||||
func SubtractMonths(date time.Time, m int) time.Time {
|
||||
return date.AddDate(0, -m, 0)
|
||||
}
|
||||
|
||||
func SubtractDays(date time.Time, d int) time.Time {
|
||||
return date.AddDate(0, 0, -d)
|
||||
}
|
||||
|
||||
func SubtractAYear(date time.Time) time.Time {
|
||||
return date.AddDate(-1, 0, 0)
|
||||
}
|
46
gz/gz.go
Normal file
46
gz/gz.go
Normal file
@@ -0,0 +1,46 @@
|
||||
// Copyright 2025 Patial Tech (Ankit Patial).
|
||||
//
|
||||
// This file is part of code.patial.tech/go/appcore, which is MIT licensed.
|
||||
// See http://opensource.org/licenses/MIT
|
||||
|
||||
package gz
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"compress/gzip"
|
||||
"io"
|
||||
)
|
||||
|
||||
func Zip(data []byte) ([]byte, error) {
|
||||
var b bytes.Buffer
|
||||
gz := gzip.NewWriter(&b)
|
||||
if _, err := gz.Write(data); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if err := gz.Flush(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if err := gz.Close(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return b.Bytes(), nil
|
||||
}
|
||||
|
||||
func UnZip(data []byte) ([]byte, error) {
|
||||
b := bytes.NewBuffer(data)
|
||||
var r io.Reader
|
||||
r, err := gzip.NewReader(b)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var resB bytes.Buffer
|
||||
if _, err := resB.ReadFrom(r); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return resB.Bytes(), nil
|
||||
}
|
Reference in New Issue
Block a user