diff options
author | Kenneth Pouncey <kjpou@pt.lu> | 2020-04-04 14:37:21 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-04-04 14:37:21 +0300 |
commit | eb468076a79982568e3bc66ae236fa1d2949ee1d (patch) | |
tree | 9cec5a97f9a73123b87b3af9c80d1ff2974bfd7d /sdks/wasm/tests/src/tzdtest.cs | |
parent | b3210bf1e5cddb78c0b78c365e8587ceab774e51 (diff) |
[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
Diffstat (limited to 'sdks/wasm/tests/src/tzdtest.cs')
-rw-r--r-- | sdks/wasm/tests/src/tzdtest.cs | 114 |
1 files changed, 114 insertions, 0 deletions
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); + } + } + + /// <summary> + /// Returns a UTC time in the user's specified timezone. + /// </summary> + /// <param name="utcTime">The utc time to convert</param> + /// <param name="timeZoneName">Name of the timezone (Eastern Standard Time)</param> + /// <returns>New local time</returns> + public static DateTime GetUserTime(DateTime? utcTime = null) + { + if (utcTime == null) + utcTime = DateTime.UtcNow; + + return TimeZoneInfo.ConvertTimeFromUtc(utcTime.Value, TimeZoneInstance); + } + + /// <summary> + /// 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 + /// </summary> + /// <param name="localServerTime"></param> + /// <returns></returns> + public static DateTime GetUtcUserTime(DateTime? localServerTime) + { + if (localServerTime == null) + localServerTime = DateTime.Now; + + return TimeZoneInfo.ConvertTime(localServerTime.Value, TimeZoneInstance).ToUniversalTime(); + } + + /// <summary> + /// The users TimeZone using .NET TimeZoneNames + /// </summary> + 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 |