- Home /
TimeSpan error on ios platform
Hello, I'm struggling in TimeSpan class's error on ios platform(System.TimeSpan). Whenever I use TimeSpan.FromSeconds, it throws out outside range exception [MinValue, MaxValue], so I tried to show the two values.
Just use
Debug.Log(TimeSpan.MinValue.TotalSeconds.ToString() + "," + TimeSpan.MaxValue.TotalSeconds.ToString());
In editor, it prints the right value which is "-922337203685.478,922337203685.478". But on ios, the same code prints "4.0917397079469E+142,4.0917397079469E+142", that's really not right, and I guess it is this bug which caused the exception.
I know I can write a timespan class by myself but I'm wondering if I did something wrong because I cannot see anyone else find the same bug, or it is really a bug.
update: I just checked DateTime.Ticks
var time = DateTime.Now; Debug.Log(time.Ticks.ToString());
In editor, this prints a reasonable value in range. And on ios, this prints a value which is out of DateTime's max Ticks value, really sucks. I guess it is is because ios has a different TicksPer$$anonymous$$illsecond at bottom, but the mono used a constant range limit which does not suit ios's tick system.
Then I can't use any thing related to DateTime's calculation. Some one can help?
Answer by DarkMagicCK · Dec 11, 2013 at 09:27 AM
OK, bug confirmed in Unity 4.3.1f1. Because I have a slow mac and a fast pc, I always build XCode project on PC and transfer it to Mac to build on ios. This bug will occur if you build xCode project using Windows Unity Editor, maybe as I said the ticks base is different on Windows and iOS. I've reported this bug to Unity team.
Just wanted to write that I had no issues with unity 4.1 using TimeSpan on iOS e.g.
TimeSpan timeSpan = TimeSpan.FromSeconds(time);
string timeText = string.Format("{0:D2}:{1:D2}:{2:D2}", timeSpan.Hours, timeSpan.$$anonymous$$inutes, timeSpan.Seconds);
That project was built on a mac.
Thanks, I've noticed that. Build on mac is O$$anonymous$$. Bug only occurs when you use Windows Unity to build XCode Project.
Thanks for posting this Dark$$anonymous$$agicC$$anonymous$$, I spent the past day trying to figure this out. I am experiencing the same issue on 4.3.1f1.
Answer by dupreezarmand · Apr 03, 2014 at 08:26 PM
I went for an interesting approach :-)
Create a class and have a function to get the Unix Time natively and include that in your IOS project. Call the function from your C# script.
Function in Objective C:
extern "C" {
double dateToUnixTimestamp() {
return [[NSDate date] timeIntervalSince1970];
}
}
C# Implementation:
#if UNITY_IOS [DllImport("__Internal")] private static extern double dateToUnixTimestamp();
public Int32 DateToUnixTimestamp() {
return (Int32)dateToUnixTimestamp();
}
#else
public Int32 DateToUnixTimestamp() {
return (Int32)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds;
}
#endif
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Bug with EtceteraGUIManager.cs . 1 Answer
Why can't Unity use DLLImport twice for overloaded functions? 2 Answers
Multiple Cars not working 1 Answer
grab script not working 0 Answers