Stylus Studio XML Editor

Table of contents

Appendices

E Adding durations to dateTimes

Adding durations to dateTimes

Given a [dateTime] S and a [duration] D, this appendix specifies how to compute a [dateTime] E where E is the end of the time period with start S and duration D i.e. E = S + D. Such computations are used, for example, to determine whether a [dateTime] is within a specific time period. This appendix also addresses the addition of [duration]s to the datatypes [date], [gYearMonth], [gYear], [gDay] and [gMonth], which can be viewed as a set of [dateTime]s. In such cases, the addition is made to the first or starting [dateTime] in the set.

This is a logical explanation of the process. Actual implementations are free to optimize as long as they produce the same results. The calculation uses the notation S[year] to represent the year field of S, S[month] to represent the month field, and so on. It also depends on the following functions:

  • fQuotient(a, b) = the greatest integer less than or equal to a/b

    • fQuotient(-1,3) = -1

    • fQuotient(0,3)...fQuotient(2,3) = 0

    • fQuotient(3,3) = 1

    • fQuotient(3.123,3) = 1

  • modulo(a, b) = a - fQuotient(a,b)*b

    • modulo(-1,3) = 2

    • modulo(0,3)...modulo(2,3) = 0...2

    • modulo(3,3) = 0

    • modulo(3.123,3) = 0.123

  • fQuotient(a, low, high) = fQuotient(a - low, high - low)

    • fQuotient(0, 1, 13) = -1

    • fQuotient(1, 1, 13) ... fQuotient(12, 1, 13) = 0

    • fQuotient(13, 1, 13) = 1

    • fQuotient(13.123, 1, 13) = 1

  • modulo(a, low, high) = modulo(a - low, high - low) + low

    • modulo(0, 1, 13) = 12

    • modulo(1, 1, 13) ... modulo(12, 1, 13) = 1...12

    • modulo(13, 1, 13) = 1

    • modulo(13.123, 1, 13) = 1.123

  • maximumDayInMonthFor(yearValue, monthValue) =

    • M := modulo(monthValue, 1, 13)

    • Y := yearValue + fQuotient(monthValue, 1, 13)

    • Return a value based on M and Y:

1
background-color:#FFFF991131 21M = January, March, May, July, August, October, or December
background-color:#FFFF991130 21M = April, June, September, or November
background-color:#FFFF991129 11M = February AND (modulo(Y, 400) = 0 OR (modulo(Y, 100) != 0) AND modulo(Y, 4) = 0)
background-color:#FFFF991128 11Otherwise