github.com/kilpkonn/gtm-enhanced@v1.3.5/util/date.go (about)

     1  // Copyright 2016 Michael Schenk. All rights reserved.
     2  // Use of this source code is governed by a MIT-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package util
     6  
     7  import (
     8  	"fmt"
     9  	"time"
    10  
    11  	"github.com/jinzhu/now"
    12  )
    13  
    14  // Now is the func used for system time within gtm
    15  // This allows for manipulating system time during testing
    16  var Now = func() time.Time { return time.Now() }
    17  
    18  // DateRange creates predefined date ranges and validates if dates are within the range
    19  type DateRange struct {
    20  	Start time.Time
    21  	End   time.Time
    22  }
    23  
    24  // IsSet returns true if the date range has a starting and/or ending date
    25  func (d DateRange) IsSet() bool {
    26  	return !d.Start.IsZero() || !d.End.IsZero()
    27  }
    28  
    29  // String returns a date range as a string
    30  func (d DateRange) String() string {
    31  	return fmt.Sprintf("%s - %s", d.Start.Format(time.UnixDate), d.End.Format(time.UnixDate))
    32  }
    33  
    34  // Within determines if a date is within the date range
    35  func (d DateRange) Within(t time.Time) bool {
    36  	switch {
    37  	case !d.Start.IsZero() && !d.End.IsZero():
    38  		return t.Equal(d.Start) || t.Equal(d.End) || (t.After(d.Start) && t.Before(d.End))
    39  	case !d.Start.IsZero():
    40  		return t.Equal(d.Start) || t.After(d.Start)
    41  	case !d.End.IsZero():
    42  		return t.Equal(d.End) || t.Before(d.End)
    43  	default:
    44  		return false
    45  	}
    46  
    47  }
    48  
    49  // AfterNow returns a date range ending n days in the past
    50  func AfterNow(n int) DateRange {
    51  	end := now.New(Now()).EndOfDay().AddDate(0, 0, -n)
    52  	return DateRange{End: end}
    53  }
    54  
    55  // TodayRange returns a date range for today
    56  func TodayRange() DateRange {
    57  	now := now.New(Now())
    58  
    59  	start := now.BeginningOfDay()
    60  	end := now.EndOfDay()
    61  
    62  	return DateRange{Start: start, End: end}
    63  }
    64  
    65  // YesterdayRange returns a date range for yesterday
    66  func YesterdayRange() DateRange {
    67  	now := now.New(Now())
    68  
    69  	start := now.BeginningOfDay().AddDate(0, 0, -1)
    70  	end := start.AddDate(0, 0, 1).Add(-time.Nanosecond)
    71  
    72  	return DateRange{Start: start, End: end}
    73  }
    74  
    75  // ThisWeekRange returns a date range for this week
    76  func ThisWeekRange() DateRange {
    77  	now := now.New(Now())
    78  
    79  	start := now.BeginningOfWeek()
    80  	end := now.EndOfWeek()
    81  
    82  	return DateRange{End: end, Start: start}
    83  }
    84  
    85  // LastWeekRange returns a date for last week
    86  func LastWeekRange() DateRange {
    87  	now := now.New(Now())
    88  
    89  	start := now.BeginningOfWeek().AddDate(0, 0, -7)
    90  	end := start.AddDate(0, 0, 7).Add(-time.Nanosecond)
    91  
    92  	return DateRange{End: end, Start: start}
    93  }
    94  
    95  // ThisMonthRange returns a date range for this month
    96  func ThisMonthRange() DateRange {
    97  	now := now.New(Now())
    98  
    99  	start := now.BeginningOfMonth()
   100  	end := now.EndOfMonth()
   101  
   102  	return DateRange{End: end, Start: start}
   103  }
   104  
   105  // LastMonthRange returns a date range for last month
   106  func LastMonthRange() DateRange {
   107  	now := now.New(Now())
   108  
   109  	start := now.BeginningOfMonth().AddDate(0, -1, 0)
   110  	end := start.AddDate(0, 1, 0).Add(-time.Nanosecond)
   111  
   112  	return DateRange{End: end, Start: start}
   113  }
   114  
   115  // ThisYearRange returns a date range for this year
   116  func ThisYearRange() DateRange {
   117  	now := now.New(Now())
   118  
   119  	start := now.BeginningOfYear()
   120  	end := now.EndOfYear()
   121  
   122  	return DateRange{End: end, Start: start}
   123  }
   124  
   125  // LastYearRange returns a date range for last year
   126  func LastYearRange() DateRange {
   127  	now := now.New(Now())
   128  
   129  	start := now.BeginningOfYear().AddDate(-1, 0, 0)
   130  	end := start.AddDate(1, 0, 0).Add(-time.Nanosecond)
   131  
   132  	return DateRange{End: end, Start: start}
   133  }