HanSolo / medusa

A JavaFX library for Gauges
Apache License 2.0
693 stars 131 forks source link

Setting clock to zero #119

Closed WolfgangFahl closed 7 years ago

WolfgangFahl commented 7 years ago

Thx for the excellent library.

I'd like to use a clock as a stop watch. It should display zero when I use setTime(0) - which it does.

 /**
   * set the time to the given milliSeconds
   * 
   * @param mSecs
   */
  public void setTime(long mSecs) {
    long epochSecond = mSecs / 1000;
    int nanoOfSecond = (int) ((mSecs % 1000) * 1000000);
    ZoneOffset zoneoffset = ZoneOffset.ofHours(0);
    LocalDateTime localDateTime = LocalDateTime.ofEpochSecond(epochSecond,
        nanoOfSecond, zoneoffset);
    ZonedDateTime time = ZonedDateTime.of(localDateTime, zoneoffset);
    stopWatch.setTime(time);
  }

but when I retrieve the time with:

@Override
  public long getTime() {    
    ZonedDateTime time = stopWatch.getTime();
    int offset=time.getOffset().getTotalSeconds();
    long mSecs = ((time.getDayOfMonth()-1)*86400 + time.getHour() * 3600 + time.getMinute() * 60
        + time.getSecond() + time.getNano() / 1000000) * 1000;
    System.out.println(" d:"+(time.getDayOfMonth()-1));
    System.out.println(" h:"+time.getHour());
    System.out.println(" m:"+time.getMinute());
    System.out.println(" s:"+time.getSecond());
    System.out.println(" n:"+time.getNano());
    System.out.println(" o:"+offset);
    System.out.println("ms:"+mSecs);

    return mSecs;
  }

the second value is 1 and I get 1000 msecs.

Why is that and how can it be fixed?

WolfgangFahl commented 7 years ago

It's not the library - it was my code. May be if the API would have the ability to set time in millisecs?

/**
   * set the time to the given milliSeconds
   * 
   * @param mSecs
   */
  public void setTime(long mSecs) {
    long epochSecond = mSecs / 1000;
    int nanoOfSecond = (int) ((mSecs % 1000) * 1000000);
    ZoneOffset zoneoffset = ZoneOffset.ofHours(0);
    LocalDateTime localDateTime = LocalDateTime.ofEpochSecond(epochSecond,
        nanoOfSecond, zoneoffset);
    ZonedDateTime time = ZonedDateTime.of(localDateTime, zoneoffset);
    stopWatch.setTime(time);
  }
@Override
  public long getTime() {    
    ZonedDateTime time = stopWatch.getTime();
    int offset=time.getOffset().getTotalSeconds();
    long mSecs =((time.getDayOfMonth()-1)*86400+time.getHour()*3600+time.getMinute()*60+time.getSecond())*1000+time.getNano()/1000000;
    System.out.println(" d:"+(time.getDayOfMonth()-1));
    System.out.println(" h:"+time.getHour());
    System.out.println(" m:"+time.getMinute());
    System.out.println(" s:"+time.getSecond());
    System.out.println(" n:"+time.getNano());
    System.out.println(" o:"+offset);
    System.out.println("ms:"+mSecs);

    return mSecs;
  }
HanSolo commented 7 years ago

Added methods to get and set the time in epoch milliseconds with commit 686890178744f78f9c1c5fb22321155b60f2eb2f Thank's for the request :)