5

I want to provide some styling to an app that is aware of work days and weekends, but I need it to be locale-aware. I'm aware of Calendar#getFirstDayOfWeek, but this only returns the first day of the week, not the first day of the work week.

For example, with English(Canada) Calendar#getFirstDayOfWeek returns Calendar.SUNDAY, which is the first day of our week. Our work week starts on Calendar.MONDAY, and our weekend days are Calendar.SATURDAY and Calendar.SUNDAY.

In English(United Kingdom), Calendar#getFirstDayOfWeek returns Calendar.MONDAY, and the first day of their work-week is Monday. Their weekend days are Calendar.SATURDAY and Calendar.SUNDAY.

Things get tricky for locales like Hebrew. The first day of their week is Calendar.SUNDAY, but instead of the first day of their work week being Calendar.MONDAY, it's Calendar.SUNDAY. Their weekend days are Calendar.FRIDAY and Calendar.SATURDAY. Egypt is the same.

Iran only has one weekend day, Calendar.FRIDAY.

Is there any method in Android to determine weekend days? We have some logic in place that covers most Western cases, but it's fragile, and a locale-aware platform API would be ideal, but I'm not aware of one.

Please note, I am not asking for a recommendation on some "best" library or anything of the sort. I'm asking if any such locale data exists on the Android platform at all, and if so, what API calls exist to access this data.

Michael0x2a
  • 58,192
  • 30
  • 175
  • 224
JymmyZ
  • 126
  • 8
  • 1
    Curious about the -1 vote on the question? Did I not do something correctly when asking? I've done plenty of research, asked a clear question, and there's no need for sample code. – JymmyZ Sep 04 '15 at 18:26
  • Oh, so 2 negative votes with no explanation. Yeah, thanks for that. – JymmyZ Sep 04 '15 at 19:44
  • Presumably, you should use any such API only to guess the default settings, and let the user pick something else in case their locale wasn't actually a good indication of their workweek ... – SamB Sep 04 '15 at 20:22
  • That capability is already in place, it's the locale default setting that is causing problems for users in some of these regions. – JymmyZ Sep 04 '15 at 20:35
  • 4
    This question is perfectly fine and not off topic. – Derk-Jan Sep 08 '15 at 23:35
  • Good question. You could possibly parse (or copy) the list from this page: https://en.wikipedia.org/wiki/Workweek_and_weekend. It has the weekend days for each country (I believe that all the others are just default Mon-Fri work days, but haven't checked) – Omri374 Dec 14 '15 at 19:38
  • 1
    The raw data is available from the Unicode CLDR supplementary data at http://www.unicode.org/repos/cldr/trunk/common/supplemental/supplementalData.xml (search for weekend) I wanted to avoid loading and keeping track of new locale data, as that means I would have to maintain this (likely quite stable) data in my app going forward. Alas, this seems to be the case. – JymmyZ Dec 16 '15 at 00:50

1 Answers1

1

There are newly released snapshot and fence APIs in Awareness API for your use-case for a locale-aware API for time.

Please take a look at Awareness.SnapshotApi.getTimeIntervals which returns a TimeIntervalsResult. You can call getTimeIntervals() method to return an object that has information about the properties of the current time at current locale (specifically the weekend and holidays that you have asked for).

For example, the following code snippet checks if it is a weekend at the device's locale at the time of the snapshot api call.

Awareness.SnapshotApi.getTimeIntervalsmGoogleApiClient)
    .setResultCallback(new ResultCallback<TimeIntervalsResult>() {
        @Override
        public void onResult(@NonNull TimeIntervalsResult result) {
            if (!result.getStatus().isSuccess()) {
                Log.e(TAG, "Could not get the time intervals");
                return;
            }
            if (result.getTimeIntervals().hasTimeInterval(
                TimeFence.TIME_INTERVAL_WEEKEND)) {
                // do your custom action, this means it is weekend at the 
                // device locale at this time
            }
        }

If you'd like your app to get a callback on the next weekend or holiday at the device locale, you can use the new TimeFence Api inTimeInterval(). For example, borrowing the code snippet from the developer docs,

// Create TimeFence
AwarenessFence weekendHolidayFence = Awareness.or(
    TimeFence.inTimeInterval(TimeFence.TIME_INTERVAL_WEEKEND),
    TimeFence.inTimeInterval(TimeFence.TIME_INTERVAL_HOLIDAY))

Awareness.FenceApi.updateFences(
    mGoogleApiClient,
    new FenceUpdateRequest.Builder()
        .addFence("fenceKey", weekendHolidayFence, myPendingIntent)
        .build())
    .setResultCallback(new ResultCallback<Status>() {
        @Override
        public void onResult(@NonNull Status status) {
            if (status.isSuccess()) {
                Log.i(TAG, "Fence was successfully registered.");
            } else {
                Log.e(TAG, "Fence could not be registered: " + status);
            }
        }
    });
MRC
  • 56
  • 4