Skip to content
This repository has been archived by the owner on Jan 9, 2023. It is now read-only.

Feature/datetime factory improvement #492

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 23 additions & 8 deletions charts_common/lib/src/common/date_time_factory.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,12 @@
// limitations under the License.

import 'package:intl/intl.dart' show DateFormat;
import 'package:timezone/timezone.dart' as tz;

/// Interface for factory that creates [DateTime] and [DateFormat].
///
/// This allows for creating of locale specific date time and date format.
abstract class DateTimeFactory {
// TODO: Per cbraun@, we need to allow setting the timezone that
// is used globally (along with other settings like which day the week starts
// on. Use DateTimeFactory - either return a local DateTime or a UTC date time
// based on the setting.

// TODO: We need to incorporate the time zoned calendar here
// because Dart DateTime doesn't do this. TZDateTime implements DateTime, so
// we can use DateTime as the interface.
DateTime createDateTimeFromMilliSecondsSinceEpoch(int millisecondsSinceEpoch);

DateTime createDateTime(int year,
Expand Down Expand Up @@ -96,3 +89,25 @@ class UTCDateTimeFactory implements DateTimeFactory {
return DateFormat(pattern);
}
}

/// A TimeZone aware time [DateTimeFactory].
class TimeZoneAwareDateTimeFactory implements DateTimeFactory {
final tz.Location location;

const TimeZoneAwareDateTimeFactory(this.location) : assert(location != null);

DateTime createDateTimeFromMilliSecondsSinceEpoch(int millisecondsSinceEpoch) {
return tz.TZDateTime.fromMillisecondsSinceEpoch(location, millisecondsSinceEpoch);
}

DateTime createDateTime(int year,
[int month = 1, int day = 1, int hour = 0, int minute = 0, int second = 0, int millisecond = 0, int microsecond = 0]) {
return tz.TZDateTime(location, year, month, day, hour, minute, second, millisecond, microsecond);
}

/// Returns a [DateFormat].
DateFormat createDateFormat(String pattern) {
return new DateFormat(pattern);
}
}

2 changes: 2 additions & 0 deletions charts_common/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ dependencies:
logging: any
meta: ^1.1.1
vector_math: ^2.0.8
timezone: ^0.5.7


dev_dependencies:
mockito: ^4.0.0
Expand Down
6 changes: 4 additions & 2 deletions charts_flutter/lib/src/time_series_chart.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ import 'package:charts_common/common.dart' as common
NumericAxisSpec,
Series,
SeriesRendererConfig,
TimeSeriesChart;
TimeSeriesChart,
LocalDateTimeFactory;
import 'behaviors/chart_behavior.dart' show ChartBehavior;
import 'behaviors/line_point_highlighter.dart' show LinePointHighlighter;
import 'cartesian_chart.dart' show CartesianChart;
Expand Down Expand Up @@ -82,7 +83,8 @@ class TimeSeriesChart extends CartesianChart<DateTime> {
layoutConfig: layoutConfig?.commonLayoutConfig,
primaryMeasureAxis: primaryMeasureAxis?.createAxis(),
secondaryMeasureAxis: secondaryMeasureAxis?.createAxis(),
disjointMeasureAxes: createDisjointMeasureAxes());
disjointMeasureAxes: createDisjointMeasureAxes(),
dateTimeFactory: dateTimeFactory ?? const common.LocalDateTimeFactory());
}

@override
Expand Down