add a helper extension function for getting formatted time

This commit is contained in:
tibbi 2018-03-11 22:50:36 +01:00
parent ef9c8fe0aa
commit 9bb9caf7c5
3 changed files with 21 additions and 17 deletions

View File

@ -9,6 +9,7 @@ import android.widget.SeekBar
import com.simplemobiletools.clock.R
import com.simplemobiletools.clock.extensions.config
import com.simplemobiletools.clock.extensions.getFormattedDate
import com.simplemobiletools.clock.extensions.getFormattedTime
import com.simplemobiletools.clock.helpers.MyWidgetDateTimeProvider
import com.simplemobiletools.commons.dialogs.ColorPickerDialog
import com.simplemobiletools.commons.extensions.adjustAlpha
@ -71,13 +72,7 @@ class WidgetDateTimeConfigureActivity : SimpleActivity() {
private fun updateCurrentDateTime() {
val calendar = Calendar.getInstance()
val offset = calendar.timeZone.rawOffset
val passedSeconds = ((calendar.timeInMillis + offset) / 1000).toInt()
val hours = (passedSeconds / 3600) % 24
val minutes = (passedSeconds / 60) % 60
val format = "%02d:%02d"
config_time.text = String.format(format, hours, minutes)
config_time.text = getFormattedTime(calendar, false)
config_date.text = getFormattedDate(calendar)
}

View File

@ -36,6 +36,23 @@ fun Context.getFormattedDate(calendar: Calendar): String {
return "$shortDayString, $dayOfMonth $monthString"
}
fun Context.getFormattedTime(calendar: Calendar, showSeconds: Boolean): String {
val offset = calendar.timeZone.rawOffset
val passedSeconds = ((calendar.timeInMillis + offset) / 1000).toInt()
val hours = (passedSeconds / 3600) % 24
val minutes = (passedSeconds / 60) % 60
val seconds = passedSeconds % 60
var format = "%02d:%02d"
return if (showSeconds) {
format += ":%02d"
String.format(format, hours, minutes, seconds)
} else {
String.format(format, hours, minutes)
}
}
fun Context.getEditedTimeZonesMap(): HashMap<Int, String> {
val editedTimeZoneTitles = config.editedTimeZoneTitles
val editedTitlesMap = HashMap<Int, String>()

View File

@ -14,6 +14,7 @@ import com.simplemobiletools.clock.dialogs.EditTimeZoneDialog
import com.simplemobiletools.clock.extensions.config
import com.simplemobiletools.clock.extensions.getAllTimeZonesModified
import com.simplemobiletools.clock.extensions.getFormattedDate
import com.simplemobiletools.clock.extensions.getFormattedTime
import com.simplemobiletools.clock.models.MyTimeZone
import com.simplemobiletools.commons.extensions.beVisibleIf
import com.simplemobiletools.commons.extensions.updateTextColors
@ -75,16 +76,7 @@ class ClockFragment : Fragment() {
val hours = (passedSeconds / 3600) % 24
val minutes = (passedSeconds / 60) % 60
val seconds = passedSeconds % 60
var format = "%02d:%02d"
val formattedText = if (context!!.config.showSeconds) {
format += ":%02d"
String.format(format, hours, minutes, seconds)
} else {
String.format(format, hours, minutes)
}
view.clock_time.text = formattedText
view.clock_time.text = context!!.getFormattedTime(calendar, context!!.config.showSeconds)
if (seconds == 0) {
if (hours == 0 && minutes == 0) {