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

Unified Diff: sdk/lib/core/date_time.dart

Issue 165723005: Add toIso8601String to DateTime. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Address comments. Add test. Created 6 years, 10 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
« no previous file with comments | « no previous file | tests/corelib/date_time_test.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: sdk/lib/core/date_time.dart
diff --git a/sdk/lib/core/date_time.dart b/sdk/lib/core/date_time.dart
index b9a454757f8c28de0a397b70f167506fce1c99c2..21e3645777c99d29dddb51a3ed204d8cfbdbfa06 100644
--- a/sdk/lib/core/date_time.dart
+++ b/sdk/lib/core/date_time.dart
@@ -397,6 +397,26 @@ class DateTime implements Comparable {
isUtc: true);
}
+ static String _fourDigits(int n) {
+ int absN = n.abs();
+ String sign = n < 0 ? "-" : "";
+ if (absN >= 1000) return "$n";
+ if (absN >= 100) return "${sign}0$absN";
+ if (absN >= 10) return "${sign}00$absN";
+ return "${sign}000$absN";
+ }
+
+ static String _threeDigits(int n) {
+ if (n >= 100) return "${n}";
+ if (n >= 10) return "0${n}";
+ return "00${n}";
+ }
+
+ static String _twoDigits(int n) {
+ if (n >= 10) return "${n}";
+ return "0${n}";
+ }
+
/**
* Returns a human-readable string for this instance.
*
@@ -407,33 +427,13 @@ class DateTime implements Comparable {
* at the pub shared packages repo.
*/
String toString() {
- String fourDigits(int n) {
- int absN = n.abs();
- String sign = n < 0 ? "-" : "";
- if (absN >= 1000) return "$n";
- if (absN >= 100) return "${sign}0$absN";
- if (absN >= 10) return "${sign}00$absN";
- return "${sign}000$absN";
- }
-
- String threeDigits(int n) {
- if (n >= 100) return "${n}";
- if (n >= 10) return "0${n}";
- return "00${n}";
- }
-
- String twoDigits(int n) {
- if (n >= 10) return "${n}";
- return "0${n}";
- }
-
- String y = fourDigits(year);
- String m = twoDigits(month);
- String d = twoDigits(day);
- String h = twoDigits(hour);
- String min = twoDigits(minute);
- String sec = twoDigits(second);
- String ms = threeDigits(millisecond);
+ String y = _fourDigits(year);
+ String m = _twoDigits(month);
+ String d = _twoDigits(day);
+ String h = _twoDigits(hour);
+ String min = _twoDigits(minute);
+ String sec = _twoDigits(second);
+ String ms = _threeDigits(millisecond);
if (isUtc) {
return "$y-$m-$d $h:$min:$sec.${ms}Z";
} else {
@@ -442,12 +442,32 @@ class DateTime implements Comparable {
}
/**
+ * Returns an ISO-8601 full-precision extended format representation.
+ *
+ * The format is "YYYY-MM-DDTHH:mm:ss.sssZ" for UTC time, and
+ * "YYYY-MM-DDTHH:mm:ss.sss" (no trailing "Z") for local/non-UTC time.
+ */
+ String toIso8601String() {
+ String y = _fourDigits(year);
+ String m = _twoDigits(month);
+ String d = _twoDigits(day);
+ String h = _twoDigits(hour);
+ String min = _twoDigits(minute);
+ String sec = _twoDigits(second);
+ String ms = _threeDigits(millisecond);
+ if (isUtc) {
+ return "$y-$m-${d}T$h:$min:$sec.${ms}Z";
+ } else {
+ return "$y-$m-${d}T$h:$min:$sec.$ms";
+ }
+ }
+
+ /**
* Returns a new [DateTime] instance with [duration] added to [this].
*
* DateTime today = new DateTime.now();
* DateTime sixtyDaysFromNow = today.add(new Duration(days: 60));
*/
-
DateTime add(Duration duration) {
int ms = millisecondsSinceEpoch;
return new DateTime.fromMillisecondsSinceEpoch(
« no previous file with comments | « no previous file | tests/corelib/date_time_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698