Time Module

The Language::Time module provides date/time handling, duration calculations, and performance timing.

xxml
#import Language::Time;

DateTime

Represents a specific point in time with date and time components.

Static Constructors

MethodParametersReturnsDescription
nowDateTime^Current date/time
fromTimestampms: Integer^DateTime^Create from Unix timestamp (ms)
createyear, month, day, hour, min, sec: Integer^DateTime^Create from components

Component Access

MethodParametersReturnsDescription
yearInteger^Get year
monthInteger^Get month (1-12)
dayInteger^Get day of month
hourInteger^Get hour (0-23)
minuteInteger^Get minute
secondInteger^Get second
millisecondInteger^Get millisecond
dayOfWeekInteger^Day of week (0=Sunday)
dayOfYearInteger^Day of year (1-366)
isLeapYearBool^Check if leap year
xxml
// Get current date/time
Instantiate Time::DateTime^ As <now> = Time::DateTime::now();

Run System::Console::printLine(
    String::Constructor("Date: ")
        .append(now.year().toString())
        .append(String::Constructor("-"))
        .append(now.month().toString())
        .append(String::Constructor("-"))
        .append(now.day().toString())
);

Run System::Console::printLine(
    String::Constructor("Time: ")
        .append(now.hour().toString())
        .append(String::Constructor(":"))
        .append(now.minute().toString())
        .append(String::Constructor(":"))
        .append(now.second().toString())
);

Arithmetic

MethodParametersReturnsDescription
addYearsyears: Integer^DateTime^Add years
addMonthsmonths: Integer^DateTime^Add months
addDaysdays: Integer^DateTime^Add days
addHourshours: Integer^DateTime^Add hours
addMinutesminutes: Integer^DateTime^Add minutes
addSecondsseconds: Integer^DateTime^Add seconds
xxml
Instantiate Time::DateTime^ As <now> = Time::DateTime::now();

// Add 7 days
Instantiate Time::DateTime^ As <nextWeek> = now.addDays(Integer::Constructor(7));

// Add 2 hours
Instantiate Time::DateTime^ As <later> = now.addHours(Integer::Constructor(2));

// Subtract 1 month (negative value)
Instantiate Time::DateTime^ As <lastMonth> = now.addMonths(Integer::Constructor(-1));

Comparison

MethodParametersReturnsDescription
compareToother: DateTime^Integer^-1, 0, or 1
equalsother: DateTime^Bool^Test equality
isBeforeother: DateTime^Bool^Test if before
isAfterother: DateTime^Bool^Test if after

Conversion

MethodParametersReturnsDescription
timestampInteger^Get Unix timestamp (ms)
formatpattern: String^String^Format with pattern
toStringString^ISO 8601 format
xxml
Instantiate Time::DateTime^ As <now> = Time::DateTime::now();

// Format options
Run System::Console::printLine(now.format(String::Constructor("YYYY-MM-DD")));
Run System::Console::printLine(now.format(String::Constructor("HH:mm:ss")));
Run System::Console::printLine(now.format(String::Constructor("YYYY-MM-DD HH:mm:ss")));
Run System::Console::printLine(now.toString());  // ISO 8601

TimeSpan

Represents a duration or interval of time.

Static Constructors

MethodParametersReturnsDescription
fromMillisecondsms: Integer^TimeSpan^Create from ms
fromSecondssec: Integer^TimeSpan^Create from seconds
fromMinutesmin: Integer^TimeSpan^Create from minutes
fromHourshours: Integer^TimeSpan^Create from hours
fromDaysdays: Integer^TimeSpan^Create from days

Conversion

MethodParametersReturnsDescription
totalMillisecondsInteger^Get total milliseconds
totalSecondsInteger^Get total seconds
totalMinutesInteger^Get total minutes
totalHoursInteger^Get total hours
totalDaysInteger^Get total days

Arithmetic

MethodParametersReturnsDescription
addother: TimeSpan^TimeSpan^Add durations
subtractother: TimeSpan^TimeSpan^Subtract durations
multiplyfactor: Integer^TimeSpan^Multiply duration
negateTimeSpan^Negate duration
absTimeSpan^Absolute value
xxml
// Create durations
Instantiate Time::TimeSpan^ As <oneHour> = Time::TimeSpan::fromHours(Integer::Constructor(1));
Instantiate Time::TimeSpan^ As <thirtyMin> = Time::TimeSpan::fromMinutes(Integer::Constructor(30));

// Add durations
Instantiate Time::TimeSpan^ As <total> = oneHour.add(thirtyMin);
Run System::Console::printLine(
    String::Constructor("Total minutes: ").append(total.totalMinutes().toString())
);  // 90

