Bug 41492

Summary: DateTimeOffset.ToLocalTime() throwing erroneous error
Product: [Mono] Class Libraries Reporter: Eirik Tsarpalis <eirik>
Component: mscorlibAssignee: Marek Safar <masafa>
Status: RESOLVED FIXED    
Severity: normal CC: masafa, mono-bugs+mono
Priority: ---    
Version: 4.5.X   
Target Milestone: Untriaged   
Hardware: PC   
OS: Linux   
Tags: Is this bug a regression?: ---
Last known good build:

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