public override object ConvertFromPropertyBagValue(IPropertyDefinition propertyDefinition, object value) { if (value == null) { return(null); } if (value is int) { return(Instant.FromUnixTimeSeconds((int)value)); } if (value is long) { return(Instant.FromUnixTimeSeconds((long)value)); } if (value is string) { var parseResult = ParsePattern.Parse((string)value); if (parseResult.Success) { return(parseResult.Value.ToInstant()); } throw new ValueTypeException(this, $"Error while parsing value {value} as a datetime using pattern {ParsePattern.PatternText}.", parseResult.Exception); } throw new ValueTypeException(this, $"Unable to convert value {value} to a datetime."); }
private static IEnumerable <SourceLogEntry> LoadEntries(IEnumerable <string> lines) { var builder = new SourceLogEntry.Builder(); var messageBuilder = new StringBuilder(); foreach (var line in lines) { if (line.StartsWith(CommitPrefix)) { if (builder.Hash != null) { builder.Message = messageBuilder.ToString(); messageBuilder.Length = 0; yield return(builder.Build()); builder.Reset(); } builder.Hash = line.Substring(CommitPrefix.Length); } else if (line.StartsWith(AuthorPrefix)) { var match = AuthorWithEmailPattern.Match(line); if (match.Success) { builder.AuthorName = match.Groups["name"].Value; builder.AuthorEmail = match.Groups["email"].Value; } else { builder.AuthorName = line.Substring(AuthorPrefix.Length); } } else if (line.StartsWith(DatePrefix)) { builder.Date = DatePattern.Parse(line.Substring(DatePrefix.Length).Trim()).Value.ToDateTimeOffset(); } else { string messageLine = line.Trim(); if (messageLine.Trim() == "") { continue; } if (messageBuilder.Length > 0) { messageBuilder.Append(" "); } messageBuilder.Append(messageLine); } } if (builder.Hash != null) { yield return(builder.Build()); } }
public async Task getWaterLevel(Measurement m) { LocalDateTime timeStamp = new LocalDateTime(m.dateOfMeasurment.Year, m.dateOfMeasurment.Month, m.dateOfMeasurment.Day, m.timeOfMeasurement.Hour, m.timeOfMeasurement.Minute); Instant instantTimeStamp = Instant.FromDateTimeOffset(timeStamp.WithOffset(Offset.FromHours(1)).ToDateTimeOffset()); string requestURL = apiURL + string.Format("lat={0}&lon={1}&fromtime={2}&totime={3}&datatype=all&refcode=cd&place=&file=&lang=en&interval=10&dst=1&tzone=1&tide_request=locationdata", m.latitudeOfMeasurement, m.longitudeOfMeasurement, getUrlEncodedTime(timeStamp, -30), getUrlEncodedTime(timeStamp, 30)); client.DefaultRequestHeaders.Accept.Clear(); var stringTask = client.GetStringAsync(requestURL); var msg = await stringTask; Tide entity = FromXml <Tide>(msg); //TODO: Needs proper error handling for cases where connections times out or no response is returned. List <Waterlevel> waterlevel = entity.Locationdata.Data.Find(e => e.Type == "observation").Waterlevel; if (waterlevel.Count == 7) { float tmp; if (float.TryParse(waterlevel[3].Value, out tmp)) { m.chartdatumWaterLevel = tmp; } } else { OffsetDateTimePattern offset = OffsetDateTimePattern.ExtendedIso; long x = instantTimeStamp.ToUnixTimeSeconds(); long x1 = Instant.FromDateTimeOffset(offset.Parse(waterlevel[2].Time).Value.ToDateTimeOffset()).ToUnixTimeSeconds(); long x2 = Instant.FromDateTimeOffset(offset.Parse(waterlevel[3].Time).Value.ToDateTimeOffset()).ToUnixTimeSeconds(); float y1 = float.Parse(waterlevel[2].Value); float y2 = float.Parse(waterlevel[3].Value); m.chartdatumWaterLevel = y1 + (((float)(x - x1) / (float)(x2 - x1)) * (y2 - y1)); } }
public void ParseWithNumbersToSecond() { PatternWithNumbersToSecond.Parse("26/12/2009 10:08:30 +02").GetValueOrThrow(); }
public void ParseLongMonthAndDay() { PatternWithLongMonthAndDay.Parse("Friday April 12 2013 20:28:42 +02").GetValueOrThrow(); }
static void Main(string[] args) { if (args.Length == 0) { Console.WriteLine("Error: Expected at least 1 file"); return; } OffsetDateTimePattern pattern = OffsetDateTimePattern.CreateWithInvariantCulture("uuuu'-'MM'-'dd'T'HH':'mm':'ss.FFFo<m>"); List <XmlDocument> docs = new List <XmlDocument>(); List <(OffsetDateTime, XmlDocument)> unorderedDocs = new List <(OffsetDateTime, XmlDocument)>(); foreach (var file in args) { XmlDocument doc = new XmlDocument(); try { doc.Load(file); } catch (XmlException ex) { Console.WriteLine(ex.Message); return; } catch (FileNotFoundException ex) { Console.WriteLine(ex.Message); return; } var nodeList = doc.GetElementsByTagName("Id"); var node = nodeList.Item(0); ParseResult <OffsetDateTime> time = pattern.Parse(node.InnerText); if (!time.Success) { Console.WriteLine($"Couldn't parse time: {node.InnerText}"); continue; } unorderedDocs.Add((time.Value, doc)); } unorderedDocs.Sort((d1, d2) => { return(OffsetDateTime.Comparer.Local.Compare(d1.Item1, d2.Item1)); }); Console.WriteLine("Loaded in files"); Console.Write("New file name: "); string newFileName = Console.ReadLine(); foreach (var doc in unorderedDocs) { docs.Add(doc.Item2); } double totalTimeSeconds = 0; double distanceMeters = 0; int calories = 0; foreach (var doc in docs) { var node = doc.GetElementsByTagName("TotalTimeSeconds").Item(0); totalTimeSeconds += double.Parse(node.InnerText); distanceMeters += double.Parse(doc.GetElementsByTagName("DistanceMeters").Item(0).InnerText); calories += int.Parse(doc.GetElementsByTagName("Calories").Item(0).InnerText); } docs[0].GetElementsByTagName("TotalTimeSeconds").Item(0).InnerText = totalTimeSeconds.ToString(); docs[0].GetElementsByTagName("DistanceMeters").Item(0).InnerText = distanceMeters.ToString(); docs[0].GetElementsByTagName("Calories").Item(0).InnerText = calories.ToString(); XmlNode firstDocNode = docs[0].GetElementsByTagName("Track").Item(0); for (int i = 1; i < docs.Count; i++) { XmlDocument doc = docs[i]; var nodeList = doc.GetElementsByTagName("Track").Item(0); foreach (XmlNode node in nodeList) { XmlNode importedNode = docs[0].ImportNode(node, true); firstDocNode.AppendChild(importedNode); } } docs[0].Save($"{newFileName}.tcx"); Console.WriteLine("Done"); }
public bool IsValid(string literal) { return(_hasOffset ? _offsetPattern.Parse(literal).Success : _localPattern.Parse(literal).Success); }