1use crate::compiler::prelude::*;
2use chrono::Utc;
3
4#[derive(Clone, Copy, Debug)]
5pub struct Now;
6
7impl Function for Now {
8 fn identifier(&self) -> &'static str {
9 "now"
10 }
11
12 fn usage(&self) -> &'static str {
13 "Returns the current timestamp in the UTC timezone with nanosecond precision."
14 }
15
16 fn category(&self) -> &'static str {
17 Category::Timestamp.as_ref()
18 }
19
20 fn return_kind(&self) -> u16 {
21 kind::TIMESTAMP
22 }
23
24 fn examples(&self) -> &'static [Example] {
25 &[example! {
26 title: "Generate a current timestamp",
27 source: "now()",
28 result: Ok("2012-03-04T12:34:56.789012345Z"),
29 deterministic: false,
30 }]
31 }
32
33 fn compile(
34 &self,
35 _state: &state::TypeState,
36 _ctx: &mut FunctionCompileContext,
37 _: ArgumentList,
38 ) -> Compiled {
39 Ok(NowFn.as_expr())
40 }
41}
42
43#[derive(Debug, Clone)]
44struct NowFn;
45
46impl FunctionExpression for NowFn {
47 fn resolve(&self, _: &mut Context) -> Resolved {
48 Ok(Utc::now().into())
49 }
50
51 fn type_def(&self, _: &state::TypeState) -> TypeDef {
52 TypeDef::timestamp()
53 }
54}