SPB Git

Repository Public MIT

DateTimeTests.swift 13.0 KB 242 lines swift

1// =============================================================================2//                  SP BOUCHER | SOFTWARE & RESEARCH3//             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// PROJECT METADATA7// -----------------------------------------------------------------------------8// Project          : ORIX Astra9// File             : Tests/AstraAgentsTests/DateTimeTests.swift10// Module           : AstraAgentsTests11// Repository       : https://git.spboucher.ai/orix-astra12// Created          : 2026-11-2113// Last Modified    : 2026-11-2114//15// =============================================================================16// AUTHOR & PROFESSIONAL AFFILIATION17// =============================================================================18//19// Author           : Simon-Pierre Boucher20// Position         : Professor of Finance21// Institution      : Université du Québec en Outaouais (UQO)22// Department       : Département des sciences administratives23// Organization     : SP Boucher24//25// Website          : https://spboucher.ai26// Git              : https://git.spboucher.ai27//28// =============================================================================29// OFFICIAL CONTACT DIRECTORY30// =============================================================================31//32// General          : info@spboucher.ai33// Professional     : contact@spboucher.ai34// Direct           : simon@spboucher.ai35// Research         : research@spboucher.ai36// Development      : dev@spboucher.ai37// Support          : support@spboucher.ai38// Security/Admin   : admin@spboucher.ai39// Notifications    : notifications@spboucher.ai40//41// Preferred Contact: support@spboucher.ai42//43// =============================================================================44// COPYRIGHT & INTELLECTUAL PROPERTY45// =============================================================================46//47// Copyright (c) 202648// Simon-Pierre Boucher49//50// All rights reserved where applicable.51//52// This file is governed by the applicable project license,53// copyright notices, institutional agreements and relevant law.54//55// Unless expressly authorized by the applicable license or56// rights holder, copying, redistribution, sublicensing,57// publication and commercial exploitation are not permitted.58//59// Existing third-party rights and notices remain unaffected.60//61// =============================================================================62// CONFIDENTIALITY & SECURITY63// =============================================================================64//65// Classification   : Public66// Distribution     : Public repository; use is governed by the project license67// Security Contact : admin@spboucher.ai68//69// Access to this source does not independently grant permission70// to redistribute or disclose confidential materials.71//72// Do not embed credentials, private keys, access tokens,73// certificates or confidential personal information.74//75// =============================================================================76// LICENSE & MAINTENANCE77// =============================================================================78//79// License          : Proprietary, all rights reserved (see license file)80// License File     : LICENSE81// Maintainer       : Simon-Pierre Boucher82// Technical Contact: dev@spboucher.ai83//84// =============================================================================85// END OF SP BOUCHER SOURCE HEADER86// =============================================================================8788import Foundation89import Testing90import AstraCore91@testable import AstraAgents9293@Suite struct DateTimeTests {94    // Saturday 7 March 2026, 12:00 UTC: the day before clocks go forward in North America.95    static let fixed = Date(timeIntervalSince1970: 1_772_884_800)96    let tool = DateTimeTool(now: { DateTimeTests.fixed }, defaultTimeZone: TimeZone(identifier: "UTC")!)97    let toronto = TimeZone(identifier: "America/Toronto")!9899    func iso(_ text: String, _ zone: String = "America/Toronto") throws -> Date {100        try DateTimeEngine.parse(text, zone: TimeZone(identifier: zone)!)101    }102103    @Test func fixedClockIsWhatTheCommentSays() {104        #expect(DateTimeEngine.iso(Self.fixed, zone: TimeZone(identifier: "UTC")!) == "2026-03-07T12:00:00Z")105    }106107    @Test func nowUsesTheInjectedClock() async throws {108        let utc = try await runTool(tool, ["operation": "now"])109        #expect(utc.contains("iso: 2026-03-07T12:00:00Z"))110        #expect(utc.contains("weekday: Saturday"))111        #expect(utc.contains("unix: 1772884800"))112        let local = try await runTool(tool, ["operation": "now", "timezone": "America/Toronto"])113        #expect(local.contains("iso: 2026-03-07T07:00:00-05:00"))114        #expect(local.contains("utc: 2026-03-07T12:00:00Z"))115        #expect(local.contains("local: Saturday, 7 March 2026, 07:00:00"))116        #expect(local.contains("timezone: America/Toronto (UTC-05:00, standard time)"))117        let tokyo = try await runTool(tool, ["operation": "now", "timezone": "Asia/Tokyo"])118        #expect(tokyo.contains("iso: 2026-03-07T21:00:00+09:00"))119        let kolkata = try await runTool(tool, ["operation": "now", "timezone": "Asia/Kolkata"])120        #expect(kolkata.contains("(UTC+05:30, standard time)"))121    }122123    @Test func parsing() throws {124        #expect(try iso("2026-03-08T14:30:00-04:00") == iso("2026-03-08T18:30:00Z"))125        #expect(try iso("2026-03-08T14:30:00+0530") == iso("2026-03-08T09:00:00Z"))126        #expect(try iso("2026-01-15") == iso("2026-01-15T00:00:00-05:00"))127        #expect(try iso("2026-01-15 08:05") == iso("2026-01-15T13:05:00Z"))128        #expect(try iso("2026-01-15T08:05:09.500") == iso("2026-01-15T13:05:09.5Z"))129        for bad in ["tomorrow", "2026-02-30", "2026-13-01", "2026-00-10", "15/01/2026", "2026-01-15T25:00", "2027-02-29", ""] {130            #expect(throws: ToolError.self, "\(bad)") { try iso(bad) }131        }132        #expect(try iso("2028-02-29") < iso("2028-03-01"))133    }134135    @Test func springForward() throws {136        // 8 March 2026: 02:00 EST becomes 03:00 EDT in Toronto.137        let noon = try iso("2026-03-07T12:00:00")138        let nextDay = try DateTimeEngine.add(.init(days: 1), to: noon, zone: toronto)139        #expect(DateTimeEngine.iso(nextDay, zone: toronto) == "2026-03-08T12:00:00-04:00")       // same wall-clock time140        #expect(nextDay.timeIntervalSince(noon) == 23 * 3600)                                     // one hour shorter141        let plus24h = try DateTimeEngine.add(.init(hours: 24), to: noon, zone: toronto)142        #expect(DateTimeEngine.iso(plus24h, zone: toronto) == "2026-03-08T13:00:00-04:00")       // exact elapsed time143        // 02:30 does not exist that night: it is read as 03:30 EDT.144        #expect(DateTimeEngine.iso(try iso("2026-03-08T02:30:00"), zone: toronto) == "2026-03-08T03:30:00-04:00")145        let justBefore = try iso("2026-03-08T01:59:59")146        #expect(DateTimeEngine.iso(justBefore.addingTimeInterval(1), zone: toronto) == "2026-03-08T03:00:00-04:00")147        let d = DateTimeEngine.difference(from: noon, to: nextDay, zone: toronto)148        #expect(d.seconds == 82_800)149        #expect(d.calendarDays == 1)150        #expect(d.days == 1 && d.hours == 0)151    }152153    @Test func fallBack() throws {154        // 1 November 2026: 02:00 EDT becomes 01:00 EST. 01:30 happens twice; the first one (EDT) is used.155        let first = try iso("2026-11-01T01:30:00")156        #expect(DateTimeEngine.iso(first, zone: toronto) == "2026-11-01T01:30:00-04:00")157        let second = try DateTimeEngine.add(.init(hours: 1), to: first, zone: toronto)158        #expect(DateTimeEngine.iso(second, zone: toronto) == "2026-11-01T01:30:00-05:00")159        let day = try DateTimeEngine.add(.init(days: 1), to: try iso("2026-10-31T12:00:00"), zone: toronto)160        #expect(DateTimeEngine.iso(day, zone: toronto) == "2026-11-01T12:00:00-05:00")161        #expect(day.timeIntervalSince(try iso("2026-10-31T12:00:00")) == 25 * 3600)162        #expect(toronto.isDaylightSavingTime(for: first))163        #expect(!toronto.isDaylightSavingTime(for: second))164    }165166    @Test func calendarArithmetic() throws {167        let utc = TimeZone(identifier: "UTC")!168        func add(_ start: String, _ offset: DateTimeEngine.Offset) throws -> String {169            DateTimeEngine.iso(try DateTimeEngine.add(offset, to: try DateTimeEngine.parse(start, zone: utc), zone: utc), zone: utc)170        }171        #expect(try add("2026-01-31", .init(months: 1)) == "2026-02-28T00:00:00Z")172        #expect(try add("2028-01-31", .init(months: 1)) == "2028-02-29T00:00:00Z")173        #expect(try add("2028-02-29", .init(years: 1)) == "2029-02-28T00:00:00Z")174        #expect(try add("2026-12-31T23:59:59", .init(seconds: 1)) == "2027-01-01T00:00:00Z")175        #expect(try add("2026-03-01", .init(days: -1)) == "2026-02-28T00:00:00Z")176        #expect(try add("2026-01-01", .init(weeks: 2, days: 1)) == "2026-01-16T00:00:00Z")177        #expect(try add("2026-01-01", .init(years: 1, months: 2, days: 3, hours: 4, minutes: 5, seconds: 6)) == "2027-03-04T04:05:06Z")178        #expect(throws: ToolError.self) { try add("9999-06-01", .init(years: 1)) }179    }180181    @Test func differences() throws {182        let utc = TimeZone(identifier: "UTC")!183        let a = try DateTimeEngine.parse("2026-01-15", zone: utc), b = try DateTimeEngine.parse("2026-03-20T06:00:00", zone: utc)184        let d = DateTimeEngine.difference(from: a, to: b, zone: utc)185        #expect(d.months == 2 && d.days == 5 && d.hours == 6 && d.years == 0)186        #expect(d.calendarDays == 64)187        #expect(d.seconds == 64 * 86_400 + 6 * 3600)188        let back = DateTimeEngine.difference(from: b, to: a, zone: utc)189        #expect(back.seconds == -d.seconds && back.calendarDays == -64)190    }191192    @Test func weekdays() throws {193        let utc = TimeZone(identifier: "UTC")!194        #expect(DateTimeEngine.weekday(try DateTimeEngine.parse("2026-11-21", zone: utc), zone: utc) == "Saturday")195        #expect(DateTimeEngine.weekday(try DateTimeEngine.parse("2000-01-01", zone: utc), zone: utc) == "Saturday")196        #expect(DateTimeEngine.weekday(try DateTimeEngine.parse("2024-02-29", zone: utc), zone: utc) == "Thursday")197        // The weekday depends on the zone: 03:00 UTC on a Sunday is still Saturday evening in Toronto.198        let instant = try DateTimeEngine.parse("2026-03-08T03:00:00Z", zone: utc)199        #expect(DateTimeEngine.weekday(instant, zone: utc) == "Sunday")200        #expect(DateTimeEngine.weekday(instant, zone: toronto) == "Saturday")201    }202203    @Test func toolOperations() async throws {204        let added = try await runTool(tool, ["operation": "add", "date": "2026-03-07T12:00:00", "timezone": "America/Toronto", "days": 1])205        #expect(added.contains("start: 2026-03-07T12:00:00-05:00"))206        #expect(added.contains("iso: 2026-03-08T12:00:00-04:00"))207        #expect(added.contains("daylight saving time"))208        #expect(added.contains("weekday: Sunday"))209210        let fromNow = try await runTool(tool, ["operation": "add", "hours": -36])211        #expect(fromNow.contains("iso: 2026-03-06T00:00:00Z"))212213        let difference = try await runTool(tool, ["operation": "difference", "date": "2026-03-07", "other_date": "2026-12-25", "timezone": "UTC"])214        #expect(difference.contains("calendar_days: 293"))215        #expect(difference.contains("elapsed_seconds: 25315200"))216        #expect(difference.contains("breakdown: 0 years, 9 months, 18 days, 0 hours, 0 minutes, 0 seconds"))217        #expect(difference.contains("other_date is later than date"))218219        let weekday = try await runTool(tool, ["operation": "weekday", "date": "2026-07-01"])220        #expect(weekday.hasPrefix("weekday: Wednesday"))221222        let converted = try await runTool(tool, ["operation": "convert", "date": "2026-07-01T09:00:00", "timezone": "America/Toronto", "to_timezone": "Europe/Paris"])223        #expect(converted.contains("iso: 2026-07-01T09:00:00-04:00"))224        #expect(converted.contains("iso: 2026-07-01T15:00:00+02:00"))225226        let formatted = try await runTool(tool, ["operation": "format", "date": "2026-12-25T18:30:00+01:00", "timezone": "Australia/Sydney"])227        #expect(formatted.contains("iso: 2026-12-26T04:30:00+11:00"))228        #expect(formatted.contains("day_of_year: 360"))229    }230231    @Test func toolErrors() async {232        #expect(await toolError(tool, ["operation": "now", "timezone": "Mars/Olympus_Mons"])?.contains("Unknown time zone") == true)233        #expect(await toolError(tool, ["operation": "add"])?.contains("at least one of") == true)234        #expect(await toolError(tool, ["operation": "difference"])?.contains("other_date") == true)235        #expect(await toolError(tool, ["operation": "convert", "date": "2026-01-01"])?.contains("to_timezone") == true)236        #expect(await toolError(tool, ["operation": "weekday", "date": "next friday"])?.contains("ISO 8601") == true)237        #expect(await toolError(tool, ["operation": "travel"])?.contains("must be one of") == true)238        #expect(await toolError(tool, ["operation": "add", "days": 1.5])?.contains("must be of type integer") == true)239        #expect(await toolError(tool, ["operation": "add", "days": 99_999_999])?.contains("at most 1000000") == true)240    }241}242
Files main
Menu

Theme