ambers 0.4.0

Pure Rust reader for SPSS .sav and .zsav files
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
/// SPSS system-missing value (specific NaN bit pattern used by SPSS).
pub const SYSMIS_BITS: u64 = 0xFFEF_FFFF_FFFF_FFFF;

/// Highest representable double in SPSS.
pub const HIGHEST_BITS: u64 = 0x7FEF_FFFF_FFFF_FFFF;

/// Lowest representable double in SPSS.
pub const LOWEST_BITS: u64 = 0xFFEF_FFFF_FFFF_FFFE;

/// Default compression bias (added to bytecodes 1..=251).
pub const DEFAULT_BIAS: f64 = 100.0;

// -- Bytecode compression control codes --

/// Padding / skip.
pub const COMPRESS_SKIP: u8 = 0;
/// End of file marker.
pub const COMPRESS_END_OF_FILE: u8 = 252;
/// Next 8 raw bytes follow as uncompressed data.
pub const COMPRESS_RAW_FOLLOWS: u8 = 253;
/// Represents 8 ASCII spaces (0x20).
pub const COMPRESS_EIGHT_SPACES: u8 = 254;
/// System-missing value.
pub const COMPRESS_SYSMIS: u8 = 255;

// -- SAV record type codes --

pub const RECORD_TYPE_VARIABLE: i32 = 2;
pub const RECORD_TYPE_VALUE_LABEL: i32 = 3;
pub const RECORD_TYPE_VALUE_LABEL_VARS: i32 = 4;
pub const RECORD_TYPE_DOCUMENT: i32 = 6;
pub const RECORD_TYPE_INFO: i32 = 7;
pub const RECORD_TYPE_DICT_TERMINATION: i32 = 999;

// -- Info record subtypes --

pub const INFO_MR_SETS: i32 = 7;
pub const INFO_MR_SETS_V2: i32 = 19;
pub const INFO_INTEGER: i32 = 3;
pub const INFO_FLOAT: i32 = 4;
pub const INFO_VAR_DISPLAY: i32 = 11;
pub const INFO_LONG_NAMES: i32 = 13;
pub const INFO_VERY_LONG_STRINGS: i32 = 14;
pub const INFO_ENCODING: i32 = 20;
pub const INFO_VAR_ATTRIBUTES: i32 = 18;
pub const INFO_LONG_STRING_LABELS: i32 = 21;
pub const INFO_LONG_STRING_MISSING: i32 = 22;

// -- Enums --

/// SPSS compression type.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Compression {
    None,
    Bytecode,
    Zlib,
}

impl Compression {
    pub fn from_i32(val: i32) -> Option<Compression> {
        match val {
            0 => Some(Compression::None),
            1 => Some(Compression::Bytecode),
            2 => Some(Compression::Zlib),
            _ => None,
        }
    }

    pub fn to_i32(self) -> i32 {
        match self {
            Compression::None => 0,
            Compression::Bytecode => 1,
            Compression::Zlib => 2,
        }
    }
}

/// Variable measurement level.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Measure {
    Unknown,
    Nominal,
    Ordinal,
    Scale,
}

impl Measure {
    pub fn from_i32(val: i32) -> Measure {
        match val {
            1 => Measure::Nominal,
            2 => Measure::Ordinal,
            3 => Measure::Scale,
            _ => Measure::Unknown,
        }
    }

    pub fn as_str(&self) -> &'static str {
        match self {
            Measure::Unknown => "unknown",
            Measure::Nominal => "nominal",
            Measure::Ordinal => "ordinal",
            Measure::Scale => "scale",
        }
    }

    pub fn to_i32(self) -> i32 {
        match self {
            Measure::Unknown => 0,
            Measure::Nominal => 1,
            Measure::Ordinal => 2,
            Measure::Scale => 3,
        }
    }
}

/// Variable alignment.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Alignment {
    Unknown,
    Left,
    Right,
    Center,
}

