MenoData / Time4J

Advanced date, time and interval library for Java with sun/moon-astronomy and calendars like Chinese, Coptic, Ethiopian, French Republican, Hebrew, Hijri, Historic Christian, Indian National, Japanese, Julian, Korean, Minguo, Persian, Thai, Vietnamese
GNU Lesser General Public License v2.1
424 stars 62 forks source link

initiate with a certain nanoTime #980

Open Shefaee opened 1 year ago

Shefaee commented 1 year ago

Hi, Is it possible to set the base time manually? For example: from the server (not the system)

val nanoTime  = 0;//from Server
ApplicationStarter.initialize(context, prefetch, nanoTime);

Thank you very much for your great work

MenoData commented 1 year ago

Sorry for late reply. Mea culpa.

The third parameter nanoTime does not exist. I interprete your question as proposal to introduce such a third parameter. Well, I think there is no need for such a parameter in this initialization method which is rather about resource and context class loading.

Probably you look for a way to set up an application-wide clock which is once initiated by the server time. Time4J has such a clock package but Time4A unfortunately not yet. You might look at the actual implementations of the clock package of Time4J to model a suitable clock for Time4A.

Maybe this is an inspiration:

public class ServerTimeSource implements TimeSource<Moment> {
  private final long offset;

  public ServerTimeSource(long serverTimeUTInMillisecsSince1970) {
    this.offset = serverTimeUTInMillisecsSince1970 - System.currentTimeMillis();
  }

  public Moment currentTime() {
    long timeInMillis = System.currentTimeMillis() + this.offset;
    return TemporalType.MILLIS_SINCE_UNIX.translate(timeInMillis);
  }

}

Then you would instantiate the class once and store it in an application-wide location so it can be reused many times whenever you want to know the server time (as UT). If you also want to use the server time zone you have to store it as TZID or offset, too, in an application-wide location.