## Representing and Processing Time & Dates

### Time Data Types

(*Some of the features explained in this post are available in the next Bossam release.*)

With Bossam, time data can be represented and processed. The following time types supported.

#### Date (corresponds to xsd:date)

- Syntax:
`yyyy-MM-dd`

- Example:
`2004-10-09`

#### Time (corresponds to xsd:time)

- Syntax:
`HH:mm:ss`

- Example:
`22:10:30`

#### DateTime (corresponds to xsd:dateTime)

- Syntax:
`yyyy-MM-dd'T'HH:mm:ss`

- Example:
`2004-10-09T22:10:30`

#### Time Duration (corresponds to xsd:duration)

*(refer to XSD Duration on how to write time durations)*

- ex1)
`P10Y9M8DT10H9M8S`

represents 10 years 9 months 8 days 10 hours 9 minutes 8 seconds. - ex2)
`P10Y8DT10H`

represents 10 years 8 days 10 hours. - ex1)
`P10Y9M8D`

represents 10 years 9 months 8 days. - ex1)
`PT10H9S`

represents 10 hours 9 seconds.

### Getting the Current Time/Date

Three builtin functions are available for getting the current time, current date, and current dateTime, as follows.

*The namespace URI, http://www.etri.re.kr/2003/10/bossam-builtin#, is the reserved namespace for designating Bossam’s builtin functions. Let’s assume func corresponds to http://www.etri.re.kr/2003/10/bossam-builtin#.*

`func:thisTime()`

: returns the current time`func:thisDate()`

: returns the current date`func:thisDateTime()`

: returns the current date-time

### Builtin Functions for Comparing Date/Time

There are three builtin functions for processing time data.

`func:after(time-const1,time-const2)`

- returns true if time-const1 follows
`time-const2`

- returns true if time-const1 follows
`func:before(time-const1,time-const2)`

- returns true if time-const1 precedes
`time-const2`

- returns true if time-const1 precedes
`func:containedIn(time-const1,time-begin,time-end)`

- returns true if
`time-const1`

is in the duration formed by`time-begin`

and`time-end`

- returns true if

A sample rulebase utilizing time constants is shown below.

prefix xsd = http://www.w3.org/2001/XMLSchema#; prefix rdfs = http://www.w3.org/2000/01/rdf-schema#; prefix func = http://www.etri.re.kr/2003/10/bossam-builtin#; namespace is http://etri.re.kr/2003/10/Bossam#; rulebase TimeConstants { class Person; property birthdate for Person is xsd:date; individual John is Person and birthdate = 1970-10-05; individual Sam is Person and birthdate = 1970-05-05; rule r1 is if birthdate(?x,?date1) and birthdate(?y,?date2) and [func:after(?date1,?date2) = true] then isYoungerThan(?x,?y); rule r2 is if birthdate(?x,?date1) and birthdate(?y,?date2) and [func:before(?date1,?date2) = true] then isOlderThan(?x,?y); fact f01 is beginsAt(MeetingA,2005-10-04T12:00:00); fact f02 is endsAt(MeetingA,2005-10-04T15:00:00); fact f03 is beginsAt(MeetingB,2005-10-04T14:00:00); fact f04 is endsAt(MeetingB,2005-10-04T17:00:00); fact f05 is beginsAt(MeetingC,2005-10-04T16:00:00); fact f06 is endsAt(MeetingC,2005-10-04T17:00:00); rule CompatibleMeetings is if endsAt(?m1,?t1) and beginsAt(?m2,?t2) and [func:after(?t2,?t1) = true] then Compatible(?m1,?m2); }

### Expressions on Time Data

Simple addition and subtraction on time and duration can be processed.

#### Calculating Time Difference

By subtracting a time from another time, it’s possible to calculate the difference between the two time points. Some examples follow:

`2005-10-20 - 2005-09-10`

gives`P0Y0M40DT0H0M0S`

which is 40 days.`23:10:20 - 10:30:40`

gives`P0Y0M0DT12H39M40S`

which is 12 hours 39 minutes 40 seconds.`2007-12-31T24:00:00 - 2007-02-20T10:48:18`

gives`P0Y0M314DT13H11M42S`

which is 314 days 13 hours 11 minutes 42 seconds.

As such, it’s possible to get a time difference between two points in time.

#### Forwarding and Backwarding Times

By adding a duration to a time, we get a forwarded time. By subtracting a duration from a time, we get a bacwarded time. Addition examples follow:

`0003-12-10 + P1Y1M1D`

gives`0005-01-11`

.`0003-12-10 + P13M9D`

gives`0005-01-19`

.`12:10:20 + PT1H1M1S`

gives`13:11:21`

.`12:10:20 + PT13H20M30S`

gives`01:30:50`

.`0003-12-10T12:10:20 + P1Y1M1DT1H1M1S`

gives`0005-01-11T13:11:21`

.`0003-12-10T12:10:20 + P1Y13M2DT13H20M30S`

gives`0006-01-13T01:30:50`

.

Subtraction examples follow:

`0003-12-10 - P1Y1M1D`

gives`0002-11-09`

.`0003-12-10 - P13M9D`

gives`0002-11-01`

.`12:10:20 - PT1H1M1S`

gives`11:09:19`

.`12:10:20 - PT13H20M30S`

gives`22:49:50`

.`0003-12-10T12:10:20 - P1Y1M1DT1H1M1S`

gives`0002-11-09T11:09:19`

.`0003-12-10T12:10:20 - P1Y13M2DT13H20M30S`

gives`0001-11-07T22:49:50`

.

Some rule examples are as follows.

fact f is time(2002-10-10); fact g is time(2002-12-31); rule r is if time(?t1) and time(?t2) then result([?t1 - ?t2]); fact f is time(2002-10-10); fact g is duration(P10Y3M); rule r is if time(?t) and duration(?d) then result([?t - ?d]); fact f is time(12:30:00); fact g is duration(PT1H30M); rule r is if time(?t) and duration(?d) then result([?t + ?d]); fact f is time(2002-10-10T12:30:00); fact g is duration(P10DT1H30M); rule r is if time(?t) and duration(?d) then result([?t - ?d]);