blob: e4c8afdbad95659fa1dc96b9fc218a8ccda7db66 [file] [log] [blame]
Wentao Shangbd63e462012-12-03 16:19:33 -08001/**
Jeff Thompson146d7de2012-11-17 16:15:28 -08002 * @author: Meki Cheraoui
Jeff Thompson745026e2012-10-13 12:49:20 -07003 * See COPYING for copyright and distribution information.
Meki Cherkaouif441d3a2012-04-22 15:17:52 -07004 * This class represents CCNTime Objects
5 */
6
7var CCNTime = function CCNTime(
Meki Cherkaouif3d8f692012-05-18 15:44:28 -07008
Meki Cherkaoui8f173612012-06-06 01:05:40 -07009 input) {
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070010
11
12
13
14 this.NANOS_MAX = 999877929;
15
Meki Cherkaoui8f173612012-06-06 01:05:40 -070016 /*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 Cherkaouif3d8f692012-05-18 15:44:28 -070024 }
25 else{
26 if(LOG>1) console.log('UNRECOGNIZED TYPE FOR TIME');
27 }
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070028};
29
30
Meki Cherkaoui8f173612012-06-06 01:05:40 -070031CCNTime.prototype.getJavascriptDate = function(){
32 var d = new Date();
33 d.setTime( this.msec );
34 return d
35};
36
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070037 /**
38 * Create a CCNTime
39 * @param timestamp source timestamp to initialize from, some precision will be lost
40 */
41
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070042
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070043 /**
44 * Create a CCNTime from its binary encoding
45 * @param binaryTime12 the binary representation of a CCNTime
46 */
Meki Cherkaoui8f173612012-06-06 01:05:40 -070047/*CCNTime.prototype.setDateBinary = function(
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070048 //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 Cherkaouif441d3a2012-04-22 15:17:52 -070060 b = (binaryTime12[i]) & 0xFF;
61 value |= b;
62 }
Meki Cherkaoui8f173612012-06-06 01:05:40 -070063
Meki Cherkaouif3d8f692012-05-18 15:44:28 -070064 //this.date = new Date(value);
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070065
66};
67
68//byte[]
69CCNTime.prototype.toBinaryTime = function() {
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070070
Meki Cherkaouif3d8f692012-05-18 15:44:28 -070071 return this.msec; //unsignedLongToByteArray(this.date.getTime());
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070072
Meki Cherkaoui8f173612012-06-06 01:05:40 -070073}*/
Meki Cherkaouif3d8f692012-05-18 15:44:28 -070074/*
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070075unsignedLongToByteArray= 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 Cherkaouif3d8f692012-05-18 15:44:28 -0700102}*/
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700103