@@ -2,7 +2,9 @@ package jp.studyplus.android.sdk.record
2
2
3
3
import androidx.annotation.IntRange
4
4
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
6
8
import java.util.*
7
9
8
10
/* *
@@ -20,14 +22,27 @@ data class StudyRecord @JvmOverloads constructor(
20
22
val duration : Int ,
21
23
val amount : StudyRecordAmount ? = null ,
22
24
val comment : String? = null ,
23
- val recordedTime : Calendar = Calendar .getInstance( DATE_TIME_ZONE , DATE_LOCALE )
25
+ val recordedTime : OffsetDateTime = OffsetDateTime .now( )
24
26
) {
25
27
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
+
26
41
internal fun toJson (): String = if (duration > DURATION_RANGE_MAX_24H ) {
27
42
throw IllegalArgumentException (" duration must be 24 hours or less" )
28
43
} else {
29
44
JSONObject ().apply {
30
- put(" recorded_at " , formatTime( recordedTime))
45
+ put(" record_datetime " , recordedTime.format( DateTimeFormatter . ISO_OFFSET_DATE_TIME ))
31
46
put(" duration" , duration)
32
47
comment?.let { put(" comment" , it) }
33
48
amount?.let { studyRecordAmount ->
@@ -40,16 +55,6 @@ data class StudyRecord @JvmOverloads constructor(
40
55
41
56
companion object {
42
57
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
- }
53
58
}
54
59
55
60
}
0 commit comments