Created
November 11, 2024 21:30
-
-
Save jordanbeck/4b1d4a5be6d8d915b05f6e02af5891b8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| diff --git a/app/src/main/java/com/etsy/android/interview/Message.kt b/app/src/main/java/com/etsy/android/interview/Message.kt | |
| index 4760acd..762da20 100644 | |
| --- a/app/src/main/java/com/etsy/android/interview/Message.kt | |
| +++ b/app/src/main/java/com/etsy/android/interview/Message.kt | |
| @@ -1,6 +1,9 @@ | |
| package com.etsy.android.interview | |
| import java.text.SimpleDateFormat | |
| +import java.time.ZoneId | |
| +import java.util.Locale | |
| +import java.util.TimeZone | |
| data class Message( | |
| val content: String, | |
| @@ -11,7 +14,12 @@ data class Message( | |
| /** | |
| * Use a time formatter (e.g. [SimpleDateFormat]) to create a human readable time string | |
| */ | |
| - fun formatTime(): String { | |
| - return "" | |
| + fun formatTime( | |
| + locale: Locale = Locale.getDefault(), | |
| + timezone: TimeZone = TimeZone.getTimeZone(ZoneId.systemDefault()) | |
| + ): String { | |
| + val format = SimpleDateFormat("MMM d, h:mm aaa", locale) | |
| + format.timeZone = timezone | |
| + return format.format(time) | |
| } | |
| } | |
| \ No newline at end of file | |
| diff --git a/app/src/test/java/com/etsy/android/interview/ui/MessageTest.kt b/app/src/test/java/com/etsy/android/interview/ui/MessageTest.kt | |
| index b3b5c00..5268cba 100644 | |
| --- a/app/src/test/java/com/etsy/android/interview/ui/MessageTest.kt | |
| +++ b/app/src/test/java/com/etsy/android/interview/ui/MessageTest.kt | |
| @@ -3,6 +3,8 @@ package com.etsy.android.interview.ui | |
| import com.etsy.android.interview.Message | |
| import org.junit.Assert.assertEquals | |
| import org.junit.Test | |
| +import java.util.Locale | |
| +import java.util.TimeZone | |
| class MessageTest { | |
| @Test | |
| @@ -14,10 +16,12 @@ class MessageTest { | |
| // Given | |
| val message = Message( | |
| content = "Hello", | |
| - time = 1630719120000, | |
| + time = 1704122100000, | |
| isFromUser = false | |
| ) | |
| - val result = message.formatTime() | |
| + val testLocale = Locale.US | |
| + val testTimeZone = TimeZone.getTimeZone("GMT") | |
| + val result = message.formatTime(testLocale, testTimeZone) | |
| // Then | |
| assertEquals(expected, result) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment