| 468 | * @see #on(HebrewCalendar, SolarTime) |
| 469 | */ |
| 470 | public static ChronoFunction<Moment, Optional<HebrewTime>> at(SolarTime geoLocation) { |
| 471 | |
| 472 | return (moment) -> { |
| 473 | ZonalOffset offset = ZonalOffset.atLongitude(new BigDecimal(geoLocation.getLongitude())); |
| 474 | PlainTimestamp tsp = moment.toZonalTimestamp(offset); // local mean time |
| 475 | Optional<Moment> sunset = tsp.toDate().get(geoLocation.sunset()); |
| 476 | |
| 477 | if (sunset.isPresent()) { |
| 478 | Optional<Moment> sunrise; |
| 479 | ClockCycle cycle = null; |
| 480 | Moment t1 = null; |
| 481 | Moment t2 = null; |
| 482 | if (moment.isBefore(sunset.get())) { |
| 483 | sunrise = tsp.toDate().get(geoLocation.sunrise()); |
| 484 | if (sunrise.isPresent()) { |
| 485 | if (moment.isBefore(sunrise.get())) { |
| 486 | sunset = tsp.toDate().minus(1, CalendarUnit.DAYS).get(geoLocation.sunset()); |
| 487 | if (sunset.isPresent()) { |
| 488 | cycle = ClockCycle.NIGHT; |
| 489 | t1 = sunset.get(); |
| 490 | t2 = sunrise.get(); |
| 491 | } |
| 492 | } else { |
| 493 | cycle = ClockCycle.DAY; |
| 494 | t1 = sunrise.get(); |
| 495 | t2 = sunset.get(); |
| 496 | } |
| 497 | } |
| 498 | } else { |
| 499 | sunrise = tsp.toDate().plus(1, CalendarUnit.DAYS).get(geoLocation.sunrise()); |
| 500 | if (sunrise.isPresent()) { |
| 501 | cycle = ClockCycle.NIGHT; |
| 502 | t1 = sunset.get(); |
| 503 | t2 = sunrise.get(); |
| 504 | } |
| 505 | } |
| 506 | if (cycle != null && t1 != null && t2 != null) { |
| 507 | long halfDay = |
| 508 | t1.until(t2, TimeUnit.SECONDS) * 1_000_000_000L |
| 509 | + t2.getNanosecond() |
| 510 | - t1.getNanosecond(); |
| 511 | long delta = |
| 512 | t1.until(moment, TimeUnit.SECONDS) * 1_000_000_000L |
| 513 | + moment.getNanosecond() |
| 514 | - t1.getNanosecond(); |
| 515 | double halakim = (12.0 * PARTS_IN_HOUR * delta) / halfDay; |
| 516 | int hourOfCycle = (int) Math.floor(halakim / PARTS_IN_HOUR); |
| 517 | int partOfHour = (int) Math.floor(halakim - hourOfCycle * PARTS_IN_HOUR); |
| 518 | return Optional.of(new HebrewTime(cycle, ((hourOfCycle == 0) ? 12 : hourOfCycle), partOfHour)); |
| 519 | } |
| 520 | } |
| 521 | |
| 522 | return Optional.empty(); |
| 523 | }; |
| 524 | |
| 525 | } |
| 526 | |
| 527 | /** |