Skip to content

Commit af2bd2b

Browse files
authored
Merge pull request #205 from eljeko/main
Added functions now and format_timestamp
2 parents 659dfd6 + 4afc724 commit af2bd2b

File tree

3 files changed

+45
-11
lines changed

3 files changed

+45
-11
lines changed

pkg/functions/functions.go

+13-11
Original file line numberDiff line numberDiff line change
@@ -165,17 +165,19 @@ var fmap = map[string]interface{}{
165165
"wkn": Wkn,
166166

167167
// time and dates
168-
"birthdate": BirthDate,
169-
"date_between": DateBetween,
170-
"dates_between": DatesBetween,
171-
"future": Future,
172-
"past": Past,
173-
"recent": Recent,
174-
"just_passed": Justpassed,
175-
"now_sub": Nowsub,
176-
"now_add": Nowadd,
177-
"soon": Soon,
178-
"unix_time_stamp": UnixTimeStamp,
168+
"birthdate": BirthDate,
169+
"date_between": DateBetween,
170+
"dates_between": DatesBetween,
171+
"future": Future,
172+
"past": Past,
173+
"recent": Recent,
174+
"just_passed": Justpassed,
175+
"format_timestamp": FormatTimestamp,
176+
"now": Now,
177+
"now_sub": Nowsub,
178+
"now_add": Nowadd,
179+
"soon": Soon,
180+
"unix_time_stamp": UnixTimeStamp,
179181

180182
// phone
181183
"country_code": CountryCode,

pkg/functions/functionsDescription.go

+20
Original file line numberDiff line numberDiff line change
@@ -664,6 +664,26 @@ var funcDesc = map[string]FunctionDescription{
664664
Example: "jr template run --embedded '{{just_passed 60000}}'",
665665
Output: "2024-11-10 22:59:5",
666666
},
667+
"now": {
668+
Name: "now",
669+
Category: "time",
670+
Description: "returns the current time as a Unix timestamp",
671+
Parameters: "",
672+
Localizable: false,
673+
Return: "int",
674+
Example: "jr template run --embedded '{{now}}'",
675+
Output: "2024-11-10 22:59:5",
676+
},
677+
"format_timestamp": {
678+
Name: "format_timestamp",
679+
Category: "time",
680+
Description: "formats the given timestamp with given pattern according to https://go.dev/src/time/format.go",
681+
Parameters: "timestamp int64, format string",
682+
Localizable: false,
683+
Return: "string",
684+
Example: "jr template run --embedded '{{format_timestamp 1729857168 \"2006-01-02 15:04:05.000000000\" }}'",
685+
Output: "2024-11-10 22:59:5",
686+
},
667687
"key": {
668688
Name: "key",
669689
Category: "utilities",

pkg/functions/timeAndDates.go

+12
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,18 @@ func Justpassed(milliseconds int64) string {
7474
return pastTime.Format(time.DateTime)
7575
}
7676

77+
// Now returns the current time as a Unix millisecond timestamp
78+
func Now() int64 {
79+
return time.Now().UnixMilli()
80+
}
81+
82+
// FormatTimestamp formats a unix millisecond timestamp with the given pattern
83+
func FormatTimestamp(timestamp int64, format string) string {
84+
t := time.Unix(0, timestamp*int64(time.Millisecond))
85+
formattedDate := t.Format(format)
86+
return formattedDate
87+
}
88+
7789
// Nowsub returns a date in the past of given milliseconds
7890
func Nowsub(milliseconds int64) string {
7991
now := time.Now()

0 commit comments

Comments
 (0)