Bug 41492 - DateTimeOffset.ToLocalTime() throwing erroneous error
Summary: DateTimeOffset.ToLocalTime() throwing erroneous error
Status: RESOLVED FIXED
Alias: None
Product: Class Libraries
Classification: Mono
Component: mscorlib (show other bugs)
Version: 4.5.X
Hardware: PC Linux
: --- normal
Target Milestone: Untriaged
Assignee: Marek Safar
URL:
Depends on:
Blocks:
 
Reported: 2016-06-03 16:59 UTC by Eirik Tsarpalis
Modified: 2016-06-10 20:00 UTC (History)
2 users (show)

Tags:
Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on GitHub or Developer Community with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED FIXED

Description Eirik Tsarpalis 2016-06-03 16:59:39 UTC
The following code

long ticks = 604094112000000000;
TimeSpan offset = TimeSpan.Zero;
var dto = new DateTimeOffset(ticks, offset);
dto.ToLocalTime(); // error!

Results in the following error:

System.ArgumentException: Offset must be specified in whole minutes.                                                                                                                    Parameter name: offset                                                                                                                                                                    at System.DateTimeOffset.ValidateOffset (System.TimeSpan offset) <0x7fd626f5e760 + 0x000c7> in <filename unknown>:0                                                                     at System.DateTimeOffset..ctor (System.DateTime dateTime) <0x7fd626f5c150 + 0x00064> in <filename unknown>:0                                                                            at System.DateTimeOffset.ToLocalTime (System.Boolean throwOnOverflow) <0x7fd626f5e2a0 + 0x00058> in <filename unknown>:0                                                                at System.DateTimeOffset.ToLocalTime () <0x7fd626f5e260 + 0x0001d> in <filename unknown>:0                                                                                              at <InteractiveExpressionClass>.Host (System.Object& $retval) <0x40bbed70 + 0x00021> in <filename unknown>:0                                                                            at Mono.CSharp.Evaluator.Evaluate (System.String input, System.Object& result, System.Boolean& result_set) <0x40b2e280 + 0x000dd> in <filename unknown>:0                               at Mono.CSharpShell.Evaluate (System.String input) <0x40b2e180 + 0x00053> in <filename unknown>:0 

This does not occur when run with CLR. I can reproduce this with latest commits of the master branch (627709367794a53694497aea57aef66a8e088bcd).
Comment 1 Eirik Tsarpalis 2016-06-03 17:09:46 UTC
I neglected to mention that that my local timezone has a +03:00 offset. This might be related to the issue.
Comment 2 Marek Safar 2016-06-07 21:48:25 UTC
I cannot reproduce the issue.

What `TimeZoneInfo.Local` says on your machine and what local time do you run it?
Comment 3 Eirik Tsarpalis 2016-06-08 03:31:55 UTC
Apologies. My local TimeZoneInfo is 'Europe/Athens'.
Comment 4 Eirik Tsarpalis 2016-06-08 03:36:22 UTC
My Local time (in ticks) that can reproduce the issue is 636009640251191610
Comment 5 Marek Safar 2016-06-08 14:16:27 UTC
For some reason I still cannot reproduce it. Could you give me output of following line on your machine?

Console.WriteLine (dto.UtcDateTime.ToLocalTime ().Ticks);
Console.WriteLine (dto.UtcDateTime.ToLocalTime ().Minute);
Comment 6 Eirik Tsarpalis 2016-06-08 21:56:08 UTC
It gives

604094168920000000
34

Btw, are you running this on Linux? I cannot reproduce the same problem in OSX.
Comment 7 Marek Safar 2016-06-10 09:22:24 UTC
I tested this on osx and linux mint and both work for me.

It looks like your linux version has some broken timezone info (or it's using some new/different features).

As I still cannot reproduce it locally.

What does TimeZoneInfo.Local.BaseUtcOffset show on your machine?
Comment 8 Eirik Tsarpalis 2016-06-10 09:29:37 UTC
I'm using Ubuntu 16.04, you're probably right that this might be machine configuration related. Running the same code in a docker container does not reproduce the issue.

To answer your question, TimeZoneInfo.Local.BaseUtcOffset evaluates to 02:00:00
Comment 9 Marek Safar 2016-06-10 09:57:53 UTC
I am not sure what's special on your 16.04 machine but it looks like some local issue. Perhaps reinstalling TZ data would fix that
Comment 10 Eirik Tsarpalis 2016-06-10 10:32:20 UTC
I'm reproducing the issue on a vanilla ubuntu server 16.04 running on Azure. Reproduction steps:

sudo timedatectl set-timezone Europe/Athens
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 3FA7E0328081BFF6A14DA29AA6A19B38D3D831EF
echo "deb http://download.mono-project.com/repo/debian wheezy main" | sudo tee /etc/apt/sources.list.d/mono-xamarin.list
sudo apt-get update
sudo apt-get install mono-complete
csharp
Comment 11 Marek Safar 2016-06-10 20:00:22 UTC
Fixed in master and Mono 4.5.1