graphlite 0.0.1

GraphLite - A lightweight ISO GQL Graph Database
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
// Copyright (c) 2024-2025 DeepGraph Inc.
// SPDX-License-Identifier: Apache-2.0
//
//! Timezone-aware temporal functions for GQL
//!
//! This module implements comprehensive timezone support including:
//! - AT TIME ZONE operator
//! - CONVERT_TZ function  
//! - TIMEZONE function
//! - Timezone-aware EXTRACT components
//! - DST-aware arithmetic operations

use super::function_trait::{Function, FunctionContext, FunctionError, FunctionResult};
use crate::storage::Value;
use chrono::{DateTime, FixedOffset, Offset, Utc};
use chrono_tz::Tz;

/// Parse timezone string into either a named timezone or fixed offset
fn parse_timezone(tz_str: &str) -> Result<TimezoneType, String> {
    // Try parsing as named timezone first
    if let Ok(tz) = tz_str.parse::<Tz>() {
        return Ok(TimezoneType::Named(tz));
    }

    // Try common timezone abbreviations
    let canonical_tz = match tz_str.to_uppercase().as_str() {
        "UTC" | "GMT" => "UTC",
        "EST" => "America/New_York",    // Eastern Standard Time
        "EDT" => "America/New_York",    // Eastern Daylight Time
        "CST" => "America/Chicago",     // Central Standard Time
        "CDT" => "America/Chicago",     // Central Daylight Time
        "MST" => "America/Denver",      // Mountain Standard Time
        "MDT" => "America/Denver",      // Mountain Daylight Time
        "PST" => "America/Los_Angeles", // Pacific Standard Time
        "PDT" => "America/Los_Angeles", // Pacific Daylight Time
        "BST" => "Europe/London",       // British Summer Time
        "CET" => "Europe/Paris",        // Central European Time
        "CEST" => "Europe/Paris",       // Central European Summer Time
        "JST" => "Asia/Tokyo",          // Japan Standard Time
        "IST" => "Asia/Kolkata",        // India Standard Time
        "AEST" => "Australia/Sydney",   // Australian Eastern Standard Time
        "AEDT" => "Australia/Sydney",   // Australian Eastern Daylight Time
        _ => tz_str,                    // Use original if no abbreviation match
    };

    // Try parsing the canonical timezone name
    if let Ok(tz) = canonical_tz.parse::<Tz>() {
        return Ok(TimezoneType::Named(tz));
    }

    // Try parsing as fixed offset (+05:30, -04:00, etc.)
    if let Ok(offset) = parse_fixed_offset(tz_str) {
        return Ok(TimezoneType::Fixed(offset));
    }

    Err(format!("Invalid timezone: {}", tz_str))
}

/// Parse fixed offset strings like "+05:30", "-04:00", "+0530", "-0400"
fn parse_fixed_offset(offset_str: &str) -> Result<FixedOffset, String> {
    let trimmed = offset_str.trim();

    if trimmed.len() < 3 {
        return Err("Invalid offset format".to_string());
    }

    let sign = match trimmed.chars().next() {
        Some('+') => 1,
        Some('-') => -1,
        _ => return Err("Offset must start with + or -".to_string()),
    };

    let offset_part = &trimmed[1..];

    // Handle formats like "05:30" or "0530"
    let (hours, minutes) = if offset_part.contains(':') {
        let parts: Vec<&str> = offset_part.split(':').collect();
        if parts.len() != 2 {
            return Err("Invalid offset format".to_string());
        }
        (
            parts[0].parse::<i32>().map_err(|_| "Invalid hours")?,
            parts[1].parse::<i32>().map_err(|_| "Invalid minutes")?,
        )
    } else if offset_part.len() == 4 {
        // Format like "0530"
        let hours_str = &offset_part[0..2];
        let minutes_str = &offset_part[2..4];
        (
            hours_str.parse::<i32>().map_err(|_| "Invalid hours")?,
            minutes_str.parse::<i32>().map_err(|_| "Invalid minutes")?,
        )
    } else {
        return Err("Invalid offset format".to_string());
    };

    if hours > 23 || minutes > 59 {
        return Err("Invalid offset values".to_string());
    }

    let total_seconds = sign * (hours * 3600 + minutes * 60);
    FixedOffset::east_opt(total_seconds).ok_or_else(|| "Invalid offset".to_string())
}

