Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added function now and format_timestamp #205

Merged
merged 2 commits into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 13 additions & 11 deletions pkg/functions/functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,17 +165,19 @@ var fmap = map[string]interface{}{
"wkn": Wkn,

// time and dates
"birthdate": BirthDate,
"date_between": DateBetween,
"dates_between": DatesBetween,
"future": Future,
"past": Past,
"recent": Recent,
"just_passed": Justpassed,
"now_sub": Nowsub,
"now_add": Nowadd,
"soon": Soon,
"unix_time_stamp": UnixTimeStamp,
"birthdate": BirthDate,
"date_between": DateBetween,
"dates_between": DatesBetween,
"future": Future,
"past": Past,
"recent": Recent,
"just_passed": Justpassed,
"format_timestamp": FormatTimestamp,
"now": Now,
"now_sub": Nowsub,
"now_add": Nowadd,
"soon": Soon,
"unix_time_stamp": UnixTimeStamp,

// phone
"country_code": CountryCode,
Expand Down
20 changes: 20 additions & 0 deletions pkg/functions/functionsDescription.go
Original file line number Diff line number Diff line change
Expand Up @@ -664,6 +664,26 @@ var funcDesc = map[string]FunctionDescription{
Example: "jr template run --embedded '{{just_passed 60000}}'",
Output: "2024-11-10 22:59:5",
},
"now": {
Name: "now",
Category: "time",
Description: "returns the current time as a Unix timestamp",
Parameters: "",
Localizable: false,
Return: "int",
Example: "jr template run --embedded '{{now}}'",
Output: "2024-11-10 22:59:5",
},
"format_timestamp": {
Name: "format_timestamp",
Category: "time",
Description: "formats the given timestamp with given pattern according to https://go.dev/src/time/format.go",
Parameters: "timestamp int64, format string",
Localizable: false,
Return: "string",
Example: "jr template run --embedded '{{format_timestamp 1729857168 \"2006-01-02 15:04:05.000000000\" }}'",
Output: "2024-11-10 22:59:5",
},
"key": {
Name: "key",
Category: "utilities",
Expand Down
12 changes: 12 additions & 0 deletions pkg/functions/timeAndDates.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,18 @@ func Justpassed(milliseconds int64) string {
return pastTime.Format(time.DateTime)
}

// Now returns the current time as a Unix millisecond timestamp
func Now() int64 {
return time.Now().UnixMilli()
}

// FormatTimestamp formats a unix millisecond timestamp with the given pattern
func FormatTimestamp(timestamp int64, format string) string {
t := time.Unix(0, timestamp*int64(time.Millisecond))
formattedDate := t.Format(format)
return formattedDate
}

// Nowsub returns a date in the past of given milliseconds
func Nowsub(milliseconds int64) string {
now := time.Now()
Expand Down
Loading