dates.go (2297B)
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 (
17 "time"
18
19 "github.com/gohugoio/hugo/common/htime"
20 )
21
22 var _ Dated = Dates{}
23
24 // Dated wraps a "dated resource". These are the 4 dates that makes
25 // the date logic in Hugo.
26 type Dated interface {
27 // Date returns the date of the resource.
28 Date() time.Time
29
30 // Lastmod returns the last modification date of the resource.
31 Lastmod() time.Time
32
33 // PublishDate returns the publish date of the resource.
34 PublishDate() time.Time
35
36 // ExpiryDate returns the expiration date of the resource.
37 ExpiryDate() time.Time
38 }
39
40 // Dates holds the 4 Hugo dates.
41 type Dates struct {
42 FDate time.Time
43 FLastmod time.Time
44 FPublishDate time.Time
45 FExpiryDate time.Time
46 }
47
48 func (d *Dates) UpdateDateAndLastmodIfAfter(in Dated) {
49 if in.Date().After(d.Date()) {
50 d.FDate = in.Date()
51 }
52 if in.Lastmod().After(d.Lastmod()) {
53 d.FLastmod = in.Lastmod()
54 }
55 }
56
57 // IsFuture returns whether the argument represents the future.
58 func IsFuture(d Dated) bool {
59 if d.PublishDate().IsZero() {
60 return false
61 }
62
63 return d.PublishDate().After(htime.Now())
64 }
65
66 // IsExpired returns whether the argument is expired.
67 func IsExpired(d Dated) bool {
68 if d.ExpiryDate().IsZero() {
69 return false
70 }
71 return d.ExpiryDate().Before(htime.Now())
72 }
73
74 // IsZeroDates returns true if all of the dates are zero.
75 func IsZeroDates(d Dated) bool {
76 return d.Date().IsZero() && d.Lastmod().IsZero() && d.ExpiryDate().IsZero() && d.PublishDate().IsZero()
77 }
78
79 func (p Dates) Date() time.Time {
80 return p.FDate
81 }
82
83 func (p Dates) Lastmod() time.Time {
84 return p.FLastmod
85 }
86
87 func (p Dates) PublishDate() time.Time {
88 return p.FPublishDate
89 }
90
91 func (p Dates) ExpiryDate() time.Time {
92 return p.FExpiryDate
93 }