Orbit TV Times: every TV starts at Issue 1. Ported from release-0.108 (0.110.5)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -82,7 +82,8 @@ fetch('/api/times', {credentials: 'same-origin'}).then(r => { if (!r.ok) throw n
|
||||
// A saved edition opens at today's page, as you'd turn to it in the magazine.
|
||||
const today = new Date(); const iso = today.getFullYear() + '-' + String(today.getMonth() + 1).padStart(2, '0') + '-' + String(today.getDate()).padStart(2, '0');
|
||||
shown = Math.max(0, j.days.findIndex(d => d.date === iso));
|
||||
if (a) $('#issue').textContent = (j.issue ? 'Issue ' + j.issue + ' · ' : '') + new Date(a.date + 'T12:00:00').toLocaleDateString('en-GB', {day: 'numeric', month: 'short'}) + ' – ' + new Date(b.date + 'T12:00:00').toLocaleDateString('en-GB', {day: 'numeric', month: 'short'});
|
||||
// "Week of" its first day, so a first edition that starts mid-week says so (8 November 2026).
|
||||
if (a) $('#issue').textContent = (j.issue ? 'Issue ' + j.issue + ' · ' : '') + 'Week of ' + new Date(a.date + 'T12:00:00').toLocaleDateString('en-GB', {weekday: 'short', day: 'numeric', month: 'short'});
|
||||
render();
|
||||
}).catch(() => { $('#main').innerHTML = '<div class="empty">Orbit isn\'t answering. Open Orbit on the TV and try again.</div>'; });
|
||||
</script>
|
||||
|
||||
@@ -237,7 +237,9 @@ internal object OrbitCompanion {
|
||||
val who = (Profiles.current?.id ?: "all").filter { it.isLetterOrDigit() }.take(20).ifEmpty { "all" } + if (AfterDarkLock.locked) "-locked" else ""
|
||||
val file = dir?.let { java.io.File(it, "times-$sunday-$who.json") }
|
||||
file?.takeIf { it.isFile }?.let { f -> runCatching { JSONObject(f.readText()) }.getOrNull()?.takeIf { it.optInt("v") == TIMES_EDITION_V } }?.let { return it }
|
||||
val fresh = times(screen, nowMs, zone).put("v", TIMES_EDITION_V).put("issue", timesIssue(sunday)).put("weekOf", sunday.toString())
|
||||
// Issues count from this TV's own first edition (8 November 2026), so every household starts at Issue 1.
|
||||
val firstSunday = dir?.let { timesFirstSunday(it, sunday) } ?: sunday
|
||||
val fresh = times(screen, nowMs, zone).put("v", TIMES_EDITION_V).put("issue", timesIssue(sunday, firstSunday)).put("weekOf", sunday.toString())
|
||||
if (file != null && fresh.getJSONArray("days").length() > 0) runCatching {
|
||||
writeSafely(file, fresh.toString())
|
||||
dir.listFiles { f -> f.name.startsWith("times-") && f.name.endsWith(".json") }?.filter { it.name.substring(6, 16) < sunday.minusWeeks(TIMES_KEEP_WEEKS).toString() }?.forEach { it.delete() }
|
||||
@@ -356,5 +358,20 @@ internal object TimesBlurbs {
|
||||
/** The Sunday an edition belongs to: the Sunday on or before [day]. */
|
||||
internal fun timesSunday(day: java.time.LocalDate): java.time.LocalDate = day.minusDays((day.dayOfWeek.value % 7).toLong())
|
||||
|
||||
/** Issue 1 is the week of Sunday 8 November 2026, when Orbit TV Times began. */
|
||||
internal fun timesIssue(sunday: java.time.LocalDate): Long = java.time.temporal.ChronoUnit.WEEKS.between(java.time.LocalDate.of(2026, 11, 8), sunday) + 1
|
||||
/** The issue for the week of [sunday], counting this TV's first edition (the week of [firstSunday]) as Issue 1. */
|
||||
internal fun timesIssue(sunday: java.time.LocalDate, firstSunday: java.time.LocalDate): Long =
|
||||
maxOf(0L, java.time.temporal.ChronoUnit.WEEKS.between(firstSunday, sunday)) + 1
|
||||
|
||||
/**
|
||||
* The Sunday of this TV's first edition, kept in times-first; set the first time an edition is made (or, for a TV that
|
||||
* already had editions before this was kept, the oldest one still there).
|
||||
*/
|
||||
internal fun timesFirstSunday(dir: java.io.File, sunday: java.time.LocalDate): java.time.LocalDate {
|
||||
val marker = java.io.File(dir, "times-first")
|
||||
runCatching { java.time.LocalDate.parse(marker.readText().trim()) }.getOrNull()?.let { return it }
|
||||
val oldest = dir.listFiles { f -> Regex("times-\\d{4}-\\d{2}-\\d{2}-.+\\.json").matches(f.name) }.orEmpty()
|
||||
.mapNotNull { runCatching { java.time.LocalDate.parse(it.name.substring(6, 16)) }.getOrNull() }.minOrNull()
|
||||
val first = listOfNotNull(oldest, sunday).min()
|
||||
runCatching { writeSafely(marker, first.toString()) }
|
||||
return first
|
||||
}
|
||||
|
||||
@@ -36,7 +36,11 @@ class UpNextAndTimesTest {
|
||||
assertEquals(java.time.LocalDate.of(2026, 11, 8), timesSunday(java.time.LocalDate.of(2026, 11, 8)))
|
||||
assertEquals(java.time.LocalDate.of(2026, 11, 8), timesSunday(java.time.LocalDate.of(2026, 11, 14)))
|
||||
assertEquals(java.time.LocalDate.of(2026, 11, 15), timesSunday(java.time.LocalDate.of(2026, 11, 15)))
|
||||
assertEquals(1L, timesIssue(java.time.LocalDate.of(2026, 11, 8))); assertEquals(2L, timesIssue(java.time.LocalDate.of(2026, 11, 15)))
|
||||
val first8 = java.time.LocalDate.of(2026, 11, 8)
|
||||
assertEquals(1L, timesIssue(first8, first8)); assertEquals(2L, timesIssue(java.time.LocalDate.of(2026, 11, 15), first8))
|
||||
// A household that starts later starts at Issue 1 too.
|
||||
val late = java.time.LocalDate.of(2026, 11, 29)
|
||||
assertEquals(1L, timesIssue(late, late)); assertEquals(2L, timesIssue(java.time.LocalDate.of(2026, 12, 6), late))
|
||||
val zone = ZoneId.of("Europe/London"); val dir = java.nio.file.Files.createTempDirectory("times").toFile()
|
||||
val day = ZonedDateTime.of(2026, 11, 9, 13, 0, 0, 0, zone).toInstant().toEpochMilli()
|
||||
val eight = ZonedDateTime.of(2026, 11, 9, 20, 0, 0, 0, zone).toInstant().toEpochMilli()
|
||||
@@ -54,6 +58,11 @@ class UpNextAndTimesTest {
|
||||
assertEquals(first.toString(), later.toString())
|
||||
// The next Sunday is a new issue.
|
||||
val next = OrbitCompanion.timesEdition(screenWith("Shaun of the Dead"), dir, ZonedDateTime.of(2026, 11, 15, 13, 0, 0, 0, zone).toInstant().toEpochMilli(), zone)
|
||||
assertEquals(2, next.optInt("issue", 2))
|
||||
assertEquals(2, next.getInt("issue"))
|
||||
// The first edition's Sunday is remembered for this TV.
|
||||
assertEquals("2026-11-08", java.io.File(dir, "times-first").readText())
|
||||
// A TV that started later counts from its own first week.
|
||||
val other = java.nio.file.Files.createTempDirectory("times2").toFile()
|
||||
assertEquals(1, OrbitCompanion.timesEdition(screenWith("Hot Fuzz"), other, ZonedDateTime.of(2026, 11, 25, 13, 0, 0, 0, zone).toInstant().toEpochMilli(), zone).getInt("issue"))
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user