Eve Date and Time
Calendar Date
In Eve, we represent calendar dates using a Date object that contains four fields: era, year, month, day.
Date Structure
The Date type is defined as an object with the following fields:
era: Ordinal {BCE, CE}, the default is CEyear: Integer representing the year, 1 or more (there is no year 0)month: Integer representing the month (1-12)day: Integer representing the day of the month (1-31)
Note: Dates before the Common Era have the era BCE. A comparison puts every BCE date before every CE date.
Date Literals
A date is created in two ways: from a string, using the method parse() with a format constant, or from an object literal with a type hint. The format constants are YMD, DMY and MDY:
YMDformat: "YYYY/MM/DD"DMYformat: "DD/MM/YYYY"MDYformat: "MM/DD/YYYY"
Note: A string may end with an era label: CE, AD, BCE or BC. If it is omitted, the era is CE.
Example Usage
#date format demo
driver date_demo is
process main is
new date1 := "2023/06/15".parse(YMD); ** June 15, 2023 CE
new date2 := "30/11/753 BCE".parse(DMY); ** November 30, 753 BCE
new date3 := "07/04/1776 AD".parse(MDY); ** July 4, 1776 AD
new date4 := "01/01/2000".parse(DMY); ** January 1, 2000 CE
** Date comparisons
expect date3 > date2; ** 1776 AD is after 753 BCE
expect date1 > date3; ** 2023 CE is after 1776 AD
expect date4 > date3; ** 2000 is after 1776
** Access individual fields
print "Year of date1: {date1.year}";
print "Month of date2: {date2.month}";
print "Era of date2: {date2.era}";
** Create a new date
new custom_date = {era: CE, year: 1066, month: 10, day: 14} :Date;
print custom_date; ** {era: CE, year: 1066, month: 10, day: 14}
** Format a date for printing (one direction)
print date1 as YMD; ** 2023/06/15
return;
end date_demo;
Additional Methods
The Date object provides various methods for manipulation and querying:
is_bce(): Returns True if the era is BCE, False otherwiseto_julian(): Converts the date to Julian Day Numberfrom_julian(jdn): Creates a date from a Julian Day Numberadd_days(n): Returns a new Date object n days after this datesubtract_days(n): Returns a new Date object n days before this datedays_between(other_date): Calculates days between two dates
Time & Duration
We use Time or Duration data types. These are primitive types not nullable.
Time
A Time is created from a string using the method parse() with a format constant. The constants T12 and T24 represent the standard time formats:
- Constant: T12 = "hh:mm:ssxx, 999ms"
- Constant: T24 = "hh:mm:ss, 999ms"
hh: hours, 00..23 in T24 and 01..12 in T12
mm: minutes, 00..59
ss: seconds, 00..59
xx: can be: (am/pm)
999ms: milliseconds
Example:
# time demo
driver time_demo is
process main is
** define 3 variable of same type
new time1, time2, time3 :Time;
** alter variables using method parse(), the format is expected
let time1 := "00:23:59, 0ms".parse(T24);
new time2 := "23:59:59, 999ms".parse(T24);
new time3 := "11:59:59pm, 10ms".parse(T12);
** check time
expect time1.h == 0;
expect time1.m == 23;
expect time1.s == 59;
expect time2.ms == 999;
expect time3.ms == 10;
expect time3.t == pm;
return;
end time_demo;
Duration
Duration is an object with the fields year, days, hours, min, sec and ms. Internally it is stored as a signed 64-bit number of milliseconds. The field min means minutes: the name is written in full to avoid confusion with months.
- A Duration literal is an object literal:
{year:34, days:365, hours:4, min:30, sec:26, ms:444}. - The fields you omit are zero.
- The range is -2^63 .. 2^63-1 milliseconds.
Example
# duration demo
driver duration_demo is
process main is
** define 3 variable of same type
new d1 = {ms: 10} :Duration; ** 10 millisec
new d2 = {sec: 1} :Duration; ** 1000 millisec
new d3 = {min: 1} :Duration; ** 60000 millisec
** create duration from string
new d4 := Duration("01:01:01");
** check time
print d3; ** 60000ms
print d4; ** 3661000ms
return;
end duration_demo;
Quick Format
For printing output using a format we use operator as that is the "quick format" operator. We can use system constant templates that are available for making different formats for different data types. For other values use string interpolation ({name % format}).
The operator as works in one direction only: it formats a value into a string. The other direction is a method of String: "15:30:00, 0ms".parse(T24). The argument of parse() is the format that is expected.
Date Format
Eve has constants to define the date format:
| Constant | Value |
|---|---|
| YMD | YYYY/MM/DD |
| DMY | DD/MM/YYYY |
| MDY | MM/DD/YYYY |
Time Format
Eve has constants to define the time format:
| Constant | Value |
|---|---|
| T24 | hh:mm:ss, 999ms |
| T12 | hh:mm:ssxx, 999ms (xx is am or pm) |
Numeric Format
Eve can print comma "," for decimal numbers but also can print dot.
| Constant | Value |
|---|---|
| EUR | ,. |
| USA | ., |
Read next: Control Flow