Thanks to visit codestin.com
Credit goes to Github.com

Skip to content

datetime: fix parse and use of %s (epoch)#12146

Open
tmr-g wants to merge 1 commit intotarantool:masterfrom
tmr-g:i12143
Open

datetime: fix parse and use of %s (epoch)#12146
tmr-g wants to merge 1 commit intotarantool:masterfrom
tmr-g:i12143

Conversation

@tmr-g
Copy link
Contributor

@tmr-g tmr-g commented Dec 11, 2025

  • The epoch seconds value parsed with '%s' by datetime.parse() isn't ignored and used now as a timestamp value by datetime.new().
  • The zero value doesn't produce an error.

Closes #12143

NO_DOC=bugfix

@coveralls
Copy link

coveralls commented Dec 12, 2025

Coverage Status

coverage: 87.59% (-0.008%) from 87.598%
when pulling 7049aa3 on tmr-g:i12143
into 033540c
on tarantool:master
.

tmr-g added a commit to tmr-g/tarantool that referenced this pull request Dec 16, 2025
Missing test suit for invalid strptime-format strings added
to lua and c unit tests. Some invalid cases added also.

The suit is need for this (tarantool#12060) and other existing PRs.
For example, tarantool#12146 awaits it.

Part of tarantool#12060

NO_CHANGELOG=test
NO_DOC=test
tmr-g added a commit to tmr-g/tarantool that referenced this pull request Dec 17, 2025
Missing test suit for invalid strptime-format strings added
to lua and c unit tests. Some invalid cases added also.

The suit is need for this (tarantool#12060) and other existing PRs.
For example, tarantool#12146 awaits it.

NO_CHANGELOG=test
NO_DOC=test
@tmr-g tmr-g force-pushed the i12143 branch 4 times, most recently from 35c7838 to 32b36d9 Compare December 18, 2025 22:06
@tmr-g tmr-g marked this pull request as ready for review December 18, 2025 22:08
@tmr-g tmr-g requested a review from a team as a code owner December 18, 2025 22:08
@tmr-g tmr-g force-pushed the i12143 branch 2 times, most recently from 1da129e to 567160b Compare December 20, 2025 22:48
@tmr-g tmr-g requested a review from ligurio December 25, 2025 10:25
@tmr-g tmr-g force-pushed the i12143 branch 2 times, most recently from db9346d to b180431 Compare December 28, 2025 23:39
## bugfix/datetime

* The epoch seconds value parsed with `'%s'`
by `datetime.parse()` isn't ignored and used
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
by `datetime.parse()` isn't ignored and used
by `datetime.parse()` isn't ignored and is used

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Comment on lines 37 to 40
local MIN_EPOCH_SECS_VALUE = INT_MIN * -- MIN_DT_DAY_VALUE
SECS_PER_DAY - SECS_EPOCH_OFFSET
local MAX_EPOCH_SECS_VALUE = INT_MAX * -- MAX_DT_DAY_VALUE
SECS_PER_DAY - SECS_EPOCH_OFFSET
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

comments before a line please

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Replace comments by "constants".

Comment on lines 2773 to 2788
-- mix of timestamp with y/m/d
buf = '26-08-2024 1724630400',
fmt = '%d-%m-%Y %s',
},
{
-- mix of timestamp with h/m/s
buf = '123 12:12:12',
fmt = '%d-%m-%Y %s',
},
{
-- timestamp < MIN_EPOCH_SECS_VALUE
buf = string.format('%d', MIN_EPOCH_SECS_VALUE - 1),
fmt = '%s',
},
{
-- timestamp > MAX_EPOCH_SECS_VALUE
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

comment started uppercase and the dot at the end

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

bool use_timestamp = (dt == 0) &&
(tm->tm_hour | tm->tm_min | tm->tm_sec) == 0;
if (use_timestamp) {
local_secs = tm->tm_epoch;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can set it by default and leave a single branch

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems, that variant will be less clear for a reader. Using the timestamp is not a default case.


const int ymd_flags = FLAG_YEAR | FLAG_MONTH | FLAG_MDAY;
if ((flags & FLAG_EPOCH) == 0)
;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

leave a comment here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

test:test("Parse invalid string with a custom format", function(test)
local formats = {
{
-- mix of timestamp with y/m/d
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

first letter in uppercase and dot at the end.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

-- %s is replaced by the number of seconds since the
-- Epoch, UTC (see mktime(3)).
{
buf = '26-08-2024 1724630400',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why it is removed?

before the patch:

tarantool> dt.parse('26-08-2024 1724630400', { format = '%d-%m-%Y %s' })
---
- 2024-08-26T00:00:00Z
- 21
...

after the patch:

tarantool> dt.parse('26-08-2024 1724630400', { format = '%d-%m-%Y %s' })
---
- error: 'builtin/datetime.lua:897: could not parse ''26-08-2024 1724630400'' using
    ''%d-%m-%Y %s'' format'

@tmr-g tmr-g force-pushed the i12143 branch 3 times, most recently from 4202905 to 5693757 Compare January 22, 2026 17:42
- The epoch seconds value parsed with `'%s'`
  by `datetime.parse()` isn't ignored and used
  now as a timestamp value by `datetime.new()`.
- The zero value doesn't produce an error.

Closes tarantool#12143

NO_DOC=bugfix
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

strptime like parse: %s is ignored

4 participants