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