Skip to content

Commit

Permalink
Merge pull request #205 from eljeko/main
Browse files Browse the repository at this point in the history
Added functions now and format_timestamp
  • Loading branch information
eljeko authored Nov 21, 2024
2 parents 659dfd6 + 4afc724 commit af2bd2b
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 11 deletions.
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

0 comments on commit af2bd2b

Please sign in to comment.