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
| Method | Parameters | Returns | Description |
|---|---|---|---|
| now | — | DateTime^ | Current date/time |
| fromTimestamp | ms: Integer^ | DateTime^ | Create from Unix timestamp (ms) |
| create | year, month, day, hour, min, sec: Integer^ | DateTime^ | Create from components |
Component Access
| Method | Parameters | Returns | Description |
|---|---|---|---|
| year | — | Integer^ | Get year |
| month | — | Integer^ | Get month (1-12) |
| day | — | Integer^ | Get day of month |
| hour | — | Integer^ | Get hour (0-23) |
| minute | — | Integer^ | Get minute |
| second | — | Integer^ | Get second |
| millisecond | — | Integer^ | Get millisecond |
| dayOfWeek | — | Integer^ | Day of week (0=Sunday) |
| dayOfYear | — | Integer^ | Day of year (1-366) |
| isLeapYear | — | Bool^ | 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
| Method | Parameters | Returns | Description |
|---|---|---|---|
| addYears | years: Integer^ | DateTime^ | Add years |
| addMonths | months: Integer^ | DateTime^ | Add months |
| addDays | days: Integer^ | DateTime^ | Add days |
| addHours | hours: Integer^ | DateTime^ | Add hours |
| addMinutes | minutes: Integer^ | DateTime^ | Add minutes |
| addSeconds | seconds: 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
| Method | Parameters | Returns | Description |
|---|---|---|---|
| compareTo | other: DateTime^ | Integer^ | -1, 0, or 1 |
| equals | other: DateTime^ | Bool^ | Test equality |
| isBefore | other: DateTime^ | Bool^ | Test if before |
| isAfter | other: DateTime^ | Bool^ | Test if after |
Conversion
| Method | Parameters | Returns | Description |
|---|---|---|---|
| timestamp | — | Integer^ | Get Unix timestamp (ms) |
| format | pattern: String^ | String^ | Format with pattern |
| toString | — | String^ | 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 8601TimeSpan
Represents a duration or interval of time.
Static Constructors
| Method | Parameters | Returns | Description |
|---|---|---|---|
| fromMilliseconds | ms: Integer^ | TimeSpan^ | Create from ms |
| fromSeconds | sec: Integer^ | TimeSpan^ | Create from seconds |
| fromMinutes | min: Integer^ | TimeSpan^ | Create from minutes |
| fromHours | hours: Integer^ | TimeSpan^ | Create from hours |
| fromDays | days: Integer^ | TimeSpan^ | Create from days |
Conversion
| Method | Parameters | Returns | Description |
|---|---|---|---|
| totalMilliseconds | — | Integer^ | Get total milliseconds |
| totalSeconds | — | Integer^ | Get total seconds |
| totalMinutes | — | Integer^ | Get total minutes |
| totalHours | — | Integer^ | Get total hours |
| totalDays | — | Integer^ | Get total days |
Arithmetic
| Method | Parameters | Returns | Description |
|---|---|---|---|
| add | other: TimeSpan^ | TimeSpan^ | Add durations |
| subtract | other: TimeSpan^ | TimeSpan^ | Subtract durations |
| multiply | factor: Integer^ | TimeSpan^ | Multiply duration |
| negate | — | TimeSpan^ | Negate duration |
| abs | — | TimeSpan^ | 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())
); // 90Comparison
| Method | Parameters | Returns | Description |
|---|---|---|---|
| compareTo | other: TimeSpan^ | Integer^ | -1, 0, or 1 |
| equals | other: TimeSpan^ | Bool^ | Test equality |
| toString | — | String^ | String representation |
Timer
High-resolution stopwatch for measuring elapsed time.
| Method | Parameters | Returns | Description |
|---|---|---|---|
| Constructor | — | Timer^ | Create stopped timer |
| start | — | None | Start or resume timing |
| stop | — | None | Stop timing |
| reset | — | None | Reset to zero |
| restart | — | None | Reset and start |
| elapsedMilliseconds | — | Integer^ | Get elapsed ms |
| elapsedSeconds | — | Double^ | Get elapsed seconds |
| isRunning | — | Bool^ | 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 ]