public static long ToUnixTime(this DateTime dateTime, TimeUnitPrecision precision = TimeUnitPrecision.Millisecond) { var span = dateTime - Epoch; double fractionalSpan; switch (precision) { case TimeUnitPrecision.Millisecond: fractionalSpan = span.TotalMilliseconds; break; case TimeUnitPrecision.Second: fractionalSpan = span.TotalSeconds; break; case TimeUnitPrecision.Minute: fractionalSpan = span.TotalMinutes; break; case TimeUnitPrecision.Hour: fractionalSpan = span.TotalHours; break; default: throw new ArgumentOutOfRangeException(nameof(precision), precision, null); } return(Convert.ToInt64(fractionalSpan)); }
public void FirstOfAugust2017NoonGivesCorrectUnixTimestamp(TimeUnitPrecision precision, long expectedTimestamp) { var dateTime = 1.August(2017); var timestamp = dateTime.ToUnixTime(precision); timestamp.Should().Be(expectedTimestamp); }
public void UnixEpochGivesUnixTimestampOfZero(TimeUnitPrecision precision) { var unixEpoch = 1.January(1970); var timestamp = unixEpoch.ToUnixTime(precision); timestamp.Should().Be(0); }
public static string FormatPoints(this IEnumerable <Point> points, TimeUnitPrecision precision = TimeUnitPrecision.Millisecond) { var sb = new StringBuilder(); foreach (var point in points) { PointFormatter.Append(sb, point, precision); } return(sb.ToString()); }
public static void Append(StringBuilder sb, Point point, TimeUnitPrecision precision = TimeUnitPrecision.Millisecond) { sb.Append(Escape(point.Measurement)); if (point.Tags.Any()) { sb.Append($",{string.Join(",", point.Tags)}"); } sb.Append($" {string.Join(",", point.Fields)}"); if (point.Timestamp.HasValue) { sb.Append($" {point.Timestamp.Value.ToUnixTime(precision)}\n"); } }
public InfluxDbClient(Uri endpoint, string username = null, string password = null, TimeUnitPrecision precision = TimeUnitPrecision.Millisecond, bool throwOnException = false) { if (username != null && password == null || username == null && password != null) { throw new ArgumentException("When username or password is defined, both must be defined"); } this.username = username; this.password = password; this.precision = precision; this.throwOnException = throwOnException; this.endpoint = new UriBuilder(new Uri(endpoint, "write")) { Query = endpoint.Query.TrimStart('?') }; }
public static string ToPrecisionString(this TimeUnitPrecision precision) { switch (precision) { case TimeUnitPrecision.Millisecond: return("ms"); case TimeUnitPrecision.Second: return("s"); case TimeUnitPrecision.Minute: return("m"); case TimeUnitPrecision.Hour: return("h"); default: throw new ArgumentOutOfRangeException(nameof(precision), precision, null); } }
private static UriBuilder CreateQueryString(UriBuilder endpoint, string username, string password, string dbName, string retentionPolicy = null, TimeUnitPrecision precision = TimeUnitPrecision.Millisecond) { var updatedEndpoint = new UriBuilder(endpoint.Uri); var queryString = HttpUtility.ParseQueryString(updatedEndpoint.Query); if (retentionPolicy != null) { queryString.Add("rp", retentionPolicy); } queryString.Add("precision", precision.ToPrecisionString()); queryString.Add("db", dbName); queryString.Add("u", username); queryString.Add("p", password); updatedEndpoint.Query = queryString.ToString(); return(updatedEndpoint); }
public void StringRepresentationFollowsLineProtocolSpecification(TimeUnitPrecision precision, string expected) { precision.ToPrecisionString().Should().Be(expected); }