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

Add durationformat(duration, string) function. #1992

Merged
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Unreleased

- DQL: Adds new `durationformat(duration, string)` function.
- DQL: New math rounding functions, `trunc(number)`, `floor(number)`, `ceil(number)`.

# 0.5.56
Expand Down
24 changes: 24 additions & 0 deletions docs/docs/reference/functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -710,6 +710,30 @@ dateformat(date(now),"x") = "1407287224054"
dateformat(file.mtime,"ffff") = "Wednesday, August 6, 2014, 1:07 PM Eastern Daylight Time"
```

### `durationformat(duration, string)`

Format a Dataview duration using a formatting string.
Anything inside single quotes will not be treated as a token and
instead will be shown in the output as written. See examples.

You may use these tokens:

- `S` for milliseconds
- `s` for seconds
- `m` for minutes
- `h` for hours
- `d` for days
- `w` for weeks
- `M` for months
- `y` for years

```js
durationformat(dur("3 days 7 hours 43 seconds"), "ddd'd' hh'h' ss's'") = "003d 07h 43s"
durationformat(dur("365 days 5 hours 49 minutes"), "yyyy ddd hh mm ss") = "0001 000 05 49 00"
durationformat(dur("2000 years"), "M months") = "24000 months"
durationformat(dur("14d"), "s 'seconds'") = "1209600 seconds"
```

### `localtime(date)`

Converts a date in a fixed timezone to a date in the current timezone.
Expand Down
6 changes: 6 additions & 0 deletions src/expression/functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,12 @@ export namespace DefaultFunctions {
.vectorize(2, [0])
.build();

export const durationformat = new FunctionBuilder("durationformat")
.add2("duration", "string", (dur, format) => dur.toFormat(format))
.add2("null", "string", (_nul, _format) => null)
.vectorize(2, [0])
.build();

export const localtime = new FunctionBuilder("localtime")
.add1("date", d => d.toLocal())
.add1("null", () => null)
Expand Down
Loading