2 * Copyright (C) 2008 The Android Open Source Project
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 * System clock functions.
22 #ifdef HAVE_ANDROID_OS
23 #include <linux/ioctl.h>
24 #include <linux/rtc.h>
25 #include <utils/Atomic.h>
26 #include <linux/android_alarm.h>
37 #include "android_runtime/AndroidRuntime.h"
42 #include <utils/SystemClock.h>
47 * Set the current time. This only works when running as root.
49 static int setCurrentTimeMillis(int64_t millis)
57 if (millis <= 0 || millis / 1000LL >= INT_MAX) {
61 tv.tv_sec = (time_t) (millis / 1000LL);
62 tv.tv_usec = (suseconds_t) ((millis % 1000LL) * 1000LL);
64 ALOGD("Setting time of day to sec=%d\n", (int) tv.tv_sec);
66 fd = open("/dev/alarm", O_RDWR);
68 ALOGW("Unable to open alarm driver: %s\n", strerror(errno));
71 ts.tv_sec = tv.tv_sec;
72 ts.tv_nsec = tv.tv_usec * 1000;
73 res = ioctl(fd, ANDROID_ALARM_SET_RTC, &ts);
75 ALOGW("Unable to set rtc to %ld: %s\n", tv.tv_sec, strerror(errno));
83 * native public static void setCurrentTimeMillis(long millis)
85 * Set the current time. This only works when running as root.
87 static jboolean android_os_SystemClock_setCurrentTimeMillis(JNIEnv* env,
88 jobject clazz, jlong millis)
90 return (setCurrentTimeMillis(millis) == 0);
94 * native public static long uptimeMillis();
96 static jlong android_os_SystemClock_uptimeMillis(JNIEnv* env,
99 return (jlong)uptimeMillis();
103 * native public static long elapsedRealtime();
105 static jlong android_os_SystemClock_elapsedRealtime(JNIEnv* env,
108 return (jlong)elapsedRealtime();
112 * native public static long currentThreadTimeMillis();
114 static jlong android_os_SystemClock_currentThreadTimeMillis(JNIEnv* env,
117 #if defined(HAVE_POSIX_CLOCKS)
120 clock_gettime(CLOCK_THREAD_CPUTIME_ID, &tm);
122 return tm.tv_sec * 1000LL + tm.tv_nsec / 1000000;
126 gettimeofday(&tv, NULL);
127 return tv.tv_sec * 1000LL + tv.tv_usec / 1000;
132 * native public static long currentThreadTimeMicro();
134 static jlong android_os_SystemClock_currentThreadTimeMicro(JNIEnv* env,
137 #if defined(HAVE_POSIX_CLOCKS)
140 clock_gettime(CLOCK_THREAD_CPUTIME_ID, &tm);
142 return tm.tv_sec * 1000000LL + tm.tv_nsec / 1000;
146 gettimeofday(&tv, NULL);
147 return tv.tv_sec * 1000000LL + tv.tv_nsec / 1000;
152 * native public static long currentTimeMicro();
154 static jlong android_os_SystemClock_currentTimeMicro(JNIEnv* env,
159 gettimeofday(&tv, NULL);
160 return tv.tv_sec * 1000000LL + tv.tv_usec;
164 * public static native long elapsedRealtimeNano();
166 static jlong android_os_SystemClock_elapsedRealtimeNano(JNIEnv* env,
169 return (jlong)elapsedRealtimeNano();
175 static JNINativeMethod gMethods[] = {
176 /* name, signature, funcPtr */
177 { "setCurrentTimeMillis", "(J)Z",
178 (void*) android_os_SystemClock_setCurrentTimeMillis },
179 { "uptimeMillis", "()J",
180 (void*) android_os_SystemClock_uptimeMillis },
181 { "elapsedRealtime", "()J",
182 (void*) android_os_SystemClock_elapsedRealtime },
183 { "currentThreadTimeMillis", "()J",
184 (void*) android_os_SystemClock_currentThreadTimeMillis },
185 { "currentThreadTimeMicro", "()J",
186 (void*) android_os_SystemClock_currentThreadTimeMicro },
187 { "currentTimeMicro", "()J",
188 (void*) android_os_SystemClock_currentTimeMicro },
189 { "elapsedRealtimeNanos", "()J",
190 (void*) android_os_SystemClock_elapsedRealtimeNano },
192 int register_android_os_SystemClock(JNIEnv* env)
194 return AndroidRuntime::registerNativeMethods(env,
195 "android/os/SystemClock", gMethods, NELEM(gMethods));
198 }; // namespace android