blob: bf7782fd49db8f2ba5bd359bc7abce16fe6715fd [file] [log] [blame]
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="generator" content="made with love by dartdoc 0.8.1">
<title>parse method - DateTime class - ex library - Dart API</title>
<!-- required because all the links are pseudo-absolute -->
<base href="../..">
<link href='https://fonts.googleapis.com/css?family=Source+Code+Pro|Roboto:500,400italic,300,400' rel='stylesheet' type='text/css'>
<link rel="stylesheet" href="static-assets/prettify.css">
<link rel="stylesheet" href="static-assets/css/bootstrap.min.css">
<link rel="stylesheet" href="static-assets/styles.css">
<meta name="description" content="API docs for the parse method from the DateTime class, for the Dart programming language.">
<link rel="icon" href="static-assets/favicon.png">
<!-- Do not remove placeholder -->
<!-- Header Placeholder -->
</head>
<body>
<div id="overlay-under-drawer"></div>
<header class="container-fluid" id="title">
<nav class="navbar navbar-fixed-top">
<div class="container">
<div class="row">
<div class="col-sm-12 contents">
<button id="sidenav-left-toggle" type="button">&nbsp;</button>
<ol class="breadcrumbs gt-separated hidden-xs">
<li><a href="index.html">test_package</a></li>
<li><a href="ex/ex-library.html">ex</a></li>
<li><a href="ex/DateTime-class.html">DateTime</a></li>
<li class="self-crumb">parse</li>
</ol>
<div class="self-name">parse</div>
<form class="search navbar-right" role="search">
<input type="text" id="search-box" autocomplete="off" disabled class="form-control typeahead" placeholder="Loading search...">
</form>
</div> <!-- /col -->
</div> <!-- /row -->
</div> <!-- /container -->
</nav>
<div class="container masthead">
<div class="row">
<div class="col-sm-12 contents">
<ol class="breadcrumbs gt-separated visible-xs">
<li><a href="index.html">test_package</a></li>
<li><a href="ex/ex-library.html">ex</a></li>
<li><a href="ex/DateTime-class.html">DateTime</a></li>
<li class="self-crumb">parse</li>
</ol>
<div class="title-description">
<h1 class="title">
<span class="kind">method</span> parse
</h1>
</div>
<ul class="subnav">
<li><a href="ex/DateTime/parse.html#source">Source</a></li>
</ul>
</div> <!-- /col -->
</div> <!-- /row -->
</div> <!-- /container -->
</header>
<div class="container body">
<div class="row">
<div class="col-xs-6 col-sm-3 col-md-2 sidebar sidebar-offcanvas-left">
<h5><a href="index.html">test_package</a></h5>
<h5><a href="ex/ex-library.html">ex</a></h5>
<h5><a href="ex/DateTime-class.html">DateTime</a></h5>
<ol>
<li class="section-title"><a href="ex/DateTime-class.html#constants">Constants</a></li>
<li><a href="ex/DateTime/APRIL.html">APRIL</a></li>
<li><a href="ex/DateTime/AUGUST.html">AUGUST</a></li>
<li><a href="ex/DateTime/DAYS_PER_WEEK.html">DAYS_PER_WEEK</a></li>
<li><a href="ex/DateTime/DECEMBER.html">DECEMBER</a></li>
<li><a href="ex/DateTime/FEBRUARY.html">FEBRUARY</a></li>
<li><a href="ex/DateTime/FRIDAY.html">FRIDAY</a></li>
<li><a href="ex/DateTime/JANUARY.html">JANUARY</a></li>
<li><a href="ex/DateTime/JULY.html">JULY</a></li>
<li><a href="ex/DateTime/JUNE.html">JUNE</a></li>
<li><a href="ex/DateTime/MARCH.html">MARCH</a></li>
<li><a href="ex/DateTime/MAY.html">MAY</a></li>
<li><a href="ex/DateTime/MONDAY.html">MONDAY</a></li>
<li><a href="ex/DateTime/MONTHS_PER_YEAR.html">MONTHS_PER_YEAR</a></li>
<li><a href="ex/DateTime/NOVEMBER.html">NOVEMBER</a></li>
<li><a href="ex/DateTime/OCTOBER.html">OCTOBER</a></li>
<li><a href="ex/DateTime/SATURDAY.html">SATURDAY</a></li>
<li><a href="ex/DateTime/SEPTEMBER.html">SEPTEMBER</a></li>
<li><a href="ex/DateTime/SUNDAY.html">SUNDAY</a></li>
<li><a href="ex/DateTime/THURSDAY.html">THURSDAY</a></li>
<li><a href="ex/DateTime/TUESDAY.html">TUESDAY</a></li>
<li><a href="ex/DateTime/WEDNESDAY.html">WEDNESDAY</a></li>
<li class="section-title"><a href="ex/DateTime-class.html#static-methods">Static methods</a></li>
<li><a href="ex/DateTime/parse.html">parse</a></li>
<li class="section-title"><a href="ex/DateTime-class.html#constructors">Constructors</a></li>
<li><a href="ex/DateTime/DateTime.html">DateTime</a></li>
<li><a href="ex/DateTime/DateTime.fromMillisecondsSinceEpoch.html">fromMillisecondsSinceEpoch</a></li>
<li><a href="ex/DateTime/DateTime.now.html">now</a></li>
<li><a href="ex/DateTime/DateTime.utc.html">utc</a></li>
<li class="section-title">
<a href="ex/DateTime-class.html#instance-properties">Properties</a>
</li>
<li><a href="ex/DateTime/day.html">day</a></li>
<li><a href="ex/DateTime/hashCode.html">hashCode</a></li>
<li><a href="ex/DateTime/hour.html">hour</a></li>
<li><a href="ex/DateTime/isUtc.html">isUtc</a></li>
<li><a href="ex/DateTime/millisecond.html">millisecond</a></li>
<li><a href="ex/DateTime/millisecondsSinceEpoch.html">millisecondsSinceEpoch</a></li>
<li><a href="ex/DateTime/minute.html">minute</a></li>
<li><a href="ex/DateTime/month.html">month</a></li>
<li class="inherited"><a href="ex/DateTime/runtimeType.html">runtimeType</a></li>
<li><a href="ex/DateTime/second.html">second</a></li>
<li><a href="ex/DateTime/timeZoneName.html">timeZoneName</a></li>
<li><a href="ex/DateTime/timeZoneOffset.html">timeZoneOffset</a></li>
<li><a href="ex/DateTime/weekday.html">weekday</a></li>
<li><a href="ex/DateTime/year.html">year</a></li>
<li class="section-title"><a href="ex/DateTime-class.html#operators">Operators</a></li>
<li><a href="ex/DateTime/operator_equals.html">operator ==</a></li>
<li class="section-title"><a href="ex/DateTime-class.html#instance-methods">Methods</a></li>
<li><a href="ex/DateTime/add.html">add</a></li>
<li><a href="ex/DateTime/compareTo.html">compareTo</a></li>
<li><a href="ex/DateTime/difference.html">difference</a></li>
<li><a href="ex/DateTime/isAfter.html">isAfter</a></li>
<li><a href="ex/DateTime/isAtSameMomentAs.html">isAtSameMomentAs</a></li>
<li><a href="ex/DateTime/isBefore.html">isBefore</a></li>
<li class="inherited"><a href="ex/DateTime/noSuchMethod.html">noSuchMethod</a></li>
<li><a href="ex/DateTime/subtract.html">subtract</a></li>
<li><a href="ex/DateTime/toIso8601String.html">toIso8601String</a></li>
<li><a href="ex/DateTime/toLocal.html">toLocal</a></li>
<li><a href="ex/DateTime/toString.html">toString</a></li>
<li><a href="ex/DateTime/toUtc.html">toUtc</a></li>
</ol>
</div><!--/.sidebar-offcanvas-->
<div class="col-xs-12 col-sm-9 col-md-8 main-content">
<section class="multi-line-signature">
<span class="returntype"><a href="ex/DateTime-class.html">DateTime</a></span>
<span class="name ">parse</span>(<wbr><span class="parameter" id="parse-param-formattedString"><span class="type-annotation">String</span> <span class="parameter-name">formattedString</span></span>)
</section>
<section class="desc markdown">
<p>Constructs a new <a href="ex/DateTime-class.html">DateTime</a> instance based on <code>formattedString</code>.</p>
<p>Throws a <code>FormatException</code> if the input cannot be parsed.</p>
<p>The function parses a subset of ISO 8601
which includes the subset accepted by RFC 3339.</p>
<p>The accepted inputs are currently:</p><ul><li>
<p>A date: A signed four-to-six digit year, two digit month and
two digit day, optionally separated by <code>-</code> characters.
Examples: "19700101", "-0004-12-24", "81030-04-01".</p></li><li>
<p>An optional time part, separated from the date by either <code>T</code> or a space.
The time part is a two digit hour,
then optionally a two digit minutes value,
then optionally a two digit seconds value, and
then optionally a '.' followed by a one-to-six digit second fraction.
The minuts and seconds may be separated from the previous parts by a ':'.
Examples: "12", "12:30:24.124", "123010.50".</p></li><li>
<p>An optional time-zone offset part,
possibly separated from the previous by a space.
The time zone is either 'z' or 'Z', or it is a signed two digit hour
part and an optional two digit minute part. The sign must be either
"+" or "-", and can not be omitted.
The minutes may be separted from the hours by a ':'.
Examples: "Z", "-10", "01:30", "1130".</p></li></ul>
<p>This includes the output of both <a href="ex/DateTime/toString.html">toString</a> and <a href="ex/DateTime/toIso8601String.html">toIso8601String</a>, which
will be parsed back into a <code>DateTime</code> object with the same time as the
original.</p>
<p>The result is always in either local time or UTC.
If a time zone offset other than UTC is specified,
the time is converted to the equivalent UTC time.</p>
<p>Examples of accepted strings:</p><ul><li><code>"2012-02-27 13:27:00"</code></li><li><code>"2012-02-27 13:27:00.123456z"</code></li><li><code>"20120227 13:27:00"</code></li><li><code>"20120227T132700"</code></li><li><code>"20120227"</code></li><li><code>"+20120227"</code></li><li><code>"2012-02-27T14Z"</code></li><li><code>"2012-02-27T14+00:00"</code></li><li><code>"-123450101 00:00:00 Z"</code>: in the year -12345.</li><li><code>"2002-02-27T14:00:00-0500"</code>: Same as <code>"2002-02-27T19:00:00Z"</code></li></ul>
</section>
<section class="summary source-code" id="source">
<h2><span>Source</span> </h2>
<pre><code class="prettyprint lang-dart">// TODO(lrn): restrict incorrect values like 2003-02-29T50:70:80.
// Or not, that may be a breaking change.
static DateTime parse(String formattedString) {
/*
* date ::= yeardate time_opt timezone_opt
* yeardate ::= year colon_opt month colon_opt day
* year ::= sign_opt digit{4,6}
* colon_opt :: &lt;empty&gt; | &apos;:&apos;
* sign ::= &apos;+&apos; | &apos;-&apos;
* sign_opt ::= &lt;empty&gt; | sign
* month ::= digit{2}
* day ::= digit{2}
* time_opt ::= &lt;empty&gt; | (&apos; &apos; | &apos;T&apos;) hour minutes_opt
* minutes_opt ::= &lt;empty&gt; | colon_opt digit{2} seconds_opt
* seconds_opt ::= &lt;empty&gt; | colon_opt digit{2} millis_opt
* millis_opt ::= &lt;empty&gt; | &apos;.&apos; digit{1,6}
* timezone_opt ::= &lt;empty&gt; | space_opt timezone
* space_opt :: &apos; &apos; | &lt;empty&gt;
* timezone ::= &apos;z&apos; | &apos;Z&apos; | sign digit{2} timezonemins_opt
* timezonemins_opt ::= &lt;empty&gt; | colon_opt digit{2}
*/
final RegExp re = new RegExp(
r&apos;^([+-]?\d{4,6})-?(\d\d)-?(\d\d)&apos; // Day part.
r&apos;(?:[ T](\d\d)(?::?(\d\d)(?::?(\d\d)(?:\.(\d{1,6}))?)?)?&apos; // Time part.
r&apos;( ?[zZ]| ?([-+])(\d\d)(?::?(\d\d))?)?)?$&apos;); // Timezone part.
Match match = re.firstMatch(formattedString);
if (match != null) {
int parseIntOrZero(String matched) {
if (matched == null) return 0;
return int.parse(matched);
}
// Parses fractional second digits of &apos;.(\d{1,6})&apos; into milliseconds.
// Uses first three digits (assumed to be zero if not there), and
// rounds up if fourth digit is 5 or greater.
// Should be equivalent to `(double.parse(&quot;.$matchd&quot;)*1000).round()`.
int parseMilliseconds(String matched) {
if (matched == null) return 0;
int length = matched.length;
assert(length &gt;= 1);
assert(length &lt;= 6);
int result = (matched.codeUnitAt(0) ^ 0x30);
if (length &lt;= 3) {
int i = 1;
while (i &lt; length) {
result *= 10;
result += matched.codeUnitAt(i) ^ 0x30;
i++;
}
while (i &lt; 3) {
result *= 10;
i++;
}
return result;
}
// Parse the prefix from 0..3 without creating a new substring.
result = result * 10 + (matched.codeUnitAt(1) ^ 0x30);
result = result * 10 + (matched.codeUnitAt(2) ^ 0x30);
if (matched.codeUnitAt(3) &gt;= 0x35) {
result += 1;
}
return result;
}
int years = int.parse(match[1]);
int month = int.parse(match[2]);
int day = int.parse(match[3]);
int hour = parseIntOrZero(match[4]);
int minute = parseIntOrZero(match[5]);
int second = parseIntOrZero(match[6]);
bool addOneMillisecond = false;
int millisecond = parseMilliseconds(match[7]);
if (millisecond == 1000) {
addOneMillisecond = true;
millisecond = 999;
}
bool isUtc = false;
if (match[8] != null) { // timezone part
isUtc = true;
if (match[9] != null) {
// timezone other than &apos;Z&apos; and &apos;z&apos;.
int sign = (match[9] == &apos;-&apos;) ? -1 : 1;
int hourDifference = int.parse(match[10]);
int minuteDifference = parseIntOrZero(match[11]);
minuteDifference += 60 * hourDifference;
minute -= sign * minuteDifference;
}
}
int millisecondsSinceEpoch = _brokenDownDateToMillisecondsSinceEpoch(
years, month, day, hour, minute, second, millisecond, isUtc);
if (millisecondsSinceEpoch == null) {
throw new FormatException(&quot;Time out of range&quot;, formattedString);
}
if (addOneMillisecond) millisecondsSinceEpoch++;
return new DateTime.fromMillisecondsSinceEpoch(millisecondsSinceEpoch,
isUtc: isUtc);
} else {
throw new FormatException(&quot;Invalid date format&quot;, formattedString);
}
}</code></pre>
</section>
</div> <!-- /.main-content -->
</div> <!-- row -->
</div> <!-- container -->
<footer>
<div class="container-fluid">
<div class="container">
<p class="text-center">
<span class="no-break">
test_package 0.0.1
</span>
&bull;
<span class="no-break">
<a href="https://www.dartlang.org">
<img src="static-assets/favicon.png" alt="Dart" title="Dart" width="16" height="16">
</a>
</span>
&bull;
<span class="copyright no-break">
<a href="http://creativecommons.org/licenses/by-sa/4.0/">cc license</a>
</span>
</p>
</div>
</div>
</footer>
<script src="https://code.jquery.com/jquery-2.1.4.min.js"></script>
<script src="static-assets/typeahead.bundle.min.js"></script>
<script src="static-assets/prettify.js"></script>
<script src="static-assets/script.js"></script>
<!-- Do not remove placeholder -->
<!-- Footer Placeholder -->
</body>
</html>