directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From akaras...@apache.org
Subject svn commit: rev 55565 - in incubator/directory/ldap/trunk/common/src: java/org/apache/ldap/common/util test/org/apache/ldap/common/util
Date Tue, 26 Oct 2004 03:47:22 GMT
Author: akarasulu
Date: Mon Oct 25 20:47:22 2004
New Revision: 55565

Added:
   incubator/directory/ldap/trunk/common/src/java/org/apache/ldap/common/util/DateUtils.java
   incubator/directory/ldap/trunk/common/src/test/org/apache/ldap/common/util/
   incubator/directory/ldap/trunk/common/src/test/org/apache/ldap/common/util/DateUtilsTest.java
Log:
adding utility methods for generating generalizedTime strings

Added: incubator/directory/ldap/trunk/common/src/java/org/apache/ldap/common/util/DateUtils.java
==============================================================================
--- (empty file)
+++ incubator/directory/ldap/trunk/common/src/java/org/apache/ldap/common/util/DateUtils.java
Mon Oct 25 20:47:22 2004
@@ -0,0 +1,190 @@
+/*
+ *   Copyright 2004 The Apache Software Foundation
+ *
+ *   Licensed under the Apache License, Version 2.0 (the "License");
+ *   you may not use this file except in compliance with the License.
+ *   You may obtain a copy of the License at
+ *
+ *       http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *   Unless required by applicable law or agreed to in writing, software
+ *   distributed under the License is distributed on an "AS IS" BASIS,
+ *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *   See the License for the specific language governing permissions and
+ *   limitations under the License.
+ *
+ */
+package org.apache.ldap.common.util;
+
+
+import java.util.Calendar;
+
+
+/**
+ * Document this class.
+ *
+ * @author <a href="mailto:directory-dev@incubator.apache.org">Apache Directory Project</a>
+ * @version $Rev$
+ */
+public class DateUtils
+{
+    /**
+     * Gets the generalized time using the g-differential option rather than
+     * zulu time described by [<a href=
+     * "http://ietf.org/internet-drafts/draft-ietf-ldapbis-syntaxes-09.txt">
+     * SYNTAXES</a>] section 3.3.13.  We include this section below:
+     * <pre>
+     *
+     * 3.3.13.  Generalized Time
+     *
+     * A value of the Generalized Time syntax is a character string
+     * representing a date and time.  The LDAP-specific encoding of a value
+     * of this syntax is a restriction of the format defined in [ISO8601],
+     * and is described by the following ABNF:
+     *
+     * century = 2(%x30-39) ; "00" to "99"
+     * year    = 2(%x30-39) ; "00" to "99"
+     * month   =   ( %x30 %x31-39 ) ; "01" (January) to "09"
+     *           / ( %x31 %x30-32 ) ; "10" to "12"
+     * day     =   ( %x30 %x31-39 )    ; "01" to "09"
+     *           / ( %x31-32 %x30-39 ) ; "10" to "29"
+     *           / ( %x33 %x30-31 )    ; "30" to "31"
+     * hour    = ( %x30-31 %x30-39 ) / ( %x32 %x30-33 ) ; "00" to "23"
+     * minute  = %x30-35 %x30-39                        ; "00" to "59"
+     * second  =   ( %x30-35 %x30-39 )  ; "00" to "59"
+     *           / ( %x36 %x30 )        ; "60" (a leap second)
+     *
+     * GeneralizedTime = century year month day hour
+     *                      [ minute [ second ] ] [ fraction ]
+     *                      g-time-zone
+     * fraction        = ( DOT / COMMA ) 1*(%x30-39)
+     * g-time-zone     = %x5A  ; "Z"
+     *                   / g-differential
+     * g-differential  = ( MINUS / PLUS ) hour [ minute ]
+     * MINUS           = %x2D  ; minus sign ("-")
+     *
+     * The <DOT>, <COMMA> and <PLUS> rules are defined in [MODELS].
+     *
+     * The time value represents coordinated universal time (equivalent to
+     * Greenwich Mean Time) if the "Z" form of <g-time-zone> is used,
+     *
+     * otherwise the value represents a local time in the time zone
+     * indicated by <g-differential>.  In the latter case, coordinated
+     * universal time can be calculated by subtracting the differential from
+     * the local time.  The "Z" form of <g-time-zone> SHOULD be used in
+     * preference to <g-differential>.
+     *
+     * Examples:
+     *    199412161032Z
+     *    199412160532-0500
+     *
+     * Both example values represent the same coordinated universal time:
+     * 10:32 AM, December 16, 1994.
+     *
+     * The LDAP definition for the Generalized Time syntax is:
+     *
+     * ( 1.3.6.1.4.1.1466.115.121.1.24 DESC 'Generalized Time' )
+     *
+     * This syntax corresponds to the GeneralizedTime ASN.1 type from
+     * [ASN.1], with the constraint that local time without a differential
+     * SHALL NOT be used.
+     * </pre>
+     *
+     * @param millis time in milliseconds to calculate the generalized time
+     * @return the generalized time using the g-differential option for
+     * the default timezone
+     */
+    public static String getGeneralizedTime( long millis )
+    {
+        StringBuffer buf = new StringBuffer();
+        Calendar cal = Calendar.getInstance();
+        cal.setTimeInMillis( millis );
+
+        // append the century and year in one shot
+        buf.append( cal.get( Calendar.YEAR ) );
+
+        /*
+         * we add one because cal uses 0 based and LDAP uses 1 based month
+         * indexing - also we need to make sure we left pad the value w/ a 0
+         */
+        int month = cal.get( Calendar.MONTH ) + 1;
+        if ( month < 10 )
+        {
+            buf.append( '0' ).append( month );
+        }
+        else
+        {
+            buf.append( month );
+        }
+
+        // we need to make sure we left pad the value w/ a 0
+        int dayOfMonth = cal.get( Calendar.DAY_OF_MONTH );
+        if ( dayOfMonth < 10 )
+        {
+            buf.append( '0' ).append( dayOfMonth );
+        }
+        else
+        {
+            buf.append( dayOfMonth );
+        }
+
+        int hourOfDay = cal.get( Calendar.HOUR_OF_DAY );
+        if ( hourOfDay < 10 )
+        {
+            buf.append( '0' ).append( hourOfDay );
+        }
+        else
+        {
+            buf.append( hourOfDay );
+        }
+
+        int minute = cal.get( Calendar.MINUTE );
+        if ( minute < 10 )
+        {
+            buf.append( '0' ).append( minute );
+        }
+        else
+        {
+            buf.append( minute );
+        }
+
+        int second = cal.get( Calendar.SECOND );
+        if ( second < 10 )
+        {
+            buf.append( '0' ).append( second );
+        }
+        else
+        {
+            buf.append( second );
+        }
+
+        // calculate the offset, the -/+ value for g-differential, and take abs
+        int offset = cal.getTimeZone().getRawOffset();
+        buf.append( offset > 0 ? '+' : '-' );
+        offset = Math.abs( offset );
+
+        // calculate the offset hours
+        int offsetHours = ( offset / 3600000 );
+        if ( offsetHours < 10 )
+        {
+            buf.append( '0' ).append( offsetHours );
+        }
+        else
+        {
+            buf.append( offsetHours );
+        }
+
+        // calculate the offset minutes
+        int offsetMinutes = ( offset / 1000 ) % 3600;
+        if ( offsetMinutes < 10 )
+        {
+            buf.append( '0' ).append( offsetMinutes );
+        }
+        else
+        {
+            buf.append( offsetMinutes );
+        }
+
+        return buf.toString();
+    }
+}

