From 0b26b8274a95da6a240b6ae3f2c7dfe27e7c0bc0 Mon Sep 17 00:00:00 2001 From: zhxycn Date: Sun, 26 Jul 2026 17:16:33 +0800 Subject: [PATCH 1/3] =?UTF-8?q?:recycle:=20refactor:=20=E7=B2=BE=E7=AE=80?= =?UTF-8?q?=E5=B0=8F=E7=BB=84=E4=BB=B6=E6=97=A5=E6=9C=9F=E8=AE=A1=E7=AE=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/dev/tokenteam/iwut/widget/ScheduleData.kt | 14 +++++++++----- targets/widget/Models/WidgetData.swift | 5 ++--- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/modules/widget/android/src/main/java/dev/tokenteam/iwut/widget/ScheduleData.kt b/modules/widget/android/src/main/java/dev/tokenteam/iwut/widget/ScheduleData.kt index 1a11ea4..3d51084 100644 --- a/modules/widget/android/src/main/java/dev/tokenteam/iwut/widget/ScheduleData.kt +++ b/modules/widget/android/src/main/java/dev/tokenteam/iwut/widget/ScheduleData.kt @@ -8,6 +8,7 @@ import com.google.gson.Gson import com.google.gson.annotations.SerializedName import java.text.SimpleDateFormat import java.util.Calendar +import java.util.Date import java.util.Locale import java.util.concurrent.TimeUnit @@ -82,20 +83,22 @@ object ScheduleData { return if (locales.isEmpty) Locale.getDefault() else locales.get(0) } - fun getCurrentWeek(termStart: String): Int { + private fun getWeek(termStart: String, now: Date): Int { val sdf = SimpleDateFormat("yyyy-MM-dd", Locale.US) val startDate = try { sdf.parse(termStart) ?: return 1 } catch (e: Exception) { return 1 } - val now = Calendar.getInstance().time val diffMs = now.time - startDate.time if (diffMs < 0) return 0 val diffDays = TimeUnit.MILLISECONDS.toDays(diffMs) return (diffDays / 7 + 1).toInt() } + fun getCurrentWeek(termStart: String): Int = + getWeek(termStart, Calendar.getInstance().time) + fun getDayOfWeek(): Int { val cal = Calendar.getInstance() val dow = cal.get(Calendar.DAY_OF_WEEK) @@ -108,9 +111,10 @@ object ScheduleData { } fun getTomorrowWeek(termStart: String): Int { - val today = getDayOfWeek() - val week = getCurrentWeek(termStart) - return if (today == 7) week + 1 else week + val tomorrow = Calendar.getInstance().apply { + add(Calendar.DAY_OF_YEAR, 1) + }.time + return getWeek(termStart, tomorrow) } fun getWeekStr(context: Context, week: Int): String = diff --git a/targets/widget/Models/WidgetData.swift b/targets/widget/Models/WidgetData.swift index a22adbe..c97a80a 100644 --- a/targets/widget/Models/WidgetData.swift +++ b/targets/widget/Models/WidgetData.swift @@ -122,9 +122,8 @@ struct ScheduleHelper { } static func tomorrowWeek(termStart: String, now: Date) -> Int { - let today = dayOfWeek(for: now) - let week = currentWeek(termStart: termStart, now: now) - return today == 7 ? week + 1 : week + let tomorrow = Calendar.current.date(byAdding: .day, value: 1, to: now) ?? now + return currentWeek(termStart: termStart, now: tomorrow) } static func weekStr(week: Int) -> String { From 4ff76523e25f66aebf14e69d3853c31fabaee06d Mon Sep 17 00:00:00 2001 From: zhxycn Date: Sun, 26 Jul 2026 17:24:31 +0800 Subject: [PATCH 2/3] =?UTF-8?q?:bug:=20fix:=20=E6=8C=89=E6=9C=AC=E5=9C=B0?= =?UTF-8?q?=E6=97=A5=E6=9C=9F=E8=AE=A1=E7=AE=97=E5=B0=8F=E7=BB=84=E4=BB=B6?= =?UTF-8?q?=E5=91=A8=E6=AC=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../dev/tokenteam/iwut/widget/ScheduleData.kt | 34 +++++++++++++------ 1 file changed, 23 insertions(+), 11 deletions(-) diff --git a/modules/widget/android/src/main/java/dev/tokenteam/iwut/widget/ScheduleData.kt b/modules/widget/android/src/main/java/dev/tokenteam/iwut/widget/ScheduleData.kt index 3d51084..ba678f8 100644 --- a/modules/widget/android/src/main/java/dev/tokenteam/iwut/widget/ScheduleData.kt +++ b/modules/widget/android/src/main/java/dev/tokenteam/iwut/widget/ScheduleData.kt @@ -6,11 +6,10 @@ import android.content.res.Configuration import android.os.Build import com.google.gson.Gson import com.google.gson.annotations.SerializedName -import java.text.SimpleDateFormat import java.util.Calendar import java.util.Date import java.util.Locale -import java.util.concurrent.TimeUnit +import java.util.TimeZone data class WidgetCourse( @SerializedName("name") val name: String = "", @@ -84,18 +83,31 @@ object ScheduleData { } private fun getWeek(termStart: String, now: Date): Int { - val sdf = SimpleDateFormat("yyyy-MM-dd", Locale.US) - val startDate = try { - sdf.parse(termStart) ?: return 1 - } catch (e: Exception) { - return 1 - } - val diffMs = now.time - startDate.time - if (diffMs < 0) return 0 - val diffDays = TimeUnit.MILLISECONDS.toDays(diffMs) + val match = Regex("^(\\d{4})-(\\d{1,2})-(\\d{1,2})").find(termStart) ?: return 1 + val startDay = normalizedDay( + match.groupValues[1].toInt(), + match.groupValues[2].toInt() - 1, + match.groupValues[3].toInt(), + ) + val nowCalendar = Calendar.getInstance().apply { time = now } + val nowDay = normalizedDay( + nowCalendar.get(Calendar.YEAR), + nowCalendar.get(Calendar.MONTH), + nowCalendar.get(Calendar.DAY_OF_MONTH), + ) + val diffDays = (nowDay - startDay) / DAY_MS + if (diffDays < 0) return 0 return (diffDays / 7 + 1).toInt() } + private fun normalizedDay(year: Int, month: Int, day: Int): Long = + Calendar.getInstance(TimeZone.getTimeZone("UTC")).apply { + clear() + set(year, month, day) + }.timeInMillis + + private const val DAY_MS = 24 * 60 * 60 * 1000L + fun getCurrentWeek(termStart: String): Int = getWeek(termStart, Calendar.getInstance().time) From 4e0e846e2349383b945af550c5bf2eaaffb61b04 Mon Sep 17 00:00:00 2001 From: zhxycn Date: Sun, 26 Jul 2026 17:31:42 +0800 Subject: [PATCH 3/3] =?UTF-8?q?:bug:=20fix:=20=E4=BD=BF=E7=94=A8=E5=85=AC?= =?UTF-8?q?=E5=8E=86=E8=AE=A1=E7=AE=97=E5=B0=8F=E7=BB=84=E4=BB=B6=E6=97=A5?= =?UTF-8?q?=E6=9C=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../dev/tokenteam/iwut/widget/ScheduleData.kt | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/modules/widget/android/src/main/java/dev/tokenteam/iwut/widget/ScheduleData.kt b/modules/widget/android/src/main/java/dev/tokenteam/iwut/widget/ScheduleData.kt index ba678f8..11bfc49 100644 --- a/modules/widget/android/src/main/java/dev/tokenteam/iwut/widget/ScheduleData.kt +++ b/modules/widget/android/src/main/java/dev/tokenteam/iwut/widget/ScheduleData.kt @@ -8,6 +8,7 @@ import com.google.gson.Gson import com.google.gson.annotations.SerializedName import java.util.Calendar import java.util.Date +import java.util.GregorianCalendar import java.util.Locale import java.util.TimeZone @@ -89,7 +90,7 @@ object ScheduleData { match.groupValues[2].toInt() - 1, match.groupValues[3].toInt(), ) - val nowCalendar = Calendar.getInstance().apply { time = now } + val nowCalendar = localCalendar().apply { time = now } val nowDay = normalizedDay( nowCalendar.get(Calendar.YEAR), nowCalendar.get(Calendar.MONTH), @@ -101,18 +102,21 @@ object ScheduleData { } private fun normalizedDay(year: Int, month: Int, day: Int): Long = - Calendar.getInstance(TimeZone.getTimeZone("UTC")).apply { + GregorianCalendar(TimeZone.getTimeZone("UTC"), Locale.US).apply { clear() set(year, month, day) }.timeInMillis + private fun localCalendar(): Calendar = + GregorianCalendar(TimeZone.getDefault(), Locale.US) + private const val DAY_MS = 24 * 60 * 60 * 1000L fun getCurrentWeek(termStart: String): Int = - getWeek(termStart, Calendar.getInstance().time) + getWeek(termStart, Date()) fun getDayOfWeek(): Int { - val cal = Calendar.getInstance() + val cal = localCalendar() val dow = cal.get(Calendar.DAY_OF_WEEK) return if (dow == Calendar.SUNDAY) 7 else dow - 1 } @@ -123,7 +127,7 @@ object ScheduleData { } fun getTomorrowWeek(termStart: String): Int { - val tomorrow = Calendar.getInstance().apply { + val tomorrow = localCalendar().apply { add(Calendar.DAY_OF_YEAR, 1) }.time return getWeek(termStart, tomorrow) @@ -133,7 +137,7 @@ object ScheduleData { context.getString(R.string.widget_week_n, week) fun getDateStr(context: Context): String { - val cal = Calendar.getInstance() + val cal = localCalendar() return context.getString( R.string.widget_month_day, cal.get(Calendar.MONTH) + 1,