Skip to content

Commit

Permalink
Supported date/time format in en-GB (#255)
Browse files Browse the repository at this point in the history
* Supported  date/time format in en-GB

* fixed time frame in timeConverterTest

* added support for date & time in en-gb and en-us formats

* fixed failing test

* Fixed unit tests

* fixed unit test TimeConverterTests.cs

Co-authored-by: Håvard Moås <hmo@dips.no>
  • Loading branch information
Iyani-NK and haavamoa authored Dec 9, 2020
1 parent 913d6c8 commit 4b58d6a
Show file tree
Hide file tree
Showing 10 changed files with 132 additions and 73 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public enum DateAndTimeConverterFormat
/// <summary>
/// The short format, which is the same as <see cref="Default" /> to use during conversion
/// </summary>
/// <example>12 Dec 1990 12:00 PM</example>
/// <example>12 Dec 1990 13:00</example>
Short = 0,

/// <summary>
Expand All @@ -32,7 +32,7 @@ public enum DateAndTimeConverterFormat
/// <summary>
/// A text format to use during conversion
/// </summary>
/// <example>Today 12:00 PM</example>
/// <example>Today 13:00</example>
Text,
}

Expand All @@ -58,11 +58,12 @@ public object Convert(object value, Type targetType, object parameter, CultureIn
if (value == null) return string.Empty;
if (!(value is DateTime dateTimeInput))
throw new XamlParseException("The input has to be of type DateTime").WithXmlLineInfo(m_serviceProvider);
return Format switch
return Format switch
{
DateAndTimeConverterFormat.Short => ConvertToShortFormat(dateTimeInput, culture),
DateAndTimeConverterFormat.Short => ConvertToShortFormat(dateTimeInput, culture),
DateAndTimeConverterFormat.Text
=> ConvertToTextFormat(dateTimeInput, culture), _ => string.Empty
=> ConvertToTextFormat(dateTimeInput, culture),
_ => string.Empty
};
}

Expand All @@ -75,24 +76,30 @@ public object ConvertBack(object value, Type targetType, object parameter, Cultu

private static string ConvertToTextFormat(DateTime dateTimeInput, CultureInfo culture)
{
var date = new DateConverter { Format = DateConverter.DateConverterFormat.Text }.Convert(dateTimeInput, null, null, culture);
var time = new TimeConverter { Format = TimeConverter.TimeConverterFormat.Default }.Convert(dateTimeInput, null, null, culture);
var date = new DateConverter {Format = DateConverter.DateConverterFormat.Text}.Convert(dateTimeInput, null,
null, culture);
var time = new TimeConverter {Format = TimeConverter.TimeConverterFormat.Default}.Convert(dateTimeInput,
null, null, culture);

if (culture.IsNorwegian())
{
if (dateTimeInput.IsToday() || dateTimeInput.IsTomorrow() || dateTimeInput.IsYesterday())
{
return $"{date},{Space}kl{Space}{time}";
}

return $"{date}{Space}kl{Space}{time}";
}

return $"{date}{Space}{time}";
}

private static string ConvertToShortFormat(DateTime dateTimeInput, CultureInfo culture)
{
var date = new DateConverter { Format = DateConverter.DateConverterFormat.Short }.Convert(dateTimeInput, null, null, culture);
var time = new TimeConverter { Format = TimeConverter.TimeConverterFormat.Default }.Convert(dateTimeInput, null, null, culture);
var date = new DateConverter {Format = DateConverter.DateConverterFormat.Short}.Convert(dateTimeInput, null,
null, culture);
var time = new TimeConverter {Format = TimeConverter.TimeConverterFormat.Default}.Convert(dateTimeInput,
null, null, culture);

return culture.IsNorwegian() ? $"{date}{Space}kl{Space}{time}" : $"{date}{Space}{time}";
}
Expand Down
37 changes: 27 additions & 10 deletions src/DIPS.Xamarin.UI/Converters/ValueConverters/DateConverter.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using System;
using System.Diagnostics.CodeAnalysis;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using DIPS.Xamarin.UI.Extensions;
using DIPS.Xamarin.UI.Internal.Utilities;
using DIPS.Xamarin.UI.Internal.Utilities;
using DIPS.Xamarin.UI.Resources.LocalizedStrings;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
Expand Down Expand Up @@ -40,9 +40,9 @@ public enum DateConverterFormat
Text,
}

