Skip to content

Commit

Permalink
Fixing timestamp conversion bug (#9)
Browse files Browse the repository at this point in the history
* Fixing timestamp conversion bug

* Implementing new date formatter to fix timezone bug.
  • Loading branch information
rodiguif authored Nov 25, 2019
1 parent dbba23a commit e5e5149
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 14 deletions.
2 changes: 2 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,7 @@ dependencies {

implementation 'com.github.PhilJay:MPAndroidChart:v3.1.0'

implementation 'com.jakewharton.threetenabp:threetenabp:1.2.1'

implementation project(path: ':core')
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.oxeanbits.forecastchart.ui
import android.os.Bundle
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import com.jakewharton.threetenabp.AndroidThreeTen
import com.oxeanbits.forecastchart.core.ui.component.forecastChartComponent
import com.oxeanbits.forecastchart.util.SetupChartExample
import trikita.anvil.BaseDSL.MATCH
Expand All @@ -13,6 +14,7 @@ class MainExample : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
AndroidThreeTen.init(this)
setContentView(getView())
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,19 @@ import android.graphics.Color
import com.github.mikephil.charting.data.Entry
import com.github.mikephil.charting.utils.ColorTemplate
import com.oxeanbits.forecastchart.core.model.Line
import java.text.SimpleDateFormat
import com.oxeanbits.forecastchart.core.util.DateFormatter

object SetupChartExample{
const val THOUS = 1000f

val dateFormat = SimpleDateFormat("yyyy-MM-dd")

val timestamp1 = (dateFormat.parse("2019-01-01").time)/THOUS
val timestamp1 = DateFormatter.stringToTimestamp("2019-01-01")
val progress1 = 25f
val timestamp2 = (dateFormat.parse("2019-01-02").time)/THOUS
val timestamp2 = DateFormatter.stringToTimestamp("2019-01-02")
val progress2 = 25f + progress1
val timestamp3 = (dateFormat.parse("2019-01-03").time)/THOUS
val timestamp3 = DateFormatter.stringToTimestamp("2019-01-03")
val progress3 = 25f + progress2
val timestamp4 = (dateFormat.parse("2019-01-04").time)/THOUS
val timestamp4 = DateFormatter.stringToTimestamp("2019-01-04")
val progress4 = 0f + progress3
val timestamp5 = (dateFormat.parse("2019-01-05").time)/THOUS
val timestamp5 = DateFormatter.stringToTimestamp("2019-01-05")
val progress5 = 25f + progress4

fun getExpectedObj(): Line{
Expand Down
2 changes: 2 additions & 0 deletions core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,6 @@ dependencies {
implementation 'co.trikita:anvil-design:0.5.2'

implementation 'com.github.PhilJay:MPAndroidChart:v3.1.0'

implementation 'com.jakewharton.threetenabp:threetenabp:1.2.1'
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
package com.oxeanbits.forecastchart.core.util

import java.text.SimpleDateFormat
import java.util.Date
import org.threeten.bp.Instant
import org.threeten.bp.LocalDate
import org.threeten.bp.LocalDateTime
import org.threeten.bp.ZoneOffset
import org.threeten.bp.format.DateTimeFormatter


object DateFormatter{

fun stringToTimestamp(date: String): Float{
return LocalDate.parse(date).atStartOfDay()
.toInstant(ZoneOffset.UTC).epochSecond.toFloat()
}

fun timestampToDate(timestamp: Long): String{
val mDate = Date()
mDate.time = (timestamp.times(1000))
return SimpleDateFormat("yyyy-MM-dd").format(mDate)
val mDate = LocalDateTime.ofInstant(Instant.ofEpochSecond(timestamp), ZoneOffset.UTC)
val formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd")
return mDate.format(formatter)
}
}

0 comments on commit e5e5149

Please sign in to comment.