OpenTimes.kt 2.4 KB
Newer Older
Robert Hitt's avatar
Robert Hitt committed
1
2
package srct.whatsopen.model

Dylan Jones's avatar
Dylan Jones committed
3
import android.util.Log
Robert Hitt's avatar
Robert Hitt committed
4
import com.google.gson.annotations.SerializedName
Dylan Jones's avatar
Dylan Jones committed
5
6
import java.util.*

Dylan Jones's avatar
Dylan Jones committed
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
// _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
}


Dylan Jones's avatar
Dylan Jones committed
28
29
30
31
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
Dylan Jones's avatar
Dylan Jones committed
32
33
    @SerializedName("start_time") var startTime: String,
    @SerializedName("end_time") var endTime: String
Dylan Jones's avatar
Dylan Jones committed
34
35
36
) {

    fun isOpen(): Boolean {
Dylan Jones's avatar
Dylan Jones committed
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
        // 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")
Robert Hitt's avatar
Robert Hitt committed
58
    }
Dylan Jones's avatar
Dylan Jones committed
59
}