<commit_before>/*
 * Copyright (C) 2008 ZXing authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.google.zxing.client.android.result;

import com.google.zxing.client.android.R;
import com.google.zxing.client.result.CalendarParsedResult;
import com.google.zxing.client.result.ParsedResult;

import android.app.Activity;

import java.text.DateFormat;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;

/**
 * Handles calendar entries encoded in QR Codes.
 *
 * @author <EMAIL> (<NAME>)
 */
public final class CalendarResultHandler extends ResultHandler {

  private static final DateFormat DATE_FORMAT = new SimpleDateFormat("yyyyMMdd", Locale.ENGLISH);
  private static final DateFormat DATE_TIME_FORMAT = new SimpleDateFormat("yyyyMMdd'T'HHmmss", Locale.ENGLISH);

  private static final int[] buttons = {
      R.string.button_add_calendar
  };

  public CalendarResultHandler(Activity activity, ParsedResult result) {
    super(activity, result);
  }

  @Override
  public int getButtonCount() {
    return buttons.length;
  }

  @Override
  public int getButtonText(int index) {
    return buttons[index];
  }

  @Override
  public void handleButtonPress(int index) {
    CalendarParsedResult calendarResult = (CalendarParsedResult) getResult();
    if (index == 0) {
      addCalendarEvent(calendarResult.getSummary(),
                       calendarResult.getStart(),
                       calendarResult.getEnd(),
                       calendarResult.getLocation(),
                       calendarResult.getDescription());
    }
  }

  @Override
  public CharSequence getDisplayContents() {
    CalendarParsedResult calResult = (CalendarParsedResult) getResult();
    StringBuilder result = new StringBuilder(100);
    ParsedResult.maybeAppend(calResult.getSummary(), result);
    String startString = calResult.getStart();
    appendTime(startString, result, false, false);

    String endString = calResult.getEnd();
    if (endString != null) {
      boolean sameStartEnd = startString.equals(endString);
      appendTime(endString, result, true, sameStartEnd);
    }

    ParsedResult.maybeAppend(calResult.getLocation(), result);
    ParsedResult.maybeAppend(calResult.getAttendee(), result);
    ParsedResult.maybeAppend(calResult.getDescription(), result);
    return result.toString();
  }

  private static void appendTime(String when, StringBuilder result, boolean end, boolean sameStartEnd) {
    if (when.length() == 8) {
      // Show only year/month/day
      Date date;
      synchronized (DATE_FORMAT) {
        date = DATE_FORMAT.parse(when, new ParsePosition(0));
      }
      // if it's all-day and this is the end date, it's exclusive, so show the user
      // that it ends on the day before to make more intuitive sense.
      // But don't do it if the event already (incorrectly?) specifies the same start/end
      if (end && !sameStartEnd) {
        date = new Date(date.getTime() - 24 * 60 * 60 * 1000);
      }
      ParsedResult.maybeAppend(DateFormat.getDateInstance().format(date.getTime()), result);
    } else {
      // The when string can be local time, or UTC if it ends with a Z
      Date date;
      synchronized (DATE_TIME_FORMAT) {
       date = DATE_TIME_FORMAT.parse(when.substring(0, 15), new ParsePosition(0));
      }
      long milliseconds = date.getTime();
      if (when.length() == 16 && when.charAt(15) == 'Z') {
        Calendar calendar = new GregorianCalendar();
        int offset = calendar.get(Calendar.ZONE_OFFSET) + calendar.get(Calendar.DST_OFFSET);
        milliseconds += offset;
      }
      ParsedResult.maybeAppend(DateFormat.getDateTimeInstance().format(milliseconds), result);
    }
  }

  @Override
  public int getDisplayTitle() {
    return R.string.result_calendar;
  }
}<commit_msg>Issue 1195 fix accounting for DST when target time DST status differs from now<commit_after>/*
 * Copyright (C) 2008 ZXing authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.google.zxing.client.android.result;

import com.google.zxing.client.android.R;
import com.google.zxing.client.result.CalendarParsedResult;
import com.google.zxing.client.result.ParsedResult;

import android.app.Activity;

import java.text.DateFormat;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;

/**
 * Handles calendar entries encoded in QR Codes.
 *
 * @author <EMAIL> (<NAME>)
 */