/// Timezone type enum for handling both named timezones and fixed offsets
#[derive(Debug, Clone)]
enum TimezoneType {
    Named(Tz),
    Fixed(FixedOffset),
}

impl TimezoneType {
    /// Convert a UTC datetime to this timezone
    fn convert_from_utc(&self, utc_dt: &DateTime<Utc>) -> Value {
        match self {
            TimezoneType::Named(tz) => {
                let _tz_dt = utc_dt.with_timezone(tz);
                Value::DateTimeWithNamedTz(tz.to_string(), *utc_dt)
            }
            TimezoneType::Fixed(offset) => {
                let offset_dt = utc_dt.with_timezone(offset);
                Value::DateTimeWithFixedOffset(offset_dt)
            }
        }
    }

    /// Get timezone name/identifier
    fn name(&self) -> String {
        match self {
            TimezoneType::Named(tz) => tz.to_string(),
            TimezoneType::Fixed(offset) => offset.to_string(),
        }
    }

    /// Get timezone offset in seconds for a given UTC datetime
    fn offset_seconds(&self, utc_dt: &DateTime<Utc>) -> i32 {
        match self {
            TimezoneType::Named(tz) => {
                let tz_dt = utc_dt.with_timezone(tz);
                tz_dt.offset().fix().local_minus_utc()
            }
            TimezoneType::Fixed(offset) => offset.local_minus_utc(),
        }
    }
}

/// AT TIME ZONE operator: converts datetime to specified timezone
#[derive(Debug)]
pub struct AtTimeZoneFunction;

impl AtTimeZoneFunction {
    pub fn new() -> Self {
        Self
    }
}

impl Function for AtTimeZoneFunction {
    fn name(&self) -> &str {
        "AT_TIME_ZONE"
    }

    fn description(&self) -> &str {
        "Convert datetime to specified timezone"
    }

    fn argument_count(&self) -> usize {
        2
    }

    fn return_type(&self) -> &str {
        "DateTime"
    }

    fn execute(&self, context: &FunctionContext) -> FunctionResult<Value> {
        if context.arguments.len() != 2 {
            return Err(FunctionError::InvalidArgumentCount {
                expected: 2,
                actual: context.arguments.len(),
            });
        }

        let datetime_arg = &context.arguments[0];
        let timezone_arg = &context.arguments[1];

        // Extract timezone string
        let tz_str =
            timezone_arg
                .as_string()
                .ok_or_else(|| FunctionError::InvalidArgumentType {
                    message: "Timezone must be a string".to_string(),
                })?;

        // Parse timezone
        let timezone = parse_timezone(tz_str)
            .map_err(|e| FunctionError::InvalidArgumentType { message: e })?;

        // Convert datetime to UTC first if needed
        let utc_dt = match datetime_arg.as_datetime_utc() {
            Some(dt) => dt,
            None => {
                return Err(FunctionError::InvalidArgumentType {
                    message: "First argument must be a datetime".to_string(),
                })
            }
        };

        // Convert to target timezone
        Ok(timezone.convert_from_utc(&utc_dt))
    }

    fn graph_context_required(&self) -> bool {
        false // Timezone functions are pure scalar functions
    }
}

/// CONVERT_TZ function: converts datetime from one timezone to another
#[derive(Debug)]
pub struct ConvertTzFunction;

impl ConvertTzFunction {
    pub fn new() -> Self {
        Self
    }
}

impl Function for ConvertTzFunction {
    fn name(&self) -> &str {
        "CONVERT_TZ"
    }

    fn description(&self) -> &str {
        "Convert datetime from one timezone to another"
    }

