Esempio n. 1
0
        /// <summary>
        /// 将int值转换为时间点
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static Instant FromInt(int value)
        {
            var segments = SplitToIntSegments(value);

            if (YearMonthDayInstant.TryParseSegments(segments, out var dInst))
            {
                return(dInst);
            }
            else if (YearMonthInstant.TryParseSegments(segments, out var ymInst))
            {
                return(ymInst);
            }
            else if (YearQuarterInstant.TryParseSegments(segments, out var yqInst))
            {
                return(yqInst);
            }
            else if (YearHalfYearInstant.TryParseSegments(segments, out var yhInst))
            {
                return(yhInst);
            }
            else if (YearInstant.TryParseSegments(segments, out var yInst))
            {
                return(yInst);
            }
            else
            {
                throw new ArgumentException(nameof(value), $"无法将值 {value} 转换为时间点");
            }
        }
        /// <summary>
        /// 尝试从int型片段转换为实例
        /// </summary>
        /// <param name="segments"></param>
        /// <param name="instant"></param>
        /// <returns></returns>
        internal static bool TryParseSegments(int[] segments, out YearQuarterInstant instant)
        {
            var year    = segments[0];
            var quarter = segments[1] - (int)InstantType.Q1 + 1;

            if (IsValidQuarter(quarter))
            {
                instant = new YearQuarterInstant(year, quarter);
                return(true);
            }
            instant = null;
            return(false);
        }