impl Alignment {
    pub fn from_i32(val: i32) -> Alignment {
        match val {
            0 => Alignment::Left,
            1 => Alignment::Right,
            2 => Alignment::Center,
            _ => Alignment::Unknown,
        }
    }

    pub fn as_str(&self) -> &'static str {
        match self {
            Alignment::Unknown => "unknown",
            Alignment::Left => "left",
            Alignment::Right => "right",
            Alignment::Center => "center",
        }
    }

    pub fn to_i32(self) -> i32 {
        match self {
            Alignment::Unknown | Alignment::Left => 0,
            Alignment::Right => 1,
            Alignment::Center => 2,
        }
    }
}

/// Variable role (stored as $@Role attribute in subtype 18).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Role {
    Input,
    Target,
    Both,
    None,
    Partition,
    Split,
}

impl Role {
    /// Parse from SPSS numeric code string ("0"=Input, "1"=Target, etc.).
    pub fn from_code(s: &str) -> Option<Role> {
        match s.trim() {
            "0" => Some(Role::Input),
            "1" => Some(Role::Target),
            "2" => Some(Role::Both),
            "3" => Some(Role::None),
            "4" => Some(Role::Partition),
            "5" => Some(Role::Split),
            _ => Option::None,
        }
    }

    /// Return the SPSS numeric code string.
    pub fn to_code(&self) -> &'static str {
        match self {
            Role::Input => "0",
            Role::Target => "1",
            Role::Both => "2",
            Role::None => "3",
            Role::Partition => "4",
            Role::Split => "5",
        }
    }

    /// Return the human-readable role name.
    pub fn as_str(&self) -> &'static str {
        match self {
            Role::Input => "input",
            Role::Target => "target",
            Role::Both => "both",
            Role::None => "none",
            Role::Partition => "partition",
            Role::Split => "split",
        }
    }
}

/// SPSS variable type.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum VarType {
    Numeric,
    String(usize), // width in bytes
}

// -- Temporal conversion constants --

/// Days from SPSS epoch (1582-10-14) to Unix epoch (1970-01-01).
/// 12,219,379,200 seconds / 86,400 seconds-per-day = 141,428 days.
pub const SPSS_EPOCH_OFFSET_DAYS: i64 = 141_428;

/// Seconds from SPSS epoch (1582-10-14) to Unix epoch (1970-01-01).
pub const SPSS_EPOCH_OFFSET_SECONDS: f64 = 12_219_379_200.0;

/// Microseconds per second.
pub const MICROS_PER_SECOND: f64 = 1_000_000.0;

/// Seconds per day.
pub const SECONDS_PER_DAY: f64 = 86_400.0;

/// The Arrow temporal type category for an SPSS date/time format.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TemporalKind {
    /// Date-only → Arrow Date32 (days since Unix epoch).
    Date,
    /// Date+time → Arrow Timestamp(Microsecond, None).
    Timestamp,
    /// Elapsed time → Arrow Duration(Microsecond).
    Duration,
}

/// SPSS print/write format type codes.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(u8)]
pub enum FormatType {
    A = 1,
    Ahex = 2,
    Comma = 3,
    Dollar = 4,
    F = 5,
    Ib = 6,
    PibHex = 7,
    P = 8,
    Pib = 9,
    Pk = 10,
    Rb = 11,
    RbHex = 12,
    Z = 15,
    N = 16,
    E = 17,
    Date = 20,
    Time = 21,
    DateTime = 22,
    ADate = 23,
    JDate = 24,
    DTime = 25,
    Wkday = 26,
    Month = 27,
    Moyr = 28,
    Qyr = 29,
    Wkyr = 30,
    Pct = 31,
    Dot = 32,
    Cca = 33,
    Ccb = 34,
    Ccc = 35,
    Ccd = 36,
    Cce = 37,
    EDate = 38,
    SDate = 39,
    MTime = 40,
    YmDhms = 41,
}

