github.com/haraldrudell/parl@v0.4.176/ptime/date.go (about)

     1  /*
     2  © 2022–present Harald Rudell <harald.rudell@gmail.com> (https://haraldrudell.github.io/haraldrudell/)
     3  ISC License
     4  */
     5  
     6  package ptime
     7  
     8  import "time"
     9  
    10  // ptime.Date is like time.Date with a flag valid that indicates
    11  // whether the provided values represents a valid date.
    12  // year is a 4-digit year, ie. 2022 is 2022.
    13  // month 1–12, day 1–31, hour 0–23 minute 0-59, sec 0–59
    14  func Date(year int, month int, day int, hour int, min int, sec int, nsec int, loc *time.Location) (t time.Time, valid bool) {
    15  	t = time.Date(year, time.Month(month), day, hour, min, sec, nsec, loc)
    16  	valid = t.Year() == year && t.Month() == time.Month(month) && t.Day() == day &&
    17  		t.Hour() == hour && t.Minute() == min && t.Second() == sec &&
    18  		t.Nanosecond() == nsec
    19  	return
    20  }
    21  
    22  // ptime.Date is like time.Date with a flag valid that indicates
    23  // whether the provided values represents a valid date.
    24  // year is a 4-digit year, ie. 2022 is 2022.
    25  // month 1–12, day 1–31, hour 0–23 minute 0-59, sec 0–59
    26  // A year below 1000 is not considered valid
    27  func Date1k(year int, month int, day int, hour int, min int, sec int, nsec int, loc *time.Location) (t time.Time, valid bool) {
    28  	t, valid = Date(year, month, day, hour, min, sec, nsec, loc)
    29  	valid = valid && year >= 1000
    30  	return
    31  }