Skip to content

Commit 7ebaa9c

Browse files
committed
Add OffsetDateTime support
1 parent 9759336 commit 7ebaa9c

File tree

2 files changed

+22
-13
lines changed

2 files changed

+22
-13
lines changed

studyplus-android-sdk/build.gradle

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,16 @@ android {
3030
}
3131

3232
compileOptions {
33+
coreLibraryDesugaringEnabled true
34+
3335
sourceCompatibility = 1.8
3436
targetCompatibility = 1.8
3537
}
3638
}
3739

3840
dependencies {
41+
coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:1.1.1'
42+
3943
implementation "androidx.annotation:annotation:1.1.0"
4044

4145
implementation platform("com.squareup.okhttp3:okhttp-bom:4.9.1")

studyplus-android-sdk/src/main/java/jp/studyplus/android/sdk/record/StudyRecord.kt

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ package jp.studyplus.android.sdk.record
22

33
import androidx.annotation.IntRange
44
import org.json.JSONObject
5-
import java.text.SimpleDateFormat
5+
import java.time.OffsetDateTime
6+
import java.time.ZoneOffset
7+
import java.time.format.DateTimeFormatter
68
import java.util.*
79

810
/**
@@ -20,14 +22,27 @@ data class StudyRecord @JvmOverloads constructor(
2022
val duration: Int,
2123
val amount: StudyRecordAmount? = null,
2224
val comment: String? = null,
23-
val recordedTime: Calendar = Calendar.getInstance(DATE_TIME_ZONE, DATE_LOCALE)
25+
val recordedTime: OffsetDateTime = OffsetDateTime.now()
2426
) {
2527

28+
constructor(
29+
@IntRange(from = 0L, to = DURATION_RANGE_MAX_24H)
30+
duration: Int,
31+
amount: StudyRecordAmount? = null,
32+
comment: String? = null,
33+
recordedTime: Calendar
34+
) : this(
35+
duration = duration,
36+
amount = amount,
37+
comment = comment,
38+
recordedTime = recordedTime.toInstant().atOffset(ZoneOffset.UTC)
39+
)
40+
2641
internal fun toJson(): String = if (duration > DURATION_RANGE_MAX_24H) {
2742
throw IllegalArgumentException("duration must be 24 hours or less")
2843
} else {
2944
JSONObject().apply {
30-
put("recorded_at", formatTime(recordedTime))
45+
put("record_datetime", recordedTime.format(DateTimeFormatter.ISO_OFFSET_DATE_TIME))
3146
put("duration", duration)
3247
comment?.let { put("comment", it) }
3348
amount?.let { studyRecordAmount ->
@@ -40,16 +55,6 @@ data class StudyRecord @JvmOverloads constructor(
4055

4156
companion object {
4257
private const val DURATION_RANGE_MAX_24H = 24L * 60L * 60L
43-
44-
private const val DATE_FORMAT = "yyyy'-'MM'-'dd' 'HH':'mm':'ss"
45-
private val DATE_LOCALE: Locale = Locale.US
46-
private val DATE_TIME_ZONE: TimeZone = TimeZone.getTimeZone("UTC")
47-
48-
internal fun formatTime(calendar: Calendar): String {
49-
val format = SimpleDateFormat(DATE_FORMAT, DATE_LOCALE)
50-
format.timeZone = calendar.timeZone
51-
return format.format(calendar.time)
52-
}
5358
}
5459

5560
}

0 commit comments

Comments
 (0)