public final class CalendarResultHandler extends ResultHandler {

  private static final DateFormat DATE_FORMAT = new SimpleDateFormat("yyyyMMdd", Locale.ENGLISH);
  private static final DateFormat DATE_TIME_FORMAT = new SimpleDateFormat("yyyyMMdd'T'HHmmss", Locale.ENGLISH);

  private static final int[] buttons = {
      R.string.button_add_calendar
  };

  public CalendarResultHandler(Activity activity, ParsedResult result) {
    super(activity, result);
  }

  @Override
  public int getButtonCount() {
    return buttons.length;
  }

  @Override
  public int getButtonText(int index) {
    return buttons[index];
  }

  @Override
  public void handleButtonPress(int index) {
    CalendarParsedResult calendarResult = (CalendarParsedResult) getResult();
    if (index == 0) {
      addCalendarEvent(calendarResult.getSummary(),
                       calendarResult.getStart(),
                       calendarResult.getEnd(),
                       calendarResult.getLocation(),
                       calendarResult.getDescription());
    }
  }

  @Override
  public CharSequence getDisplayContents() {
    CalendarParsedResult calResult = (CalendarParsedResult) getResult();
    StringBuilder result = new StringBuilder(100);
    ParsedResult.maybeAppend(calResult.getSummary(), result);
    String startString = calResult.getStart();
    appendTime(startString, result, false, false);

    String endString = calResult.getEnd();
    if (endString != null) {
      boolean sameStartEnd = startString.equals(endString);
      appendTime(endString, result, true, sameStartEnd);
    }

    ParsedResult.maybeAppend(calResult.getLocation(), result);
    ParsedResult.maybeAppend(calResult.getAttendee(), result);
    ParsedResult.maybeAppend(calResult.getDescription(), result);
    return result.toString();
  }

  private static void appendTime(String when, StringBuilder result, boolean end, boolean sameStartEnd) {
    if (when.length() == 8) {
      // Show only year/month/day
      Date date;
      synchronized (DATE_FORMAT) {
        date = DATE_FORMAT.parse(when, new ParsePosition(0));
      }
      // if it's all-day and this is the end date, it's exclusive, so show the user
      // that it ends on the day before to make more intuitive sense.
      // But don't do it if the event already (incorrectly?) specifies the same start/end
      if (end && !sameStartEnd) {
        date = new Date(date.getTime() - 24 * 60 * 60 * 1000);
      }
      ParsedResult.maybeAppend(DateFormat.getDateInstance().format(date.getTime()), result);
    } else {
      // The when string can be local time, or UTC if it ends with a Z
      Date date;
      synchronized (DATE_TIME_FORMAT) {
       date = DATE_TIME_FORMAT.parse(when.substring(0, 15), new ParsePosition(0));
      }
      long milliseconds = date.getTime();
      if (when.length() == 16 && when.charAt(15) == 'Z') {
        Calendar calendar = new GregorianCalendar();
        // Account for time zone difference
        milliseconds += calendar.get(Calendar.ZONE_OFFSET);
        // Might need to correct for daylight savings time, but use target time since
        // now might be in DST but not then, or vice versa
        calendar.setTime(new Date(milliseconds));
        milliseconds += calendar.get(Calendar.DST_OFFSET);
      }
      ParsedResult.maybeAppend(DateFormat.getDateTimeInstance().format(milliseconds), result);
    }
  }

  @Override
  public int getDisplayTitle() {
    return R.string.result_calendar;
  }
}
{'fraction_non_alphanumeric': 0.0997157227203149, 'fraction_numerical': 0.007434944237918215, 'mean_word_length': 3.4145752895752897, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2562281', 'n_tokens_mistral': 2615, 'n_tokens_neox': 2494, 'n_words': 748}