diff --git a/cyclops_utils/cyclops_utils.go b/cyclops_utils/cyclops_utils.go new file mode 100644 index 0000000..72094c4 --- /dev/null +++ b/cyclops_utils/cyclops_utils.go @@ -0,0 +1,36 @@ +package cyclops_utils + +import ( + "time" +) + +func GetReportingPeriod(inputdate string) (err error, from, to string) { + err = nil + layout := "2006-01-02" + t, err := time.Parse(layout, inputdate) + if err != nil { + return + } + y, m, d := t.Date() + + valTo := t.AddDate(0, 0, -1) + to = valTo.Format("2006-01-02") + valFrom := time.Now() + if d == 1 && (m == 1 || m == 4 || m == 7 || m == 10) { + //subtracting 1 day + valFrom = t.AddDate(0,-3,0) + + } else { + if m >= 1 && m <= 3 { + valFrom = time.Date(y, time.January, 1,0,0,0,0, time.UTC) + } else if m >= 4 && m <= 6 { + valFrom = time.Date(y, time.April, 1,0,0,0,0, time.UTC) + } else if m >= 7 && m <= 9 { + valFrom = time.Date(y, time.July, 1,0,0,0,0, time.UTC) + } else if m >= 10 && m <= 12 { + valFrom = time.Date(y, time.October, 1,0,0,0,0, time.UTC) + } + } + from = valFrom.Format("2006-01-02") + return +} \ No newline at end of file diff --git a/main.go b/main.go new file mode 100644 index 0000000..c7b97a9 --- /dev/null +++ b/main.go @@ -0,0 +1,16 @@ +package main + +import ( + "fmt" + "gitlab.com/cyclops-utilities/date-utils/cyclops_utils" + +) + +func main() { + err, from, to := cyclops_utils.GetReportingPeriod("2019-10-01") + if err != nil { + fmt.Errorf("Error in processing input: %s", err.Error()) + } else { + fmt.Printf("From: %s, To: %s\n", from, to); + } +} \ No newline at end of file