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

     1  /*
     2  © 2018–present Harald Rudell <harald.rudell@gmail.com> (https://haraldrudell.github.io/haraldrudell/)
     3  ISC License
     4  */
     5  
     6  // Package parltime provides on-time timers, 64-bit epoch, formaatting and other time functions.
     7  package ptime
     8  
     9  import (
    10  	"time"
    11  
    12  	"github.com/haraldrudell/parl/perrors"
    13  )
    14  
    15  const (
    16  	// RFC 3339 (email) time format
    17  	rfc3339 = "2006-01-02 15:04:05-07:00"
    18  	// RFC3339NanoSpace RFC3339 format, ns precision, space separator
    19  	RFC3339NanoSpace string = "2006-01-02 15:04:05.999999999Z07:00"
    20  )
    21  
    22  // Rfc3339 converts local time to string with second precision and time offset: 2006-01-02 15:04:05-07:00
    23  func Rfc3339(t time.Time) string {
    24  	return t.Format(rfc3339)
    25  }
    26  
    27  // ParseTime parses output from Rfc3339
    28  func ParseTime(dateString string) (tm time.Time, err error) {
    29  	if tm, err = time.Parse(rfc3339, dateString); err != nil {
    30  		err = perrors.Errorf("time.Parse: '%w'", err)
    31  	}
    32  	return
    33  }
    34  
    35  // Ms gets duration in milliseconds
    36  func Ms(d time.Duration) string {
    37  	return d.Truncate(time.Millisecond).String()
    38  }
    39  
    40  // Ns converts time to string with nanosecond accuracy and UTC location
    41  func Ns(t time.Time) string {
    42  	return t.UTC().Truncate(time.Nanosecond).Format(time.RFC3339Nano)
    43  }
    44  
    45  // ParseNs parses an RFC3339 time string with nanosecond accuracy
    46  func ParseNs(timeString string) (t time.Time, err error) {
    47  	t, err = time.Parse(time.RFC3339Nano, timeString)
    48  	if err != nil {
    49  		t, err = time.Parse(RFC3339NanoSpace, timeString)
    50  	}
    51  	return
    52  }
    53  
    54  // S converts time to string with second accuracy and UTC location
    55  func S(t time.Time) string {
    56  	return t.UTC().Truncate(time.Nanosecond).Format(time.RFC3339)
    57  }
    58  
    59  // ParseS parses an RFC3339 time string with nanosecond accuracy
    60  func ParseS(timeString string) (t time.Time, err error) {
    61  	t, err = time.Parse(time.RFC3339, timeString)
    62  	return
    63  }
    64  
    65  // NsLocal converts time to string with nanosecond accuracy and local time zone
    66  func NsLocal(t time.Time) string {
    67  	return t.Local().Truncate(time.Nanosecond).Format(time.RFC3339Nano)
    68  }
    69  
    70  // SGreater compares two times first rouding to second
    71  func SGreater(t1 time.Time, t2 time.Time) bool {
    72  	return t1.Truncate(time.Second).After(t2.Truncate(time.Second))
    73  }
    74  
    75  // GetTimeString rfc 3339: email time format 2020-12-04 20:20:17-08:00
    76  func GetTimeString(wallTime *time.Time) (s string) {
    77  	var when time.Time
    78  	if wallTime != nil {
    79  		when = *wallTime
    80  	} else {
    81  		when = time.Now()
    82  	}
    83  	s = when.Format(rfc3339)
    84  	return
    85  }