Skip to content

Commit

Permalink
Allow date time to fallback to default timezone when parsing, if it i…
Browse files Browse the repository at this point in the history
…s not specified
  • Loading branch information
radeusgd committed Aug 29, 2022
1 parent f2fd650 commit 9b542d6
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,24 @@
import org.enso.table.data.column.builder.object.Builder;
import org.enso.table.data.column.builder.object.DateTimeBuilder;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.util.Locale;

public class DateTimeParser extends BaseTimeParser {
public DateTimeParser(String[] formats, Locale locale) {
super(formats, locale, ZonedDateTime::parse);
super(formats, locale, DateTimeParser::parse);
}

private static ZonedDateTime parse(String text, DateTimeFormatter formatter) throws DateTimeParseException {
try {
return ZonedDateTime.parse(text, formatter);
} catch (DateTimeParseException ignored) {
return LocalDateTime.parse(text, formatter).atZone(ZoneId.systemDefault());
}
}

@Override
Expand Down
2 changes: 1 addition & 1 deletion test/Table_Tests/src/Data_Formatter_Spec.enso
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ spec =
Test.specify "should parse dates" <|
formatter = Data_Formatter
formatter.parse "2022-01-01" . should_equal (Date.new 2022)
formatter.parse "2020-05-07" . should_equal (Date.new 2020 5 7)
formatter.parse "2020-05-07" datatype=Date . should_equal (Date.new 2020 5 7)
formatter.parse "1999-01-01 00:00:00" . should_equal (Date_Time.new 1999)
formatter.parse "1999-02-03 04:05:06" . should_equal (Date_Time.new 1999 2 3 4 5 6)
formatter.parse "1999-01-01 00:00" datatype=Date_Time . should_equal (Date_Time.new 1999)
Expand Down
5 changes: 2 additions & 3 deletions test/Table_Tests/src/Table_Date_Spec.enso
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,8 @@ spec =
Test.specify "should be able to read in a table with dates" <|
table.column_count.should_equal 5
table.row_count.should_equal 7
info = table.info
info.at "Column" . to_vector . should_equal ['Number','Party', 'Title', 'From', 'To']
info.at "Storage Type" . to_vector . should_equal [Storage.Integer, Storage.Text, Storage.Text, Storage.Date, Storage.Date]
table.info.at "Column" . to_vector . should_equal ['Number','Party', 'Title', 'From', 'To']
table.info.at "Storage Type" . to_vector . should_equal [Storage.Integer, Storage.Text, Storage.Text, Storage.Date, Storage.Date]

Test.specify "should be able to treat a single value as a Date" <|
from_column = table.at 'From'
Expand Down
4 changes: 3 additions & 1 deletion test/Table_Tests/src/Table_Time_Of_Day_Spec.enso
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ from Standard.Base import all

import Standard.Table
import Standard.Table.Data.Column
import Standard.Table.Data.Storage
import Standard.Table.IO.File_Format
from Standard.Table.Data.Data_Formatter as Data_Formatter_Module import Data_Formatter

Expand All @@ -19,7 +20,8 @@ spec =
table = (enso_project.data / "time_of_day_sample.csv").read
Test.specify "should be able to read in a table with dates" <|
table.column_count.should_equal 3
table.columns.map (.name) . should_equal ['Serial number','Movement type', 'Posting time']
table.info.at "Column" . to_vector . should_equal ['Serial number','Movement type', 'Posting time']
table.info.at "Storage Type" . to_vector . should_equal [Storage.Text, Storage.Integer, Storage.Time_Of_Day]
table.row_count.should_equal 6

Test.specify "should be able to treat a single value as a Time_Of_Days" <|
Expand Down
4 changes: 3 additions & 1 deletion test/Table_Tests/src/Table_Time_Spec.enso
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ from Standard.Base import all

import Standard.Table
import Standard.Table.Data.Column
import Standard.Table.Data.Storage
import Standard.Table.IO.File_Format
from Standard.Table.Data.Data_Formatter as Data_Formatter_Module import Data_Formatter

Expand All @@ -19,7 +20,8 @@ spec =
table = (enso_project.data / "datetime_sample.csv").read
Test.specify "should be able to read in a table with dates" <|
table.column_count.should_equal 3
table.columns.map (.name) . should_equal ['Serial number','Movement type', 'Posting date']
table.info.at "Column" . to_vector . should_equal ['Serial number','Movement type', 'Posting date']
table.info.at "Storage Type" . to_vector . should_equal [Storage.Text, Storage.Integer, Storage.Date_Time]
table.row_count.should_equal 6

Test.specify "should be able to treat a single value as a Date_Time" <|
Expand Down

0 comments on commit 9b542d6

Please sign in to comment.