package srct.whatsopen.model import android.util.Log import com.google.gson.annotations.SerializedName import java.util.* // _Should_ return the number of seconds until the given time. fun secondsTillTime(hour: Int, minute: Int, day: Int): Int { val cal = Calendar.getInstance(TimeZone.getTimeZone("EST5EDT")) // Log.d( "timebug", "current: D ${(cal.get(Calendar.DAY_OF_WEEK) + Calendar.MONDAY) % 7} H ${cal.get(Calendar.HOUR_OF_DAY)} M ${cal.get( Calendar.MINUTE )}" ) val days = (day - (cal.get(Calendar.DAY_OF_WEEK) + Calendar.MONDAY) % 7) val hours = hour - cal.get(Calendar.HOUR_OF_DAY) val minutes = minute - cal.get(Calendar.MINUTE) val seconds = 60 - cal.get(Calendar.SECOND) // Log.d("timebug", "other : D $day H $hour M $minute") // Log.d("timebug", "delta : D $days H $hours M $minutes S $seconds") var time = days * (60 * 60 * 24) + (60 * 60 * hours) + (60 * minutes) + seconds if (time < 0) { time += (7 * 24 * 60 * 60) } // else { // Log.d("timebug", " good") // } // Log.d( "timebug", "final : D ${time / (24 * 60 * 60)} H ${(time % (24 * 60 * 60)) / (60 * 60)} M ${(time % 3600) / 60} S ${time % 60}\n" ) return time } data class OpenTimes( @SerializedName("start_day") var startDay: Int = 0, @SerializedName("end_day") var endDay: Int = 0, // These really should be Dates or something but idk exactly how to represent them @SerializedName("start_time") var startTime: String, @SerializedName("end_time") var endTime: String ) { fun isOpen(): Boolean { // TODO this is _broken_ // Log.d( "srct.whatsopen", "start $startTime end $endTime ttc ${timeTillClose()} tto ${timeTillOpen()}" ) return timeTillClose() < timeTillOpen() } fun timeTillOpen(): Int { val starts = startTime.split(":") return secondsTillTime(starts[0].toInt(), starts[1].toInt(), startDay) } fun timeTillClose(): Int { val ends = endTime.split(":") return secondsTillTime(ends[0].toInt(), ends[1].toInt(), endDay) } override fun toString(): String { val sh = startTime.split(":")[0].toInt() val sm = startTime.split(":")[1] val eh = endTime.split(":")[0].toInt() val em = endTime.split(":")[1] return (if (sh < 12) "$sh:${sm}am" else "${(sh - 12)}:${sm}pm") + "-" + (if (eh < 12) "$eh:${em}am" else "${eh - 12}:${em}pm") } }