|
| 1 | +.. _datetime_object: |
| 2 | + |
| 3 | +datetime_object |
| 4 | +=============== |
| 5 | + |
| 6 | +.. class:: datetime_object |
| 7 | + |
| 8 | + Since :doc:`2.10.0 </release/2.10.0>`. |
| 9 | + |
| 10 | + .. _datetime-totable: |
| 11 | + |
| 12 | + .. method:: totable() |
| 13 | + |
| 14 | + Convert the information from the ``datetime`` object into the table format. |
| 15 | + Resulting table has the following fields: |
| 16 | + |
| 17 | + .. container:: table |
| 18 | + |
| 19 | + .. list-table:: |
| 20 | + :widths: 30 70 |
| 21 | + :header-rows: 1 |
| 22 | + |
| 23 | + * - Field name |
| 24 | + - Description |
| 25 | + |
| 26 | + * - nsec |
| 27 | + - Nanosecods |
| 28 | + |
| 29 | + * - sec |
| 30 | + - Seconds |
| 31 | + |
| 32 | + * - min |
| 33 | + - Minutes |
| 34 | + |
| 35 | + * - hour |
| 36 | + - Hours |
| 37 | + |
| 38 | + * - day |
| 39 | + - Day number |
| 40 | + |
| 41 | + * - month |
| 42 | + - Month number |
| 43 | + |
| 44 | + * - year |
| 45 | + - Year |
| 46 | + |
| 47 | + * - wday |
| 48 | + - Days since the beginning of the week |
| 49 | + |
| 50 | + * - yday |
| 51 | + - Days since the beginning of the year |
| 52 | + |
| 53 | + * - isdst |
| 54 | + - Is the DST (Daylight saving time) applicable for the date. Boolean. |
| 55 | + |
| 56 | + * - tzoffset |
| 57 | + - Time zone offset from UTC |
| 58 | + |
| 59 | + :return: table with the date and time parameters |
| 60 | + :rtype: table |
| 61 | + |
| 62 | + **Example:** |
| 63 | + |
| 64 | + .. code-block:: tarantoolsession |
| 65 | +
|
| 66 | + tarantool> dt = datetime.new { |
| 67 | + sec = 20, |
| 68 | + min = 25, |
| 69 | + hour = 18, |
| 70 | +
|
| 71 | + day = 20, |
| 72 | + month = 8, |
| 73 | + year = 2021, |
| 74 | + } |
| 75 | + --- |
| 76 | + ... |
| 77 | +
|
| 78 | + tarantool> dt:totable() |
| 79 | + --- |
| 80 | + - sec: 20 |
| 81 | + min: 25 |
| 82 | + yday: 232 |
| 83 | + day: 20 |
| 84 | + nsec: 0 |
| 85 | + isdst: false |
| 86 | + wday: 6 |
| 87 | + tzoffset: 0 |
| 88 | + month: 8 |
| 89 | + year: 2021 |
| 90 | + hour: 18 |
| 91 | + ... |
| 92 | +
|
| 93 | + .. _datetime-format: |
| 94 | + |
| 95 | + .. method:: format( ['convensions'] ) |
| 96 | + |
| 97 | + 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. |
| 99 | + Additional convension for nanoseconds is `%f` which also allows a modifier to control the output precision of fractional part: `%5f` (see the example below). |
| 100 | + If no arguments are set for the method, the default convensions are used: `'%FT%T.%f%z'` (see the example below). |
| 101 | + |
| 102 | + :param string convensions: string consisting of zero or more conversion specifications and ordinary characters |
| 103 | + |
| 104 | + :return: string with the formatted date and time information |
| 105 | + :rtype: string |
| 106 | + |
| 107 | + **Example:** |
| 108 | + |
| 109 | + .. code-block:: tarantoolsession |
| 110 | +
|
| 111 | + tarantool> dt = datetime.new { |
| 112 | + nsec = 123456789, |
| 113 | +
|
| 114 | + sec = 20, |
| 115 | + min = 25, |
| 116 | + hour = 18, |
| 117 | +
|
| 118 | + day = 20, |
| 119 | + month = 8, |
| 120 | + year = 2021, |
| 121 | +
|
| 122 | + tzoffset = 180 |
| 123 | + } |
| 124 | + --- |
| 125 | + ... |
| 126 | +
|
| 127 | + tarantool> dt:format('%d.%m.%y %H:%M:%S.%5f') |
| 128 | + --- |
| 129 | + - 20.08.21 18:25:20.12345 |
| 130 | + ... |
| 131 | +
|
| 132 | + tarantool> dt:format() |
| 133 | + --- |
| 134 | + - 2021-08-20T18:25:20.123456789+0300 |
| 135 | + ... |
| 136 | +
|
| 137 | + tarantool> dt:format('%FT%T.%f%z') |
| 138 | + --- |
| 139 | + - 2021-08-20T18:25:20.123456789+0300 |
| 140 | + ... |
| 141 | +
|
| 142 | + .. _datetime-set: |
| 143 | + |
| 144 | + .. method:: set( [{ units }] ) |
| 145 | + |
| 146 | + Update the field values in the existing ``datetime`` object. |
| 147 | + |
| 148 | + :param table units: Table of time units. The :ref:`time units <datetime-new-args>` are the same as for the ``datetime.new()`` function. |
| 149 | + |
| 150 | + :return: updated datetime_object |
| 151 | + :rtype: cdata |
| 152 | + |
| 153 | + **Example:** |
| 154 | + |
| 155 | + .. code-block:: tarantoolsession |
| 156 | +
|
| 157 | + tarantool> dt = datetime.new { |
| 158 | + nsec = 123456789, |
| 159 | +
|
| 160 | + sec = 20, |
| 161 | + min = 25, |
| 162 | + hour = 18, |
| 163 | +
|
| 164 | + day = 20, |
| 165 | + month = 8, |
| 166 | + year = 2021, |
| 167 | +
|
| 168 | + tzoffset = 180 |
| 169 | + } |
| 170 | +
|
| 171 | + tarantool> dt:set {msec = 567} |
| 172 | + --- |
| 173 | + - 2021-08-20T18:25:20.567+0300 |
| 174 | + ... |
| 175 | +
|
| 176 | + tarantool> dt:set {tzoffset = 60} |
| 177 | + --- |
| 178 | + - 2021-08-20T18:25:20.567+0100 |
| 179 | + ... |
| 180 | +
|
| 181 | + .. _datetime-parse: |
| 182 | + |
| 183 | + .. method:: parse( 'input_string'[, {format, tzoffset} ] ) |
| 184 | + |
| 185 | + Convert an input string with the date and time information into a ``datetime`` object. |
| 186 | + The input string should be formatted according to one of the following standards: |
| 187 | + |
| 188 | + * ISO 8601 |
| 189 | + * RFC 3339 |
| 190 | + * extended `strftime <https://www.freebsd.org/cgi/man.cgi?query=strftime&sektion=3>`__ -- see description of the :ref:`format() <datetime-format>` for details. |
| 191 | + |
| 192 | + :param string input_string: string with the date and time information. |
| 193 | + :param string format: indicator of the input_sting format. Possible values: 'iso8601', 'rfc3339', or ``strptime``-like format string. |
| 194 | + If no value is set, the default formating is used. |
| 195 | + :param number tzoffset: time zone offset from UTC, in minutes. |
| 196 | + |
| 197 | + :return: a datetime_object |
| 198 | + :rtype: cdata |
| 199 | + |
| 200 | + **Example:** |
| 201 | + |
| 202 | + .. code-block:: tarantoolsession |
| 203 | +
|
| 204 | + tarantool> t = datetime.parse('1970-01-01T00:00:00Z') |
| 205 | +
|
| 206 | + tarantool> t |
| 207 | + --- |
| 208 | + - 1970-01-01T00:00:00Z |
| 209 | + ... |
| 210 | +
|
| 211 | + tarantool> t = datetime.parse('1970-01-01T00:00:00', {format = 'iso8601', tzoffset = 180}) |
| 212 | +
|
| 213 | + tarantool> t |
| 214 | + --- |
| 215 | + - 1970-01-01T00:00:00+0300 |
| 216 | + ... |
| 217 | +
|
| 218 | + tarantool> t = datetime.parse('2017-12-27T18:45:32.999999-05:00', {format = 'rfc3339'}) |
| 219 | +
|
| 220 | + tarantool> t |
| 221 | + --- |
| 222 | + - 2017-12-27T18:45:32.999999-0500 |
| 223 | + ... |
| 224 | +
|
| 225 | + tarantool> T = datetime.parse('Thu Jan 1 03:00:00 1970', {format = '%c'}) |
| 226 | +
|
| 227 | + tarantool> T |
| 228 | + --- |
| 229 | + - 1970-01-01T03:00:00Z |
| 230 | + ... |
| 231 | +
|
| 232 | + tarantool> T = datetime.parse('12/31/2020', {format = '%m/%d/%y'}) |
| 233 | +
|
| 234 | + tarantool> T |
| 235 | + --- |
| 236 | + - 2020-12-31T00:00:00Z |
| 237 | + ... |
| 238 | +
|
| 239 | + tarantool> T = datetime.parse('1970-01-01T03:00:00.125000000+0300', {format = '%FT%T.%f%z'}) |
| 240 | +
|
| 241 | + tarantool> T |
| 242 | + --- |
| 243 | + - 1970-01-01T03:00:00.125+0300 |
| 244 | + ... |
| 245 | +
|
0 commit comments