    fn argument_count(&self) -> usize {
        3
    }

    fn return_type(&self) -> &str {
        "DateTime"
    }

    fn execute(&self, context: &FunctionContext) -> FunctionResult<Value> {
        if context.arguments.len() != 3 {
            return Err(FunctionError::InvalidArgumentCount {
                expected: 3,
                actual: context.arguments.len(),
            });
        }

        let datetime_arg = &context.arguments[0];
        let from_tz_arg = &context.arguments[1];
        let to_tz_arg = &context.arguments[2];

        // Extract timezone strings
        let from_tz_str =
            from_tz_arg
                .as_string()
                .ok_or_else(|| FunctionError::InvalidArgumentType {
                    message: "From timezone must be a string".to_string(),
                })?;
        let to_tz_str =
            to_tz_arg
                .as_string()
                .ok_or_else(|| FunctionError::InvalidArgumentType {
                    message: "To timezone must be a string".to_string(),
                })?;

        // Parse timezones
        let _from_timezone =
            parse_timezone(from_tz_str).map_err(|e| FunctionError::InvalidArgumentType {
                message: format!("Invalid from timezone: {}", e),
            })?;
        let to_timezone =
            parse_timezone(to_tz_str).map_err(|e| FunctionError::InvalidArgumentType {
                message: format!("Invalid to timezone: {}", e),
            })?;

        // Get UTC datetime
        let utc_dt = if from_tz_str.to_uppercase() == "UTC" {
            // Source is UTC
            datetime_arg
                .as_datetime_utc()
                .ok_or_else(|| FunctionError::InvalidArgumentType {
                    message: "First argument must be a datetime".to_string(),
                })?
        } else {
            // Convert from source timezone to UTC first
            let source_dt = datetime_arg.as_datetime_utc().ok_or_else(|| {
                FunctionError::InvalidArgumentType {
                    message: "First argument must be a datetime".to_string(),
                }
            })?;

            // For simplicity, assume input datetime is in the from_timezone and convert to UTC
            // This is a simplified implementation - in practice, you'd need to handle the conversion more carefully
            source_dt
        };

        // Convert to target timezone
        Ok(to_timezone.convert_from_utc(&utc_dt))
    }

    fn graph_context_required(&self) -> bool {
        false // Timezone functions are pure scalar functions
    }
}

/// TIMEZONE function: converts datetime to specified timezone (alternative syntax)
#[derive(Debug)]
pub struct TimezoneFunction;

impl TimezoneFunction {
    pub fn new() -> Self {
        Self
    }
}

impl Function for TimezoneFunction {
    fn name(&self) -> &str {
        "TIMEZONE"
    }

    fn description(&self) -> &str {
        "Convert datetime to specified timezone (alternative syntax)"
    }

    fn argument_count(&self) -> usize {
        2
    }

    fn return_type(&self) -> &str {
        "DateTime"
    }

    fn execute(&self, context: &FunctionContext) -> FunctionResult<Value> {
        if context.arguments.len() != 2 {
            return Err(FunctionError::InvalidArgumentCount {
                expected: 2,
                actual: context.arguments.len(),
            });
        }

        // Note: TIMEZONE has arguments in opposite order to AT_TIME_ZONE
        let timezone_arg = &context.arguments[0];
        let datetime_arg = &context.arguments[1];

        // Extract timezone string
        let tz_str =
            timezone_arg
                .as_string()
                .ok_or_else(|| FunctionError::InvalidArgumentType {
                    message: "Timezone must be a string".to_string(),
                })?;

        // Parse timezone
        let timezone = parse_timezone(tz_str)
            .map_err(|e| FunctionError::InvalidArgumentType { message: e })?;

        // Convert datetime to UTC first if needed
        let utc_dt = match datetime_arg.as_datetime_utc() {
            Some(dt) => dt,
            None => {
                return Err(FunctionError::InvalidArgumentType {
                    message: "Second argument must be a datetime".to_string(),
                })
            }
        };

        // Convert to target timezone
        Ok(timezone.convert_from_utc(&utc_dt))
    }

