Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 1 | /* |
| 2 | * @author: ucla-cs |
| 3 | * This class represents CCNTime Objects |
| 4 | */ |
| 5 | |
| 6 | var CCNTime = function CCNTime( |
Meki Cherkaoui | f3d8f69 | 2012-05-18 15:44:28 -0700 | [diff] [blame] | 7 | |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 8 | input) { |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | this.NANOS_MAX = 999877929; |
| 14 | |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 15 | /*if(typeof input =='object'){ |
| 16 | this.longDate = DataUtils.byteArrayToUnsignedLong(input); |
| 17 | this.binaryDate = input; |
| 18 | }*/ |
| 19 | if(typeof input =='number'){ |
| 20 | this.msec = input; |
| 21 | //this.binaryDate = DataUtils.unsignedLongToByteArray(input); |
| 22 | |
Meki Cherkaoui | f3d8f69 | 2012-05-18 15:44:28 -0700 | [diff] [blame] | 23 | } |
| 24 | else{ |
| 25 | if(LOG>1) console.log('UNRECOGNIZED TYPE FOR TIME'); |
| 26 | } |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 27 | }; |
| 28 | |
| 29 | |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 30 | CCNTime.prototype.getJavascriptDate = function(){ |
| 31 | var d = new Date(); |
| 32 | d.setTime( this.msec ); |
| 33 | return d |
| 34 | }; |
| 35 | |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 36 | /** |
| 37 | * Create a CCNTime |
| 38 | * @param timestamp source timestamp to initialize from, some precision will be lost |
| 39 | */ |
| 40 | |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 41 | |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 42 | /** |
| 43 | * Create a CCNTime from its binary encoding |
| 44 | * @param binaryTime12 the binary representation of a CCNTime |
| 45 | */ |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 46 | /*CCNTime.prototype.setDateBinary = function( |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 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; |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 59 | b = (binaryTime12[i]) & 0xFF; |
| 60 | value |= b; |
| 61 | } |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 62 | |
Meki Cherkaoui | f3d8f69 | 2012-05-18 15:44:28 -0700 | [diff] [blame] | 63 | //this.date = new Date(value); |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 64 | |
| 65 | }; |
| 66 | |
| 67 | //byte[] |
| 68 | CCNTime.prototype.toBinaryTime = function() { |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 69 | |
Meki Cherkaoui | f3d8f69 | 2012-05-18 15:44:28 -0700 | [diff] [blame] | 70 | return this.msec; //unsignedLongToByteArray(this.date.getTime()); |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 71 | |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 72 | }*/ |
Meki Cherkaoui | f3d8f69 | 2012-05-18 15:44:28 -0700 | [diff] [blame] | 73 | /* |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 74 | unsignedLongToByteArray= function( value) { |
| 75 | if( 0 == value ) |
| 76 | return [0]; |
| 77 | |
| 78 | if( 0 <= value && value <= 0x00FF ) { |
| 79 | //byte [] |
| 80 | bb = new Array[1]; |
| 81 | bb[0] = (value & 0x00FF); |
| 82 | return bb; |
| 83 | } |
| 84 | |
| 85 | |
| 86 | //byte [] |
| 87 | out = null; |
| 88 | //int |
| 89 | offset = -1; |
| 90 | for(var i = 7; i >=0; --i) { |
| 91 | //byte |
| 92 | b = ((value >> (i * 8)) & 0xFF); |
| 93 | if( out == null && b != 0 ) { |
| 94 | out = new Array(i+1);//byte[i+1]; |
| 95 | offset = i; |
| 96 | } |
| 97 | if( out != null ) |
| 98 | out[ offset - i ] = b; |
| 99 | } |
| 100 | return out; |
Meki Cherkaoui | f3d8f69 | 2012-05-18 15:44:28 -0700 | [diff] [blame] | 101 | }*/ |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 102 | |