using System.Globalization; using Humanizer; using Humanizer.Localisation; using Microsoft.AspNetCore.Mvc.Rendering; namespace Nop.Web.Framework.Extensions; /// /// Extensions /// public static class CommonExtensions { /// /// Returns a value indicating whether real selection is not possible /// /// Items /// A value indicating whether we should ignore items with "0" value /// A value indicating whether real selection is not possible public static bool SelectionIsNotPossible(this IList items, bool ignoreZeroValue = true) { ArgumentNullException.ThrowIfNull(items); //we ignore items with "0" value? Usually it's something like "Select All", "etc return items.Count(x => !ignoreZeroValue || !x.Value.ToString().Equals("0")) < 2; } /// /// Relative formatting of DateTime (e.g. 2 hours ago, a month ago) /// /// Source (UTC format) /// Language culture code /// Formatted date and time string public static string RelativeFormat(this DateTime source, string languageCode = "en-US") { var ts = new TimeSpan(DateTime.UtcNow.Ticks - source.Ticks); var delta = ts.TotalSeconds; CultureInfo culture; try { culture = new CultureInfo(languageCode); } catch (CultureNotFoundException) { culture = new CultureInfo("en-US"); } return TimeSpan.FromSeconds(delta).Humanize(precision: 1, culture: culture, maxUnit: TimeUnit.Year); } }