Skip to content

Commit b9cc577

Browse files
committed
feat: add a nanos_at function
1 parent 7fa88b3 commit b9cc577

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ SELECT when_is('5 days ago at this hour in Asia/Tokyo');
1515
SELECT seconds_at('5 days ago at this hour in Asia/Tokyo');
1616
SELECT millis_at('5 days ago at this hour in Asia/Tokyo');
1717
SELECT micros_at('5 days ago at this hour in Asia/Tokyo');
18+
SELECT nanos_at('5 days ago at this hour in Asia/Tokyo');
1819
```
1920

2021
</details>
@@ -30,6 +31,7 @@ SELECT when_is('next friday at 8:00 pm in America/New_York');
3031
SELECT seconds_at('next friday at 8:00 pm in America/New_York');
3132
SELECT millis_at('next friday at 8:00 pm in America/New_York');
3233
SELECT micros_at('next friday at 8:00 pm in America/New_York');
34+
SELECT nanos_at('next friday at 8:00 pm in America/New_York');
3335
```
3436

3537
</details>
@@ -45,6 +47,7 @@ SELECT when_is('in 2 months at midnight in UTC-8');
4547
SELECT seconds_at('in 2 months at midnight in UTC-8');
4648
SELECT millis_at('in 2 months at midnight in UTC-8');
4749
SELECT micros_at('in 2 months at midnight in UTC-8');
50+
SELECT nanos_at('in 2 months at midnight in UTC-8');
4851
```
4952

5053
</details>
@@ -60,6 +63,7 @@ SELECT when_is('last monday at 22:30');
6063
SELECT seconds_at('last monday at 22:30');
6164
SELECT millis_at('last monday at 22:30');
6265
SELECT micros_at('last monday at 22:30');
66+
SELECT nanos_at('last monday at 22:30');
6367
```
6468

6569
</details>
@@ -75,6 +79,7 @@ SELECT when_is('December 31, 2026 at evening');
7579
SELECT seconds_at('December 31, 2026 at evening');
7680
SELECT millis_at('December 31, 2026 at evening');
7781
SELECT micros_at('December 31, 2026 at evening');
82+
SELECT nanos_at('December 31, 2026 at evening');
7883
```
7984

8085
</details>
@@ -169,6 +174,7 @@ The following functions are provided by the Postgres extension.
169174
| `seconds_at` | `BIGINT` | Returns the total seconds since the UNIX epoch. |
170175
| `millis_at` | `BIGINT` | Returns the total milliseconds since the UNIX epoch. |
171176
| `micros_at` | `BIGINT` | Returns the total microseconds since the UNIX epoch. |
177+
| `nanos_at` | `BIGINT` | Returns the total microseconds since the UNIX epoch. |
172178

173179
## Usage
174180

src/when_is.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,13 @@ fn micros_at(input: &str) -> i64 {
5555
let zoned = input.to_timestamp().unwrap_or_report();
5656
zoned.timestamp().as_microsecond()
5757
}
58+
59+
#[pg_extern(strict, immutable, parallel_safe)]
60+
fn nanos_at(input: &str) -> i64 {
61+
let input = parse_input(input);
62+
let zoned = input.to_timestamp().unwrap_or_report();
63+
match zoned.timestamp().as_nanosecond().try_into() {
64+
Ok(nanos) => nanos,
65+
Err(_) => error!("nanosecond can not be represented as a bigint"),
66+
}
67+
}

0 commit comments

Comments
 (0)