Skip to content

Commit fc6032d

Browse files
committed
lang: funcs: Add a weekday function to the datetime package
This returns the day of the week. It also includes a helpful example demonstrating how this functionality can be fun!
1 parent 43839d1 commit fc6032d

File tree

2 files changed

+79
-0
lines changed

2 files changed

+79
-0
lines changed

examples/lang/readonlyfriday.mcl

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import "datetime"
2+
import "fmt"
3+
4+
$now = datetime.now()
5+
$day = datetime.weekday($now)
6+
$is_friday = $day == "friday"
7+
8+
$s1 = template("Hello! It is now: {{ datetime_print . }}\n", $now)
9+
$s2 = if $is_friday {
10+
"It's friday!!! (don't break anything, read-only)"
11+
} else {
12+
if $day == "saturday" || $day == "sunday" {
13+
"It's the weekend!"
14+
} else {
15+
fmt.printf("Unfortunately, it is %s. Go to work!", $day)
16+
}
17+
}
18+
19+
print "msg" {
20+
msg => $s1 + $s2,
21+
}
22+
23+
file "/tmp/files/" {
24+
state => "exists",
25+
mode => if $is_friday { # this updates the mode, the instant it changes!
26+
"0550"
27+
} else {
28+
"0770"
29+
},
30+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// Mgmt
2+
// Copyright (C) 2013-2019+ James Shubin and the project contributors
3+
// Written by James Shubin <[email protected]> and the project contributors
4+
//
5+
// This program is free software: you can redistribute it and/or modify
6+
// it under the terms of the GNU 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 General Public License for more details.
14+
//
15+
// You should have received a copy of the GNU General Public License
16+
// along with this program. If not, see <http://www.gnu.org/licenses/>.
17+
18+
package coredatetime
19+
20+
import (
21+
"fmt"
22+
"strings"
23+
"time"
24+
25+
"github.com/purpleidea/mgmt/lang/funcs/simple"
26+
"github.com/purpleidea/mgmt/lang/types"
27+
)
28+
29+
func init() {
30+
simple.ModuleRegister(moduleName, "weekday", &types.FuncValue{
31+
T: types.NewType("func(a int) str"),
32+
V: Weekday,
33+
})
34+
}
35+
36+
// Weekday returns the lowercased day of the week corresponding to the input
37+
// time. The time is the number of seconds since the epoch, and matches what
38+
// comes from our Now function.
39+
func Weekday(input []types.Value) (types.Value, error) {
40+
epochDelta := input[0].Int()
41+
if epochDelta < 0 {
42+
return nil, fmt.Errorf("epoch delta must be positive")
43+
}
44+
45+
weekday := time.Unix(epochDelta, 0).Weekday()
46+
return &types.StrValue{
47+
V: strings.ToLower(weekday.String()),
48+
}, nil
49+
}

0 commit comments

Comments
 (0)