// Multiply
Instantiate Time::TimeSpan^ As <tripled> = thirtyMin.multiply(Integer::Constructor(3));
Run System::Console::printLine(
    String::Constructor("Tripled: ").append(tripled.totalMinutes().toString())
);  // 90

Comparison

MethodParametersReturnsDescription
compareToother: TimeSpan^Integer^-1, 0, or 1
equalsother: TimeSpan^Bool^Test equality
toStringString^String representation

Timer

High-resolution stopwatch for measuring elapsed time.

MethodParametersReturnsDescription
ConstructorTimer^Create stopped timer
startNoneStart or resume timing
stopNoneStop timing
resetNoneReset to zero
restartNoneReset and start
elapsedMillisecondsInteger^Get elapsed ms
elapsedSecondsDouble^Get elapsed seconds
isRunningBool^Check if running
xxml
Instantiate Time::Timer^ As <timer> = Time::Timer::Constructor();

Run timer.start();

// Do some work
Instantiate Integer^ As <sum> = Integer::Constructor(0);
For (Instantiate Integer^ As <i> = Integer::Constructor(0);
     i.lessThan(Integer::Constructor(100000)).toBool();
     Set i = i.add(Integer::Constructor(1)))
{
    Set sum = sum.add(i);
}

Run timer.stop();

Run System::Console::printLine(
    String::Constructor("Elapsed: ")
        .append(timer.elapsedMilliseconds().toString())
        .append(String::Constructor("ms"))
);

// Restart for another measurement
Run timer.restart();
// ... more work ...
Run timer.stop();

Note

Use Timer for benchmarking and performance profiling. The timer can be started, stopped, and restarted multiple times, accumulating elapsed time.

Complete Example

datetime_demo.xxml
1#import Language::Core;
2#import Language::Time;
3#import Language::System;
4
5[ Entrypoint
6 {
7 // Current date/time
8 Instantiate Time::DateTime^ As <now> = Time::DateTime::now();
9
10 Run System::Console::printLine(String::Constructor("=== Current Date/Time ==="));
11 Run System::Console::printLine(now.format(String::Constructor("YYYY-MM-DD HH:mm:ss")));
12
13 If (now.isLeapYear().toBool())
14 {
15 Run System::Console::printLine(String::Constructor("This is a leap year!"));
16 }
17
18 // Calculate age from birthdate
19 Instantiate Time::DateTime^ As <birthDate> = Time::DateTime::create(
20 Integer::Constructor(1990),
21 Integer::Constructor(6),
22 Integer::Constructor(15),
23 Integer::Constructor(0),
24 Integer::Constructor(0),
25 Integer::Constructor(0)
26 );
27
28 Instantiate Integer^ As <ageYears> = now.year().subtract(birthDate.year());
29 Run System::Console::printLine(String::Constructor(""));
30 Run System::Console::printLine(
31 String::Constructor("Age: ").append(ageYears.toString()).append(String::Constructor(" years"))
32 );
33
34 // Calculate days until new year
35 Instantiate Time::DateTime^ As <newYear> = Time::DateTime::create(
36 now.year().add(Integer::Constructor(1)),
37 Integer::Constructor(1),
38 Integer::Constructor(1),
39 Integer::Constructor(0),
40 Integer::Constructor(0),
41 Integer::Constructor(0)
42 );
43
44 Instantiate Integer^ As <daysUntilNewYear> = newYear.timestamp()
45 .subtract(now.timestamp())
46 .divide(Integer::Constructor(86400000));
47
48 Run System::Console::printLine(String::Constructor(""));
49 Run System::Console::printLine(
50 String::Constructor("Days until new year: ").append(daysUntilNewYear.toString())
51 );
52
53 // Benchmark example
54 Run System::Console::printLine(String::Constructor(""));
55 Run System::Console::printLine(String::Constructor("=== Benchmark ==="));
56
57 Instantiate Time::Timer^ As <timer> = Time::Timer::Constructor();
58 Run timer.start();
59
60 Instantiate Integer^ As <sum> = Integer::Constructor(0);
61 For (Instantiate Integer^ As <i> = Integer::Constructor(0);
62 i.lessThan(Integer::Constructor(100000)).toBool();
63 Set i = i.add(Integer::Constructor(1)))
64 {
65 Set sum = sum.add(i);
66 }
67
68 Run timer.stop();
69
70 Run System::Console::printLine(
71 String::Constructor("Sum: ").append(sum.toString())
72 );
73 Run System::Console::printLine(
74 String::Constructor("Time: ")
75 .append(timer.elapsedMilliseconds().toString())
76 .append(String::Constructor("ms"))
77 );
78
79 Exit(0);
80 }
81]

See Also