コード例 #1
0
        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));
        }
コード例 #2
0
        public void FirstOfAugust2017NoonGivesCorrectUnixTimestamp(TimeUnitPrecision precision, long expectedTimestamp)
        {
            var dateTime = 1.August(2017);

            var timestamp = dateTime.ToUnixTime(precision);

            timestamp.Should().Be(expectedTimestamp);
        }
コード例 #3
0
        public void UnixEpochGivesUnixTimestampOfZero(TimeUnitPrecision precision)
        {
            var unixEpoch = 1.January(1970);

            var timestamp = unixEpoch.ToUnixTime(precision);

            timestamp.Should().Be(0);
        }
コード例 #4
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());
        }
コード例 #5
0
        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");
            }
        }
コード例 #6
0
        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('?')
            };
        }
コード例 #7
0
        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);
            }
        }
コード例 #8
0
        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);
        }
コード例 #9
0
 public void StringRepresentationFollowsLineProtocolSpecification(TimeUnitPrecision precision, string expected)
 {
     precision.ToPrecisionString().Should().Be(expected);
 }