@@ -2,7 +2,9 @@ package jp.studyplus.android.sdk.record
22
33import androidx.annotation.IntRange
44import org.json.JSONObject
5- import java.text.SimpleDateFormat
5+ import java.time.OffsetDateTime
6+ import java.time.ZoneOffset
7+ import java.time.format.DateTimeFormatter
68import 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