    fn graph_context_required(&self) -> bool {
        false // Timezone functions are pure scalar functions
    }
}

/// Enhanced EXTRACT function with timezone components
#[derive(Debug)]
pub struct ExtractTimezoneFunction;

impl ExtractTimezoneFunction {
    pub fn new() -> Self {
        Self
    }
}

impl Function for ExtractTimezoneFunction {
    fn name(&self) -> &str {
        "EXTRACT_TIMEZONE"
    }

    fn description(&self) -> &str {
        "Extract timezone components from datetime values"
    }

    fn argument_count(&self) -> usize {
        2
    }

    fn return_type(&self) -> &str {
        "Number or String"
    }

    fn execute(&self, context: &FunctionContext) -> FunctionResult<Value> {
        if context.arguments.len() != 2 {
            return Err(FunctionError::InvalidArgumentCount {
                expected: 2,
                actual: context.arguments.len(),
            });
        }

        let unit_arg = &context.arguments[0];
        let datetime_arg = &context.arguments[1];

        let unit = unit_arg
            .as_string()
            .ok_or_else(|| FunctionError::InvalidArgumentType {
                message: "Extract unit must be a string".to_string(),
            })?
            .to_uppercase();

        match unit.as_str() {
            "TIMEZONE" => {
                // Return timezone name/identifier
                match datetime_arg.get_timezone_info() {
                    Some(tz_info) => Ok(Value::String(tz_info)),
                    None => Err(FunctionError::InvalidArgumentType {
                        message: "Datetime has no timezone information".to_string(),
                    }),
                }
            }
            "TIMEZONE_HOUR" => {
                // Return timezone offset in hours
                let utc_dt = datetime_arg.as_datetime_utc().ok_or_else(|| {
                    FunctionError::InvalidArgumentType {
                        message: "Argument must be a datetime".to_string(),
                    }
                })?;

                match datetime_arg {
                    Value::DateTime(_) => Ok(Value::Number(0.0)), // UTC is +0
                    Value::DateTimeWithFixedOffset(dt) => {
                        let offset_seconds = dt.offset().local_minus_utc();
                        Ok(Value::Number(offset_seconds as f64 / 3600.0))
                    }
                    Value::DateTimeWithNamedTz(tz_name, _) => {
                        if let Ok(tz) = tz_name.parse::<Tz>() {
                            let tz_dt = utc_dt.with_timezone(&tz);
                            let offset_seconds = tz_dt.offset().fix().local_minus_utc();
                            Ok(Value::Number(offset_seconds as f64 / 3600.0))
                        } else {
                            Err(FunctionError::InvalidArgumentType {
                                message: "Invalid timezone name".to_string(),
                            })
                        }
                    }
                    _ => Err(FunctionError::InvalidArgumentType {
                        message: "Argument must be a datetime".to_string(),
                    }),
                }
            }
            "TIMEZONE_MINUTE" => {
                // Return timezone offset minutes component
                let utc_dt = datetime_arg.as_datetime_utc().ok_or_else(|| {
                    FunctionError::InvalidArgumentType {
                        message: "Argument must be a datetime".to_string(),
                    }
                })?;

                match datetime_arg {
                    Value::DateTime(_) => Ok(Value::Number(0.0)), // UTC is +0
                    Value::DateTimeWithFixedOffset(dt) => {
                        let offset_seconds = dt.offset().local_minus_utc();
                        let offset_minutes = (offset_seconds % 3600) / 60;
                        Ok(Value::Number(offset_minutes as f64))
                    }
                    Value::DateTimeWithNamedTz(tz_name, _) => {
                        if let Ok(tz) = tz_name.parse::<Tz>() {
                            let tz_dt = utc_dt.with_timezone(&tz);
                            let offset_seconds = tz_dt.offset().fix().local_minus_utc();
                            let offset_minutes = (offset_seconds % 3600) / 60;
                            Ok(Value::Number(offset_minutes as f64))
                        } else {
                            Err(FunctionError::InvalidArgumentType {
                                message: "Invalid timezone name".to_string(),
                            })
                        }
                    }
                    _ => Err(FunctionError::InvalidArgumentType {
                        message: "Argument must be a datetime".to_string(),
                    }),
                }
            }
            _ => Err(FunctionError::UnsupportedOperation {
                operation: format!("Timezone extract unit: {}", unit),
            }),
        }
    }