impl FormatType {
    pub fn from_prefix(s: &str) -> Option<FormatType> {
        match s.to_uppercase().as_str() {
            "A" => Some(FormatType::A),
            "AHEX" => Some(FormatType::Ahex),
            "COMMA" => Some(FormatType::Comma),
            "DOLLAR" => Some(FormatType::Dollar),
            "F" => Some(FormatType::F),
            "IB" => Some(FormatType::Ib),
            "PIBHEX" => Some(FormatType::PibHex),
            "P" => Some(FormatType::P),
            "PIB" => Some(FormatType::Pib),
            "PK" => Some(FormatType::Pk),
            "RB" => Some(FormatType::Rb),
            "RBHEX" => Some(FormatType::RbHex),
            "Z" => Some(FormatType::Z),
            "N" => Some(FormatType::N),
            "E" => Some(FormatType::E),
            "DATE" => Some(FormatType::Date),
            "TIME" => Some(FormatType::Time),
            "DATETIME" => Some(FormatType::DateTime),
            "ADATE" => Some(FormatType::ADate),
            "JDATE" => Some(FormatType::JDate),
            "DTIME" => Some(FormatType::DTime),
            "WKDAY" => Some(FormatType::Wkday),
            "MONTH" => Some(FormatType::Month),
            "MOYR" => Some(FormatType::Moyr),
            "QYR" => Some(FormatType::Qyr),
            "WKYR" => Some(FormatType::Wkyr),
            "PCT" => Some(FormatType::Pct),
            "DOT" => Some(FormatType::Dot),
            "CCA" => Some(FormatType::Cca),
            "CCB" => Some(FormatType::Ccb),
            "CCC" => Some(FormatType::Ccc),
            "CCD" => Some(FormatType::Ccd),
            "CCE" => Some(FormatType::Cce),
            "EDATE" => Some(FormatType::EDate),
            "SDATE" => Some(FormatType::SDate),
            "MTIME" => Some(FormatType::MTime),
            "YMDHMS" => Some(FormatType::YmDhms),
            _ => None,
        }
    }

    pub fn from_u8(val: u8) -> Option<FormatType> {
        match val {
            1 => Some(FormatType::A),
            2 => Some(FormatType::Ahex),
            3 => Some(FormatType::Comma),
            4 => Some(FormatType::Dollar),
            5 => Some(FormatType::F),
            6 => Some(FormatType::Ib),
            7 => Some(FormatType::PibHex),
            8 => Some(FormatType::P),
            9 => Some(FormatType::Pib),
            10 => Some(FormatType::Pk),
            11 => Some(FormatType::Rb),
            12 => Some(FormatType::RbHex),
            15 => Some(FormatType::Z),
            16 => Some(FormatType::N),
            17 => Some(FormatType::E),
            20 => Some(FormatType::Date),
            21 => Some(FormatType::Time),
            22 => Some(FormatType::DateTime),
            23 => Some(FormatType::ADate),
            24 => Some(FormatType::JDate),
            25 => Some(FormatType::DTime),
            26 => Some(FormatType::Wkday),
            27 => Some(FormatType::Month),
            28 => Some(FormatType::Moyr),
            29 => Some(FormatType::Qyr),
            30 => Some(FormatType::Wkyr),
            31 => Some(FormatType::Pct),
            32 => Some(FormatType::Dot),
            33 => Some(FormatType::Cca),
            34 => Some(FormatType::Ccb),
            35 => Some(FormatType::Ccc),
            36 => Some(FormatType::Ccd),
            37 => Some(FormatType::Cce),
            38 => Some(FormatType::EDate),
            39 => Some(FormatType::SDate),
            40 => Some(FormatType::MTime),
            41 => Some(FormatType::YmDhms),
            _ => None,
        }
    }

