Bug 34502 - DateTime does not appear to work the same as .Net when handing large months
Summary: DateTime does not appear to work the same as .Net when handing large months
Status: RESOLVED FEATURE
Alias: None
Product: Class Libraries
Classification: Mono
Component: mscorlib ()
Version: 4.0.0
Hardware: Macintosh All
: --- normal
Target Milestone: Untriaged
Assignee: Marek Safar
URL:
Depends on:
Blocks:
 
Reported: 2015-10-01 15:27 UTC by Maury Markowitz
Modified: 2015-10-05 10:58 UTC (History)
3 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 FEATURE

Description Maury Markowitz 2015-10-01 15:27:49 UTC
Our app makes extensive use of "periods" to line up financial data on month-end. The period is the number of months since the end of Jan 1900. So the data from October 2000 is period 1209, for instance.

Under VS/.Net (3.x and on) I use this code to convert the month number back to a DateTime:

    return new DateTime(1900, Period + 1, 0);

This returns the last day of the month, in this case 31 October 2000. This is a general feature of the constructor, in effect it combines DateAdd functionality so "odd" numbers will still produce useful output.

In Mono/Xamarin, it raises:

"Year, Month, and Day parameters describe an un-representable DateTime."

It appears there is some difference between the APIs.
Comment 1 Adam Burgess 2015-10-03 12:17:58 UTC
According to referencesource, 
https://github.com/Microsoft/referencesource/blob/master/mscorlib/system/datetime.cs#L173 which calls https://github.com/Microsoft/referencesource/blob/master/mscorlib/system/datetime.cs#L511 checks and validates the month, so an exception is expected

Compiling and running a sample like you provide, http://pastebin.com/U76zfQrc, throws an ArgumentOutOfRangeException for me on Windows 8 w/ .NET 3 and 4.5
Comment 2 Marek Safar 2015-10-05 10:34:11 UTC
.NET throws same error for code like  http://pastebin.com/U76zfQrc

Unhandled Exception: System.ArgumentOutOfRangeException: Year, Month, and Day parameters describe an un-representable DateTime.
   at System.DateTime.DateToTicks(Int32 year, Int32 month, Int32 day)
   at System.DateTime..ctor(Int32 year, Int32 month, Int32 day)
   at Program.Main(String[] args)
Comment 3 Maury Markowitz 2015-10-05 10:58:22 UTC
It appears this is not a Mono problem but a conversion problem. When I look at the original VB code and run it through the converter it turns into incorrect C# code. The proper call is supposed to be DateSerial, which I guess it turns into DateTime. You can close this one.