github.com/wtfutil/wtf@v0.43.0/wtf/datetime.go (about)

     1  package wtf
     2  
     3  import (
     4  	"fmt"
     5  	"time"
     6  )
     7  
     8  const (
     9  	// DateFormat defines the format we expect to receive dates from BambooHR in
    10  	DateFormat = "2006-01-02"
    11  
    12  	// TimeFormat defines the format we expect to receive times from BambooHR in
    13  	TimeFormat = "15:04"
    14  )
    15  
    16  // IsToday returns TRUE if the date is today, FALSE if the date is not today
    17  func IsToday(date time.Time) bool {
    18  	now := time.Now().Local()
    19  
    20  	return (date.Year() == now.Year()) &&
    21  		(date.Month() == now.Month()) &&
    22  		(date.Day() == now.Day())
    23  }
    24  
    25  // PrettyDate takes a programmer-style date string and converts it
    26  // in a friendlier-to-read format
    27  func PrettyDate(dateStr string) string {
    28  	newTime, err := time.Parse(DateFormat, dateStr)
    29  	if err != nil {
    30  		return dateStr
    31  	}
    32  
    33  	return fmt.Sprint(newTime.Format("Jan 2, 2006"))
    34  }
    35  
    36  // UnixTime takes a Unix epoch time (in seconds) and returns a
    37  // time.Time instance
    38  func UnixTime(unix int64) time.Time {
    39  	return time.Unix(unix, 0)
    40  }