    pub fn prefix(&self) -> &'static str {
        match self {
            FormatType::A => "A",
            FormatType::Ahex => "AHEX",
            FormatType::Comma => "COMMA",
            FormatType::Dollar => "DOLLAR",
            FormatType::F => "F",
            FormatType::Ib => "IB",
            FormatType::PibHex => "PIBHEX",
            FormatType::P => "P",
            FormatType::Pib => "PIB",
            FormatType::Pk => "PK",
            FormatType::Rb => "RB",
            FormatType::RbHex => "RBHEX",
            FormatType::Z => "Z",
            FormatType::N => "N",
            FormatType::E => "E",
            FormatType::Date => "DATE",
            FormatType::Time => "TIME",
            FormatType::DateTime => "DATETIME",
            FormatType::ADate => "ADATE",
            FormatType::JDate => "JDATE",
            FormatType::DTime => "DTIME",
            FormatType::Wkday => "WKDAY",
            FormatType::Month => "MONTH",
            FormatType::Moyr => "MOYR",
            FormatType::Qyr => "QYR",
            FormatType::Wkyr => "WKYR",
            FormatType::Pct => "PCT",
            FormatType::Dot => "DOT",
            FormatType::Cca => "CCA",
            FormatType::Ccb => "CCB",
            FormatType::Ccc => "CCC",
            FormatType::Ccd => "CCD",
            FormatType::Cce => "CCE",
            FormatType::EDate => "EDATE",
            FormatType::SDate => "SDATE",
            FormatType::MTime => "MTIME",
            FormatType::YmDhms => "YMDHMS",
        }
    }

    /// Whether this format type represents a string variable.
    pub fn is_string(&self) -> bool {
        matches!(self, FormatType::A | FormatType::Ahex)
    }

    /// Returns the temporal kind if this format should produce an Arrow temporal type.
    /// Returns None for non-temporal formats and for Wkday/Month (integer codes).
    pub fn temporal_kind(&self) -> Option<TemporalKind> {
        match self {
            FormatType::Date
            | FormatType::ADate
            | FormatType::JDate
            | FormatType::EDate
            | FormatType::SDate
            | FormatType::Moyr
            | FormatType::Qyr
            | FormatType::Wkyr => Some(TemporalKind::Date),

            FormatType::DateTime | FormatType::YmDhms => Some(TemporalKind::Timestamp),

            FormatType::Time | FormatType::DTime | FormatType::MTime => {
                Some(TemporalKind::Duration)
            }

            _ => None,
        }
    }

    /// Whether this format type is a date/time type (no decimals in display).
    pub fn is_date_time(&self) -> bool {
        matches!(
            self,
            FormatType::Date
                | FormatType::Time
                | FormatType::DateTime
                | FormatType::ADate
                | FormatType::JDate
                | FormatType::DTime
                | FormatType::Wkday
                | FormatType::Month
                | FormatType::Moyr
                | FormatType::Qyr
                | FormatType::Wkyr
                | FormatType::EDate
                | FormatType::SDate
                | FormatType::MTime
                | FormatType::YmDhms
        )
    }
}

/// Decoded SPSS print/write format.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SpssFormat {
    pub format_type: FormatType,
    pub width: u8,
    pub decimals: u8,
}

impl SpssFormat {
    /// Decode a packed i32 format specification.
    /// Layout: `(type << 16) | (width << 8) | decimals`
    pub fn from_packed(packed: i32) -> Option<SpssFormat> {
        let raw = packed as u32;
        let format_type_byte = ((raw >> 16) & 0xFF) as u8;
        let width = ((raw >> 8) & 0xFF) as u8;
        let decimals = (raw & 0xFF) as u8;

        FormatType::from_u8(format_type_byte).map(|format_type| SpssFormat {
            format_type,
            width,
            decimals,
        })
    }

    /// Encode to packed i32 format: `(type << 16) | (width << 8) | decimals`.
    /// Reverse of `from_packed`.
    pub fn to_packed(&self) -> i32 {
        ((self.format_type as u8 as i32) << 16)
            | ((self.width as i32) << 8)
            | (self.decimals as i32)
    }

