Exemplo n.º 1
0
        /// <summary>
        /// Returns value with all non base 36 characters removed.
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static Base36Id Parse(string value)
        {
            if (string.IsNullOrEmpty(value))
            {
                throw new ArgumentNullException(paramName: "value");
            }
            string ret = _regex.Replace(value, "").ToUpper();

            if (ret.Length != 16)
            {
                throw new ArgumentException("Value was not a valid Base36 id", "value");
            }

            Base36Id fields = new Base36Id
            {
                Id        = ret,
                HostHash  = ret.Substring(10, 2),
                Reserved  = ret.Substring(12, 1),
                InService = _inService
            };
            int pos           = Base36Converter.CharList.IndexOf(fields.Reserved, System.StringComparison.Ordinal);
            int centuryCycles = Math.Max(0, ((35 - pos) - 1));
            // some precision is lost, as chars beyond 10 weren't originally from the timestamp
            // component of the ID:
            int probableEncodedLength =
                Base36Converter.FromInt64(InServiceDate.AddDays(365.25 * 115.89 * centuryCycles).Ticks / 10).Length;

            if (centuryCycles == 0)
            {
                probableEncodedLength = 10;
            }
            string encodedMs = ret.Substring(0, probableEncodedLength);
            long   ms        = Base36Converter.Decode(encodedMs);

            fields.Microseconds   = ms;
            fields.CreatedRoughly = _inService.AddTicks(ms * 10);             //.AddTicks((3656158440062976 * 10 + 9) * centuryCycles);
            return(fields);
        }