Added: incubator/directory/ldap/trunk/common/src/test/org/apache/ldap/common/util/DateUtilsTest.java
==============================================================================
--- (empty file)
+++ incubator/directory/ldap/trunk/common/src/test/org/apache/ldap/common/util/DateUtilsTest.java
Mon Oct 25 20:47:22 2004
@@ -0,0 +1,62 @@
+/*
+ *   Copyright 2004 The Apache Software Foundation
+ *
+ *   Licensed under the Apache License, Version 2.0 (the "License");
+ *   you may not use this file except in compliance with the License.
+ *   You may obtain a copy of the License at
+ *
+ *       http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *   Unless required by applicable law or agreed to in writing, software
+ *   distributed under the License is distributed on an "AS IS" BASIS,
+ *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *   See the License for the specific language governing permissions and
+ *   limitations under the License.
+ *
+ */
+package org.apache.ldap.common.util;
+
+
+import java.util.Calendar;
+import java.util.TimeZone;
+
+import junit.framework.TestCase;
+
+
+/**
+ * Tests the DateUtils class.
+ *
+ * @author <a href="mailto:directory-dev@incubator.apache.org">Apache Directory Project</a>
+ * @version $Rev$
+ */
+public class DateUtilsTest extends TestCase
+{
+    /** message about length max being 17 chars */
+    private static final String LENGTH_MSG =
+            "All generalized time lengths with g-differential option for the " +
+            "time zone should be a length of 19 characters but we got:\n";
+
+
+    /**
+     * Tests to make sure the format and time correspond with the returned
+     * String from the {@link DateUtils#getGeneralizedTime(long)} method.
+     */
+    public void testGetGeneralizedTime()
+    {
+        String time = DateUtils.getGeneralizedTime( System.currentTimeMillis() );
+        assertEquals( LENGTH_MSG + time, 19, time.length() );
+
+        // use the example date time from the Javadocs
+        Calendar cal = Calendar.getInstance( TimeZone.getTimeZone( "UTC" ) );
+        cal.set( Calendar.YEAR, 1994 );
+        cal.set( Calendar.MONTH, 11 );
+        cal.set( Calendar.DAY_OF_MONTH, 16 );
+        cal.set( Calendar.HOUR_OF_DAY, 10 );
+        cal.set( Calendar.MINUTE, 32 );
+        cal.set( Calendar.SECOND, 07 );
+
+        time = DateUtils.getGeneralizedTime( cal.getTimeInMillis() );
+        assertEquals( "Expected the example date of 10:32 AM, " +
+                "December 16, 1994 UTC", "19941216053207-0500", time );
+    }
+}

Mime
View raw message