public static bool TryParse(string date, out W3CDateTime result) { //------------------------------------------------------------ // Local members //------------------------------------------------------------ bool parseSucceeded; //------------------------------------------------------------ // Validate parameters //------------------------------------------------------------ if (string.IsNullOrEmpty(date)) { throw new ArgumentNullException("date"); } //------------------------------------------------------------ // Attempt to parse //------------------------------------------------------------ try { result = Parse(date); parseSucceeded = true; } catch (Exception) { result = null; parseSucceeded = false; } //------------------------------------------------------------ // Return result //------------------------------------------------------------ return(parseSucceeded); }
/// <summary> /// Loads this <see cref="DigestEntry"/> using the supplied <see cref="XPathNavigator"/>. /// </summary> /// <param name="source">The <see cref="XPathNavigator"/> to extract information from.</param> /// <param name="manager">The <see cref="XmlNamespaceManager"/> object used to resolve prefixed syndication extension elements and attributes.</param> /// <returns><b>true</b> if the <see cref="DigestEntry"/> was initialized using the supplied <paramref name="source"/>, otherwise <b>false</b>.</returns> /// <remarks> /// This method expects the supplied <paramref name="source"/> to be positioned on the XML element that represents a <see cref="DigestEntry"/>. /// </remarks> /// <exception cref="ArgumentNullException">The <paramref name="source"/> is a null reference (Nothing in Visual Basic).</exception> public bool Load(XPathNavigator source, XmlNamespaceManager manager) { //------------------------------------------------------------ // Local members //------------------------------------------------------------ var wasLoaded = false; //------------------------------------------------------------ // Validate parameter //------------------------------------------------------------ Guard.ArgumentNotNull(source, "source"); //------------------------------------------------------------ // Attempt to extract syndication information //------------------------------------------------------------ if (source.HasChildren) { var endPointNavigator = source.SelectSingleNode("sync:endpoint", manager); if (endPointNavigator != null && !string.IsNullOrEmpty(endPointNavigator.Value)) { EndPoint = endPointNavigator.Value; wasLoaded = true; } var tickNavigator = source.SelectSingleNode("sync:tick", manager); if (tickNavigator != null && !string.IsNullOrEmpty(tickNavigator.Value)) { Tick = Convert.ToInt64(tickNavigator.Value); wasLoaded = true; } var stampNavigator = source.SelectSingleNode("sync:stamp", manager); if (stampNavigator != null) { Stamp = W3CDateTime.Parse(stampNavigator.Value).DateTime; wasLoaded = true; } var conflictPriorityNavigator = source.SelectSingleNode("sync:conflictPriority", manager); if (conflictPriorityNavigator != null) { ConflictPriority = Convert.ToInt32(conflictPriorityNavigator.Value); wasLoaded = true; } } return(wasLoaded); }
public static bool TryParse(string date, out W3CDateTime result) { //------------------------------------------------------------ // Local members //------------------------------------------------------------ bool parseSucceeded; //------------------------------------------------------------ // Validate parameters //------------------------------------------------------------ if (string.IsNullOrEmpty(date)) { throw new ArgumentNullException("date"); } //------------------------------------------------------------ // Attempt to parse //------------------------------------------------------------ try { result = Parse(date); parseSucceeded = true; } catch (Exception) { result = null; parseSucceeded = false; } //------------------------------------------------------------ // Return result //------------------------------------------------------------ return parseSucceeded; }
/// <summary> /// Converts the specified string representation of a W3C date and time to its <see cref="W3CDateTime"/> equivalent. /// </summary> /// <param name="s">A string containing a date and time to convert.</param> /// <returns>A W3CDateTime equivalent to the date and time contained in s.</returns> public static W3CDateTime Parse(string s) { //------------------------------------------------------------ // Local members //------------------------------------------------------------ var offset = TimeSpan.Zero; W3CDateTime w3CDateTime; //------------------------------------------------------------ // Attempt to convert string representation //------------------------------------------------------------ //------------------------------------------------------------ // Validate parameters //------------------------------------------------------------ if (string.IsNullOrEmpty(s)) { throw new ArgumentNullException("s"); } //------------------------------------------------------------ // Initialize regular expression //------------------------------------------------------------ var regularExpression = Regex; //------------------------------------------------------------ // Extract results of regular expression match //------------------------------------------------------------ var match = regularExpression.Match(s); //------------------------------------------------------------ // Determine if string represents a W3C DateTime //------------------------------------------------------------ if (!match.Success) { //------------------------------------------------------------ // Raise exception //------------------------------------------------------------ throw new FormatException("DateTime is not in a valid format"); } //------------------------------------------------------------ // Attempt to parse string //------------------------------------------------------------ try { //------------------------------------------------------------ // Extract year and handle 2/3 digit years //------------------------------------------------------------ var year = int.Parse(match.Groups["year"].Value, CultureInfo.InvariantCulture); if (year < 1000 && match.Groups["year"].Length < 3) { if (year < 50) { year = year + 2000; } else { year = year + 1999; } } //------------------------------------------------------------ // Extract other date time parts //------------------------------------------------------------ var month = (match.Groups["month"].Success) ? int.Parse(match.Groups["month"].Value, CultureInfo.InvariantCulture) : 1; var day = match.Groups["day"].Success ? int.Parse(match.Groups["day"].Value, CultureInfo.InvariantCulture) : 1; int millisecond; int second; int minute; int hour; if (match.Groups["hour"].Success) { hour = match.Groups["hour"].Success ? int.Parse(match.Groups["hour"].Value, CultureInfo.InvariantCulture) : 0; minute = match.Groups["min"].Success ? int.Parse(match.Groups["min"].Value, CultureInfo.InvariantCulture) : 0; second = match.Groups["sec"].Success ? int.Parse(match.Groups["sec"].Value, CultureInfo.InvariantCulture) : 0; millisecond = match.Groups["ms"].Success ? (int) Math.Round((1000*double.Parse(match.Groups["ms"].Value, CultureInfo.InvariantCulture))) : 0; } else { hour = 0; minute = 0; second = 0; millisecond = 0; } //------------------------------------------------------------ // Calculate offset //------------------------------------------------------------ if (match.Groups["ofs"].Success) { offset = ParseW3COffSet(match.Groups["ofs"].Value); } //------------------------------------------------------------ // Generate result //------------------------------------------------------------ w3CDateTime = new W3CDateTime(new DateTime(year, month, day, hour, minute, second, millisecond) - offset, offset); } catch (Exception exception) { //------------------------------------------------------------ // Raise exception //------------------------------------------------------------ throw new FormatException("DateTime is not in a valid format", exception); } //------------------------------------------------------------ // Return result //------------------------------------------------------------ return w3CDateTime; }
/// <summary> /// Returns a value indicating whether the specified date represents a null date. /// </summary> /// <param name="dateTime">The <see cref="W3CDateTime"/> that needs to be checked.</param> /// <returns><b>true</b> if the value represents a null date, otherwise returns false.</returns> public static bool IsNull(W3CDateTime dateTime) { return dateTime == null || dateTime.DateTime == DateTime.MinValue; }
/*public TestContactName makeContactName() { var genders = (Gender[]) Enum.GetValues(typeof (Gender)); var gender = genders[_Random.Next(0, genders.Length)]; return NameBuilder.MakeTestContactName(gender); }*/ public string MakeRandomW3CDate(DateTime lowerBound, DateTime upperBound) { var w3cRandomDate = new W3CDateTime(MakeRandomDateTime(lowerBound, upperBound), new TimeSpan(0)).ToString(); return w3cRandomDate; }
/// <summary> /// Returns a value indicating whether the specified date represents a null date. /// </summary> /// <param name="dateTime">The <see cref="W3CDateTime"/> that needs to be checked.</param> /// <returns><b>true</b> if the value represents a null date, otherwise returns false.</returns> public static bool IsNull(W3CDateTime dateTime) { return(dateTime == null || dateTime.DateTime == DateTime.MinValue); }
/// <summary> /// Converts the specified string representation of a W3C date and time to its <see cref="W3CDateTime"/> equivalent. /// </summary> /// <param name="s">A string containing a date and time to convert.</param> /// <returns>A W3CDateTime equivalent to the date and time contained in s.</returns> public static W3CDateTime Parse(string s) { //------------------------------------------------------------ // Local members //------------------------------------------------------------ var offset = TimeSpan.Zero; W3CDateTime w3CDateTime; //------------------------------------------------------------ // Attempt to convert string representation //------------------------------------------------------------ //------------------------------------------------------------ // Validate parameters //------------------------------------------------------------ if (string.IsNullOrEmpty(s)) { throw new ArgumentNullException("s"); } //------------------------------------------------------------ // Initialize regular expression //------------------------------------------------------------ var regularExpression = Regex; //------------------------------------------------------------ // Extract results of regular expression match //------------------------------------------------------------ var match = regularExpression.Match(s); //------------------------------------------------------------ // Determine if string represents a W3C DateTime //------------------------------------------------------------ if (!match.Success) { //------------------------------------------------------------ // Raise exception //------------------------------------------------------------ throw new FormatException("DateTime is not in a valid format"); } //------------------------------------------------------------ // Attempt to parse string //------------------------------------------------------------ try { //------------------------------------------------------------ // Extract year and handle 2/3 digit years //------------------------------------------------------------ var year = int.Parse(match.Groups["year"].Value, CultureInfo.InvariantCulture); if (year < 1000 && match.Groups["year"].Length < 3) { if (year < 50) { year = year + 2000; } else { year = year + 1999; } } //------------------------------------------------------------ // Extract other date time parts //------------------------------------------------------------ var month = (match.Groups["month"].Success) ? int.Parse(match.Groups["month"].Value, CultureInfo.InvariantCulture) : 1; var day = match.Groups["day"].Success ? int.Parse(match.Groups["day"].Value, CultureInfo.InvariantCulture) : 1; int millisecond; int second; int minute; int hour; if (match.Groups["hour"].Success) { hour = match.Groups["hour"].Success ? int.Parse(match.Groups["hour"].Value, CultureInfo.InvariantCulture) : 0; minute = match.Groups["min"].Success ? int.Parse(match.Groups["min"].Value, CultureInfo.InvariantCulture) : 0; second = match.Groups["sec"].Success ? int.Parse(match.Groups["sec"].Value, CultureInfo.InvariantCulture) : 0; millisecond = match.Groups["ms"].Success ? (int)Math.Round((1000 * double.Parse(match.Groups["ms"].Value, CultureInfo.InvariantCulture))) : 0; } else { hour = 0; minute = 0; second = 0; millisecond = 0; } //------------------------------------------------------------ // Calculate offset //------------------------------------------------------------ if (match.Groups["ofs"].Success) { offset = ParseW3COffSet(match.Groups["ofs"].Value); } //------------------------------------------------------------ // Generate result //------------------------------------------------------------ w3CDateTime = new W3CDateTime(new DateTime(year, month, day, hour, minute, second, millisecond) - offset, offset); } catch (Exception exception) { //------------------------------------------------------------ // Raise exception //------------------------------------------------------------ throw new FormatException("DateTime is not in a valid format", exception); } //------------------------------------------------------------ // Return result //------------------------------------------------------------ return(w3CDateTime); }