github.com/kovansky/hugo@v0.92.3-0.20220224232819-63076e4ff19f/resources/resource/dates.go (about)

     1  // Copyright 2019 The Hugo Authors. All rights reserved.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  // http://www.apache.org/licenses/LICENSE-2.0
     7  //
     8  // Unless required by applicable law or agreed to in writing, software
     9  // distributed under the License is distributed on an "AS IS" BASIS,
    10  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package resource
    15  
    16  import "time"
    17  
    18  var _ Dated = Dates{}
    19  
    20  // Dated wraps a "dated resource". These are the 4 dates that makes
    21  // the date logic in Hugo.
    22  type Dated interface {
    23  	Date() time.Time
    24  	Lastmod() time.Time
    25  	PublishDate() time.Time
    26  	ExpiryDate() time.Time
    27  }
    28  
    29  // Dates holds the 4 Hugo dates.
    30  type Dates struct {
    31  	FDate        time.Time
    32  	FLastmod     time.Time
    33  	FPublishDate time.Time
    34  	FExpiryDate  time.Time
    35  }
    36  
    37  func (d *Dates) UpdateDateAndLastmodIfAfter(in Dated) {
    38  	if in.Date().After(d.Date()) {
    39  		d.FDate = in.Date()
    40  	}
    41  	if in.Lastmod().After(d.Lastmod()) {
    42  		d.FLastmod = in.Lastmod()
    43  	}
    44  }
    45  
    46  // IsFuture returns whether the argument represents the future.
    47  func IsFuture(d Dated) bool {
    48  	if d.PublishDate().IsZero() {
    49  		return false
    50  	}
    51  	return d.PublishDate().After(time.Now())
    52  }
    53  
    54  // IsExpired returns whether the argument is expired.
    55  func IsExpired(d Dated) bool {
    56  	if d.ExpiryDate().IsZero() {
    57  		return false
    58  	}
    59  	return d.ExpiryDate().Before(time.Now())
    60  }
    61  
    62  // IsZeroDates returns true if all of the dates are zero.
    63  func IsZeroDates(d Dated) bool {
    64  	return d.Date().IsZero() && d.Lastmod().IsZero() && d.ExpiryDate().IsZero() && d.PublishDate().IsZero()
    65  }
    66  
    67  func (p Dates) Date() time.Time {
    68  	return p.FDate
    69  }
    70  
    71  func (p Dates) Lastmod() time.Time {
    72  	return p.FLastmod
    73  }
    74  
    75  func (p Dates) PublishDate() time.Time {
    76  	return p.FPublishDate
    77  }
    78  
    79  func (p Dates) ExpiryDate() time.Time {
    80  	return p.FExpiryDate
    81  }