001: package org.bouncycastle.asn1.test;
002:
003: import org.bouncycastle.asn1.DERGeneralizedTime;
004: import org.bouncycastle.util.test.SimpleTest;
005:
006: import java.text.SimpleDateFormat;
007: import java.util.Date;
008: import java.util.SimpleTimeZone;
009: import java.util.TimeZone;
010:
011: /**
012: * X.690 test example
013: */
014: public class GeneralizedTimeTest extends SimpleTest {
015: String[] input = { "20020122122220", "20020122122220Z",
016: "20020122122220-1000", "20020122122220+00",
017: "20020122122220.1", "20020122122220.1Z",
018: "20020122122220.1-1000", "20020122122220.1+00",
019: "20020122122220.01", "20020122122220.01Z",
020: "20020122122220.01-1000", "20020122122220.01+00",
021: "20020122122220.001", "20020122122220.001Z",
022: "20020122122220.001-1000", "20020122122220.001+00",
023: "20020122122220.0001", "20020122122220.0001Z",
024: "20020122122220.0001-1000", "20020122122220.0001+00",
025: "20020122122220.0001+1000" };
026:
027: String[] output = { "20020122122220", "20020122122220GMT+00:00",
028: "20020122122220GMT-10:00", "20020122122220GMT+00:00",
029: "20020122122220.1", "20020122122220.1GMT+00:00",
030: "20020122122220.1GMT-10:00", "20020122122220.1GMT+00:00",
031: "20020122122220.01", "20020122122220.01GMT+00:00",
032: "20020122122220.01GMT-10:00", "20020122122220.01GMT+00:00",
033: "20020122122220.001", "20020122122220.001GMT+00:00",
034: "20020122122220.001GMT-10:00",
035: "20020122122220.001GMT+00:00", "20020122122220.0001",
036: "20020122122220.0001GMT+00:00",
037: "20020122122220.0001GMT-10:00",
038: "20020122122220.0001GMT+00:00",
039: "20020122122220.0001GMT+10:00" };
040:
041: String[] zOutput = { "20020122122220Z", "20020122122220Z",
042: "20020122222220Z", "20020122122220Z", "20020122122220Z",
043: "20020122122220Z", "20020122222220Z", "20020122122220Z",
044: "20020122122220Z", "20020122122220Z", "20020122222220Z",
045: "20020122122220Z", "20020122122220Z", "20020122122220Z",
046: "20020122222220Z", "20020122122220Z", "20020122122220Z",
047: "20020122122220Z", "20020122222220Z", "20020122122220Z",
048: "20020122022220Z" };
049:
050: public String getName() {
051: return "GeneralizedTime";
052: }
053:
054: public void performTest() throws Exception {
055: SimpleDateFormat dateF = new SimpleDateFormat(
056: "yyyyMMddHHmmss'Z'");
057:
058: dateF.setTimeZone(new SimpleTimeZone(0, "Z"));
059:
060: for (int i = 0; i != input.length; i++) {
061: DERGeneralizedTime t = new DERGeneralizedTime(input[i]);
062:
063: if (output[i].indexOf('G') > 0) // don't check local time the same way
064: {
065: if (!t.getTime().equals(output[i])) {
066: fail("failed conversion test");
067: }
068: if (!dateF.format(t.getDate()).equals(zOutput[i])) {
069: fail("failed date conversion test");
070: }
071: } else {
072: String offset = calculateGMTOffset(t.getDate());
073: if (!t.getTime().equals(output[i] + offset)) {
074: fail("failed conversion test");
075: }
076: }
077: }
078: }
079:
080: private String calculateGMTOffset(Date date) {
081: String sign = "+";
082: TimeZone timeZone = TimeZone.getDefault();
083: int offset = timeZone.getRawOffset();
084: if (offset < 0) {
085: sign = "-";
086: offset = -offset;
087: }
088: int hours = offset / (60 * 60 * 1000);
089: int minutes = (offset - (hours * 60 * 60 * 1000)) / (60 * 1000);
090:
091: if (timeZone.useDaylightTime() && timeZone.inDaylightTime(date)) {
092: hours += sign.equals("+") ? 1 : -1;
093: }
094:
095: return "GMT" + sign + convert(hours) + ":" + convert(minutes);
096: }
097:
098: private String convert(int time) {
099: if (time < 10) {
100: return "0" + time;
101: }
102:
103: return Integer.toString(time);
104: }
105:
106: public static void main(String[] args) {
107: runTest(new GeneralizedTimeTest());
108: }
109: }
|