github.com/minio/minio@v0.0.0-20240328213742-3f72439b8a27/internal/amztime/iso8601_time.go (about)

     1  // Copyright (c) 2015-2022 MinIO, Inc.
     2  //
     3  // This file is part of MinIO Object Storage stack
     4  //
     5  // This program is free software: you can redistribute it and/or modify
     6  // it under the terms of the GNU Affero General Public License as published by
     7  // the Free Software Foundation, either version 3 of the License, or
     8  // (at your option) any later version.
     9  //
    10  // This program is distributed in the hope that it will be useful
    11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13  // GNU Affero General Public License for more details.
    14  //
    15  // You should have received a copy of the GNU Affero General Public License
    16  // along with this program.  If not, see <http://www.gnu.org/licenses/>.
    17  
    18  package amztime
    19  
    20  import (
    21  	"strings"
    22  	"time"
    23  )
    24  
    25  // RFC3339 a subset of the ISO8601 timestamp format. e.g 2014-04-29T18:30:38Z
    26  const (
    27  	iso8601TimeFormat     = "2006-01-02T15:04:05.000Z"    // Reply date format with millisecond precision.
    28  	iso8601TimeFormatLong = "2006-01-02T15:04:05.000000Z" // Reply date format with nanosecond precision.
    29  )
    30  
    31  // ISO8601Format converts time 't' into ISO8601 time format expected in AWS S3 spec.
    32  //
    33  // This function is needed to avoid a Go's float64 precision bug, where Go avoids
    34  // padding the extra '0' before the timezone.
    35  func ISO8601Format(t time.Time) string {
    36  	value := t.Format(iso8601TimeFormat)
    37  	if len(value) < len(iso8601TimeFormat) {
    38  		value = t.Format(iso8601TimeFormat[:len(iso8601TimeFormat)-1])
    39  		// Pad necessary zeroes to full-fill the iso8601TimeFormat
    40  		return value + strings.Repeat("0", (len(iso8601TimeFormat)-1)-len(value)) + "Z"
    41  	}
    42  	return value
    43  }
    44  
    45  // ISO8601Parse parses ISO8601 date string
    46  func ISO8601Parse(iso8601 string) (t time.Time, err error) {
    47  	for _, layout := range []string{
    48  		iso8601TimeFormat,
    49  		iso8601TimeFormatLong,
    50  		time.RFC3339,
    51  	} {
    52  		t, err = time.Parse(layout, iso8601)
    53  		if err == nil {
    54  			return t, nil
    55  		}
    56  	}
    57  
    58  	return t, err
    59  }