@@ -9,6 +9,7 @@ import org.jetbrains.kotlinx.dataframe.type
9
9
import org.junit.Test
10
10
import java.time.LocalTime
11
11
import java.time.Month
12
+ import java.util.Locale
12
13
import kotlin.reflect.typeOf
13
14
import kotlin.time.Duration.Companion.days
14
15
import kotlin.time.Duration.Companion.hours
@@ -18,61 +19,74 @@ import kotlin.time.Duration.Companion.seconds
18
19
class ParseTests {
19
20
@Test
20
21
fun parseDate () {
21
- val date by columnOf(" January 1, 2020" )
22
- val pattern = " MMMM d, yyyy"
23
-
24
- val parsed = date.parse(ParserOptions (dateTimePattern = pattern)).cast<LocalDate >()
25
-
26
- parsed.type() shouldBe typeOf<LocalDate >()
27
- with (parsed[0 ]) {
28
- month shouldBe Month .JANUARY
29
- dayOfMonth shouldBe 1
30
- year shouldBe 2020
22
+ val currentLocale = Locale .getDefault()
23
+ try {
24
+ Locale .setDefault(Locale .forLanguageTag(" en-US" ))
25
+ val date by columnOf(" January 1, 2020" )
26
+ val pattern = " MMMM d, yyyy"
27
+
28
+ val parsed = date.parse(ParserOptions (dateTimePattern = pattern)).cast<LocalDate >()
29
+
30
+ parsed.type() shouldBe typeOf<LocalDate >()
31
+ with (parsed[0 ]) {
32
+ month shouldBe Month .JANUARY
33
+ dayOfMonth shouldBe 1
34
+ year shouldBe 2020
35
+ }
36
+
37
+ date.convertToLocalDate(pattern) shouldBe parsed
38
+ with (date.toDataFrame()) {
39
+ convert { date }.toLocalDate(pattern)[date] shouldBe parsed
40
+ parse(ParserOptions (dateTimePattern = pattern))[date] shouldBe parsed
41
+ }
42
+
43
+ DataFrame .parser.addDateTimePattern(pattern)
44
+
45
+ date.parse() shouldBe parsed
46
+ date.convertToLocalDate() shouldBe parsed
47
+
48
+ DataFrame .parser.resetToDefault()
49
+ } finally {
50
+ Locale .setDefault(currentLocale)
31
51
}
32
-
33
- date.convertToLocalDate(pattern) shouldBe parsed
34
- with (date.toDataFrame()) {
35
- convert { date }.toLocalDate(pattern)[date] shouldBe parsed
36
- parse(ParserOptions (dateTimePattern = pattern))[date] shouldBe parsed
37
- }
38
-
39
- DataFrame .parser.addDateTimePattern(pattern)
40
-
41
- date.parse() shouldBe parsed
42
- date.convertToLocalDate() shouldBe parsed
43
-
44
- DataFrame .parser.resetToDefault()
45
52
}
46
53
47
54
@Test
48
55
fun parseDateTime () {
49
- val dateTime by columnOf(" 3 Jun 2008 13:05:30" )
50
- val pattern = " d MMM yyyy HH:mm:ss"
51
-
52
- val parsed = dateTime.parse(ParserOptions (dateTimePattern = pattern)).cast<LocalDateTime >()
53
-
54
- parsed.type() shouldBe typeOf<LocalDateTime >()
55
- with (parsed[0 ]) {
56
- month shouldBe Month .JUNE
57
- dayOfMonth shouldBe 3
58
- year shouldBe 2008
59
- hour shouldBe 13
60
- minute shouldBe 5
61
- second shouldBe 30
62
- }
63
-
64
- dateTime.convertToLocalDateTime(pattern) shouldBe parsed
65
- with (dateTime.toDataFrame()) {
66
- convert { dateTime }.toLocalDateTime(pattern)[dateTime] shouldBe parsed
67
- parse(ParserOptions (dateTimePattern = pattern))[dateTime] shouldBe parsed
56
+ val currentLocale = Locale .getDefault()
57
+ try {
58
+ Locale .setDefault(Locale .forLanguageTag(" en-US" ))
59
+ val dateTime by columnOf(" 3 Jun 2008 13:05:30" )
60
+ val pattern = " d MMM yyyy HH:mm:ss"
61
+ val locale = Locale .forLanguageTag(" en-US" )
62
+
63
+ val parsed = dateTime.parse(ParserOptions (dateTimePattern = pattern, locale = locale)).cast<LocalDateTime >()
64
+
65
+ parsed.type() shouldBe typeOf<LocalDateTime >()
66
+ with (parsed[0 ]) {
67
+ month shouldBe Month .JUNE
68
+ dayOfMonth shouldBe 3
69
+ year shouldBe 2008
70
+ hour shouldBe 13
71
+ minute shouldBe 5
72
+ second shouldBe 30
73
+ }
74
+
75
+ dateTime.convertToLocalDateTime(pattern, locale) shouldBe parsed
76
+ with (dateTime.toDataFrame()) {
77
+ convert { dateTime }.toLocalDateTime(pattern)[dateTime] shouldBe parsed
78
+ parse(ParserOptions (dateTimePattern = pattern))[dateTime] shouldBe parsed
79
+ }
80
+
81
+ DataFrame .parser.addDateTimePattern(pattern)
82
+
83
+ dateTime.parse(ParserOptions (locale = locale)) shouldBe parsed
84
+ dateTime.convertToLocalDateTime(pattern, locale) shouldBe parsed
85
+
86
+ DataFrame .parser.resetToDefault()
87
+ } finally {
88
+ Locale .setDefault(currentLocale)
68
89
}
69
-
70
- DataFrame .parser.addDateTimePattern(pattern)
71
-
72
- dateTime.parse() shouldBe parsed
73
- dateTime.convertToLocalDateTime() shouldBe parsed
74
-
75
- DataFrame .parser.resetToDefault()
76
90
}
77
91
78
92
@Test
0 commit comments