Esempio n. 1
0
  /**
   * Parse a shows airtime ms value to an actual time. If given a TVDb day string the day will get
   * determined, too, all respecting user settings like time zone and time offset.
   */
  public static String[] parseMillisecondsToTime(
      long milliseconds, String dayofweek, Context context) {
    // return empty strings if time is missing
    if (context == null || milliseconds == -1) {
      return new String[] {"", ""};
    }

    // set calendar time and day on always PST calendar
    // this is a workaround so we can convert the air day to a another time
    // zone without actually having a date
    final Calendar cal = Calendar.getInstance(TimeZone.getTimeZone(TIMEZONE_ALWAYS_PST));
    final int year = cal.get(Calendar.YEAR);
    final int month = cal.get(Calendar.MONTH);
    final int day = cal.get(Calendar.DAY_OF_MONTH);
    cal.setTimeInMillis(milliseconds);
    // set the date back to today
    cal.set(year, month, day);

    // determine the shows common air day (Mo through Sun or daily)
    int dayIndex = -1;
    if (dayofweek != null) {
      dayIndex = getDayOfWeek(dayofweek);
      if (dayIndex > 0) {
        int today = cal.get(Calendar.DAY_OF_WEEK);
        // make sure we always assume a day which is today or later
        if (dayIndex - today < 0) {
          // we have a day before today
          cal.add(Calendar.DAY_OF_WEEK, (7 - today) + dayIndex);
        } else {
          // we have today or in the future
          cal.set(Calendar.DAY_OF_WEEK, dayIndex);
        }
      }
    }

    // convert time to local, including the day
    final Calendar localCal = Calendar.getInstance();
    localCal.setTimeInMillis(cal.getTimeInMillis());

    final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
    setOffsets(prefs, localCal, milliseconds);

    // create time string
    final java.text.DateFormat timeFormat = DateFormat.getTimeFormat(context);
    final SimpleDateFormat dayFormat = new SimpleDateFormat("E");
    final Date date = localCal.getTime();

    timeFormat.setTimeZone(TimeZone.getDefault());
    dayFormat.setTimeZone(TimeZone.getDefault());

    String daystring = "";
    if (dayIndex == 0) {
      daystring = context.getString(R.string.daily);
    } else if (dayIndex != -1) {
      daystring = dayFormat.format(date);
    }

    return new String[] {timeFormat.format(date), daystring};
  }
Esempio n. 2
0
  /**
   * Create a calendar set to the given airtime, time is adjusted according to 'Use my time zone',
   * 'Time Offset' settings and user time zone.
   */
  public static Calendar getAirtimeCalendar(long airtime, final SharedPreferences prefs) {
    Calendar cal = Calendar.getInstance();
    cal.setTimeInMillis(airtime);

    setOffsets(prefs, cal, airtime);

    return cal;
  }