Skip to content

Commit 0217923

Browse files
authored
fix(query): today, yesterday, tomorrow need consider tz (#14841)
today, yesterday, tomorrow need consider tz
1 parent f04c367 commit 0217923

File tree

3 files changed

+29
-7
lines changed

3 files changed

+29
-7
lines changed

src/query/expression/src/utils/date_helper.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -382,8 +382,8 @@ impl AddTimesImpl {
382382
}
383383

384384
#[inline]
385-
pub fn today_date() -> i32 {
386-
let now = Utc::now();
385+
pub fn today_date(tz: TzLUT) -> i32 {
386+
let now = Utc::now().with_timezone(&tz.tz);
387387
NaiveDate::from_ymd_opt(now.year(), now.month(), now.day())
388388
.unwrap()
389389
.signed_duration_since(NaiveDate::from_ymd_opt(1970, 1, 1).unwrap())

src/query/functions/src/scalars/datetime.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -837,19 +837,19 @@ fn register_real_time_functions(registry: &mut FunctionRegistry) {
837837
registry.register_0_arg_core::<DateType, _, _>(
838838
"today",
839839
|_| FunctionDomain::Full,
840-
|_| Value::Scalar(today_date()),
840+
|ctx| Value::Scalar(today_date(ctx.func_ctx.tz)),
841841
);
842842

843843
registry.register_0_arg_core::<DateType, _, _>(
844844
"yesterday",
845845
|_| FunctionDomain::Full,
846-
|_| Value::Scalar(today_date() - 1),
846+
|ctx| Value::Scalar(today_date(ctx.func_ctx.tz) - 1),
847847
);
848848

849849
registry.register_0_arg_core::<DateType, _, _>(
850850
"tomorrow",
851851
|_| FunctionDomain::Full,
852-
|_| Value::Scalar(today_date() + 1),
852+
|ctx| Value::Scalar(today_date(ctx.func_ctx.tz) + 1),
853853
);
854854
}
855855

tests/sqllogictests/suites/query/02_function/02_0012_function_datetimes.test

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,34 @@ drop table if exists t all
44
statement ok
55
set timezone = 'UTC'
66

7-
query B
8-
SELECT today() >= 18869::DATE
7+
query I
8+
select to_string(today())=substr(to_string(now()),1,10);
99
----
1010
1
1111

12+
statement ok
13+
set timezone = 'Asia/Shanghai'
1214

15+
query I
16+
select to_string(today())=substr(to_string(now()),1,10);
17+
----
18+
1
19+
20+
statement ok
21+
set timezone = 'America/Los_Angeles'
22+
23+
query I
24+
select to_string(today())=substr(to_string(now()),1,10);
25+
----
26+
1
27+
28+
statement ok
29+
set timezone = 'UTC'
30+
31+
query B
32+
SELECT today() >= 18869::DATE
33+
----
34+
1
1335

1436
query B
1537
SELECT now() >= 1630295616::TIMESTAMP

0 commit comments

Comments
 (0)