Skip to content
Snippets Groups Projects
Commit 88db7899 authored by Francé Wilke's avatar Francé Wilke
Browse files

Export db_utils functions

parent 8db41332
No related branches found
Tags v1.275.0
No related merge requests found
...@@ -20,7 +20,7 @@ const ( ...@@ -20,7 +20,7 @@ const (
Yesterday string = "^yesterday$" Yesterday string = "^yesterday$"
) )
func checkAbsoluteRegex(regex string, value string) bool { func CheckAbsoluteRegex(regex string, value string) bool {
matchesRegex, err := regexp.Match(regex, []byte(value)) matchesRegex, err := regexp.Match(regex, []byte(value))
if matchesRegex && err == nil { if matchesRegex && err == nil {
return true return true
...@@ -29,7 +29,7 @@ func checkAbsoluteRegex(regex string, value string) bool { ...@@ -29,7 +29,7 @@ func checkAbsoluteRegex(regex string, value string) bool {
return false return false
} }
func pullAbsoluteValue(value string) int64 { func PullAbsoluteValue(value string) int64 {
reg, err := regexp.Compile("[a-zA-Z]+") reg, err := regexp.Compile("[a-zA-Z]+")
if err != nil { if err != nil {
return 0 return 0
...@@ -45,32 +45,32 @@ func AbsoluteDateStringQuery(absolute string) string { ...@@ -45,32 +45,32 @@ func AbsoluteDateStringQuery(absolute string) string {
var value int64 var value int64
timeNow := date_utils.CurrentDate() timeNow := date_utils.CurrentDate()
if checkAbsoluteRegex(LastXHours, absolute) { if CheckAbsoluteRegex(LastXHours, absolute) {
value = pullAbsoluteValue(absolute) value = PullAbsoluteValue(absolute)
since := timeNow.Add(time.Duration(-value) * time.Hour) since := timeNow.Add(time.Duration(-value) * time.Hour)
return fmt.Sprintf(" >= '%v'", date_utils.DateDBFormattedString(since)) return fmt.Sprintf(" >= '%v'", date_utils.DateDBFormattedString(since))
} }
if checkAbsoluteRegex(LastDay, absolute) { if CheckAbsoluteRegex(LastDay, absolute) {
startDay := time.Date(timeNow.Year(), timeNow.Month(), timeNow.Day(), 0, 0, 0, 0, date_utils.CurrentLocation()) startDay := time.Date(timeNow.Year(), timeNow.Month(), timeNow.Day(), 0, 0, 0, 0, date_utils.CurrentLocation())
endDay := time.Date(timeNow.Year(), timeNow.Month(), timeNow.Day(), 23, 59, 59, 999999999, date_utils.CurrentLocation()) endDay := time.Date(timeNow.Year(), timeNow.Month(), timeNow.Day(), 23, 59, 59, 999999999, date_utils.CurrentLocation())
return fmt.Sprintf(" BETWEEN '%v' AND '%v'", date_utils.DateDBFormattedString(startDay), date_utils.DateDBFormattedString(endDay)) return fmt.Sprintf(" BETWEEN '%v' AND '%v'", date_utils.DateDBFormattedString(startDay), date_utils.DateDBFormattedString(endDay))
} }
if checkAbsoluteRegex(LastXDays, absolute) { if CheckAbsoluteRegex(LastXDays, absolute) {
value = pullAbsoluteValue(absolute) value = PullAbsoluteValue(absolute)
since := timeNow.AddDate(0, 0, -int(value)) since := timeNow.AddDate(0, 0, -int(value))
return fmt.Sprintf(" >= '%v'", date_utils.DateDBFormattedString(since)) return fmt.Sprintf(" >= '%v'", date_utils.DateDBFormattedString(since))
} }
if checkAbsoluteRegex(LastMonth, absolute) { if CheckAbsoluteRegex(LastMonth, absolute) {
firstOfMonth := time.Date(timeNow.Year(), timeNow.Month(), 1, 0, 0, 0, 0, date_utils.CurrentLocation()) firstOfMonth := time.Date(timeNow.Year(), timeNow.Month(), 1, 0, 0, 0, 0, date_utils.CurrentLocation())
lastDay := firstOfMonth.AddDate(0, 1, -1) lastDay := firstOfMonth.AddDate(0, 1, -1)
lastOfMonth := time.Date(lastDay.Year(), lastDay.Month(), lastDay.Day(), 23, 59, 59, 999999999, date_utils.CurrentLocation()) lastOfMonth := time.Date(lastDay.Year(), lastDay.Month(), lastDay.Day(), 23, 59, 59, 999999999, date_utils.CurrentLocation())
return fmt.Sprintf(" BETWEEN '%v' AND '%v'", date_utils.DateDBFormattedString(firstOfMonth), date_utils.DateDBFormattedString(lastOfMonth)) return fmt.Sprintf(" BETWEEN '%v' AND '%v'", date_utils.DateDBFormattedString(firstOfMonth), date_utils.DateDBFormattedString(lastOfMonth))
} }
if checkAbsoluteRegex(Yesterday, absolute) { if CheckAbsoluteRegex(Yesterday, absolute) {
yesterday := timeNow.AddDate(0, 0, -1) yesterday := timeNow.AddDate(0, 0, -1)
startDay := time.Date(yesterday.Year(), yesterday.Month(), yesterday.Day(), 0, 0, 0, 0, date_utils.CurrentLocation()) startDay := time.Date(yesterday.Year(), yesterday.Month(), yesterday.Day(), 0, 0, 0, 0, date_utils.CurrentLocation())
endDay := time.Date(yesterday.Year(), yesterday.Month(), yesterday.Day(), 23, 59, 59, 999999999, date_utils.CurrentLocation()) endDay := time.Date(yesterday.Year(), yesterday.Month(), yesterday.Day(), 23, 59, 59, 999999999, date_utils.CurrentLocation())
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment