using System.Globalization;
using System.Text;
namespace AIStudio.Tools;
public static class CommonTools
{
///
/// Get all the values (the names) of an enum as a string, separated by commas.
///
/// The enum type to get the values of.
/// The values to exclude from the result.
/// The values of the enum as a string, separated by commas.
public static string GetAllEnumValues(params TEnum[] exceptions) where TEnum : struct, Enum
{
var sb = new StringBuilder();
foreach (var value in Enum.GetValues())
if(!exceptions.Contains(value))
sb.Append(value).Append(", ");
return sb.ToString();
}
///
/// Resolves a from the active language plugin's IETF tag.
///
/// The IETF language tag provided by the active language plugin.
/// The matching culture when the tag is valid; otherwise .
public static CultureInfo DeriveActiveCultureOrInvariant(string? ietfTag)
{
if (string.IsNullOrWhiteSpace(ietfTag))
return CultureInfo.InvariantCulture;
try
{
return CultureInfo.GetCultureInfo(ietfTag);
}
catch (CultureNotFoundException)
{
return CultureInfo.InvariantCulture;
}
}
///
/// Formats a timestamp using the short date and time pattern of the specified culture.
///
/// The timestamp to format.
/// The culture whose short date and time pattern should be used.
/// The localized timestamp string.
public static string FormatTimestampToGeneral(DateTime timestamp, CultureInfo culture) => timestamp.ToString("g", culture);
}