AdysTech.InfluxDB.Client.Net.StringHelper.ParseDuration C# (CSharp) Метод

ParseDuration() публичный статический Метод

public static ParseDuration ( this strDuration ) : System.TimeSpan
strDuration this
Результат System.TimeSpan
        public static TimeSpan ParseDuration(this string strDuration)
        {
            var durationLiterals = new string[] { "w", "d", "h", "m", "s", "ms", "u" };
            TimeSpan duration = TimeSpan.Zero;
            if (strDuration != "0")
            {

                var indexes = durationLiterals.Select(s => strDuration.IndexOf(s, 0)).ToArray();
                //handle only ms duration, and confusing with m and s with ms.
                if (indexes[3] == indexes[5] && indexes[4] > indexes[5])
                {
                    indexes[3] = indexes[5] = -1;
                }

                for (var index = 0; index < durationLiterals.Length; index++)
                {
                    if (indexes[index] > -1)
                    {
                        try
                        {
                            int val = 0;
                            var len = index == 0 || indexes[index - 1] == -1 ? indexes[index] : indexes[index] - indexes[index - 1] - 1;
                            var start = index == 0 || indexes[index - 1] == -1 ? 0 : indexes[index - 1] + 1;
                            if (int.TryParse(strDuration.Substring(start, len), out val))
                            {
                                switch (durationLiterals[index])
                                {
                                    case "w": duration += TimeSpan.FromDays(val * 7); break;
                                    case "d": duration += TimeSpan.FromDays(val); break;
                                    case "h": duration += TimeSpan.FromHours(val); break;
                                    case "m": duration += TimeSpan.FromMinutes(val); break;
                                    case "s": duration += TimeSpan.FromSeconds(val); break;
                                    case "ms": duration += TimeSpan.FromMilliseconds(val); break;
                                    case "u": duration += TimeSpan.FromMilliseconds(val / 1000); break;
                                }
                            }
                        }
                        catch (Exception)
                        {

                            throw;
                        }
                    }
                }

            }
            else
            {
                duration = TimeSpan.MaxValue;
            }

            return duration;
        }

Usage Example

Пример #1
0
        /// <summary>
        /// Gets the list of retention policies present in a DB
        /// </summary>
        /// <param name="dbName">Name of the database</param>
        /// <returns>List of InfluxRetentionPolicy objects</returns>
        public async Task <List <IInfluxRetentionPolicy> > GetRetentionPoliciesAsync(string dbName)
        {
            var rawpolicies = await QueryMultiSeriesAsync(dbName, "show retention policies on " + dbName);

            var policies = new List <IInfluxRetentionPolicy>();

            foreach (var policy in rawpolicies.FirstOrDefault()?.Entries)
            {
                var pol = new InfluxRetentionPolicy()
                {
                    DBName    = dbName,
                    Name      = policy.Name,
                    Duration  = StringHelper.ParseDuration(policy.Duration),
                    IsDefault = (policy.Default == "true"),
                    ReplicaN  = int.Parse(policy.ReplicaN),
                    Saved     = true
                };
                try
                {
                    //supported from Influx 12 onwards
                    pol.ShardDuration = StringHelper.ParseDuration(policy.ShardGroupDuration);
                }
                catch (Exception) { }
                policies.Add(pol);
            }
            return(policies);
        }
All Usage Examples Of AdysTech.InfluxDB.Client.Net.StringHelper::ParseDuration