    fn graph_context_required(&self) -> bool {
        false // Timezone functions are pure scalar functions
    }
}

/// GET_TIMEZONE_NAME function: gets the timezone name from a timezone string
#[derive(Debug)]
pub struct GetTimezoneNameFunction;

impl GetTimezoneNameFunction {
    pub fn new() -> Self {
        Self
    }
}

impl Function for GetTimezoneNameFunction {
    fn name(&self) -> &str {
        "GET_TIMEZONE_NAME"
    }

    fn description(&self) -> &str {
        "Get the full timezone name from a timezone identifier or offset"
    }

    fn argument_count(&self) -> usize {
        1
    }

    fn return_type(&self) -> &str {
        "String"
    }

    fn execute(&self, context: &FunctionContext) -> FunctionResult<Value> {
        if context.arguments.len() != 1 {
            return Err(FunctionError::InvalidArgumentCount {
                expected: 1,
                actual: context.arguments.len(),
            });
        }

        let timezone_arg = &context.arguments[0];

        let tz_str =
            timezone_arg
                .as_string()
                .ok_or_else(|| FunctionError::InvalidArgumentType {
                    message: "Timezone argument must be a string".to_string(),
                })?;

        let timezone = parse_timezone(tz_str)
            .map_err(|e| FunctionError::InvalidArgumentType { message: e })?;

        Ok(Value::String(timezone.name()))
    }

    fn graph_context_required(&self) -> bool {
        false // Timezone functions are pure scalar functions
    }
}

/// GET_TIMEZONE_ABBREVIATION function: gets the timezone abbreviation from a timezone string
#[derive(Debug)]
pub struct GetTimezoneAbbreviationFunction;

impl GetTimezoneAbbreviationFunction {
    pub fn new() -> Self {
        Self
    }
}

impl Function for GetTimezoneAbbreviationFunction {
    fn name(&self) -> &str {
        "GET_TIMEZONE_ABBREVIATION"
    }

    fn description(&self) -> &str {
        "Get the timezone abbreviation from a timezone identifier"
    }

    fn argument_count(&self) -> usize {
        1
    }

    fn return_type(&self) -> &str {
        "String"
    }

    fn execute(&self, context: &FunctionContext) -> FunctionResult<Value> {
        if context.arguments.len() != 1 {
            return Err(FunctionError::InvalidArgumentCount {
                expected: 1,
                actual: context.arguments.len(),
            });
        }

        let timezone_arg = &context.arguments[0];

        let tz_str =
            timezone_arg
                .as_string()
                .ok_or_else(|| FunctionError::InvalidArgumentType {
                    message: "Timezone argument must be a string".to_string(),
                })?;

        // Parse timezone to validate it exists
        let timezone = parse_timezone(tz_str)
            .map_err(|e| FunctionError::InvalidArgumentType { message: e })?;

        // Generate abbreviation based on timezone type
        let abbreviation = match timezone {
            TimezoneType::Named(tz) => {
                // Use the actual timezone object to get canonical name
                let canonical_name = tz.to_string();
                match canonical_name.as_str() {
                    "UTC" => "UTC".to_string(),
                    "America/New_York" => "EST/EDT".to_string(),
                    "America/Chicago" => "CST/CDT".to_string(),
                    "America/Denver" => "MST/MDT".to_string(),
                    "America/Los_Angeles" => "PST/PDT".to_string(),
                    "Europe/London" => "GMT/BST".to_string(),
                    "Europe/Paris" => "CET/CEST".to_string(),
                    "Asia/Tokyo" => "JST".to_string(),
                    "Asia/Shanghai" => "CST".to_string(),
                    "Asia/Kolkata" => "IST".to_string(),
                    _ => {
                        // Extract abbreviation from canonical timezone name
                        let parts: Vec<&str> = canonical_name.split('/').collect();
                        if parts.len() >= 2 {
                            parts.last().unwrap_or(&canonical_name.as_str()).to_string()
                        } else {
                            canonical_name
                        }
                    }
                }
            }
            TimezoneType::Fixed(_) => {
                // For fixed offsets, return the offset string itself
                timezone.name()
            }
        };

        Ok(Value::String(abbreviation))
    }