private const string Space = " ";
private IServiceProvider m_serviceProvider;

private const string Space = " ";
private IServiceProvider m_serviceProvider;

/// <summary>
/// The format to choose between, see <see cref="DateConverterFormat" />
/// </summary>
Expand All @@ -62,11 +62,12 @@ public object Convert(object value, Type targetType, object parameter, CultureIn
if (value == null) return string.Empty;
if (!(value is DateTime dateTimeInput))
throw new XamlParseException("The input has to be of type DateTime").WithXmlLineInfo(m_serviceProvider);
return Format switch
{
DateConverterFormat.Short => ConvertToDefaultDateTime(dateTimeInput, culture),
DateConverterFormat.Text =>
ConvertDateTimeAsText(dateTimeInput, culture), _ => string.Empty
return Format switch
{
DateConverterFormat.Short => ConvertToDefaultDateTime(dateTimeInput, culture),
DateConverterFormat.Text =>
ConvertDateTimeAsText(dateTimeInput, culture),
_ => string.Empty
};
}

Expand All @@ -83,6 +84,11 @@ private static string ConvertToDefaultDateTime(DateTime dateTime, CultureInfo cu

var month = GetMonthBasedOnCulture(dateTime, culture);
var year = dateTime.ToString("yyyy", culture);
if (culture.ThreeLetterWindowsLanguageName.Equals("ENU"))
{
return $"{month}{Space}{day}{Space}{year}";
}

return $"{day}{Space}{month}{Space}{year}";
}

Expand All @@ -95,6 +101,11 @@ private static string GetDayBasedOnCulture(DateTime dateTime, CultureInfo cultur
day += dateTime.GetEnglishDaySuffix();
}

if (culture.ThreeLetterWindowsLanguageName.Equals("ENU"))
{
day += ",";
}

if (culture.IsNorwegian())
{
day += ".";
Expand Down Expand Up @@ -122,6 +133,12 @@ private static string ConvertDateTimeAsText(DateTime dateTime, CultureInfo cultu

var month = GetMonthBasedOnCulture(dateTime, culture);
var day = GetDayBasedOnCulture(dateTime, culture);

if (culture.ThreeLetterWindowsLanguageName.Equals("ENU"))
{
return $"{month}{Space}{day}";
}

return $"{day}{Space}{month}";
}

Expand Down
12 changes: 9 additions & 3 deletions src/DIPS.Xamarin.UI/Converters/ValueConverters/TimeConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public enum TimeConverterFormat
/// <summary>
/// The default time converter format
/// </summary>
/// <example>12:00 PM</example>
/// <example>13:00</example>
Default = 0,
}

Expand All @@ -46,7 +46,8 @@ public object Convert(object value, Type targetType, object parameter, CultureIn
var dateTimeInput = DateTime.MinValue;
if (value == null) return string.Empty;
if (!(value is DateTime) && !(value is TimeSpan))
throw new XamlParseException("The input has to be of type DateTime or TimeSpan").WithXmlLineInfo(m_serviceProvider);
throw new XamlParseException("The input has to be of type DateTime or TimeSpan").WithXmlLineInfo(
m_serviceProvider);

switch (value)
{
Expand Down Expand Up @@ -74,14 +75,19 @@ public object ConvertBack(object value, Type targetType, object parameter, Cultu

private static string ConvertToDefaultFormat(DateTime dateTimeInput, CultureInfo culture)
{
var time = dateTimeInput.ToString("hh:mm tt", culture);
var time = dateTimeInput.ToString("HH:mm", culture);
if (culture.IsNorwegian())
{
var hour = dateTimeInput.ToString("HH", culture);
var minutes = dateTimeInput.ToString("mm", culture);
time = $"{hour}:{minutes}";
}

if (culture.ThreeLetterWindowsLanguageName.Equals("ENU"))
{
time = dateTimeInput.ToString("hh:mm tt", culture);
}

return time;
}
}
Expand Down
7 changes: 4 additions & 3 deletions src/DIPS.Xamarin.UI/Extensions/CultureInfoExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ public static class CultureInfoExtensions
/// <param name="cultureInfo"></param>
/// <returns></returns>
public static bool IsNorwegian(this CultureInfo cultureInfo) =>
cultureInfo.TwoLetterISOLanguageName.Equals("nb") || cultureInfo.TwoLetterISOLanguageName.Equals("nn");

cultureInfo.ThreeLetterWindowsLanguageName.Equals("NOR") ||
cultureInfo.ThreeLetterWindowsLanguageName.Equals("NOB") ||
cultureInfo.ThreeLetterWindowsLanguageName.Equals("NNO");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public TimeSpan Time
public DateTime DateTime => Date + Time;

public ICommand OpenLocaleMobileSettingsCommand { get; }
public string Locale => System.Threading.Thread.CurrentThread.CurrentCulture.TwoLetterISOLanguageName;
public string Locale => System.Threading.Thread.CurrentThread.CurrentCulture.ThreeLetterWindowsLanguageName;

public event PropertyChangedEventHandler PropertyChanged;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,6 @@ public DateTime Date

public ICommand OpenLocaleMobileSettingsCommand { get; }

public string Locale => System.Threading.Thread.CurrentThread.CurrentCulture.TwoLetterISOLanguageName;
public string Locale => System.Threading.Thread.CurrentThread.CurrentCulture.ThreeLetterWindowsLanguageName;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,6 @@ public TimeSpan Time
public event PropertyChangedEventHandler PropertyChanged;

public ICommand OpenLocaleMobileSettingsCommand { get; }
public string Locale => System.Threading.Thread.CurrentThread.CurrentCulture.TwoLetterISOLanguageName;
public string Locale => System.Threading.Thread.CurrentThread.CurrentCulture.ThreeLetterWindowsLanguageName;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@
using FluentAssertions;
using Xamarin.Forms.Xaml;
using Xunit;
using DateAndTimeConverterFormat = DIPS.Xamarin.UI.Converters.ValueConverters.DateAndTimeConverter.DateAndTimeConverterFormat;
using DateAndTimeConverterFormat =
DIPS.Xamarin.UI.Converters.ValueConverters.DateAndTimeConverter.DateAndTimeConverterFormat;

namespace DIPS.Xamarin.UI.Tests.Converters.ValueConverters
{
[Collection("Sequential")] //This test class is using an static shared property that is used in other tests
public class DateAndTimeConverterTests
{
private readonly DateTime m_now = new DateTime(1990, 12, 12, 12, 00, 00);
private readonly DateTime m_now = new DateTime(1990, 12, 12, 13, 00, 00);
private readonly DateAndTimeConverter m_dateAndTimeConverter = new DateAndTimeConverter();

[Theory]
Expand All @@ -41,14 +42,17 @@ public void Convert_NullInput_ShouldReturnEmptyString()
public static IEnumerable<object[]> TestDataForShortFormat =>
new List<object[]>()
{
new object[] { "no", new DateTime(1991, 12, 12,12,12,12), "12. des 1991 kl 12:12" },
new object[] { "en", new DateTime(1991, 12, 12,12,12,12), "12th Dec 1991 12:12 PM" },
new object[] { "en", new DateTime(1991, 12, 12,10,12,12), "12th Dec 1991 10:12 AM" },
new object[] {"no", new DateTime(1991, 12, 12, 13, 12, 12), "12. des 1991 kl 13:12"},
new object[] {"en-us", new DateTime(1991, 12, 12, 10, 12, 12), "Dec 12th, 1991 10:12 AM"},
new object[] {"en-us", new DateTime(1991, 12, 12, 13, 12, 12), "Dec 12th, 1991 01:12 PM"},
new object[] {"en-gb", new DateTime(1991, 12, 12, 13, 12, 12), "12th Dec 1991 13:12"},
new object[] {"en-gb", new DateTime(1991, 12, 12, 10, 12, 00), "12th Dec 1991 10:12"},
};

[Theory]
[MemberData(nameof(TestDataForShortFormat))]
public void Convert_WithShortFormat_WithCulture_CorrectFormat(string cultureName, DateTime date, string expected)
public void Convert_WithShortFormat_WithCulture_CorrectFormat(string cultureName, DateTime date,
string expected)
{
m_dateAndTimeConverter.Format = DateAndTimeConverterFormat.Short;

Expand All @@ -60,27 +64,32 @@ public void Convert_WithShortFormat_WithCulture_CorrectFormat(string cultureName
public static IEnumerable<object[]> TestDataForTextFormat =>
new List<object[]>()
{
new object[] { "en", new DateTime(1990,12,12,12,12,00), "Today 12:12 PM" },
new object[] { "en", new DateTime(1991, 12, 10, 12, 12, 12), "10th Dec 12:12 PM" },
new object[] { "en", new DateTime(1990, 12, 12, 12, 12, 00).AddDays(-1), "Yesterday 12:12 PM" },
new object[] { "en", new DateTime(1990, 12, 12, 12, 12, 00).AddDays(1), "Tomorrow 12:12 PM" },
new object[] { "no", new DateTime(1990, 12, 12, 10, 12, 00), "I dag, kl 10:12" },
new object[] { "no", new DateTime(1990, 12, 12, 12, 12, 00).AddDays(-1), "I går, kl 12:12" },
new object[] { "no", new DateTime(1990, 12, 12, 12, 12, 00).AddDays(1), "I morgen, kl 12:12" },
new object[] { "no", new DateTime(1990, 12, 10, 09, 09, 00), "10. des kl 09:09" }
new object[] {"en-gb", new DateTime(1990, 12, 12, 13, 00, 00), "Today 13:00"},
new object[] {"en-gb", new DateTime(1991, 12, 10, 13, 00, 00), "10th Dec 13:00"},
new object[] {"en-gb", new DateTime(1990, 12, 12, 13, 00, 00).AddDays(-1), "Yesterday 13:00"},
new object[] {"en-gb", new DateTime(1990, 12, 12, 13, 00, 00).AddDays(1), "Tomorrow 13:00"},
new object[] {"en-us", new DateTime(1990, 12, 12, 13, 00, 00), "Today 01:00 PM"},
new object[] {"en-us", new DateTime(1991, 12, 10, 13, 00, 00), "Dec 10th, 01:00 PM"},
new object[] {"en-us", new DateTime(1990, 12, 12, 13, 00, 00).AddDays(-1), "Yesterday 01:00 PM"},
new object[] {"en-us", new DateTime(1990, 12, 12, 13, 00, 00).AddDays(1), "Tomorrow 01:00 PM"},
new object[] {"no", new DateTime(1990, 12, 12, 13, 00, 00), "I dag, kl 13:00"},
new object[] {"no", new DateTime(1990, 12, 12, 13, 00, 00).AddDays(-1), "I går, kl 13:00"},
new object[] {"no", new DateTime(1990, 12, 12, 13, 00, 00).AddDays(1), "I morgen, kl 13:00"},
new object[] {"no", new DateTime(1990, 12, 10, 13, 00, 00), "10. des kl 13:00"}
};

[Theory]
[MemberData(nameof(TestDataForTextFormat))]
public void Convert_WithTextFormat_WithDate_WithCulture_CorrectFormat(string cultureName, DateTime date, string expected)
public void Convert_WithTextFormat_WithDate_WithCulture_CorrectFormat(string cultureName, DateTime date,
string expected)
{
Clock.OverrideClock(m_now);

m_dateAndTimeConverter.Format = DateAndTimeConverterFormat.Text;
InternalLocalizedStrings.Culture = new CultureInfo(cultureName);//To force localized strings
InternalLocalizedStrings.Culture = new CultureInfo(cultureName); //To force localized strings

var actual = m_dateAndTimeConverter.Convert<string>(date, InternalLocalizedStrings.Culture);
actual.Should().Be(expected);
}
}
}
}
Loading

0 comments on commit 4b58d6a

Please sign in to comment.