blob: 26e98df2af2a91aea48872b4bcb71af2a1d24baf [file] [log] [blame]
Meki Cherkaouif441d3a2012-04-22 15:17:52 -07001/*
2 * @author: ucla-cs
3 * This class represents CCNTime Objects
4 */
5
6var CCNTime = function CCNTime(
Meki Cherkaouif3d8f692012-05-18 15:44:28 -07007
8 msec) {
Meki Cherkaouif441d3a2012-04-22 15:17:52 -07009
10
11
12
13 this.NANOS_MAX = 999877929;
14
Meki Cherkaouif3d8f692012-05-18 15:44:28 -070015 if(typeof msec =='object'){
16 this.setDateBinary(msec);
17 this.msec = msec;
18 this.msecHex = toHex(msec);
19 }
20 else if(typeof msec =='string'){
21
22 this.msec = toNumbers(msec);
23 this.setDateBinary(this.msec);
24 this.msecHex = msec;
25 }
26 else{
27 if(LOG>1) console.log('UNRECOGNIZED TYPE FOR TIME');
28 }
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070029};
30
31
32 /**
33 * Create a CCNTime
34 * @param timestamp source timestamp to initialize from, some precision will be lost
35 */
36
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070037
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070038 /**
39 * Create a CCNTime from its binary encoding
40 * @param binaryTime12 the binary representation of a CCNTime
41 */
42CCNTime.prototype.setDateBinary = function(
43 //byte []
44 binaryTime12) {
45
46
47 if ((null == binaryTime12) || (binaryTime12.length == 0)) {
48 throw new IllegalArgumentException("Invalid binary time!");
49 }
50
51
52 value = 0;
53 for(i = 0; i < binaryTime12.length; i++) {
54 value = value << 8;
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070055 b = (binaryTime12[i]) & 0xFF;
56 value |= b;
57 }
58
Meki Cherkaouif3d8f692012-05-18 15:44:28 -070059 this.date = value;
60 //this.date = new Date(value);
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070061
62};
63
64//byte[]
65CCNTime.prototype.toBinaryTime = function() {
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070066
Meki Cherkaouif3d8f692012-05-18 15:44:28 -070067 return this.msec; //unsignedLongToByteArray(this.date.getTime());
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070068
69}
Meki Cherkaouif3d8f692012-05-18 15:44:28 -070070/*
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070071unsignedLongToByteArray= function( value) {
72 if( 0 == value )
73 return [0];
74
75 if( 0 <= value && value <= 0x00FF ) {
76 //byte []
77 bb = new Array[1];
78 bb[0] = (value & 0x00FF);
79 return bb;
80 }
81
82
83 //byte []
84 out = null;
85 //int
86 offset = -1;
87 for(var i = 7; i >=0; --i) {
88 //byte
89 b = ((value >> (i * 8)) & 0xFF);
90 if( out == null && b != 0 ) {
91 out = new Array(i+1);//byte[i+1];
92 offset = i;
93 }
94 if( out != null )
95 out[ offset - i ] = b;
96 }
97 return out;
Meki Cherkaouif3d8f692012-05-18 15:44:28 -070098}*/
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070099