Skip to content

Commit

Permalink
Add helper methods for converting numeric/datetime values to User Obj…
Browse files Browse the repository at this point in the history
…ect attribute values
  • Loading branch information
adams85 committed Sep 15, 2023
1 parent 6d387f4 commit 4038358
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/ConfigCatClient/User.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using ConfigCat.Client.Utils;

#if USE_NEWTONSOFT_JSON
using Newtonsoft.Json;
Expand All @@ -13,6 +16,27 @@ namespace ConfigCat.Client;
/// </summary>
public class User
{
/// <summary>
/// Converts the specified <see cref="DateTime"/> value to the format expected by UTC datetime comparison operators.
/// </summary>
/// <param name="dateTime">The <see cref="DateTime"/> value to convert.</param>
/// <returns>The User Object attribute value in the expected format.</returns>
public static string ToAttributeValue(DateTime dateTime)
{
var unixTimeSeconds = DateTimeUtils.ToUnixTimeMilliseconds(dateTime) / 1000.0;
return unixTimeSeconds.ToString("0.###", CultureInfo.InvariantCulture);
}

/// <summary>
/// Converts the specified <see cref="double"/> value to the format expected by number comparison operators.
/// </summary>
/// <param name="number">The <see cref="double"/> value to convert.</param>
/// <returns>The User Object attribute value in the expected format.</returns>
public static string ToAttributeValue(double number)
{
return number.ToString("g", CultureInfo.InvariantCulture); // format "g" allows scientific notation as well
}

internal const string DefaultIdentifierValue = "";

/// <summary>
Expand Down

0 comments on commit 4038358

Please sign in to comment.