MCPcopy Create free account
hub / github.com/erans/pgsqlite / register_datetime_functions

Function register_datetime_functions

src/functions/datetime_functions.rs:6–398  ·  view source on GitHub ↗

Register datetime-related functions in SQLite

(conn: &Connection)

Source from the content-addressed store, hash-verified

4
5/// Register datetime-related functions in SQLite
6pub fn register_datetime_functions(conn: &Connection) -> Result<()> {
7 // now() / current_timestamp - Return current timestamp as formatted string
8 // PostgreSQL clients expect NOW() to return formatted timestamp strings
9 conn.create_scalar_function(
10 "now",
11 0,
12 FunctionFlags::SQLITE_UTF8,
13 |_ctx| {
14 let now = Utc::now();
15 Ok(now.format("%Y-%m-%d %H:%M:%S%.6f").to_string())
16 },
17 )?;
18
19 conn.create_scalar_function(
20 "current_timestamp",
21 0,
22 FunctionFlags::SQLITE_UTF8,
23 |_ctx| {
24 let now = Utc::now();
25 Ok(now.format("%Y-%m-%d %H:%M:%S%.6f").to_string())
26 },
27 )?;
28
29 // Don't override SQLite's built-in CURRENT_DATE function
30 // SQLite's CURRENT_DATE returns text in YYYY-MM-DD format
31
32 // current_time - Return microseconds since midnight
33 conn.create_scalar_function(
34 "current_time",
35 0,
36 FunctionFlags::SQLITE_UTF8,
37 |_ctx| {
38 let now = Utc::now();
39 let time = now.time();
40 let micros = time.num_seconds_from_midnight() as i64 * 1_000_000
41 + (time.nanosecond() / 1000) as i64;
42 Ok(micros)
43 },
44 )?;
45
46 // date_part(field, timestamp) / extract(field FROM timestamp)
47 // Extract a specific part from a timestamp
48 conn.create_scalar_function(
49 "date_part",
50 2,
51 FunctionFlags::SQLITE_UTF8,
52 |ctx| {
53 let field: String = ctx.get(0)?;
54 let timestamp: i64 = ctx.get(1)?;
55 extract_date_part(&field, timestamp)
56 },
57 )?;
58
59 conn.create_scalar_function(
60 "extract",
61 2,
62 FunctionFlags::SQLITE_UTF8 | FunctionFlags::SQLITE_DETERMINISTIC,
63 |ctx| {

Callers 2

register_all_functionsFunction · 0.85

Calls 6

extract_date_partFunction · 0.85
truncate_dateFunction · 0.85
is_numericMethod · 0.80
getMethod · 0.45
lenMethod · 0.45
cloneMethod · 0.45

Tested by 1