Implement a Relative DateTime Formatter
|Reported by:||akshay||Owned by:||pulkomandy|
Currently, there is a BDurationFormat which takes in a time interval and gives a formatted string such as "1 hour:2 minutes:3 seconds". A RelativeDateTime formatter is required which formats relative dates.
I have done some initial work on the same taking a lot of help from existing classes in locale kit(BDurationFormat/BTimeUnitFormat). Currently it's a basic implementation and I would like to have views before working further on this.
It's a simple cut-off logic based upon icu::RelativeDateTimeFormatter using the numeric-only style.
Input: Takes in a string buffer to fill with the formatted string, and the time (no. of seconds since epoch) to format, relative to the current time.
Returns: status code.
Currently it formats the relative date as follows:
- time same as current calendar time: " in 0 seconds"
- within 1 minute(before or after): '20 seconds ago', 'in 20 seconds'.
- within 1 hour: '20 minutes ago', 'in 20 minutes'.
- within 1 day: '5 hours ago', 'in 1 hour'.
- within 1 week: '4 days ago', 'in 5 days'.
- within 1 month: '2 weeks ago', 'in 3 weeks'.
- within 1 year: '1 month ago', 'in 5 months'.
- anything after that: '1 year ago', 'in 5 years'.
This result is with an offset of 1. For an offset of 2 we can have it as within 2 days: '36 hours ago', 'in 14 hours'. Also for time same as current time, a text term such as 'now' can be shown.
Instead of whole numbers values for years, months etc, icu::RelativeDateTimeFormatter also supports fractional values (for e.g 3.2 years, 2.5 months). Numeric style (for e.g: in 1 day, 1 week ago), or text style (for e.g: tomorrow, next week, last year) or a combination of both can be used. Day of week can also be incorporated in formatting (for e.g next Tuesday, in 4 Tuesdays).
Various cut-off logic can be implemented based upon icu::RelativeDateTimeFormatter.
Also for formatting I'm currently using icu::RelativeDateTimeFormatter::formatNumeric(), which I am not very sure about using, because it is a part of ICU Draft API and may be changed in the future versions(introduced in ICU 57). A combination of UDateDirection and UDateRelativeUnit can be used in calls to icu::RelativeDateTimeFormatter::format() which is a part of stable API, to achieve the same. So maybe I'll base this upon it.
Change History (14)
by , 3 years ago
by , 3 years ago