diff --git a/src/ConfigCatClient/User.cs b/src/ConfigCatClient/User.cs index 603f15cf..9cd2fc8e 100644 --- a/src/ConfigCatClient/User.cs +++ b/src/ConfigCatClient/User.cs @@ -1,4 +1,8 @@ +using System; using System.Collections.Generic; +using System.Globalization; +using ConfigCat.Client.Utils; +using System.Linq; #if USE_NEWTONSOFT_JSON using Newtonsoft.Json; @@ -13,6 +17,48 @@ namespace ConfigCat.Client; /// public class User { + /// + /// Converts the specified value to the format expected by UTC datetime comparison operators. + /// + /// The value to convert. + /// The User Object attribute value in the expected format. + public static string AttributeValueFrom(DateTime dateTime) + { + var unixTimeSeconds = DateTimeUtils.ToUnixTimeMilliseconds(dateTime) / 1000.0; + return unixTimeSeconds.ToString("0.###", CultureInfo.InvariantCulture); + } + + /// + /// Converts the specified value to the format expected by number comparison operators. + /// + /// The value to convert. + /// The User Object attribute value in the expected format. + public static string AttributeValueFrom(double number) + { + return number.ToString("g", CultureInfo.InvariantCulture); // format "g" allows scientific notation as well + } + + /// + /// Converts the specified items to the format expected by array comparison operators. + /// + /// The items to convert. + /// The User Object attribute value in the expected format. + public static string AttributeValueFrom(params string[] items) + { + return AttributeValueFrom(items.AsEnumerable()); + } + + /// + /// Converts the specified items to the format expected by array comparison operators. + /// + /// The items to convert. + /// The User Object attribute value in the expected format. + public static string AttributeValueFrom(IEnumerable items) + { + // TODO: escape the separator character in items? + return string.Join(",", items ?? throw new ArgumentNullException("items")); + } + internal const string DefaultIdentifierValue = ""; ///