Skip to content

Commit

Permalink
Remove static locale state, fixes nissl-lab#358
Browse files Browse the repository at this point in the history
  • Loading branch information
nja committed May 19, 2020
1 parent ebc15d9 commit dcf7f3e
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 84 deletions.
2 changes: 1 addition & 1 deletion main/SS/UserModel/DateUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,7 @@ public static DateTime GetJavaCalendar(double date, bool use1904windowing, TimeZ
}
else
{
calendar = LocaleUtil.GetLocaleCalendar(); // using default time-zone
calendar = LocaleUtil.GetLocaleCalendar(TimeZone.CurrentTimeZone); // using default time-zone
}
//calendar = DateTime.Now; // using default time-zone
SetCalendar(ref calendar, wholeDays, millisecondsInDay, use1904windowing, roundSeconds);
Expand Down
79 changes: 0 additions & 79 deletions main/Util/LocaleUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,87 +7,8 @@

namespace NPOI.Util
{
/**
* This utility class is used to set locale and time zone settings beside
* of the JDK internal {@link java.util.Locale#setDefault(Locale)} and
* {@link java.util.TimeZone#setDefault(TimeZone)} methods, because
* the locale/time zone specific handling of certain office documents -
* maybe for different time zones / locales ... - shouldn't affect
* other java components.
*
* The settings are saved in a {@link java.lang.ThreadLocal},
* so they only apply to the current thread and can't be set globally.
*/
public class LocaleUtil
{
static LocaleUtil()
{
userTimeZone = new ThreadLocal<TimeZone>();
userTimeZone.Value = TimeZone.CurrentTimeZone;
}
/**
* Excel doesn't store TimeZone information in the file, so if in doubt,
* use UTC to perform calculations
*/
public static TimeZoneInfo TIMEZONE_UTC = TimeZoneInfo.Utc;

/**
* Default encoding for unknown byte encodings of native files
* (at least it's better than to rely on a platform dependent encoding
* for legacy stuff ...)
*/
public static string CHARSET_1252 = CodePageUtil.CodepageToEncoding(CodePageUtil.CP_WINDOWS_1252);// ("CP1252");

private static ThreadLocal<TimeZone> userTimeZone;

private static ThreadLocal<CultureInfo> userLocale = new ThreadLocal<CultureInfo>();
//private static ThreadLocal<Locale> userLocale = new ThreadLocal<Locale>();

/**
* As time zone information is not stored in any format, it can be
* set before any date calculations take place.
* This setting is specific to the current thread.
*
* @param timezone the timezone under which date calculations take place
*/
public static void SetUserTimeZone(TimeZone timezone)
{
userTimeZone.Value = (timezone);
}

/**
* @return the time zone which is used for date calculations, defaults to UTC
*/
public static TimeZone GetUserTimeZone()
{
return userTimeZone.Value;
}

/**
* Sets default user locale.
* This setting is specific to the current thread.
*/
public static void SetUserLocale(CultureInfo locale)
{
userLocale.Value = (locale);
}

/**
* @return the default user locale, defaults to {@link Locale#ROOT}
*/
public static CultureInfo GetUserLocale()
{
return userLocale.Value;
}

/**
* @return a calendar for the user locale and time zone
*/
public static DateTime GetLocaleCalendar()
{
return GetLocaleCalendar(GetUserTimeZone());
}

/**
* Convenience method - month is 0-based as in java.util.Calendar
*
Expand Down
9 changes: 5 additions & 4 deletions testcases/main/SS/UserModel/TestDateUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ namespace TestCases.SS.UserModel
using NUnit.Framework;
using System.Globalization;

[TestFixture]
public class TestDateUtil
{

Expand All @@ -40,7 +41,7 @@ private void EnsureDateException()
public void GetJavaDate_InvalidValue()
{
double dateValue = -1;
TimeZone tz = LocaleUtil.GetUserTimeZone();
TimeZone tz = TimeZone.CurrentTimeZone;
bool use1904windowing = false;
bool roundSeconds = false;

Expand Down Expand Up @@ -85,7 +86,7 @@ public void GetJavaDate_InvalidValue()
public void GetJavaDate_ValidValue()
{
double dateValue = 0;
TimeZone tz = LocaleUtil.GetUserTimeZone();
TimeZone tz = TimeZone.CurrentTimeZone;
bool use1904windowing = false;
bool roundSeconds = false;

Expand All @@ -104,7 +105,7 @@ public void GetJavaDate_ValidValue()
public void GetJavaCalendar_InvalidValue()
{
double dateValue = -1;
TimeZone tz = LocaleUtil.GetUserTimeZone();
TimeZone tz = TimeZone.CurrentTimeZone;
bool use1904windowing = false;
bool roundSeconds = false;

Expand Down Expand Up @@ -142,7 +143,7 @@ public void GetJavaCalendar_InvalidValue()
public void GetJavaCalendar_ValidValue()
{
double dateValue = 0;
TimeZone tz = LocaleUtil.GetUserTimeZone();
TimeZone tz = TimeZone.CurrentTimeZone;
bool use1904windowing = false;
bool roundSeconds = false;

Expand Down

0 comments on commit dcf7f3e

Please sign in to comment.