github.com/haraldrudell/parl@v0.4.176/ptime/format.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 (
     9  	"time"
    10  )
    11  
    12  const (
    13  	Date6      = "060102"
    14  	rfc3339sz  = "2006-01-02T15:04:05Z"
    15  	rfc3339msz = "2006-01-02T15:04:05.000Z"
    16  	rfc3339usz = "2006-01-02T15:04:05.000000Z"
    17  	rfc3339nsz = "2006-01-02T15:04:05.000000000Z"
    18  )
    19  
    20  // Rfc3339nsz prints a time using UTC and nanoseconds precision.
    21  //
    22  //	"2022-01-01T08:00:00.000000000Z"
    23  func Rfc3339nsz(t time.Time) (s string) {
    24  	// this must be a function because of the .UTC incovation
    25  	return t.UTC().Format(rfc3339nsz)
    26  }
    27  
    28  // Rfc3339usz prints a time using UTC and microseconds precision.
    29  //
    30  //	"2022-01-01T08:00:00.000000Z"
    31  func Rfc3339usz(t time.Time) (s string) {
    32  	// this must be a function because of the .UTC incovation
    33  	return t.UTC().Format(rfc3339usz)
    34  }
    35  
    36  // Rfc3339msz prints a time using UTC and milliseconds precision.
    37  //
    38  //	"2022-01-01T08:00:00.000Z"
    39  func Rfc3339msz(t time.Time) (s string) {
    40  	// this must be a function because of the .UTC incovation
    41  	return t.UTC().Format(rfc3339msz)
    42  }
    43  
    44  // Rfc3339sz prints a time using UTC and seconds precision.
    45  //
    46  //	"2022-01-01T08:00:00Z"
    47  func Rfc3339sz(t time.Time) (s string) {
    48  	// this must be a function because of the .UTC incovation
    49  	return t.UTC().Format(rfc3339sz)
    50  }
    51  
    52  // ParseRfc3339nsz parses a UTC time string at nanoseconds precision.
    53  //
    54  //	"2022-01-01T08:00:00.000000000Z"
    55  func ParseRfc3339nsz(timeString string) (t time.Time, err error) {
    56  	t, err = time.Parse(rfc3339nsz, timeString)
    57  	return
    58  }