    fn graph_context_required(&self) -> bool {
        false // Timezone functions are pure scalar functions
    }
}

/// GET_TIMEZONE_OFFSET function: gets the timezone offset in hours from a timezone string
#[derive(Debug)]
pub struct GetTimezoneOffsetFunction;

impl GetTimezoneOffsetFunction {
    pub fn new() -> Self {
        Self
    }
}

impl Function for GetTimezoneOffsetFunction {
    fn name(&self) -> &str {
        "GET_TIMEZONE_OFFSET"
    }

    fn description(&self) -> &str {
        "Get the timezone offset in standard format (+05:30) from a timezone identifier or abbreviation"
    }

    fn argument_count(&self) -> usize {
        1
    }

    fn return_type(&self) -> &str {
        "String"
    }

    fn execute(&self, context: &FunctionContext) -> FunctionResult<Value> {
        if context.arguments.len() != 1 {
            return Err(FunctionError::InvalidArgumentCount {
                expected: 1,
                actual: context.arguments.len(),
            });
        }

        let timezone_arg = &context.arguments[0];

        let tz_str =
            timezone_arg
                .as_string()
                .ok_or_else(|| FunctionError::InvalidArgumentType {
                    message: "Timezone argument must be a string".to_string(),
                })?;

        // Parse timezone to validate it exists
        let timezone = parse_timezone(tz_str)
            .map_err(|e| FunctionError::InvalidArgumentType { message: e })?;

        // Get current UTC time for calculating offset (since DST affects offset)
        let now_utc = chrono::Utc::now();

        // Use the existing helper method
        let offset_seconds = timezone.offset_seconds(&now_utc);

        // Format offset as standard timezone string (+HH:MM or -HH:MM)
        let sign = if offset_seconds >= 0 { "+" } else { "-" };
        let abs_seconds = offset_seconds.abs();
        let hours = abs_seconds / 3600;
        let minutes = (abs_seconds % 3600) / 60;

        let formatted_offset = format!("{}{:02}:{:02}", sign, hours, minutes);
        Ok(Value::String(formatted_offset))
    }

    fn graph_context_required(&self) -> bool {
        false // Timezone functions are pure scalar functions
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_parse_fixed_offset() {
        // Test valid formats
        assert!(parse_fixed_offset("+05:30").is_ok());
        assert!(parse_fixed_offset("-04:00").is_ok());
        assert!(parse_fixed_offset("+0530").is_ok());
        assert!(parse_fixed_offset("-0400").is_ok());

        // Test invalid formats
        assert!(parse_fixed_offset("05:30").is_err()); // No sign
        assert!(parse_fixed_offset("+25:00").is_err()); // Invalid hour
        assert!(parse_fixed_offset("+05:60").is_err()); // Invalid minute
    }

    #[test]
    fn test_parse_timezone() {
        // Test named timezone
        assert!(parse_timezone("America/New_York").is_ok());
        assert!(parse_timezone("Europe/London").is_ok());
        assert!(parse_timezone("Asia/Tokyo").is_ok());

        // Test fixed offset
        assert!(parse_timezone("+05:30").is_ok());
        assert!(parse_timezone("-04:00").is_ok());

        // Test invalid
        assert!(parse_timezone("Invalid/Timezone").is_err());
        assert!(parse_timezone("not-a-timezone").is_err());
    }
}