github.com/igggame/nebulas-go@v2.1.0+incompatible/nf/nvm/v8/lib/1.0.0/date.js (about) 1 // Copyright (C) 2018 go-nebulas authors 2 // 3 // This file is part of the go-nebulas library. 4 // 5 // the go-nebulas library is free software: you can redistribute it and/or modify 6 // it under the terms of the GNU General Public License as published by 7 // the Free Software Foundation, either version 3 of the License, or 8 // (at your option) any later version. 9 // 10 // the go-nebulas library is distributed in the hope that it will be useful, 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 // GNU General Public License for more details. 14 // 15 // You should have received a copy of the GNU General Public License 16 // along with the go-nebulas library. If not, see <http://www.gnu.org/licenses/>. 17 // 18 19 20 var NebDate = (function(ProtoDate) { 21 // compatibility 22 var Date = function() { 23 throw new Error("Date is not allowed in nvm."); 24 } 25 26 function allow() { 27 return Blockchain.block.seed != null && typeof(Blockchain.block.seed) !== 'undefined'; 28 } 29 30 function NebDate() { 31 if (!Blockchain) { 32 throw new Error("'Blockchain' is not defined."); 33 } 34 if (!Blockchain.block) { 35 throw new Error("'Blockchain.block' is not defined."); 36 } 37 if (!allow()) { 38 throw new Error("Date is not allowed in nvm."); 39 } 40 41 var date = new(Function.prototype.bind.apply(ProtoDate, [ProtoDate].concat(Array.prototype.slice.call(arguments))))(); 42 if (arguments.length == 0) { 43 // unit of timestamp is second 44 date.setTime(Blockchain.block.timestamp * 1000); 45 } 46 Object.setPrototypeOf(date, NebDate.prototype); 47 return date; 48 } 49 NebDate.now = function() { 50 if (!allow()) { 51 Date.now(); 52 } 53 return new NebDate().getTime(); 54 } 55 NebDate.UTC = function() { 56 if (!allow()) { 57 Date.UTC(); 58 } 59 return ProtoDate.UTC.apply(null, arguments); 60 } 61 NebDate.parse = function(dateString) { 62 if (!allow()) { 63 Date.parse(dateString); 64 } 65 return ProtoDate.parse(dateString); 66 } 67 68 NebDate.prototype.getTimezoneOffset = function() { 69 throw new Error("Unsupported method!"); 70 } 71 NebDate.prototype.getDate = function() { 72 return this.getUTCDate(); 73 } 74 NebDate.prototype.getDay = function() { 75 return this.getUTCDay(); 76 } 77 NebDate.prototype.getFullYear = function() { 78 return this.getUTCFullYear(); 79 } 80 NebDate.prototype.getHours = function() { 81 return this.getUTCHours(); 82 } 83 NebDate.prototype.getMilliseconds = function() { 84 return this.getUTCMilliseconds(); 85 } 86 NebDate.prototype.getMinutes = function() { 87 return this.getUTCMinutes(); 88 } 89 NebDate.prototype.getMonth = function() { 90 return this.getUTCMonth(); 91 } 92 NebDate.prototype.getSeconds = function() { 93 return this.getUTCSeconds(); 94 }, 95 NebDate.prototype.getYear = function() { 96 throw new Error("Deprecated!"); 97 } 98 NebDate.prototype.setYear = function() { 99 throw new Error("Deprecated!"); 100 } 101 NebDate.prototype.setDate = function() { 102 return this.setUTCDate.apply(this, arguments); 103 } 104 NebDate.prototype.setFullYear = function() { 105 return this.setUTCFullYear.apply(this, arguments); 106 } 107 NebDate.prototype.setHours = function() { 108 return this.setUTCHours.apply(this, arguments); 109 } 110 NebDate.prototype.setMilliseconds = function() { 111 return this.setUTCMilliseconds.apply(this, arguments); 112 } 113 NebDate.prototype.setMinutes = function() { 114 return this.setUTCMinutes.apply(this, arguments); 115 } 116 NebDate.prototype.setMonth = function() { 117 return this.setUTCMonth.apply(this, arguments); 118 } 119 NebDate.prototype.setSeconds = function() { 120 return this.setUTCSeconds.apply(this, arguments); 121 } 122 NebDate.prototype.toString = function() { 123 // return UTC string 124 return this.toUTCString.apply(this, arguments); 125 } 126 NebDate.prototype.toDateString = function() { 127 throw new Error("Unsupported method!"); 128 } 129 NebDate.prototype.toTimeString = function() { 130 throw new Error("Unsupported method!"); 131 } 132 133 NebDate.prototype = new Proxy(NebDate.prototype, { 134 getPrototypeOf: function(target) { 135 throw new Error("Unsupported method!"); 136 }, 137 }); 138 139 Object.setPrototypeOf(NebDate.prototype, ProtoDate.prototype); 140 return NebDate; 141 })(Date); 142 143 module.exports = NebDate;