properly handle daylight savings

This commit is contained in:
tibbi 2018-03-14 09:43:37 +01:00
parent b3873e726c
commit b6e0d8381f
3 changed files with 14 additions and 5 deletions

View File

@ -83,8 +83,13 @@ class TimeZonesAdapter(activity: SimpleActivity, var timeZones: ArrayList<MyTime
}
private fun setupView(view: View, timeZone: MyTimeZone) {
val calendar = Calendar.getInstance(TimeZone.getTimeZone(timeZone.zoneName))
val offset = calendar.timeZone.rawOffset
val currTimeZone = TimeZone.getTimeZone(timeZone.zoneName)
val calendar = Calendar.getInstance(currTimeZone)
var offset = calendar.timeZone.rawOffset
val isDaylightSavingActive = currTimeZone.inDaylightTime(Date())
if (isDaylightSavingActive) {
offset += currTimeZone.dstSavings
}
val passedSeconds = ((calendar.timeInMillis + offset) / 1000).toInt()
val formattedTime = activity.getFormattedTime(passedSeconds, false, false)
val formattedDate = activity.getFormattedDate(calendar)

View File

@ -15,6 +15,7 @@ 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.helpers.getPassedSeconds
import com.simplemobiletools.clock.models.MyTimeZone
import com.simplemobiletools.commons.extensions.beVisibleIf
import com.simplemobiletools.commons.extensions.updateTextColors
@ -62,8 +63,7 @@ class ClockFragment : Fragment() {
private fun setupDateTime() {
calendar = Calendar.getInstance()
val offset = calendar.timeZone.rawOffset
passedSeconds = ((calendar.timeInMillis + offset) / 1000).toInt()
passedSeconds = getPassedSeconds()
displayOtherTimeZones = context!!.config.displayOtherTimeZones
updateCurrentTime()
updateDate()

View File

@ -48,7 +48,11 @@ fun getMSTillNextMinute(): Long {
fun getPassedSeconds(): Int {
val calendar = Calendar.getInstance()
val offset = calendar.timeZone.rawOffset
val isDaylightSavingActive = TimeZone.getDefault().inDaylightTime(Date())
var offset = calendar.timeZone.rawOffset
if (isDaylightSavingActive) {
offset += TimeZone.getDefault().dstSavings
}
return ((calendar.timeInMillis + offset) / 1000).toInt()
}