Revision: 1752abe3f530
Author: lasithwaruna <[email protected]>
Date: Tue Jan 8 07:04:14 2013
Log: correct TimeImpl internal structure implementation
http://code.google.com/p/jss7/source/detail?r=1752abe3f530
Modified:
/map/map-api/src/main/java/org/mobicents/protocols/ss7/map/api/primitives/Time.java
/map/map-impl/src/main/java/org/mobicents/protocols/ss7/map/primitives/TimeImpl.java
/map/map-impl/src/test/java/org/mobicents/protocols/ss7/map/functional/MAPFunctionalTest.java
/map/map-impl/src/test/java/org/mobicents/protocols/ss7/map/primitives/TimeTest.java
=======================================
---
/map/map-api/src/main/java/org/mobicents/protocols/ss7/map/api/primitives/Time.java
Tue Jan 8 06:39:43 2013
+++
/map/map-api/src/main/java/org/mobicents/protocols/ss7/map/api/primitives/Time.java
Tue Jan 8 07:04:14 2013
@@ -51,16 +51,40 @@
public byte[] getData();
+ /**
+ * Returns the value in UTC
+ *
+ */
public int getYear();
+ /**
+ * Returns the value in UTC
+ *
+ */
public int getMonth();
+ /**
+ * Returns the value in UTC
+ *
+ */
public int getDay();
+ /**
+ * Returns the value in UTC
+ *
+ */
public int getHour();
+ /**
+ * Returns the value in UTC
+ *
+ */
public int getMinute();
+ /**
+ * Returns the value in UTC
+ *
+ */
public int getSecond();
}
=======================================
---
/map/map-impl/src/main/java/org/mobicents/protocols/ss7/map/primitives/TimeImpl.java
Tue Jan 8 06:39:43 2013
+++
/map/map-impl/src/main/java/org/mobicents/protocols/ss7/map/primitives/TimeImpl.java
Tue Jan 8 07:04:14 2013
@@ -21,8 +21,11 @@
*/
package org.mobicents.protocols.ss7.map.primitives;
-import java.sql.Date;
+
import java.util.Calendar;
+import java.util.Date;
+import java.util.TimeZone;
+import java.nio.ByteBuffer;
import org.mobicents.protocols.ss7.map.api.primitives.Time;
@@ -32,7 +35,7 @@
*
*/
public class TimeImpl extends OctetStringBase implements Time {
-
+
private static final long msbZero = 2085978496000L;
private static final long msbOne = -2208988800000L;
@@ -48,10 +51,11 @@
public TimeImpl(int year, int month, int day, int hour, int minute, int
second) {
super(4, 4, "Time");
- Calendar cal = Calendar.getInstance();
+ Calendar cal =
Calendar.getInstance(TimeZone.getTimeZone("UTC"));
cal.set(year, month - 1, day, hour, minute, second);
long ntpTime = getNtpTime(cal.getTimeInMillis());
- this.data = hexToBytes(Long.toHexString(ntpTime));
+ this.data = new byte[4];
+ System.arraycopy(longToBytes(ntpTime), 4, this.data, 0, 4);
}
@Override
@@ -61,60 +65,60 @@
@Override
public int getYear() {
- long time = Long.parseLong(bytesToHex(data),16);
+ long time = bytesToLong(this.data);
time = getTime(time);
Date d = new Date(time);
- Calendar cal = Calendar.getInstance();
+ Calendar cal =
Calendar.getInstance(TimeZone.getTimeZone("UTC"));
cal.setTime(d);
return cal.get(Calendar.YEAR);
}
@Override
public int getMonth() {
- long time = Long.parseLong(bytesToHex(data),16);
+ long time = bytesToLong(this.data);
time = getTime(time);
Date d = new Date(time);
- Calendar cal = Calendar.getInstance();
+ Calendar cal =
Calendar.getInstance(TimeZone.getTimeZone("UTC"));
cal.setTime(d);
return cal.get(Calendar.MONTH) + 1;
}
@Override
public int getDay() {
- long time = Long.parseLong(bytesToHex(data),16);
+ long time = bytesToLong(this.data);
time = getTime(time);
Date d = new Date(time);
- Calendar cal = Calendar.getInstance();
+ Calendar cal =
Calendar.getInstance(TimeZone.getTimeZone("UTC"));
cal.setTime(d);
return cal.get(Calendar.DATE);
}
@Override
public int getHour() {
- long time = Long.parseLong(bytesToHex(data),16);
+ long time = bytesToLong(this.data);
time = getTime(time);
Date d = new Date(time);
- Calendar cal = Calendar.getInstance();
+ Calendar cal =
Calendar.getInstance(TimeZone.getTimeZone("UTC"));
cal.setTime(d);
return cal.get(Calendar.HOUR_OF_DAY);
}
@Override
public int getMinute() {
- long time = Long.parseLong(bytesToHex(data),16);
+ long time = bytesToLong(this.data);
time = getTime(time);
Date d = new Date(time);
- Calendar cal = Calendar.getInstance();
+ Calendar cal =
Calendar.getInstance(TimeZone.getTimeZone("UTC"));
cal.setTime(d);
return cal.get(Calendar.MINUTE);
}
@Override
public int getSecond() {
- long time = Long.parseLong(bytesToHex(data),16);
+ long time = bytesToLong(this.data);
time = getTime(time);
Date d = new Date(time);
- Calendar cal = Calendar.getInstance();
+ Calendar cal =
Calendar.getInstance(TimeZone.getTimeZone("UTC"));
cal.setTime(d);
return cal.get(Calendar.SECOND);
}
@@ -144,30 +148,23 @@
} else {
return msbOne + (ntpTime * 1000);
}
- }
-
- private byte[] hexToBytes(String s) {
- int len = s.length();
- byte[] data = new byte[4];
- for (int i = 0; i < len; i += 2) {
- data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)
- + Character.digit(s.charAt(i+1), 16));
- }
- return data;
}
- private String bytesToHex(byte[] bytes) {
- final char[] hexArray =
{'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
- char[] hexChars = new char[bytes.length * 2];
- int v;
- for ( int j = 0; j < bytes.length; j++ ) {
- v = bytes[j] & 0xFF;
- hexChars[j * 2] = hexArray[v >>> 4];
- hexChars[j * 2 + 1] = hexArray[v & 0x0F];
- }
- return new String(hexChars);
+ public byte[] longToBytes(long x) {
+ ByteBuffer buffer = ByteBuffer.allocate(8);
+ buffer.putLong(x);
+ return buffer.array();
}
+ public long bytesToLong(byte[] bytes) {
+ ByteBuffer buffer = ByteBuffer.allocate(8);
+ byte[] eightbytes = new byte[8];
+ System.arraycopy(bytes, 0, eightbytes, 4, 4);
+ buffer.put(eightbytes);
+ buffer.flip();
+ return buffer.getLong();
+ }
+
@Override
public String toString() {
=======================================
---
/map/map-impl/src/test/java/org/mobicents/protocols/ss7/map/functional/MAPFunctionalTest.java
Sat Jan 5 00:50:38 2013
+++
/map/map-impl/src/test/java/org/mobicents/protocols/ss7/map/functional/MAPFunctionalTest.java
Tue Jan 8 07:04:14 2013
@@ -6416,16 +6416,10 @@
SendRoutingInformationResponseImpl ind
=(SendRoutingInformationResponseImpl)response;
IMSI imsi = ind.getIMSI();
- ExtendedRoutingInfo extRoutingInfo =
ind.getExtendedRoutingInfo();
- RoutingInfo routingInfo =
extRoutingInfo.getRoutingInfo();
- ISDNAddressString roamingNumber =
routingInfo.getRoamingNumber();
+ assertNull(ind.getExtendedRoutingInfo());
assertNotNull(imsi);
- assertEquals(imsi.getData(), "011220200198227");
- assertNotNull(roamingNumber);
- assertEquals(roamingNumber.getAddressNature(),
AddressNature.international_number);
- assertEquals(roamingNumber.getNumberingPlan(),
NumberingPlan.ISDN);
- assertEquals(roamingNumber.getAddress(),
"79273605819");
+ assertEquals(imsi.getData(), "011220200198227");
long mapProtocolVersion =
ind.getMapProtocolVersion();
assertEquals(mapProtocolVersion, 2);
=======================================
---
/map/map-impl/src/test/java/org/mobicents/protocols/ss7/map/primitives/TimeTest.java
Fri Dec 28 07:11:22 2012
+++
/map/map-impl/src/test/java/org/mobicents/protocols/ss7/map/primitives/TimeTest.java
Tue Jan 8 07:04:14 2013
@@ -39,11 +39,11 @@
public class TimeTest {
public byte[] getData() {
- return new byte[] {4, 4, -95, 16, -24, 70};
+ return new byte[] {4, 4, -95, 17, 53, -98};
};
public byte[] getData2() {
- return new byte[] {4, 4, 127, -2, -92, -58};
+ return new byte[] {4, 4, 127, -2, -14, 30};
}
@Test(groups = { "functional.decode", "primitives" })