Skip to content

Commit 016aed2

Browse files
committed
Allow now to be used as a Nullable<Timestamptz>
Fixes diesel-rs#1031
1 parent 2c1d146 commit 016aed2

2 files changed

Lines changed: 29 additions & 0 deletions

File tree

diesel/src/expression/functions/date_and_time.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,12 @@ impl AsExpression<Timestamptz> for now {
6969
Coerce::new(self)
7070
}
7171
}
72+
73+
#[cfg(feature="postgres")]
74+
impl AsExpression<Nullable<Timestamptz>> for now {
75+
type Expression = Coerce<now, Nullable<Timestamptz>>;
76+
77+
fn as_expression(self) -> Self::Expression {
78+
Coerce::new(self)
79+
}
80+
}

diesel_tests/tests/expressions/date_and_time.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use schema::{connection, TestConnection};
22
use diesel::*;
33
use diesel::data_types::*;
44
use diesel::expression::dsl::*;
5+
use diesel::types::Nullable;
56

67
table! {
78
has_timestamps {
@@ -59,6 +60,25 @@ fn now_can_be_used_as_timestamptz() {
5960
assert_eq!(Ok(vec![1]), before_now);
6061
}
6162

63+
#[test]
64+
#[cfg(feature = "postgres")]
65+
// FIXME: Replace this with an actual timestamptz expression
66+
fn now_can_be_used_as_nullable_timestamptz() {
67+
use self::has_timestamps::dsl::*;
68+
use diesel::types::Timestamptz;
69+
70+
let connection = connection();
71+
setup_test_table(&connection);
72+
connection.execute("INSERT INTO has_timestamps (created_at) VALUES \
73+
(NOW() - '1 day'::interval)").unwrap();
74+
75+
let created_at_tz = sql::<Nullable<Timestamptz>>("created_at");
76+
let before_now = has_timestamps.select(id)
77+
.filter(created_at_tz.lt(now))
78+
.load::<i32>(&connection);
79+
assert_eq!(Ok(vec![1]), before_now);
80+
}
81+
6282
#[test]
6383
#[cfg(feature = "sqlite")]
6484
fn now_executes_sql_function_now() {

0 commit comments

Comments
 (0)