Skip to content

Commit fbf4763

Browse files
committed
lua, datetime: unixtime, timestamp setters in datetime.lua
* implemented proper range checks for date attributes values; * created `.unixtime` attribute, which is alias to `.secs`, with corresponding setter/getter; * similarly to `unixtime`, created virtual `timestamp` attribute setter. Which is a convenient way to simultaneously assign unixtime (seconds since epoch) and nanoseconds Part of tarantool#5941
1 parent 79ddb27 commit fbf4763

File tree

1 file changed

+42
-29
lines changed

1 file changed

+42
-29
lines changed

src/lua/datetime.lua

Lines changed: 42 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -365,8 +365,10 @@ end
365365
-- described via table direction should be +1 or -1
366366
local function interval_increment(self, o, direction)
367367
assert(direction == -1 or direction == 1)
368-
check_date(self, "interval_increment(date, object, -+1)")
369-
assert(type(o) == 'table')
368+
check_date(self, "interval_increment(date, object, direction)")
369+
if type(o) ~= 'table' then
370+
error('interval_increment(date, object, direction) - object expected', 2)
371+
end
370372

371373
local ym_updated = false
372374
local dhms_updated = false
@@ -378,49 +380,43 @@ local function interval_increment(self, o, direction)
378380
for key, value in pairs(o) do
379381
local handlers = {
380382
years = function(v)
381-
assert(v > 0 and v < 10000)
383+
check_range(v, {0, 9999}, key)
382384
dt = cdt.dt_add_years(dt, direction * v, cdt.DT_LIMIT)
383385
ym_updated = true
384386
end,
385387

386388
months = function(v)
387-
assert(v > 0 and v < 13 )
389+
check_range(v, {0, 12}, key)
388390
dt = cdt.dt_add_months(dt, direction * v, cdt.DT_LIMIT)
389391
ym_updated = true
390392
end,
391393

392394
weeks = function(v)
393-
assert(v > 0 and v < 32)
395+
check_range(v, {0, 52}, key)
394396
secs = secs + direction * 7 * v * SECS_PER_DAY
395397
dhms_updated = true
396398
end,
397399

398400
days = function(v)
399-
assert(v > 0 and v < 32)
401+
check_range(v, {0, 31}, key)
400402
secs = secs + direction * v * SECS_PER_DAY
401403
dhms_updated = true
402404
end,
403405

404406
hours = function(v)
405-
assert(v >= 0 and v < 24)
407+
check_range(v, {0, 23}, key)
406408
secs = secs + direction * 60 * 60 * v
407409
dhms_updated = true
408410
end,
409411

410412
minutes = function(v)
411-
assert(v >= 0 and v < 60)
413+
check_range(v, {0, 59}, key)
412414
secs = secs + direction * 60 * v
413415
end,
414416

415417
seconds = function(v)
416-
assert(v >= 0 and v < 61)
417-
local s, frac
418-
frac = v % 1
419-
if frac > 0 then
420-
s = v - (v % 1)
421-
else
422-
s = v
423-
end
418+
check_range(v, {0, 60}, key)
419+
local s, frac = seconds_fraction(v)
424420
secs = secs + direction * s
425421
nsec = nsec + direction * frac * 1e9 -- convert fraction to nanoseconds
426422
dhms_updated = true
@@ -448,6 +444,9 @@ end
448444

449445
local datetime_index = function(self, key)
450446
local attributes = {
447+
unixtime = function(self)
448+
return self.secs
449+
end,
451450
timestamp = function(self)
452451
return self.secs + self.nsec / 1e9
453452
end,
@@ -486,6 +485,24 @@ local datetime_index = function(self, key)
486485
return attributes[key] ~= nil and attributes[key](self) or nil
487486
end
488487

488+
local function datetime_newindex(self, key, value)
489+
local attributes = {
490+
unixtime = function(self, value)
491+
self.secs = value
492+
self.nsec, self.offset = 0, 0
493+
end,
494+
timestamp = function(self, value)
495+
local secs, frac = seconds_fraction(value)
496+
self.secs = secs
497+
self.nsec = frac * 1e9
498+
self.offset = 0
499+
end,
500+
}
501+
if attributes[key] ~= nil then
502+
attributes[key](self, value)
503+
end
504+
end
505+
489506
local function datetime_new_raw(secs, nsec, offset)
490507
local dt_obj = ffi.new(datetime_t)
491508
dt_obj.secs = secs
@@ -537,50 +554,45 @@ local function datetime_new(o)
537554
end,
538555

539556
year = function(v)
540-
assert(v > 0 and v < 10000)
557+
check_range(v, {1, 9999}, key)
541558
y = v
542559
ymd = true
543560
end,
544561

545562
month = function(v)
546-
assert(v > 0 and v < 13 )
563+
check_range(v, {1, 12}, key)
547564
M = v
548565
ymd = true
549566
end,
550567

551568
day = function(v)
552-
assert(v > 0 and v < 32)
569+
check_range(v, {1, 31}, key)
553570
d = v
554571
ymd = true
555572
end,
556573

557574
hour = function(v)
558-
assert(v >= 0 and v < 24)
575+
check_range(v, {0, 23}, key)
559576
h = v
560577
hms = true
561578
end,
562579

563580
minute = function(v)
564-
assert(v >= 0 and v < 60)
581+
check_range(v, {0, 59}, key)
565582
m = v
566583
hms = true
567584
end,
568585

569586
second = function(v)
570-
assert(v >= 0 and v < 61)
571-
frac = v % 1
572-
if frac > 0 then
573-
s = v - (v % 1)
574-
else
575-
s = v
576-
end
587+
check_range(v, {0, 60}, key)
588+
s, frac = seconds_fraction(v)
577589
frac = frac * 1e9 -- convert fraction to nanoseconds
578590
hms = true
579591
end,
580592

581593
-- tz offset in minutes
582594
tz = function(v)
583-
assert(v >= 0 and v <= 720)
595+
check_range(v, {0, 720}, key)
584596
offset = v
585597
end
586598
}
@@ -937,6 +949,7 @@ local datetime_mt = {
937949
__sub = datetime_sub,
938950
__add = datetime_add,
939951
__index = datetime_index,
952+
__newindex = datetime_newindex,
940953
add = function(self, o)
941954
self = interval_increment(self, o, 1)
942955
return self

0 commit comments

Comments
 (0)