Advertisement
dex_investments

Waves RIDE Example: Unix Timestamp to Date/Time String

Jan 29th, 2020
1,678
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
F# 5.05 KB | None | 0 0
  1. {-# STDLIB_VERSION 3 #-}
  2. {-# SCRIPT_TYPE ACCOUNT #-}
  3. {-# CONTENT_TYPE DAPP #-}
  4.  
  5. # Waves RIDE Example: Unix Timestamp to Date/Time String
  6. #
  7. # https://waves-dapp.com/3PBfofqWr6bxiEyZ26DySjaLAsPudzeeHEW
  8. # https://wavesexplorer.com/tx/3j9PcgL2zam9DPMYPCShepdzLzRM9AH7kMWSHxtPiTUL
  9. # https://wavesexplorer.com/address/3PBfofqWr6bxiEyZ26DySjaLAsPudzeeHEW/data
  10.  
  11. # Copyright © 2020 John Silver aka. Джон Ведьмолов, <https://t.me/dex_investments>.
  12. #
  13. # This program is free software: you can redistribute it and/or modify
  14. # it under the terms of the GNU Affero General Public License as
  15. # published by the Free Software Foundation, either version 3 of the
  16. # License, or (at your option) any later version.
  17. #
  18. # This program is distributed in the hope that it will be useful,
  19. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. # GNU Affero General Public License for more details.
  22. #
  23. # You should have received a copy of the GNU Affero General Public License
  24. # along with this program. If not, see <https://www.gnu.org/licenses/>.
  25.  
  26.  
  27. # The function converts the Unix timestamp to a date of the Gregorian calendar.
  28. # rus: Функция преобразует метку времени Unix в дату григорианского календаря.
  29. func toDateTimeString(timestamp: Int) = {
  30.     let sp = " "       # delimiter (default: "T")
  31.     let zz = " UTC"    # zone designator (default: "Z")
  32.  
  33.     # Days until the end of the months for normal and leap years.
  34.     let norm = [0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365]
  35.     let leap = [0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366]
  36.  
  37.     if (timestamp < -12212553600000 || timestamp > 253402300799999) then
  38.         # rus: ISO 8601: допускаются только годы с 1583 по 9999.
  39.         throw("ISO 8601: only years from 1583 to 9999 are permitted.")
  40.    
  41.     else {
  42.         # test0: timestamp = 946684799999 => ce = 63113903999999
  43.         # test1: timestamp = 946684800000 => ce = 63113904000000
  44.  
  45.         # min timestamp: -62009366400000 (0005-01-01T00:00:00.000Z)
  46.         # note: In fact, 0 and 4 years (1 BC and 4 AD) aren't a leap years.
  47.         let epoch = (1969 * 365) + (1969 / 4) - (1969 / 100) + (1969 / 400) + 366
  48.         let ce = timestamp + (epoch * 86400 * 1000)    # new era with zero compensation
  49.  
  50.         let date = {
  51.             let yy = ce / (31556952 * 1000) - 1    # 1998 | 1999 years
  52.  
  53.             # abbr: dty - new era days to a year, ytd - year-to-date days
  54.             let dty = (yy * 365) + (yy / 4) - (yy / 100) + (yy / 400) + 366
  55.             let ytd = ce / (86400 * 1000) - dty    # 730484 - 730120 = 364 | 730485 => 0 days
  56.             let months = if (
  57.                 (yy + 1) % 4 == 0 && (yy + 1) % 100 != 0 || (yy + 1) % 400 == 0
  58.             ) then leap else norm
  59.  
  60.             let mm =
  61.                 if (ytd >= months[12]) then throw("Month overflow error!")
  62.                 else if (ytd >= months[11]) then 11
  63.                 else if (ytd >= months[10]) then 10
  64.                 else if (ytd >= months[9]) then 9
  65.                 else if (ytd >= months[8]) then 8
  66.                 else if (ytd >= months[7]) then 7
  67.                 else if (ytd >= months[6]) then 6
  68.                 else if (ytd >= months[5]) then 5
  69.                 else if (ytd >= months[4]) then 4
  70.                 else if (ytd >= months[3]) then 3
  71.                 else if (ytd >= months[2]) then 2
  72.                 else if (ytd >= months[1]) then 1
  73.                 else if (ytd >= months[0]) then 0
  74.                 else throw("Month overflow error!")
  75.             let dd = ytd - months[mm]    # 30 | 0 days
  76.  
  77.             toString(yy + 1) + "-" + toString(mm + 101).drop(1) + "-" + toString(dd + 101).drop(1)
  78.         }
  79.  
  80.         let time = {
  81.             let rd = ce % (86400 * 1000)    # mod: 86399999 | 0
  82.  
  83.             let hh = rd / (3600 * 1000)     # 23 | 0 hours
  84.             let rh = rd % (3600 * 1000)     # mod: 3599999 | 0
  85.  
  86.             let mm = rh / (60 * 1000)       # 59 | 0 minutes
  87.             let rm = rh % (60 * 1000)       # mod: 59999 | 0
  88.  
  89.             let ss = rm / (1000)            # 59 | 0 seconds
  90.             let ms = rm % (1000)            # 999 | 0 milliseconds
  91.  
  92.             toString(hh + 100).drop(1) + ":" + toString(mm + 100).drop(1)
  93.             + ":" + toString(ss + 100).drop(1) #+ "." + toString(ms + 1000).drop(1)
  94.         }
  95.  
  96.         date + sp + time + zz
  97.     }
  98. }
  99.  
  100.  
  101. @Callable(inv)
  102. func setDateTime(keyName: String, timestamp: Int) = {
  103.     if (inv.caller != this) then
  104.         # Display data as an error (test output) if the sender isn't the owner.
  105.         # rus: Отображать данные как ошибку, если отправитель не является владельцем.
  106.         throw(keyName + ": " + toDateTimeString(timestamp))
  107.  
  108.     else
  109.         WriteSet([
  110.             DataEntry(keyName, toDateTimeString(timestamp).take(16) + " UTC")
  111.         ])
  112. }
  113.  
  114.  
  115. @Verifier(tx)
  116. func verify() = sigVerify(tx.bodyBytes, tx.proofs[0], tx.senderPublicKey)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement