Skip to content

Commit 27e9dff

Browse files
committed
Update info about arithmetic operations with datetime and interal objects
Part of #2835
1 parent ba78d12 commit 27e9dff

File tree

5 files changed

+103
-6
lines changed

5 files changed

+103
-6
lines changed

doc/reference/reference_lua/datetime.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ Below is a list of the ``datetime`` module functions and methods.
4949
* - :ref:`interval_object:totable() <interval-totable>`
5050
- Convert the information from an ``interval`` object into the table format.
5151

52+
* - :doc:`./datetime/interval_arithm`
53+
- ... [TBD]
54+
5255
.. toctree::
5356
:hidden:
5457

doc/reference/reference_lua/datetime/datetime_object.rst

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,14 +92,14 @@ datetime_object
9292
9393
.. _datetime-format:
9494

95-
.. method:: format( ['convensions'] )
95+
.. method:: format( ['convensions'] ) [TBD]
9696

9797
Convert the standard ``datetime`` object presentation into a formatted string.
98-
The formatting convension specifications are the same as in the `strftime <https://www.freebsd.org/cgi/man.cgi?query=strftime&sektion=3>`__ library.
98+
The formatting convension specifications are the same as in the `strftime <https://www.freebsd.org/cgi/man.cgi?query=strftime&sektion=3>`__ library. [TBD]
9999
Additional convension for nanoseconds is `%f` which also allows a modifier to control the output precision of fractional part: `%5f` (see the example below).
100100
If no arguments are set for the method, the default convensions are used: `'%FT%T.%f%z'` (see the example below).
101101

102-
:param string convensions: string consisting of zero or more conversion specifications and ordinary characters
102+
:param string convensions: string consisting of zero or more conversion specifications and ordinary characters [TBD]
103103

104104
:return: string with the formatted date and time information
105105
:rtype: string
@@ -251,7 +251,7 @@ datetime_object
251251

252252
:param table input: an :ref:`interval object <interval-new>` or an equivalent table (see **Example #1**)
253253
:param string adjust: defines how to round days in a month after an arithmetic operation.
254-
Possible values: ``none``, ``last``, ``excess`` (see **Example #2**). Defaults to ``none``.
254+
Possible values: ``none``, ``last``, ``excess`` (see **Example #2**). Defaults to ``none``. [TBD]
255255

256256
:return: datetime_object
257257
:rtype: cdata
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
.. _inverval_arithm:
2+
3+
Inverval arithmetic
4+
===================
5+
6+
Since :doc:`2.10.0 </release/2.10.0>`.
7+
8+
The :doc:`datetime module </reference/reference_lua/datetime>` enables creating objects of two types: ``datetime`` and ``interval``.
9+
10+
If you need to shift the ``datetime`` object values, you can use either the modifier methods, that is, the :ref:`:add <datetime-add>` or :ref:`:sub <datetime-sub>` methods,
11+
or apply interval arithmetic using overloaded `+ (__add)` or `- (__sub)` methods.
12+
13+
``:add``/``:sub`` modify the current object, but ``+``/``+`` create copy of the object as the operation result.
14+
15+
In the interval operation, each of the interval subcomponents are sequentially calculated starting from the largest (``year``) to the smallest (``nsec``):
16+
17+
* year -- years
18+
* month -- months
19+
* week -- weeks
20+
* day -- days
21+
* hour -- hours
22+
* min -- minutes
23+
* sec -- seconds
24+
* nsec -- nanoseconds.
25+
26+
If results of the operation exceed the allowed range for any of the components, an exception is raised.
27+
28+
The ``datetime`` and ``interval`` objects can participate in arithmetic operations:
29+
30+
* The sum of two intervals is an interval object, which fields are sum of each particular component of operands.
31+
32+
* The result of subtraction of two intervals is similar: it's an interval object where each subcomponent is the result of subtraction of particular fields in the original operands.
33+
34+
* If you add datetime and interval objects, the result is a datetime object. Addition is being performed in a determined order from the largest component (``year``) to the smallest (``nsec``).
35+
36+
* Subtraction of two datetime objects produce an interval object. Difference of two time values is performed not as difference of the epoch seconds, but as difference of all the subcomponents, that is, years, months, days, hours, minutes, and seconds.
37+
38+
* An untyped table object can be used in each context where the typed datetime or interval objects are used if left operand is typed object with overloaded operation of ``+`` or ``-``.
39+
40+
The matrix of the ``addition`` operands eligibility and their result types:
41+
42+
.. container:: table
43+
44+
.. list-table::
45+
:widths: 25 25 25 25
46+
:header-rows: 1
47+
48+
* -
49+
- datetime
50+
- interval
51+
- table
52+
53+
* - **datetime**
54+
-
55+
- datetime
56+
- datetime
57+
58+
* - **interval**
59+
- datetime
60+
- interval
61+
- interval
62+
63+
* - **table**
64+
-
65+
-
66+
-
67+
68+
The matrix of the ``subtraction`` operands eligibility and their result types:
69+
70+
.. container:: table
71+
72+
.. list-table::
73+
:widths: 25 25 25 25
74+
:header-rows: 1
75+
76+
* -
77+
- datetime
78+
- interval
79+
- table
80+
81+
* - **datetime**
82+
- interval
83+
- datetime
84+
- datetime
85+
86+
* - **interval**
87+
-
88+
- interval
89+
- interval
90+
91+
* - **table**
92+
-
93+
-
94+
-

doc/reference/reference_lua/datetime/interval_new.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ datetime.interval.new()
1313
:param table input: Table with :ref:`time units and parameters<interval-new-args>`. For all possible time units, the values are not restricted.
1414
If an empty table or no arguments are passed, the ``interval`` object with the default value ``0 seconds`` is created.
1515

16-
:return: :doc: interval object
16+
:return: interval_object
1717
:rtype: cdata
1818

1919
.. _interval-new-args:

doc/reference/reference_lua/datetime/new.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ datetime.new()
1818

1919
.. _datetime-new-args:
2020

21-
**Possible input time units for ``datetime.new()**
21+
**Possible input time units for ``datetime.new()``**
2222

2323
.. container:: table
2424

0 commit comments

Comments
 (0)