From a5223c8f3f950deb7616e8e0793cec2cd44bedf9 Mon Sep 17 00:00:00 2001 From: Adam Simon Date: Fri, 15 Sep 2023 11:22:46 +0200 Subject: [PATCH] Add helper methods for converting numeric/datetime values to User Object attribute values --- src/ConfigCatClient/User.cs | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/ConfigCatClient/User.cs b/src/ConfigCatClient/User.cs index 603f15cf..e0a2f897 100644 --- a/src/ConfigCatClient/User.cs +++ b/src/ConfigCatClient/User.cs @@ -1,4 +1,7 @@ +using System; using System.Collections.Generic; +using System.Globalization; +using ConfigCat.Client.Utils; #if USE_NEWTONSOFT_JSON using Newtonsoft.Json; @@ -13,6 +16,27 @@ 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 MakeAttributeValue(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 MakeAttributeValue(double number) + { + return number.ToString("g", CultureInfo.InvariantCulture); // format "g" allows scientific notation as well + } + internal const string DefaultIdentifierValue = ""; ///