You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by mt...@apache.org on 2011/04/15 09:23:54 UTC

svn commit: r1092609 - in /commons/sandbox/runtime/trunk/src/main: java/org/apache/commons/runtime/Time.java native/Makefile.msc.in native/Makefile.unx.in native/shared/utime.c test/org/apache/commons/runtime/TestUtils.java

Author: mturk
Date: Fri Apr 15 07:23:54 2011
New Revision: 1092609

URL: http://svn.apache.org/viewvc?rev=1092609&view=rev
Log:
Add replacement for System.currentTimeMillis and friends

Added:
    commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Time.java   (with props)
    commons/sandbox/runtime/trunk/src/main/native/shared/utime.c   (with props)
Modified:
    commons/sandbox/runtime/trunk/src/main/native/Makefile.msc.in
    commons/sandbox/runtime/trunk/src/main/native/Makefile.unx.in
    commons/sandbox/runtime/trunk/src/main/test/org/apache/commons/runtime/TestUtils.java

Added: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Time.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Time.java?rev=1092609&view=auto
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Time.java (added)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Time.java Fri Apr 15 07:23:54 2011
@@ -0,0 +1,69 @@
+/* Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.commons.runtime;
+
+/**
+ * Time class.
+ *
+ * @since Runtime 1.0
+ *
+ */
+public final class Time
+{
+
+    private Time()
+    {
+        // No class instance
+    }
+
+    public static final long   USEC_PER_SEC    =  1000000L;
+    public static final long   MSEC_PER_SEC    =  1000L;
+    public static final long   INFINITE        = -1L;
+
+    /**
+     * Returns the current time in microseconds.
+     * Returns number of microseconds since 00:00:00 january 1, 1970 UTC
+     */
+    public static native long currentTimeMicros();
+
+    /**
+     * Returns the current time in milliseconds.
+     * Returns number of milliseconds since 00:00:00 january 1, 1970 UTC
+     */
+    public static native long currentTimeMillis();
+
+    /**
+     * Returns the current time in MS-DOS time format.
+     * The MS-DOS date format can represent only dates between january 1, 1980
+     * and december 31, 2107.
+     */
+    public static native int  currentDosTime();
+
+    /**
+     * Converts the microseconds time to MS-DOS time.
+     * The MS-DOS date format can represent only dates between january 1, 1980
+     * and december 31, 2107.
+     */
+    public static native int  asDosTime(long usec);
+
+    /**
+     * Converts the MS-DOS time to microseconds.
+     */
+    public static native long fromDosTime(int dosTime);
+    
+}
+

Propchange: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Time.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: commons/sandbox/runtime/trunk/src/main/native/Makefile.msc.in
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/Makefile.msc.in?rev=1092609&r1=1092608&r2=1092609&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/Makefile.msc.in (original)
+++ commons/sandbox/runtime/trunk/src/main/native/Makefile.msc.in Fri Apr 15 07:23:54 2011
@@ -112,6 +112,7 @@ LIBSOURCES=\
 	$(TOPDIR)\shared\string.c \
 	$(TOPDIR)\shared\system.c \
 	$(TOPDIR)\shared\unsafe.c \
+	$(TOPDIR)\shared\utime.c \
 	$(TOPDIR)\shared\uuid.c \
 	$(TOPDIR)\shared\version.c \
 	$(TOPDIR)\shared\buildmark.c

Modified: commons/sandbox/runtime/trunk/src/main/native/Makefile.unx.in
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/Makefile.unx.in?rev=1092609&r1=1092608&r2=1092609&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/Makefile.unx.in (original)
+++ commons/sandbox/runtime/trunk/src/main/native/Makefile.unx.in Fri Apr 15 07:23:54 2011
@@ -108,6 +108,7 @@ LIBSOURCES=\
 	$(TOPDIR)/shared/string.c \
 	$(TOPDIR)/shared/system.c \
 	$(TOPDIR)/shared/unsafe.c \
+	$(TOPDIR)/shared/utime.c \
 	$(TOPDIR)/shared/uuid.c \
 	$(TOPDIR)/shared/version.c \
 	$(TOPDIR)/shared/buildmark.c

Added: commons/sandbox/runtime/trunk/src/main/native/shared/utime.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/shared/utime.c?rev=1092609&view=auto
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/shared/utime.c (added)
+++ commons/sandbox/runtime/trunk/src/main/native/shared/utime.c Fri Apr 15 07:23:54 2011
@@ -0,0 +1,43 @@
+/* Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.
+ */
+
+#include "acr/time.h"
+#include "acr/jniapi.h"
+
+ACR_JNI_EXPORT(jlong, Time, currentTimeMicros)(JNI_STDARGS)
+{
+    return AcrTimeNow();
+}
+
+ACR_JNI_EXPORT(jlong, Time, currentTimeMillis)(JNI_STDARGS)
+{
+    return AcrTimeMilliseconds();
+}
+
+ACR_JNI_EXPORT(jlong, Time, fromDosTime)(JNI_STDARGS, jint dos)
+{
+    return AcrDOSTimeToTime(dos);
+}
+
+ACR_JNI_EXPORT(jint, Time, asDosTime)(JNI_STDARGS, jlong usec)
+{
+    return AcrTimeToDOSTime(usec);
+}
+
+ACR_JNI_EXPORT(jint, Time, currentDosTime)(JNI_STDARGS)
+{
+    return AcrTimeToDOSTime(AcrTimeNow());
+}

Propchange: commons/sandbox/runtime/trunk/src/main/native/shared/utime.c
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: commons/sandbox/runtime/trunk/src/main/test/org/apache/commons/runtime/TestUtils.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/test/org/apache/commons/runtime/TestUtils.java?rev=1092609&r1=1092608&r2=1092609&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/test/org/apache/commons/runtime/TestUtils.java (original)
+++ commons/sandbox/runtime/trunk/src/main/test/org/apache/commons/runtime/TestUtils.java Fri Apr 15 07:23:54 2011
@@ -61,4 +61,20 @@ public class TestUtils
         Assert.assertEquals("cafebabedeadbeef", Utils.hex(l));
     }
 
+    @Test(groups = { "core" })
+    public void dosTime()
+        throws IOException
+    {
+        long now = Time.currentTimeMicros();
+        int  dos = Time.asDosTime(now);
+        long cnv = Time.fromDosTime(dos);
+        long sec = now / Time.USEC_PER_SEC;
+        long css = cnv / Time.USEC_PER_SEC;
+        // MS-DOS time has 2 second resolution
+        long asc = Memory.align(sec, 2);
+        long acs = Memory.align(css, 2);
+        
+        Assert.assertEquals(asc, acs);
+    }
+
 }