Search Unity

[Android] Missing time when using int instead of TimeSpan

Discussion in 'Scripting' started by Mikenekro, Jul 30, 2015.

  1. Mikenekro

    Mikenekro

    Joined:
    May 11, 2014
    Posts:
    21
    I'm developing a game for Android and the timer I'm using displays some weird results.

    all it's doing is taking the total of a few timers and displaying them to the screen as a single amount of time.

    Here's the code that displays the time:

    Code (CSharp):
    1. int totalT = (int)totalTime.TotalSeconds;
    2. //    gTime.text = String.Format("{0:00}", totalTime);
    3. gTime.text = totalT.ToString ("00:00:00");
    The first line does nothing but convert the TimeSpan into an int so I could use
    Code (CSharp):
    1. int.ToString("00:00:00");
    because the .NET version Unity runs with won't allow
    Code (CSharp):
    1. TimeSpan.ToString()
    to use "00:00:00" or any arguments.

    The second commented line works fine and displays the correctly added time but when the game is built and running on my Android phone, it constantly displays the "." at the end of the countdown timers, displays "hh:mm:ss.ffffff" at the end of a static time and is not something I want to leave in my game.

    In the Editor the static values only show "hh:mm:ss" Like they should and the countdown timer does not display -->.<-- at the end of the time, on and off, while counting down. This only happens when it is built and running on Android.

    The third line works perfectly and displays the time exactly like I want it too on my Android phone. The only problem is that a chunk of time goes missing when I use it. I'm using
    Code (CSharp):
    1. TimeSpan.TotalSeconds;
    so all of the time in seconds should be allocated to
    Code (CSharp):
    1. int totalT;
    In fact, I added Debug.Log() to check "TimeSpan totalTime" before I sent it to "int totalT" and then I checked the int after it had the value assigned to it. Both of the Values were 1440 in my static time display, which is 24 minutes in seconds format. so something around "gTime.text = totalT.ToString("00:00:00");" somehow subtracts time off of the value.

    The time should read "00:24:00" when the static time is displayed but instead it reads "00:14:30" so there's about 10 minutes (totalT is displaying about 600 less than it should be) missing from the static time.

    From my countdown timer, the time should start at "00:05:20" before it starts counting down but with the third line it reads "00:03:20" before it starts counting down. so 2 Minutes have went missing from there.

    Does anyone know why the milliseconds are not displayed in the editor but on Android they display?

    or know of a way to omit the milliseconds at the end of
    Code (CSharp):
    1. gTime.text = String.Format("{0:00}", totalTime);
    or know why time is missing from the TimeSpan to Int conversion?

    I'll use either method or any method that works.

    **EDIT**
    oh wow I feel stupid. It's actually just displaying the seconds broken up into hh:mm:ss format...

    Anyways, if anyone else runs into this and you don't know how you could format a TimeSpan into an int while keeping the original value, here's what I did:

    Code (CSharp):
    1. //Use the inspector to assign the text you want to display
    2. public Text gTime;
    3.  
    4. public void calcTime()
    5. {
    6.      DateTime currentT;
    7.      DateTime after1;
    8.      DateTime after2;
    9.      TimeSpan totalTime;
    10.  
    11.      int sec1 = 500;
    12.      int sec2 = 20;
    13.  
    14.      after1 = System.DateTime.UtcNow.AddSeconds(sec1);
    15.      after2 = System.DateTime.UtcNow.AddSeconds(sec2);
    16.      currentT = System.DateTime.UtcNow;
    17.      totalTime += (after1 + after 2) - (currentT + currentT);
    18.  
    19.      //Converts the TimeSpan to separate ints
    20.      int S = (int)totalTime.Seconds;
    21.      int M = (int)totalTime.Minutes;
    22.      int H = (int)totalTime.TotalHours;
    23.      //Displays the time on your gTime Text object
    24.      gTime.text = string.Format ("{0:00}:{1:00}:{2:00}", H, M, S);
    25. }
    26.  
     
    Last edited: Aug 1, 2015