|
| 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