Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- {-# STDLIB_VERSION 3 #-}
- {-# SCRIPT_TYPE ACCOUNT #-}
- {-# CONTENT_TYPE DAPP #-}
- # Waves RIDE Example: Unix Timestamp to Date/Time String
- #
- # https://waves-dapp.com/3PBfofqWr6bxiEyZ26DySjaLAsPudzeeHEW
- # https://wavesexplorer.com/tx/3j9PcgL2zam9DPMYPCShepdzLzRM9AH7kMWSHxtPiTUL
- # https://wavesexplorer.com/address/3PBfofqWr6bxiEyZ26DySjaLAsPudzeeHEW/data
- # Copyright © 2020 John Silver aka. Джон Ведьмолов, <https://t.me/dex_investments>.
- #
- # This program is free software: you can redistribute it and/or modify
- # it under the terms of the GNU Affero General Public License as
- # published by the Free Software Foundation, either version 3 of the
- # License, or (at your option) any later version.
- #
- # This program is distributed in the hope that it will be useful,
- # but WITHOUT ANY WARRANTY; without even the implied warranty of
- # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- # GNU Affero General Public License for more details.
- #
- # You should have received a copy of the GNU Affero General Public License
- # along with this program. If not, see <https://www.gnu.org/licenses/>.
- # The function converts the Unix timestamp to a date of the Gregorian calendar.
- # rus: Функция преобразует метку времени Unix в дату григорианского календаря.
- func toDateTimeString(timestamp: Int) = {
- let sp = " " # delimiter (default: "T")
- let zz = " UTC" # zone designator (default: "Z")
- # Days until the end of the months for normal and leap years.
- let norm = [0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365]
- let leap = [0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366]
- if (timestamp < -12212553600000 || timestamp > 253402300799999) then
- # rus: ISO 8601: допускаются только годы с 1583 по 9999.
- throw("ISO 8601: only years from 1583 to 9999 are permitted.")
- else {
- # test0: timestamp = 946684799999 => ce = 63113903999999
- # test1: timestamp = 946684800000 => ce = 63113904000000
- # min timestamp: -62009366400000 (0005-01-01T00:00:00.000Z)
- # note: In fact, 0 and 4 years (1 BC and 4 AD) aren't a leap years.
- let epoch = (1969 * 365) + (1969 / 4) - (1969 / 100) + (1969 / 400) + 366
- let ce = timestamp + (epoch * 86400 * 1000) # new era with zero compensation
- let date = {
- let yy = ce / (31556952 * 1000) - 1 # 1998 | 1999 years
- # abbr: dty - new era days to a year, ytd - year-to-date days
- let dty = (yy * 365) + (yy / 4) - (yy / 100) + (yy / 400) + 366
- let ytd = ce / (86400 * 1000) - dty # 730484 - 730120 = 364 | 730485 => 0 days
- let months = if (
- (yy + 1) % 4 == 0 && (yy + 1) % 100 != 0 || (yy + 1) % 400 == 0
- ) then leap else norm
- let mm =
- if (ytd >= months[12]) then throw("Month overflow error!")
- else if (ytd >= months[11]) then 11
- else if (ytd >= months[10]) then 10
- else if (ytd >= months[9]) then 9
- else if (ytd >= months[8]) then 8
- else if (ytd >= months[7]) then 7
- else if (ytd >= months[6]) then 6
- else if (ytd >= months[5]) then 5
- else if (ytd >= months[4]) then 4
- else if (ytd >= months[3]) then 3
- else if (ytd >= months[2]) then 2
- else if (ytd >= months[1]) then 1
- else if (ytd >= months[0]) then 0
- else throw("Month overflow error!")
- let dd = ytd - months[mm] # 30 | 0 days
- toString(yy + 1) + "-" + toString(mm + 101).drop(1) + "-" + toString(dd + 101).drop(1)
- }
- let time = {
- let rd = ce % (86400 * 1000) # mod: 86399999 | 0
- let hh = rd / (3600 * 1000) # 23 | 0 hours
- let rh = rd % (3600 * 1000) # mod: 3599999 | 0
- let mm = rh / (60 * 1000) # 59 | 0 minutes
- let rm = rh % (60 * 1000) # mod: 59999 | 0
- let ss = rm / (1000) # 59 | 0 seconds
- let ms = rm % (1000) # 999 | 0 milliseconds
- toString(hh + 100).drop(1) + ":" + toString(mm + 100).drop(1)
- + ":" + toString(ss + 100).drop(1) #+ "." + toString(ms + 1000).drop(1)
- }
- date + sp + time + zz
- }
- }
- @Callable(inv)
- func setDateTime(keyName: String, timestamp: Int) = {
- if (inv.caller != this) then
- # Display data as an error (test output) if the sender isn't the owner.
- # rus: Отображать данные как ошибку, если отправитель не является владельцем.
- throw(keyName + ": " + toDateTimeString(timestamp))
- else
- WriteSet([
- DataEntry(keyName, toDateTimeString(timestamp).take(16) + " UTC")
- ])
- }
- @Verifier(tx)
- func verify() = sigVerify(tx.bodyBytes, tx.proofs[0], tx.senderPublicKey)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement