Esempio n. 1
0
 public bool ObserveException(Exception exception, object component = default, string message = default, IContext exceptionCtx = default)
 =>
 _observe
 ?.Invoke(
     arg1: exception,
     arg2: _component.Substitute(value: component),
     arg3: _message.Substitute(value: message),
     arg4: _exceptionCtx.Substitute(value: exceptionCtx))
 ?? false;
Esempio n. 2
0
 /// <summary>
 /// Возвращает подстроку после последнего вхождения указанного символа <paramref name="chr"/>.
 /// <para>Если входная строка есть <see langword="null"/>, то метод возвращает либо явно заданное значение <paramref name="defaultStr"/>, либо <paramref name="str"/>.</para>
 /// <para>Если последнее вхождение символа <paramref name="chr"/> соответствует последнему символу входной строки, то метод возвращает <see cref="string.Empty"/>.</para>
 /// <para>Если символ <paramref name="chr"/> отсутствует во входной строке, то метод возвращает либо явно заданное значение <paramref name="defaultStr"/>, либо <paramref name="str"/>.</para>
 /// </summary>
 /// <param name="str">
 /// Входная строка.
 /// <para>Может быть <see langword="null"/>.</para>
 /// </param>
 /// <param name="chr">Символ.</param>
 /// <param name="defaultStr">
 /// Строка, возвращаемая в случае, если во входной строке указанный символ <paramref name="chr"/> отсутствует, либо входная строка есть <see langword="null"/>.
 /// </param>
 /// <returns>Объект <see cref="string"/>.</returns>
 public static string SubstringAfterLast(this string str, char chr, ArgumentPlaceholder <string> defaultStr = default)
 {
     if (str is null)
     {
         return(defaultStr.Substitute(str));
     }
     else
     {
         var index = str.LastIndexOf(chr);
         if (index < 0)
         {
             return(defaultStr.Substitute(str));
         }
         else if (index == str.Length - 1)
         {
             return(string.Empty);
         }
         else
         {
             return(str.Substring(index + 1));
         }
     }
 }
