Meki Cherkaoui | 97e7a59 | 2012-04-14 02:50:06 -0700 | [diff] [blame^] | 1 | |
| 2 | var CCNTime = function CCNTime( |
| 3 | //long |
| 4 | msec) { |
| 5 | |
| 6 | |
| 7 | |
| 8 | //byte [] |
| 9 | //this.binarytime = null; |
| 10 | |
| 11 | |
| 12 | this.NANOS_MAX = 999877929; |
| 13 | |
| 14 | this.date = new Date(msec); |
| 15 | }; |
| 16 | |
| 17 | |
| 18 | //public CCNTime(long msec) { |
| 19 | //this((msec/1000) * 1000, (msec % 1000) * 1000000L); |
| 20 | //} |
| 21 | |
| 22 | /** |
| 23 | * Create a CCNTime |
| 24 | * @param timestamp source timestamp to initialize from, some precision will be lost |
| 25 | */ |
| 26 | //public CCNTime(Timestamp timestamp) { |
| 27 | //this(timestamp.getTime(), timestamp.getNanos()); |
| 28 | //} |
| 29 | |
| 30 | /** |
| 31 | * Create a CCNTime |
| 32 | * @param time source Date to initialize from, some precision will be lost |
| 33 | * as CCNTime does not round to unitary milliseconds |
| 34 | */ |
| 35 | CCNTime.prototype.setDate = function( |
| 36 | //Date |
| 37 | date) { |
| 38 | |
| 39 | this.date = date; |
| 40 | }; |
| 41 | |
| 42 | /** |
| 43 | * Create a CCNTime from its binary encoding |
| 44 | * @param binaryTime12 the binary representation of a CCNTime |
| 45 | */ |
| 46 | CCNTime.prototype.setDateBinary = function( |
| 47 | //byte [] |
| 48 | binaryTime12) { |
| 49 | |
| 50 | |
| 51 | if ((null == binaryTime12) || (binaryTime12.length == 0)) { |
| 52 | throw new IllegalArgumentException("Invalid binary time!"); |
| 53 | } |
| 54 | |
| 55 | |
| 56 | value = 0; |
| 57 | for(i = 0; i < binaryTime12.length; i++) { |
| 58 | value = value << 8; |
| 59 | // Java will assume the byte is signed, so extend it and trim it. |
| 60 | b = (binaryTime12[i]) & 0xFF; |
| 61 | value |= b; |
| 62 | } |
| 63 | |
| 64 | this.date = new Date(value); |
| 65 | |
| 66 | }; |
| 67 | |
| 68 | //byte[] |
| 69 | CCNTime.prototype.toBinaryTime = function() { |
| 70 | |
| 71 | |
| 72 | |
| 73 | return unsignedLongToByteArray(this.date.getTime()); |
| 74 | |
| 75 | } |
| 76 | |
| 77 | unsignedLongToByteArray= function( value) { |
| 78 | if( 0 == value ) |
| 79 | return [0]; |
| 80 | |
| 81 | if( 0 <= value && value <= 0x00FF ) { |
| 82 | //byte [] |
| 83 | bb = new Array[1]; |
| 84 | bb[0] = (value & 0x00FF); |
| 85 | return bb; |
| 86 | } |
| 87 | |
| 88 | |
| 89 | //byte [] |
| 90 | out = null; |
| 91 | //int |
| 92 | offset = -1; |
| 93 | for(var i = 7; i >=0; --i) { |
| 94 | //byte |
| 95 | b = ((value >> (i * 8)) & 0xFF); |
| 96 | if( out == null && b != 0 ) { |
| 97 | out = new Array(i+1);//byte[i+1]; |
| 98 | offset = i; |
| 99 | } |
| 100 | if( out != null ) |
| 101 | out[ offset - i ] = b; |
| 102 | } |
| 103 | return out; |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * Generate the binary representation of a CCNTime |
| 108 | * @return the binary representation we use for encoding |
| 109 | */ |
| 110 | //byte [] |
| 111 | /*CCCCNTime.prototype.toBinaryTime = function() { |
| 112 | |
| 113 | if( null == _binarytime ) { |
| 114 | byte [] b = DataUtils.unsignedLongToByteArray(toBinaryTimeAsLong()); |
| 115 | _binarytime = b; |
| 116 | } |
| 117 | return _binarytime; |
| 118 | };*/ |
| 119 | |
| 120 | /** |
| 121 | * Generate the internal long representation of a CCNTime, useful for comparisons |
| 122 | * and used internally |
| 123 | * @return the long representation of this time in our internal units |
| 124 | */ |
| 125 | //public long toBinaryTimeAsLong() { |
| 126 | //return toBinaryTimeAsLong(getTime(), getNanos()); |
| 127 | //} |
| 128 | |
| 129 | /** |
| 130 | * Static method to convert from milliseconds and nanoseconds to our |
| 131 | * internal long representation. |
| 132 | * Assumes that nanos also contains the integral milliseconds for this |
| 133 | * time. Ignores msec component in msec. |
| 134 | * @param msec milliseconds |
| 135 | * @param nanos nanoseconds |
| 136 | * @return |
| 137 | */ |
| 138 | //public static long toBinaryTimeAsLong(long msec, long nanos) { |
| 139 | //long timeVal = (msec / 1000) * 4096L + (nanos * 4096L + 500000000L) / 1000000000L; |
| 140 | //return timeVal; |
| 141 | //} |
| 142 | |
| 143 | /*protected void setFromBinaryTimeAsLong(long binaryTimeAsLong) { |
| 144 | _binarytime = null; |
| 145 | super.setTime((binaryTimeAsLong / 4096L) * 1000L); |
| 146 | super.setNanos((int)(((binaryTimeAsLong % 4096L) * 1000000000L) / 4096L)); |
| 147 | }*/ |
| 148 | |
| 149 | /*@Override |
| 150 | public void setTime(long msec) { |
| 151 | _binarytime = null; |
| 152 | long binaryTimeAsLong = toBinaryTimeAsLong((msec/1000) * 1000, (msec % 1000) * 1000000L); |
| 153 | super.setTime((binaryTimeAsLong / 4096L) * 1000L); |
| 154 | super.setNanos((int)(((binaryTimeAsLong % 4096L) * 1000000000L) / 4096L)); |
| 155 | }*/ |
| 156 | |
| 157 | /*@Override |
| 158 | public void setNanos(int nanos) { |
| 159 | _binarytime = null; |
| 160 | int quantizedNanos = (int)(((((nanos * 4096L + 500000000L) / 1000000000L)) * 1000000000L) / 4096L); |
| 161 | if ((quantizedNanos < 0) || (quantizedNanos > 999999999)) { |
| 162 | System.out.println("Quantizing nanos " + nanos + " resulted in out of range value " + quantizedNanos + "!"); |
| 163 | } |
| 164 | super.setNanos(quantizedNanos); |
| 165 | }*/ |
| 166 | |
| 167 | /*public void addNanos(int nanos) { |
| 168 | _binarytime = null; |
| 169 | setNanos(nanos + getNanos()); |
| 170 | }*/ |
| 171 | |
| 172 | /* |
| 173 | public void increment(int timeUnits) { |
| 174 | _binarytime = null; |
| 175 | long binaryTimeAsLong = toBinaryTimeAsLong(); |
| 176 | binaryTimeAsLong += timeUnits; |
| 177 | setFromBinaryTimeAsLong(binaryTimeAsLong); |
| 178 | } |
| 179 | |
| 180 | @Override |
| 181 | public boolean equals(Timestamp ts) { |
| 182 | return super.equals(new CCNTime(ts)); |
| 183 | } |
| 184 | |
| 185 | @Override |
| 186 | public int compareTo(Date o) { |
| 187 | return super.compareTo(new CCNTime(o)); |
| 188 | } |
| 189 | |
| 190 | @Override |
| 191 | public int compareTo(Timestamp ts) { |
| 192 | return super.compareTo(new CCNTime(ts)); |
| 193 | } |
| 194 | |
| 195 | @Override |
| 196 | public boolean before(Timestamp ts) { |
| 197 | return super.before(new CCNTime(ts)); |
| 198 | } |
| 199 | |
| 200 | @Override |
| 201 | public boolean after(Timestamp ts) { |
| 202 | return super.after(new CCNTime(ts)); |
| 203 | } |
| 204 | |
| 205 | @Override |
| 206 | public boolean before(Date when) { |
| 207 | return super.before(new CCNTime(when)); |
| 208 | } |
| 209 | |
| 210 | @Override |
| 211 | public boolean after(Date when) { |
| 212 | return super.after(new CCNTime(when)); |
| 213 | } |
| 214 | |
| 215 | |
| 216 | public static CCNTime now() { |
| 217 | return new CCNTime(); |
| 218 | } |
| 219 | |
| 220 | |
| 221 | public String toShortString() { |
| 222 | // use . instead of : as URI printer will make it look nicer in the logs |
| 223 | SimpleDateFormat df = new SimpleDateFormat("yy-MM-dd-HH.mm.ss"); |
| 224 | return df.format(this); |
| 225 | } |
| 226 | }*/ |