github.com/lalkh/containerd@v1.4.3/archive/strconv.go (about)

     1  // +build windows
     2  
     3  /*
     4     Copyright The containerd Authors.
     5  
     6     Licensed under the Apache License, Version 2.0 (the "License");
     7     you may not use this file except in compliance with the License.
     8     You may obtain a copy of the License at
     9  
    10         http://www.apache.org/licenses/LICENSE-2.0
    11  
    12     Unless required by applicable law or agreed to in writing, software
    13     distributed under the License is distributed on an "AS IS" BASIS,
    14     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    15     See the License for the specific language governing permissions and
    16     limitations under the License.
    17  */
    18  
    19  package archive
    20  
    21  import (
    22  	"strconv"
    23  	"strings"
    24  	"time"
    25  
    26  	"archive/tar"
    27  )
    28  
    29  // Forked from https://github.com/golang/go/blob/master/src/archive/tar/strconv.go
    30  // as archive/tar doesn't support CreationTime, but does handle PAX time parsing,
    31  // and there's no need to re-invent the wheel.
    32  
    33  // parsePAXTime takes a string of the form %d.%d as described in the PAX
    34  // specification. Note that this implementation allows for negative timestamps,
    35  // which is allowed for by the PAX specification, but not always portable.
    36  func parsePAXTime(s string) (time.Time, error) {
    37  	const maxNanoSecondDigits = 9
    38  
    39  	// Split string into seconds and sub-seconds parts.
    40  	ss, sn := s, ""
    41  	if pos := strings.IndexByte(s, '.'); pos >= 0 {
    42  		ss, sn = s[:pos], s[pos+1:]
    43  	}
    44  
    45  	// Parse the seconds.
    46  	secs, err := strconv.ParseInt(ss, 10, 64)
    47  	if err != nil {
    48  		return time.Time{}, tar.ErrHeader
    49  	}
    50  	if len(sn) == 0 {
    51  		return time.Unix(secs, 0), nil // No sub-second values
    52  	}
    53  
    54  	// Parse the nanoseconds.
    55  	if strings.Trim(sn, "0123456789") != "" {
    56  		return time.Time{}, tar.ErrHeader
    57  	}
    58  	if len(sn) < maxNanoSecondDigits {
    59  		sn += strings.Repeat("0", maxNanoSecondDigits-len(sn)) // Right pad
    60  	} else {
    61  		sn = sn[:maxNanoSecondDigits] // Right truncate
    62  	}
    63  	nsecs, _ := strconv.ParseInt(sn, 10, 64) // Must succeed
    64  	if len(ss) > 0 && ss[0] == '-' {
    65  		return time.Unix(secs, -nsecs), nil // Negative correction
    66  	}
    67  	return time.Unix(secs, nsecs), nil
    68  }