<commit_before>// Copyright (c) 2016, <NAME> <<EMAIL>>
// See LICENSE for licensing information

package cat.mvmike.minimalcalendarwidget.status;

import java.util.Calendar;

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;

public final class CalendarStatus {

    private static final String PREF_MONTH = "month";

    private static final String PREF_YEAR = "year";

    private static final int MONTH_FIRST_DAY = 1;

    private static final int DAYS_IN_WEEK = 7;

    private static final int DECEMBER_LAST_DAY = 31;

    private final int today;

    private final int todayYear;

    private final int thisMonth;

    private final int thisYear;

    private final Calendar calendar;

    public CalendarStatus(final Context context, final Calendar cal, final int firstDayOfWeek) {

        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);

        today = cal.get(Calendar.DAY_OF_YEAR);
        todayYear = cal.get(Calendar.YEAR);
        thisMonth = sp.getInt(PREF_MONTH, cal.get(Calendar.MONTH));
        thisYear = sp.getInt(PREF_YEAR, cal.get(Calendar.YEAR));

        cal.set(Calendar.DAY_OF_MONTH, MONTH_FIRST_DAY);
        cal.set(Calendar.MONTH, thisMonth);
        cal.set(Calendar.YEAR, thisYear);

        int monthStartDayOfWeek = cal.get(Calendar.DAY_OF_WEEK);

        // overlap year manually if january and not standard start week day
        if (thisMonth == Calendar.JANUARY && firstDayOfWeek != Calendar.SUNDAY) {

            cal.set(Calendar.DAY_OF_MONTH, DECEMBER_LAST_DAY - DAYS_IN_WEEK + firstDayOfWeek);
            cal.set(Calendar.MONTH, Calendar.DECEMBER);
            cal.set(Calendar.YEAR, thisYear - 1);

        } else {

            cal.add(Calendar.DAY_OF_MONTH, firstDayOfWeek - monthStartDayOfWeek);
        }

        // overlap month manually if dayOfMonth is in current month and greater than 1
        if (cal.get(Calendar.DAY_OF_MONTH) > MONTH_FIRST_DAY
                && cal.get(Calendar.DAY_OF_MONTH) < (DECEMBER_LAST_DAY / 2)) {
            cal.add(Calendar.DAY_OF_MONTH, -DAYS_IN_WEEK);
        }

        calendar = cal;
    }

    public int getToday() {
        return today;
    }

    public int getTodayYear() {
        return todayYear;
    }

    public int getThisMonth() {
        return thisMonth;
    }

    public int getThisYear() {
        return thisYear;
    }
<commit_msg>fixes #24 - fix wrong first day of week on january<commit_after>// Copyright (c) 2016, <NAME> <<EMAIL>>
// See LICENSE for licensing information

package cat.mvmike.minimalcalendarwidget.status;

import java.util.Calendar;

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;

public final class CalendarStatus {

    private static final String PREF_MONTH = "month";

    private static final String PREF_YEAR = "year";

    private static final int MONTH_FIRST_DAY = 1;

    private static final int DAYS_IN_WEEK = 7;

    private static final int DECEMBER_LAST_DAY = 31;

    private final int today;

    private final int todayYear;

    private final int thisMonth;

    private final int thisYear;

    private final Calendar calendar;

    public CalendarStatus(final Context context, final Calendar cal, final int firstDayOfWeek) {

        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);

        today = cal.get(Calendar.DAY_OF_YEAR);
        todayYear = cal.get(Calendar.YEAR);
        thisMonth = sp.getInt(PREF_MONTH, cal.get(Calendar.MONTH));
        thisYear = sp.getInt(PREF_YEAR, cal.get(Calendar.YEAR));

        cal.set(Calendar.DAY_OF_MONTH, MONTH_FIRST_DAY);
        cal.set(Calendar.MONTH, thisMonth);
        cal.set(Calendar.YEAR, thisYear);

        int monthStartDayOfWeek = cal.get(Calendar.DAY_OF_WEEK);
        cal.add(Calendar.DAY_OF_MONTH, firstDayOfWeek - monthStartDayOfWeek);

        // overlap month manually if dayOfMonth is in current month and greater than 1
        if (cal.get(Calendar.DAY_OF_MONTH) > MONTH_FIRST_DAY
                && cal.get(Calendar.DAY_OF_MONTH) < (DECEMBER_LAST_DAY / 2)) {
            cal.add(Calendar.DAY_OF_MONTH, -DAYS_IN_WEEK);
        }

        calendar = cal;
    }

    public int getToday() {
        return today;
    }

    public int getTodayYear() {
        return todayYear;
    }

    public int getThisMonth() {
        return thisMonth;
    }

    public int getThisYear() {
        return thisYear;
    }

{'fraction_non_alphanumeric': 0.08651511807548003, 'fraction_numerical': 0.005076142131979695, 'mean_word_length': 2.9204152249134947, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '144451', 'n_tokens_mistral': 1435, 'n_tokens_neox': 1333, 'n_words': 341}