    /// Parse an SPSS format string like "F8.2", "A50", "DATE11" back to SpssFormat.
    pub fn from_string(s: &str) -> Option<SpssFormat> {
        let s = s.trim();
        if s.is_empty() {
            return None;
        }
        // Split at boundary between letters and digits
        let prefix_end = s
            .find(|c: char| c.is_ascii_digit() || c == '.')
            .unwrap_or(s.len());
        let prefix = &s[..prefix_end];
        let rest = &s[prefix_end..];

        let format_type = FormatType::from_prefix(prefix)?;

        // Parse "width.decimals" or just "width"
        // Width can exceed 255 for VLS strings (e.g. "A281"), so parse as u32
        // and clamp to 255 for the packed format representation.
        let (width, decimals) = if let Some(dot_pos) = rest.find('.') {
            let w: u32 = rest[..dot_pos].parse().ok()?;
            let d: u32 = rest[dot_pos + 1..].parse().ok()?;
            ((w.min(255)) as u8, (d.min(255)) as u8)
        } else if rest.is_empty() {
            // Format with no width (rare, use defaults)
            (8, 0)
        } else {
            let w: u32 = rest.parse().ok()?;
            ((w.min(255)) as u8, 0)
        };

        Some(SpssFormat {
            format_type,
            width,
            decimals,
        })
    }

    /// Render as a human-readable SPSS format string like "F8.2" or "A50".
    pub fn to_spss_string(&self) -> String {
        if self.format_type.is_string() || self.format_type.is_date_time() {
            // String and date/time formats: no decimal suffix
            format!("{}{}", self.format_type.prefix(), self.width)
        } else {
            format!(
                "{}{}.{}",
                self.format_type.prefix(),
                self.width,
                self.decimals
            )
        }
    }
}

/// Check if a raw f64 bit pattern is SYSMIS.
#[inline(always)]
pub fn is_sysmis(val: f64) -> bool {
    val.to_bits() == SYSMIS_BITS
}

