git.iko.soy/parseable-android
on master help sign in
73 lines 2.3 kB history raw
1package com.parseable.android
3import com.parseable.android.data.formatTimestamp
4import org.junit.Assert.*
5import org.junit.Test
6import java.util.TimeZone
8class FormatTimestampTest {
10 @Test
11 fun `formats ISO8601 UTC timestamp`() {
12 // Force UTC for test determinism
13 val original = TimeZone.getDefault()
14 try {
15 TimeZone.setDefault(TimeZone.getTimeZone("UTC"))
16 val result = formatTimestamp("2024-12-15T14:30:45.123Z")
17 assertEquals("Dec 15 14:30:45.123", result)
18 } finally {
19 TimeZone.setDefault(original)
20 }
21 }
23 @Test
24 fun `formats Parseable timestamp format with +00 00 suffix`() {
25 val original = TimeZone.getDefault()
26 try {
27 TimeZone.setDefault(TimeZone.getTimeZone("UTC"))
28 val result = formatTimestamp("2024-12-15T14:30:45.123456+00:00")
29 assertEquals("Dec 15 14:30:45.123", result)
30 } finally {
31 TimeZone.setDefault(original)
32 }
33 }
35 @Test
36 fun `treats offset-less timestamp as UTC and converts to local`() {
37 val original = TimeZone.getDefault()
38 try {
39 TimeZone.setDefault(TimeZone.getTimeZone("America/New_York"))
40 // No offset/zone: should be interpreted as UTC (Parseable stores UTC),
41 // then converted to local (June = EDT = UTC-4).
42 val result = formatTimestamp("2024-06-15T18:30:45.000")
43 assertEquals("Jun 15 14:30:45.000", result)
44 } finally {
45 TimeZone.setDefault(original)
46 }
47 }
49 @Test
50 fun `returns raw string for unparseable timestamp`() {
51 val result = formatTimestamp("not-a-timestamp")
52 assertEquals("not-a-timestamp", result)
53 }
55 @Test
56 fun `handles empty string`() {
57 val result = formatTimestamp("")
58 assertEquals("", result)
59 }
61 @Test
62 fun `converts to local timezone`() {
63 val original = TimeZone.getDefault()
64 try {
65 TimeZone.setDefault(TimeZone.getTimeZone("America/New_York"))
66 val result = formatTimestamp("2024-06-15T18:30:45.000Z")
67 // June = EDT = UTC-4
68 assertEquals("Jun 15 14:30:45.000", result)
69 } finally {
70 TimeZone.setDefault(original)
71 }
72 }