Funcular.IdGenerators.Base36.Base36IdGenerator.GetTimestamp C# (CSharp) Méthode

GetTimestamp() public méthode

Get a Base36 encoded timestamp string, based on Epoch. Use for disposable strings where global/universal uniqueness is not critical. If using the default resolution of Microseconds, 5 character values are exhausted in 1 minute. 6 characters = ½ hour. 7 characters = 21 hours. 8 characters = 1 month. 9 characters = 3 years. 10 characters = 115 years. 11 characters = 4170 years. 12 characteres = 150 thousand years.
public GetTimestamp ( int length, TimestampResolution resolution = TimestampResolution.Microsecond, System.DateTime sinceUtc = null, bool strict = false ) : string
length int
resolution TimestampResolution
sinceUtc System.DateTime Defaults to Epoch
strict bool If false (default), overflow values will use the /// value modulus 36. Otherwise it will throw an overflow exception.
Résultat string
        public string GetTimestamp(int length, TimestampResolution resolution = TimestampResolution.Microsecond, DateTime? sinceUtc = null, bool strict = false)
        {
            if (length < 1 || length > 12)
                throw new ArgumentOutOfRangeException(nameof(length), "Length must be between 1 and 12; 36^13 overflows Int64.MaxValue");
            var origin = sinceUtc ?? _epoch;
            var elapsed = DateTime.UtcNow.Subtract(origin);
            long intervals;
            switch (resolution)
            {
                case TimestampResolution.Day:
                    intervals = elapsed.Days;
                    break;
                case TimestampResolution.Hour:
                    intervals = Convert.ToInt64(elapsed.TotalHours);
                    break;
                case TimestampResolution.Minute:
                    intervals = Convert.ToInt64(elapsed.TotalMinutes);
                    break;
                case TimestampResolution.Second:
                    intervals = Convert.ToInt64(elapsed.TotalSeconds);
                    break;
                case TimestampResolution.Millisecond:
                    intervals = Convert.ToInt64(elapsed.TotalMilliseconds);
                    break;
                case TimestampResolution.Microsecond:
                    intervals = (long)(elapsed.TotalMilliseconds * 1000.0); // elapsed.TotalMicroseconds();
                    break;
                case TimestampResolution.Ticks:
                    intervals = elapsed.Ticks;
                    break;
                case TimestampResolution.None:
                default:
                    throw new ArgumentOutOfRangeException(nameof(resolution));
            }
            var combinations = Math.Pow(36, length);
            if (combinations < intervals)
            {
                if (strict)
                {
                    throw new OverflowException(string.Format("At resolution {0}, value is greater than {1}-character timestamps can express.", resolution.ToString(), length));
                }
                intervals = intervals % 36;
            }
            string encoded = Base36Converter.FromLong(intervals);
            return encoded.Length > length ?
                encoded.Substring(0, length) :
                encoded.PadLeft(length, '0');
        }