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
440 stars 64 forks source link

EastAsianCalendar cannot operate on time units smaller than a day. #810

Closed Charlweed closed 6 years ago

Charlweed commented 6 years ago

I was trying to use the plus method on ChineseCalendar to add a number of seconds, and there does not seem to be any way. At least not in 4.99-beta. ChineseCalendar.Unit ChineseCalendar.Unit has no unit smaller than DAYS. None of the arithmetic methods will accept any argument types except ChineseCalendar.Unit, and there is no way to convert other units into ChineseCalendar.Unit. This effectually blocks any operations with ClockUnit on units of time smaller than an day. I'm hacking around on the source to see what happens when I insert hours-minutes-seconds into the ChineseCalendar.Unit enum, but it is unclear why there are different incompatible types for:

net.time4j.CalendarUnit net.time4j.ClockUnit net.time4j.calendar.ChineseCalendar.Unit

and no ChineseCalendar ClockUnit at all.

MenoData commented 6 years ago

In general, the units associated with calendars have no finer granularity than days because calendars itself don't bother about clock time. Calendars are not designed for this purpose.

But you can combine calendars with clock time to a general timestamp for example the following way:

GeneralTimestamp<ChineseCalendar> tsp = chineseCalendar.atTime(19, 45);

There is also a more general at(PlainTime)-method available. Once you have a general timestamp, you can either extract the calendar component or the (iso) clock time, or you can convert it to a Moment (with specifying the timezone and the start of day).

Two special calendars also have special time units via extension classes:

Historically, Chinese people (and other East Asian peoples, too) had also used other time units which are not compatible with western clock units, but this has not (yet?) been implemented in Time4J.

MenoData commented 6 years ago

By the way, the type CalendarUnit only handles ISO-calendar units where the month-based units are totally different from the Chinese month units. We should not mix both units in order to guarantee minimum compile-time type safety.

MenoData commented 6 years ago

About time arithmetic with plus()- or minus()-methods, this has not (yet?) been implemented in the class GeneralTimestamp but you can of course convert your calendar to a Moment and then apply time arithmetic in the physical space. Example:

GeneralTimestamp<ChineseCalendar> tsp = chineseCalendar.atTime(19, 45);
Moment moment = tsp.in(Timezone.ofSystem(), StartOfDay.MIDNIGHT);
Moment twoHoursLater = moment.plus(2, TimeUnit.HOURS);
Charlweed commented 6 years ago

Thank you!

Perhaps I will try that approach.

Dev

From: Meno Hochschild [mailto:notifications@github.com] Sent: Tuesday, September 11, 2018 12:45 AM To: MenoData/Time4J Time4J@noreply.github.com Cc: Charlweed Hymerfan dev@hymes.name; Author author@noreply.github.com Subject: Re: [MenoData/Time4J] EastAsianCalendar cannot operate on time units smaller than a day. (#810)

About time arithmetic with plus()- or minus()-methods, this has not (yet?) been implemented in the class GeneralTimestamp but you can of course convert your calendar to a Moment and then apply time arithmetic in the physical space.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/MenoData/Time4J/issues/810#issuecomment-420179529 , or mute the thread https://github.com/notifications/unsubscribe-auth/AAUDdwBSFTiAzsVfNU07yhvbiaT0ZcfRks5uZ2nggaJpZM4WiV8P .

MenoData commented 6 years ago

As you see, I have added another example above which demonstrates how to realize addition of time units on a Chinese calendar. I am not sure however, if an extra method in the class GeneralTimestamp is worth the effort because the signature of such a method has also to accept two extra parameters for the time zone and maybe even the start of day if deviating from the calendar standard. Feel free to make suggestions for either such a method or instead more documentation.

MenoData commented 6 years ago

Okay, I have now added some time arithmetic to the class GeneralTimestamp directly for convenience. But if you want clock units in physical space (taking time zones into account) then please follow my advise to first convert to Moment.