Welcome to mirror list, hosted at ThFree Co, Russian Federation.

TimeFormatUtils.java « data « editor « maps « mapswithme « com « src « android - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 894e6285c2f68c606896b8d015b1a62cad2ebe4d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package com.mapswithme.maps.editor.data;

import android.content.res.Resources;
import androidx.annotation.IntRange;
import androidx.annotation.NonNull;

import java.text.DateFormatSymbols;
import java.util.Locale;

import com.mapswithme.maps.MwmApplication;
import com.mapswithme.maps.R;
import com.mapswithme.util.Utils;

public class TimeFormatUtils
{
  private TimeFormatUtils() {}

  private static String[] sShortWeekdays;
  private static Locale sCurrentLocale;

  private static void refreshWithCurrentLocale()
  {
    if (!Locale.getDefault().equals(sCurrentLocale))
    {
      sCurrentLocale = Locale.getDefault();
      sShortWeekdays = DateFormatSymbols.getInstance().getShortWeekdays();
      for (int i = 0; i < sShortWeekdays.length; i++)
      {
        sShortWeekdays[i] = Utils.capitalize(sShortWeekdays[i]);
      }
    }
  }

  public static String formatShortWeekday(@IntRange(from = 1, to = 7) int day)
  {
    refreshWithCurrentLocale();
    return sShortWeekdays[day];
  }

  public static String formatWeekdays(@NonNull Timetable timetable)
  {
    refreshWithCurrentLocale();
    final int[] weekdays = timetable.weekdays;
    if (weekdays.length == 0)
      return "";

    final StringBuilder builder = new StringBuilder(sShortWeekdays[weekdays[0]]);
    boolean iteratingRange;
    for (int i = 1; i < weekdays.length; )
    {
      iteratingRange = (weekdays[i] == weekdays[i - 1] + 1);
      if (iteratingRange)
      {
        while (i < weekdays.length && weekdays[i] == weekdays[i - 1] + 1)
          i++;
        builder.append("-").append(sShortWeekdays[weekdays[i - 1]]);
        continue;
      }

      if (i < weekdays.length)
        builder.append(", ").append(sShortWeekdays[weekdays[i]]);

      i++;
    }

    return builder.toString();
  }

  public static String formatTimetables(@NonNull Timetable[] timetables)
  {
    final Resources resources = MwmApplication.get().getResources();

    if (timetables[0].isFullWeek())
    {
      return timetables[0].isFullday ? resources.getString(R.string.twentyfour_seven)
                                     : resources.getString(R.string.daily) + " " + timetables[0].workingTimespan;
    }

    final StringBuilder builder = new StringBuilder();
    for (Timetable tt : timetables)
    {
      String workingTime = tt.isFullday ? resources.getString(R.string.editor_time_allday)
                                        : tt.workingTimespan.toString();

      builder.append(String.format(Locale.getDefault(), "%-21s", formatWeekdays(tt))).append("   ")
             .append(workingTime)
             .append("\n");
    }

    return builder.toString();
  }
}