From eb468076a79982568e3bc66ae236fa1d2949ee1d Mon Sep 17 00:00:00 2001 From: Kenneth Pouncey Date: Sat, 4 Apr 2020 13:37:21 +0200 Subject: [wasm] Wasm TimeZoneInfo implementation VFS (#17760) * [wasm][bcl] TimeZoneInfo implementation for WebAssembly to read from WASM VFS `/zoneinfo`. - Remove `TimeZoneInfo.WebAssembly.cs` file - Remove source dependencies on `TimeZoneInfo.WebAssembly.cs`. - Modify TimeZoneInfo for specific WASM functionality - Default root directory is `zoneinfo` - Add icall `mono_timezone_get_local_name` for WASM in CreateLocal - Add icall implementation - ves_icall_System_TimeZoneInfo_mono_timezone_get_local_name * [wasm][build] Add `-s FORCE_FILESYSTEM=1` to mono wasm runtime build - Required so that it includes support for loading pre preload packages Message when generating the zoneinfo data. > Remember to build the main file with -s FORCE_FILESYSTEM=1 so that it includes support for loading this file package * [wasm] zoneinfo data and support files. - Three files for zoneinfo VFS support. - `mono-webassembly-zoneinfo-fs-smd.js.metadata` - This is the separate metadata output by emscripten file-packager. Used to parse the zoneinfo data in non browser environments. See `runtime-tests.js` - `mono-webassembly-zoneinfo-fs.js` - Output by emscripten file-packager that will be referenced by browser environments to load the zoneinfo data into the VFS at `/zoneinfo` - `zoneinfo.data` - The binary file output by emscripten file-packager. The file contains the actual zoneinfo data loaded into the VFS and parsed by `TimeZoneInfo`. * [wasm][driver] Add backing support for icall `mono_timezone_get_local_name` * Add TimeZone info test. * [wasm][packger] Add `-s FORCE_FILESYSTEM=1` - Required so that it includes support for loading preload packages Message when generating the zoneinfo data. > Remember to build the main file with -s FORCE_FILESYSTEM=1 so that it includes support for loading this file package * Fix AOT tzd test * Remove commented code lines for `TimeZoneInfo.WebAssembly.cs` module * Regenerated zone info without --no-heap-copy as it causes problems. This will create a warning in browser for now. * Add zoneinfo support files to package * Fix TimeZoneInfo tests to use the `--enable-fs` packager parameter * Add back icall implementation after rebase - ves_icall_System_TimeZoneInfo_mono_timezone_get_local_name * Add back backing support for icall `mono_timezone_get_local_name` after rebase * Fix build error * Add back zoneinfo tests to Makefile after rebase * Fix packager zoneinfo sample builds. * Address review comment about volatile not needed * Address review comment about volatile not needed * Address review comment about volatile not needed * Address review comment volatile not needed --- sdks/wasm/tests/src/tzdtest.cs | 114 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 114 insertions(+) create mode 100644 sdks/wasm/tests/src/tzdtest.cs (limited to 'sdks/wasm/tests/src/tzdtest.cs') diff --git a/sdks/wasm/tests/src/tzdtest.cs b/sdks/wasm/tests/src/tzdtest.cs new file mode 100644 index 00000000000..b00070c694f --- /dev/null +++ b/sdks/wasm/tests/src/tzdtest.cs @@ -0,0 +1,114 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using System.Globalization; + +public class TZDTest { + static TimeZoneInfo _timeZoneInstance; + static string _timeZone; + public static void Main (String [] args) + { + var local = TimeZoneInfo.Local; + Console.WriteLine($"TimeZone: {local}"); + var dt = DateTime.Now; + Console.WriteLine($"DateLocal: {dt}"); + var utc = DateTime.UtcNow; + Console.WriteLine($"DateUTC: {utc}"); + + Console.WriteLine($"GetUserTime: {GetUserTime()}"); + Console.WriteLine($"GetUtcUserTime: {GetUtcUserTime(dt)}"); + + DateTime localDate = DateTime.Now; + DateTime utcDate = DateTime.UtcNow; + String[] cultureNames = { "en-US", "en-GB", "fr-FR", + "de-DE", "ru-RU" } ; + + foreach (var cultureName in cultureNames) { + var culture = new CultureInfo(cultureName); + Console.WriteLine("{0}:", culture.NativeName); + Console.WriteLine(" Local date and time: {0}, {1:G}", + localDate.ToString(culture), localDate.Kind); + Console.WriteLine(" UTC date and time: {0}, {1:G}\n", + utcDate.ToString(culture), utcDate.Kind); + } + + var tzd = TimeZoneInfo.FindSystemTimeZoneById("US/Eastern"); + Console.WriteLine($"TimeZone: {tzd}"); + + Console.WriteLine($"TimeZone: {TimeZoneInfo.FindSystemTimeZoneById("Pacific/Honolulu")}"); + + var tzs = TimeZoneInfo.GetSystemTimeZones(); + foreach(var tzi in tzs) + { + Console.WriteLine(tzi); + } + } + + /// + /// Returns a UTC time in the user's specified timezone. + /// + /// The utc time to convert + /// Name of the timezone (Eastern Standard Time) + /// New local time + public static DateTime GetUserTime(DateTime? utcTime = null) + { + if (utcTime == null) + utcTime = DateTime.UtcNow; + + return TimeZoneInfo.ConvertTimeFromUtc(utcTime.Value, TimeZoneInstance); + } + + /// + /// Converts local server time to the user's timezone and + /// returns the UTC date. + /// + /// Use this to convert user captured date inputs and convert + /// them to UTC. + /// + /// User input (their local time) comes in as local server time + /// -> convert to user's timezone from server time + /// -> convert to UTC + /// + /// + /// + public static DateTime GetUtcUserTime(DateTime? localServerTime) + { + if (localServerTime == null) + localServerTime = DateTime.Now; + + return TimeZoneInfo.ConvertTime(localServerTime.Value, TimeZoneInstance).ToUniversalTime(); + } + + /// + /// The users TimeZone using .NET TimeZoneNames + /// + public static string TimeZone + { + get { return _timeZone; } + set + { + TimeZoneInstance = null; + _timeZone = value; + } + } + public static TimeZoneInfo TimeZoneInstance + { + get + { + if (_timeZoneInstance == null) + { + try + { + _timeZoneInstance = TimeZoneInfo.FindSystemTimeZoneById(TimeZone); + } + catch + { + TimeZone = "Pacific/Honolulu"; + _timeZoneInstance = TimeZoneInfo.FindSystemTimeZoneById(TimeZone); + } + } + return _timeZoneInstance; + } + private set { _timeZoneInstance = value; } + } +} \ No newline at end of file -- cgit v1.2.3