/// Get the SYSMIS value as f64.
#[inline]
pub fn sysmis() -> f64 {
    f64::from_bits(SYSMIS_BITS)
}

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

    #[test]
    fn test_sysmis_is_negative_max() {
        // SYSMIS is -DBL_MAX, the most negative finite double, NOT NaN
        let val = sysmis();
        assert!(val.is_finite());
        assert!(val < 0.0);
        assert_eq!(val, -f64::MAX);
    }

    #[test]
    fn test_is_sysmis() {
        assert!(is_sysmis(sysmis()));
        assert!(!is_sysmis(0.0));
        assert!(!is_sysmis(f64::NAN)); // regular NaN != SYSMIS
    }

    #[test]
    fn test_format_decode() {
        // F8.2 = type 5, width 8, decimals 2
        let packed = (5 << 16) | (8 << 8) | 2;
        let fmt = SpssFormat::from_packed(packed).unwrap();
        assert_eq!(fmt.format_type, FormatType::F);
        assert_eq!(fmt.width, 8);
        assert_eq!(fmt.decimals, 2);
        assert_eq!(fmt.to_spss_string(), "F8.2");
    }

    #[test]
    fn test_format_string_type() {
        // A50 = type 1, width 50, decimals 0
        let packed = (1 << 16) | (50 << 8) | 0;
        let fmt = SpssFormat::from_packed(packed).unwrap();
        assert_eq!(fmt.format_type, FormatType::A);
        assert_eq!(fmt.to_spss_string(), "A50");
    }

    #[test]
    fn test_compression_from_i32() {
        assert_eq!(Compression::from_i32(0), Some(Compression::None));
        assert_eq!(Compression::from_i32(1), Some(Compression::Bytecode));
        assert_eq!(Compression::from_i32(2), Some(Compression::Zlib));
        assert_eq!(Compression::from_i32(99), None);
    }

    #[test]
    fn test_temporal_kind() {
        // Date-only formats
        assert_eq!(FormatType::Date.temporal_kind(), Some(TemporalKind::Date));
        assert_eq!(FormatType::ADate.temporal_kind(), Some(TemporalKind::Date));
        assert_eq!(FormatType::JDate.temporal_kind(), Some(TemporalKind::Date));
        assert_eq!(FormatType::EDate.temporal_kind(), Some(TemporalKind::Date));
        assert_eq!(FormatType::SDate.temporal_kind(), Some(TemporalKind::Date));
        assert_eq!(FormatType::Moyr.temporal_kind(), Some(TemporalKind::Date));
        assert_eq!(FormatType::Qyr.temporal_kind(), Some(TemporalKind::Date));
        assert_eq!(FormatType::Wkyr.temporal_kind(), Some(TemporalKind::Date));

        // Timestamp formats
        assert_eq!(
            FormatType::DateTime.temporal_kind(),
            Some(TemporalKind::Timestamp)
        );
        assert_eq!(
            FormatType::YmDhms.temporal_kind(),
            Some(TemporalKind::Timestamp)
        );

        // Duration formats
        assert_eq!(
            FormatType::Time.temporal_kind(),
            Some(TemporalKind::Duration)
        );
        assert_eq!(
            FormatType::DTime.temporal_kind(),
            Some(TemporalKind::Duration)
        );
        assert_eq!(
            FormatType::MTime.temporal_kind(),
            Some(TemporalKind::Duration)
        );

        // Non-temporal: Wkday, Month, plain numeric
        assert_eq!(FormatType::Wkday.temporal_kind(), None);
        assert_eq!(FormatType::Month.temporal_kind(), None);
        assert_eq!(FormatType::F.temporal_kind(), None);
        assert_eq!(FormatType::A.temporal_kind(), None);
        assert_eq!(FormatType::Comma.temporal_kind(), None);
    }

    #[test]
    fn test_temporal_conversions() {
        // 1970-01-01 00:00:00 in SPSS seconds = 12,219,379,200
        let spss_unix_epoch = SPSS_EPOCH_OFFSET_SECONDS;

        // Date32: days since unix epoch → should be 0
        let days = (spss_unix_epoch / SECONDS_PER_DAY - SPSS_EPOCH_OFFSET_DAYS as f64) as i32;
        assert_eq!(days, 0);

        // Timestamp: microseconds since unix epoch → should be 0
        let micros = ((spss_unix_epoch - SPSS_EPOCH_OFFSET_SECONDS) * MICROS_PER_SECOND) as i64;
        assert_eq!(micros, 0);

        // Duration: 3600 SPSS seconds = 1 hour = 3,600,000,000 microseconds
        let dur_micros = (3600.0 * MICROS_PER_SECOND) as i64;
        assert_eq!(dur_micros, 3_600_000_000);

        // 2024-01-01: 19723 days after Unix epoch
        // In SPSS: (141428 + 19723) days * 86400 = SPSS seconds
        let days_2024 = SPSS_EPOCH_OFFSET_DAYS + 19723;
        let spss_2024 = days_2024 as f64 * SECONDS_PER_DAY;
        let date32_2024 = (spss_2024 / SECONDS_PER_DAY - SPSS_EPOCH_OFFSET_DAYS as f64) as i32;
        assert_eq!(date32_2024, 19723); // 19723 days from 1970-01-01 to 2024-01-01
    }

    #[test]
    fn test_measure_from_i32() {
        assert_eq!(Measure::from_i32(1), Measure::Nominal);
        assert_eq!(Measure::from_i32(2), Measure::Ordinal);
        assert_eq!(Measure::from_i32(3), Measure::Scale);
        assert_eq!(Measure::from_i32(0), Measure::Unknown);
    }
}