12

I am trying to get mono (Debian 2.10.8.1-5) to run properly on my raspberry pi with the raspbian distro ("Debian GNU/Linux wheezy/sid"). I have installed mono with apt-get install mono-complete.

However, I am running into an interesting issue that I cannot figure out. The DateTime.ToString() method returns an invalid string.

Below you can find my sample program with the console output:

using System;

namespace MonoTest
{
    class Program
    {
        static void Main(string[] args)
        {
            DateTime now = DateTime.Now;
            Console.WriteLine("Year: " + now.Year);
            Console.WriteLine("Month: " + now.Month);
            Console.WriteLine("Day: " + now.Day);
            Console.WriteLine("DayOfWeek: " + now.DayOfWeek);
            Console.WriteLine("DateTime.Now: " + DateTime.Now);
            Console.WriteLine(DateTime.Now.ToString("yyyy-MM-dd"));
        }
    }
}

Output:

pi@raspberrypi ~/bin $ mono MonoTest.exe
Year: 2012
Month: 8
Day: 3
DayOfWeek: Friday
DateTime.Now: 00/734718/0001 01:41:38
0001-00-734718

Interestingly enough, 734718 seems to be the the number of days elapsed till today since 01/01/0001. I have tryed it with a fresh installation on a second board but with the same issue.

Does anyone have an idea what the problem here is and how to convince DateTime.ToString() to return the right value?

Update (8/4/2012): After long digging through the mono source code I was able to trace the issue back to System.Math.Floor. Apparently, it always returns 0. I changed my test program to a simple:

static void Main(string[] args)
{
    Console.WriteLine("Floor(1.5): " + System.Math.Floor(1.5));
}

On Windows the result is "Floor(1.5): 1" Whereas on my mono setup on the raspberry pi is "Floor(1.5): 0". I have seen that System.Math.Floor is implemented as

[MethodImplAttribute (MethodImplOptions.InternalCall)]
public extern static double Floor (double d);

Tomorrow, I will look further into this issue. Does anyone know why this issue could exist?

piglet
  • 121
  • 1
  • 5

4 Answers4

6

This is Mono bug #7938, and it affects not just DateTime.ToString, but ANY function that, directly or indirectly, passes a floating-point number to a library call, when running on ARM with a Linux distro built for the hard-float ABI. More details are available at the C# on Raspberry Pi wiki page. There is currently no good fix for this, short of running a soft-float distro. I'm still hoping we can get a fixed mono runtime soon.

Joe Strout
  • 2,634
  • 2
  • 28
  • 39
3

I can't reproduce your issue on Windows (don't have raspberry, but your subject says Mono).

I copied and pasted your exact source for Main() into a new Mono console app (Mono 2.6.1), added a single line (Console.ReadLine();), and ran it:

using System;

namespace TestDateTimeNow
{

  class MainClass
  {
    public static void Main (string[] args)
    {
        DateTime now = DateTime.Now;
        Console.WriteLine("Year: " + now.Year);
        Console.WriteLine("Month: " + now.Month);
        Console.WriteLine("Day: " + now.Day);
        Console.WriteLine("DayOfWeek: " + now.DayOfWeek);
        Console.WriteLine("DateTime.Now: " + DateTime.Now);
        Console.WriteLine(DateTime.Now.ToString("yyyy-MM-dd"));
        Console.ReadLine();
    }
  }
}

I get the output below:

Mono App Console App Output

Ken White
  • 123,280
  • 14
  • 225
  • 444
  • Thanks for trying it out on Windows. I just tried it on Arch Linux 3.1.9-20 for the raspberry pi and there it works as well. But I still have no idea why it does not work on my debian installation. – piglet Aug 04 '12 at 04:33
  • Just an FYI, on my non-Arch version of the pi running raspbian (armhf), here's the ouput from your program: `mono test.exe Year: 2012 Month: 8 Day: 20 DayOfWeek: Monday DateTime.Now: 734735/00/0001 10:18:14 0001-00-734735` – chrispr Aug 20 '12 at 14:23
  • its a well known bug in the hardfp version of raspbian, seems to be a bigger problem than just recoding a few lines. But if you code your own "format string" functions, you can still pull year, month, date from the DateTime class to get around it. Just dont expect the toString() to format correctly. – BerggreenDK Feb 25 '13 at 22:31
1

That's most likely to be an ARM Mono binary layer bug. Especially since you are running a version that was release before Raspberry Pi. You will have much better luck with that on mono-devel mailing list. And it's possible that this is fixed in git already.

skolima
  • 31,963
  • 27
  • 115
  • 151
0

You need to use the soft floating point OS for this. 2013-05-29-wheezy-armel.zip for example.

The hard floating point doesn't work well with JITers. I had the same issue, resolved it by installing the soft floating point version of the OS on my RP. I wouldn't recommended using .net with a hard floating point OS under the raspberry pi platform. Hope this helps.

TravisWhidden
  • 2,142
  • 1
  • 19
  • 43