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

Getting time of day when shadow length is equal to X #890

Closed a5rar closed 3 years ago

a5rar commented 4 years ago

Ive been playing around with the library for a few days now, and wondering if there is a way to get the time of day when the shadow of object is at a particuar length. SolarPosition can give me the shadow length at a particular time of the day but i want to be able to do the compute the other way around where we get the time based on the shadow length. I can do it using recursion but there must be a better way to do this.

MenoData commented 4 years ago

This is not yet implemented. Well, I am not sure if this is really a calendrical issue (probably rather interesting for criminalogy ;-)). Can you describe your use case?

Anyway, I don't think there is a better way than recursion or computing by nested intervals (my favourite) because the underlying astronomical formula cannot be easily "reversed" like multiplication versus division. Furthermore, they are mainly based on deducing and approximation from empirical data. After some brainstorming, my suggestion would be the introduction of following two new methods in the class SolarTime:

public ChronoFunction<CalendarDate,Optional<Moment>> timeOfShadowBeforeNoon(
  double objectHeight, double shadowLength);

public ChronoFunction<CalendarDate,Optional<Moment>> timeOfShadowAfterNoon(
  double objectHeight, double shadowLength);

The resulting moment must be optional because either there is polar night with no sunshine or because the given length of shadow is too short for given location. Once you get the moment you can simply get the time of day by applying a time zone this way:

PlainTime timeOfDay = moment.toZonalTimestamp("America/New_York").getWallTime();

MenoData commented 3 years ago

I have realized this feature with the small limitation that polar regions are not supported. Reason is that a) the calculations become overly complex because the movement of the sun is no longer strict monotonic and b) the precision of calculation suffers from shallow sun angles in general.