blob: d4a8098841675d3a9ba7824b3489dc993efd2ac3 [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
Meki Cherkaoui8f173612012-06-06 01:05:40 -07008 input) {
Meki Cherkaouif441d3a2012-04-22 15:17:52 -07009
10
11
12
13 this.NANOS_MAX = 999877929;
14
Meki Cherkaoui8f173612012-06-06 01:05:40 -070015 /*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 Cherkaouif3d8f692012-05-18 15:44:28 -070023 }
24 else{
25 if(LOG>1) console.log('UNRECOGNIZED TYPE FOR TIME');
26 }
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070027};
28
29
Meki Cherkaoui8f173612012-06-06 01:05:40 -070030CCNTime.prototype.getJavascriptDate = function(){
31 var d = new Date();
32 d.setTime( this.msec );
33 return d
34};
35
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070036 /**
37 * Create a CCNTime
38 * @param timestamp source timestamp to initialize from, some precision will be lost
39 */
40
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070041
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070042 /**
43 * Create a CCNTime from its binary encoding
44 * @param binaryTime12 the binary representation of a CCNTime
45 */
Meki Cherkaoui8f173612012-06-06 01:05:40 -070046/*CCNTime.prototype.setDateBinary = function(
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070047 //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 Cherkaouif441d3a2012-04-22 15:17:52 -070059 b = (binaryTime12[i]) & 0xFF;
60 value |= b;
61 }
Meki Cherkaoui8f173612012-06-06 01:05:40 -070062
Meki Cherkaouif3d8f692012-05-18 15:44:28 -070063 //this.date = new Date(value);
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070064
65};
66
67//byte[]
68CCNTime.prototype.toBinaryTime = function() {
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070069
Meki Cherkaouif3d8f692012-05-18 15:44:28 -070070 return this.msec; //unsignedLongToByteArray(this.date.getTime());
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070071
Meki Cherkaoui8f173612012-06-06 01:05:40 -070072}*/
Meki Cherkaouif3d8f692012-05-18 15:44:28 -070073/*
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070074unsignedLongToByteArray= 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 Cherkaouif3d8f692012-05-18 15:44:28 -0700101}*/
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700102