Re: System.currentTimeMillis() out of sync with servertime (?)



On Wed, 13 Jun 2007 05:28:55 GMT, Roedy Green
<see_website@xxxxxxxxxxxxxxxxxxxx> wrote, quoted or indirectly quoted
someone who said :

Older machines would lose perhaps 5 seconds a day. You want some way
to resynch to an atomic clock from time to time . Vista has this
built in. I wrote a program called SetClock to do it, but
unfortunately it uses Windows native code to actually set the clock.
You might write a new native class for your OS -- just a few lines of
C. Or just look around for clock synching software.

If you compile this little C program for your platform, and send me
back the modified source and DLL/SO, I will add it to setclock, and
you can then use setclock on your platform to set your time from an
atomic clock. This offer is open to anyone with a non-Windows
system..

/**
* PCClock.c : jni methods in nativepcclock.dll
* Must have previously generated the pcclock.h file with
* javah.
* You must have
* J:\Program Files\Java\jdk1.6.0_01\include\
* and
* J:\Program Files\Java\jdk1.6.0_01\include\win32
* in the include list e.g.
* in Tools | options | Projects and Solutions | VC++ directories |
include files
* or
* in tools | options | directories | include
* For project as a whole:
* In project | settings | general | no MFC
* In project | settings | link | output filename | should end in DLL
* copyright (c) 2004-2007 Roedy Green Canadian Mind Products
*/

#include <windows.h>
// include prototypes generated by javah
#include "pcclock.h"

/*
* Class: com_mindprod_pcclock_PCClock
* Method: nativeSetClock
* Signature: (IIIIIII)Z
* Set system date and time.
* @return true if all ok.
*/
JNIEXPORT jboolean JNICALL
Java_com_mindprod_pcclock_PCClock_nativeSetClock
(JNIEnv* env, jobject obj, jint year, jint month, jint day,
jint hour, jint minute, jint second, jint millis) {

/* set system time from fields passed as parameters:
year=yyyy, month=1..12, day=1..31,
hour=0..23, minute=0..59, second=0..59, millis=0..999 */
/* build local System time control block */
SYSTEMTIME tcb;

// system time fields are all 16 bit unsigned.

tcb.wYear = (WORD)year;
tcb.wMonth = (WORD)month;
tcb.wDay = (WORD)day;
tcb.wHour = (WORD)hour;
tcb.wMinute = (WORD)minute;
tcb.wSecond = (WORD)second;
tcb.wMilliseconds = (WORD)millis;
return SetSystemTime(&tcb);

} // end Java_com_mindprod_setclock_PCClock_nativeSetClock

// end PCClock.c


------------------------------------------

see notes at http://mindprod.com/jgloss/jni.html

---------------------------------------------
pcclock.h

/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class com_mindprod_pcclock_PCClock */

#ifndef _Included_com_mindprod_pcclock_PCClock
#define _Included_com_mindprod_pcclock_PCClock
#ifdef __cplusplus
extern "C" {
#endif
/* Inaccessible static: UTC */
/*
* Class: com_mindprod_pcclock_PCClock
* Method: nativeSetClock
* Signature: (IIIIIII)Z
*/
JNIEXPORT jboolean JNICALL
Java_com_mindprod_pcclock_PCClock_nativeSetClock
(JNIEnv *, jobject, jint, jint, jint, jint, jint, jint, jint);

#ifdef __cplusplus
}
#endif
#endif

--
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
.