...

Source file src/github.com/sigstore/cosign/v2/internal/pkg/now/now.go

Documentation: github.com/sigstore/cosign/v2/internal/pkg/now

     1  //
     2  // Copyright 2023 The Sigstore Authors.
     3  //
     4  // Licensed under the Apache License, Version 2.0 (the "License");
     5  // you may not use this file except in compliance with the License.
     6  // You may obtain a copy of the License at
     7  //
     8  //     http://www.apache.org/licenses/LICENSE-2.0
     9  //
    10  // Unless required by applicable law or agreed to in writing, software
    11  // distributed under the License is distributed on an "AS IS" BASIS,
    12  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  // See the License for the specific language governing permissions and
    14  // limitations under the License.
    15  
    16  package now
    17  
    18  import (
    19  	"fmt"
    20  	"os"
    21  	"strconv"
    22  	"time"
    23  )
    24  
    25  // Now returns SOURCE_DATE_EPOCH or time.Now().
    26  func Now() (time.Time, error) {
    27  	// nolint
    28  	epoch := os.Getenv("SOURCE_DATE_EPOCH")
    29  	if epoch == "" {
    30  		return time.Now(), nil
    31  	}
    32  
    33  	seconds, err := strconv.ParseInt(epoch, 10, 64)
    34  	if err != nil {
    35  		return time.Now(), fmt.Errorf("SOURCE_DATE_EPOCH should be the number of seconds since January 1st 1970, 00:00 UTC, got: %w", err)
    36  	}
    37  	return time.Unix(seconds, 0), nil
    38  }
    39  

View as plain text