Esempio n. 3
0
        // TODO: Put strings into the resources.
        //
        public static void GetDatePeriod(this DateTimeOffset referenceDateTime, DatePeriodKind kind, out DateTimeOffset periodFrom, out DateTimeOffset periodUntil, ArgumentPlaceholder <DayOfWeek> firstDayOfWeek = default)
        {
            if (kind == DatePeriodKind.Any)
            {
                throw new ArgumentOutOfRangeException(nameof(kind));
            }
            //
            DateTimeOffset locPeriodFrom, locPeriodUntil;
            var            locFirstDayOfWeek = firstDayOfWeek.Substitute(value: CultureInfo.CurrentCulture.DateTimeFormat.FirstDayOfWeek);
            var            referenceDate     =
                new DateTimeOffset(
                    year: referenceDateTime.Year,
                    month: referenceDateTime.Month,
                    day: referenceDateTime.Day,
                    hour: 0,
                    minute: 0,
                    second: 0,
                    offset: referenceDateTime.Offset);
            var referenceDateUntil = referenceDate.AddDays(1.0D);

            switch (kind)
            {
            case DatePeriodKind.CurrentMonth:
                locPeriodFrom  = new DateTimeOffset(referenceDate.Year, referenceDate.Month, 1, 0, 0, 0, referenceDate.Offset);
                locPeriodUntil = locPeriodFrom.AddMonths(1);
                break;

            case DatePeriodKind.CurrentDay:
                locPeriodFrom  = referenceDate;
                locPeriodUntil = locPeriodFrom.AddDays(1.0);
                break;

            case DatePeriodKind.CurrentWeek:
                locFirstDayOfWeek = CultureInfo.CurrentCulture.DateTimeFormat.FirstDayOfWeek;
                locPeriodFrom     = referenceDate.AddDays(-(NumberOfDayOfWeek(referenceDate.DayOfWeek, locFirstDayOfWeek) - 1));
                locPeriodUntil    = locPeriodFrom.AddDays(7.0);
                break;

            case DatePeriodKind.CurrentQuarter:
                locPeriodFrom  = new DateTimeOffset(referenceDate.Year, (int)Math.Ceiling(referenceDate.Month / 3.0D) * 3, 1, 0, 0, 0, referenceDate.Offset).AddMonths(-2);
                locPeriodUntil = locPeriodFrom.AddMonths(3);
                break;

            case DatePeriodKind.CurrentHalfOfYear:
                locPeriodFrom  = referenceDate.Month > 6 ? new DateTimeOffset(referenceDate.Year, 7, 1, 0, 0, 0, referenceDate.Offset) : new DateTimeOffset(referenceDate.Year, 1, 1, 0, 0, 0, referenceDate.Offset);
                locPeriodUntil = locPeriodFrom.AddMonths(6);
                break;

            case DatePeriodKind.CurrentYear:
                locPeriodFrom  = new DateTimeOffset(referenceDate.Year, 1, 1, 0, 0, 0, referenceDate.Offset);
                locPeriodUntil = locPeriodFrom.AddYears(1);
                break;

            case DatePeriodKind.BeginOfMonth:
                locPeriodFrom  = new DateTimeOffset(referenceDate.Year, referenceDate.Month, 1, 0, 0, 0, referenceDate.Offset);
                locPeriodUntil = referenceDateUntil;
                break;

            case DatePeriodKind.BeginOfWeek:
                locPeriodFrom  = referenceDate.AddDays(-(DateTimeUtilities.NumberOfDayOfWeek(referenceDate.DayOfWeek, locFirstDayOfWeek) - 1));
                locPeriodUntil = referenceDateUntil;
                break;

            case DatePeriodKind.BeginOfQuarter:
                locPeriodFrom  = new DateTimeOffset(referenceDate.Year, (int)Math.Ceiling(referenceDate.Month / 3.0D) * 3, 1, 0, 0, 0, referenceDate.Offset).AddMonths(-2);
                locPeriodUntil = referenceDate;
                break;

            case DatePeriodKind.BeginOfHalfOfYear:
                locPeriodFrom  = referenceDate.Month > 6 ? new DateTimeOffset(referenceDate.Year, 7, 1, 0, 0, 0, referenceDate.Offset) : new DateTimeOffset(referenceDate.Year, 1, 1, 0, 0, 0, referenceDate.Offset);
                locPeriodUntil = referenceDateUntil;
                break;

            case DatePeriodKind.BeginOfYear:
                locPeriodFrom  = new DateTimeOffset(referenceDate.Year, 1, 1, 0, 0, 0, referenceDate.Offset);
                locPeriodUntil = referenceDateUntil;
                break;

            case DatePeriodKind.PreviousDay:
                locPeriodFrom  = referenceDate.AddDays(-1);
                locPeriodUntil = referenceDate;
                break;

            case DatePeriodKind.PreviousWeek:
                locPeriodFrom  = referenceDate.AddDays(-(NumberOfDayOfWeek(referenceDate.DayOfWeek, locFirstDayOfWeek) + 6));
                locPeriodUntil = locPeriodFrom.AddDays(7.0);
                break;

            case DatePeriodKind.PreviousMonth:
                locPeriodFrom  = new DateTimeOffset(referenceDate.Year, referenceDate.Month, 1, 0, 0, 0, 0, referenceDate.Offset).AddMonths(-1);
                locPeriodUntil = locPeriodFrom.AddMonths(1);
                break;

            case DatePeriodKind.PreviousQuarter:
                locPeriodFrom  = new DateTimeOffset(referenceDate.Year, (int)Math.Ceiling(referenceDate.Month / 3.0D) * 3, 1, 0, 0, 0, referenceDate.Offset).AddMonths(-5);
                locPeriodUntil = locPeriodFrom.AddMonths(3);
                break;

            case DatePeriodKind.PreviousHalfOfYear:
                locPeriodFrom  = referenceDate.Month > 6 ? new DateTimeOffset(referenceDate.Year, 7, 1, 0, 0, 0, referenceDate.Offset).AddMonths(-6) : new DateTimeOffset(referenceDate.Year, 1, 1, 0, 0, 0, referenceDate.Offset).AddMonths(-6);
                locPeriodUntil = locPeriodFrom.AddMonths(6);
                break;

            case DatePeriodKind.PreviousYear:
                locPeriodFrom  = new DateTimeOffset(referenceDate.Year, 1, 1, 0, 0, 0, referenceDate.Offset).AddYears(-1);
                locPeriodUntil = locPeriodFrom.AddYears(1);
                break;

            case DatePeriodKind.NextDay:
                locPeriodFrom  = referenceDate.AddDays(1);
                locPeriodUntil = locPeriodFrom.AddDays(1);
                break;

            case DatePeriodKind.NextWeek:
                locPeriodFrom  = referenceDate.AddDays(-(NumberOfDayOfWeek(referenceDate.DayOfWeek, locFirstDayOfWeek) - 8));
                locPeriodUntil = locPeriodFrom.AddDays(7.0);
                break;

            case DatePeriodKind.NextMonth:
                locPeriodFrom  = new DateTimeOffset(referenceDate.Year, referenceDate.Month, 1, 0, 0, 0, 0, referenceDate.Offset).AddMonths(1);
                locPeriodUntil = locPeriodFrom.AddMonths(1);
                break;

            case DatePeriodKind.NextQuarter:
                locPeriodFrom  = new DateTimeOffset(referenceDate.Year, (int)Math.Ceiling(referenceDate.Month / 3.0D) * 3, 1, 0, 0, 0, referenceDate.Offset).AddMonths(1);
                locPeriodUntil = locPeriodFrom.AddMonths(3);
                break;

            case DatePeriodKind.NextHalfOfYear:
                locPeriodFrom  = referenceDate.Month > 6 ? new DateTimeOffset(referenceDate.Year, 7, 1, 0, 0, 0, referenceDate.Offset).AddMonths(6) : new DateTimeOffset(referenceDate.Year, 1, 1, 0, 0, 0, referenceDate.Offset).AddMonths(6);
                locPeriodUntil = locPeriodFrom.AddMonths(6);
                break;

            case DatePeriodKind.NextYear:
                locPeriodFrom  = new DateTimeOffset(referenceDate.Year, 1, 1, 0, 0, 0, referenceDate.Offset).AddYears(1);
                locPeriodUntil = locPeriodFrom.AddYears(1);
                break;

            default:
                throw new ArgumentOutOfRangeException(nameof(kind), $"Значение '{kind}' (тип '{kind.GetType().FullName}' не поддерживается).");
            }
            periodFrom  = locPeriodFrom;
            periodUntil = locPeriodUntil;
        }