Bimba.git

ref: 39faa85c5d0e2ec6212f990934bc974a8783effd

app/src/main/java/ml/adamsprogs/bimba/extensions.kt


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package ml.adamsprogs.bimba

import java.text.SimpleDateFormat
import java.util.*

internal fun String.toPascalCase(): String { //check
    val builder = StringBuilder(this)
    var isLastCharSeparator = true
    builder.forEach {
        isLastCharSeparator = if ((it in 'a'..'z' || it in 'A'..'Z') && isLastCharSeparator) {
            it.toUpperCase()
            false
        } else
            true
    }
    return builder.toString()
}

internal fun Calendar.rollTime(seconds: Int): Calendar {
    val hour = seconds / 3600
    val minute = (seconds % 3600) / 60
    val second = (seconds % 60)
    this.set(Calendar.HOUR_OF_DAY, hour)
    this.set(Calendar.MINUTE, minute)
    this.set(Calendar.SECOND, second)
    this.set(Calendar.MILLISECOND, 0)
    return this
}

internal fun Calendar.secondsAfterMidnight(): Int {
    val hour = this.get(Calendar.HOUR_OF_DAY)
    val minute = this.get(Calendar.MINUTE)
    val second = this.get(Calendar.SECOND)
    return hour * 3600 + minute * 60 + second
}

const val ISO_8601_DATE_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"

fun calendarFromIso(iso: String): Calendar { // check
    val calendar = Calendar.getInstance()
    val dateFormat = SimpleDateFormat(ISO_8601_DATE_FORMAT)
    val date = dateFormat.parse(iso)
    //date.hours = date.getHours() - 1 //fixme why?
    calendar.time = date
    return calendar
}