Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(3423)

Unified Diff: pkg/serialization/lib/src/serialization_rule.dart

Issue 14578008: Add a DateTimeRule to default serialization (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: pkg/serialization/lib/src/serialization_rule.dart
diff --git a/pkg/serialization/lib/src/serialization_rule.dart b/pkg/serialization/lib/src/serialization_rule.dart
index 53c0f72990a54f60d76ce129fca8f2ac24c3800a..cad1f18603627d656edcc384d53fef8108c20d3f 100644
--- a/pkg/serialization/lib/src/serialization_rule.dart
+++ b/pkg/serialization/lib/src/serialization_rule.dart
@@ -492,6 +492,32 @@ class SymbolRule extends CustomRule {
bool get hasVariableLengthEntries => false;
}
+/** A hard-coded rule for DateTime. */
+class DateTimeRule extends CustomRule {
+ /** A helper to make it easier to create dates either locally or UTC. */
+ static DateTime makeUtc(year, month, day, hour, minute, second, millisecond)
+ => new DateTime.utc(year, month, day, hour, minute, second, millisecond);
+ /** A helper to make it easier to create dates either locally or UTC. */
+ static DateTime makeLocal(year, month, day, hour, minute, second, millisecond)
+ => new DateTime(year, month, day, hour, minute, second, millisecond);
+
+ bool appliesTo(instance, _) => instance is DateTime;
+
+ List<int> getState(DateTime date) =>
+ [date.year, date.month, date.day, date.hour, date.minute,
Jennifer Messerly 2013/05/14 23:18:33 alternatively, just store millisecondsSinceEpoch a
Alan Knight 2013/05/14 23:32:55 Doh! That's much simpler. Done.
+ date.second, date.millisecond, date.isUtc];
+
+ create(List<int> state) {
+ var creation = state.last ? makeUtc : makeLocal;
+ return Function.apply(creation, state.take(7).toList());
Jennifer Messerly 2013/05/14 23:18:33 TIL: we have Function.apply :)
Alan Knight 2013/05/14 23:32:55 But now I don't get to use it :-(
+ }
+
+ void setState(date, state) {}
+ // Let the system know we don't have to store a length for these.
+ int get dataLength => 8;
+ bool get hasVariableLengthEntries => false;
+}
+
/** Create a lazy list/map that will inflate its items on demand in [r]. */
_lazy(l, Reader r) {
if (l is List) return new _LazyList(l, r);
@@ -561,7 +587,7 @@ class _LazyList extends IterableBase implements List {
// These operations, and other inherited methods that iterate over the whole
// list will work, but may be expensive, and are probably
// best avoided.
- List get _inflated => _raw.map(_reader.inflateReference);
+ Iterable get _inflated => _raw.map(_reader.inflateReference);
Iterator get iterator => _inflated.iterator;
indexOf(x, [pos = 0]) => _inflated.toList().indexOf(x);
lastIndexOf(x, [pos]) => _inflated.toList().lastIndexOf(x);

Powered by Google App Engine
This is Rietveld 408576698