Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Time To 12 / 24 Hour Clock

Discussion in 'Scripting' started by TwoTen, Aug 5, 2016.

  1. TwoTen

    TwoTen

    Joined:
    May 25, 2016
    Posts:
    1,168
    Hello. I am currently using Network.time wich returns a value (seconds). It returns as an int.
    What I want to do is to take this value. And convert it to a 12 and 24 hour clock depending on a bool.
    Seems easy enough. my problem arrives when the value becomes something like 1515953 seconds. It needs to reset once the timer hit its final. And this calculation needs to be done equally across all the clients.

    How would this be done?
     
  2. jister

    jister

    Joined:
    Oct 9, 2009
    Posts:
    1,749
    do the calculations on the server and send it to the clients. make properties for the minutes and hours.
    Code (CSharp):
    1. private int _seconds;  
    2. private int _minutes;
    3. private int _hours;
    4.  
    5. public int seconds
    6. {
    7.     get
    8.     (
    9.         _seconds = Network.time;
    10.         return _seconds;
    11.     )
    12. }
    13. public int minutes
    14. {
    15.     get
    16.     (
    17.         _minutes = (int)((seconds/60f)%60);
    18.         return _minutes;
    19.     )
    20. }
    21. public int hours
    22. {
    23.     get
    24.     (
    25.         _hours = (int)((minutes/60f)%24);
    26.         return _hours;
    27.     )
    28. }
    untested...
     
  3. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
  4. TwoTen

    TwoTen

    Joined:
    May 25, 2016
    Posts:
    1,168
    Sadly its about calculation.
    Seems to have ALOT of formatting errors. Expecting }s & )s everywhere.
    Mind looking over it?
     
  5. TwoTen

    TwoTen

    Joined:
    May 25, 2016
    Posts:
    1,168
    Nvm. Fixed it. This is my code:
    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4. using UnityEngine.Networking;
    5.  
    6. public class TimeSync : NetworkBehaviour
    7. {
    8.     private int _seconds;
    9.     private int _minutes;
    10.     private int _hours;
    11.     private string _time;
    12.     public string CurrentTime;
    13.     public int Multiplier = 1;
    14.  
    15.     public int seconds
    16.     {
    17.         get
    18.         {
    19.             _seconds = System.Convert.ToInt32(Network.time) * Multiplier;
    20.             return _seconds;
    21.         }
    22.     }
    23.     public int minutes
    24.     {
    25.         get
    26.         {
    27.             _minutes = (int)((seconds / 60f) % 60);
    28.             return _minutes;
    29.         }
    30.     }
    31.     public int hours
    32.     {
    33.         get
    34.         {
    35.             _hours = (int)((minutes / 60f) % 24);
    36.             return _hours;
    37.         }
    38.     }
    39.  
    40.     public void Time()
    41.     {
    42.         _time = hours + " : " + minutes;
    43.         CurrentTime = _time;
    44.     }
    45.  
    46.     void Start()
    47.     {
    48.         InvokeRepeating("Time", 1, 1);
    49.     }
    50. }
    51.  
    52.  
    53.  
    What now happends is that the string currenttime. the hours field allways becomes 0. The minutes field is working fantastic though. Any clues to why this is?
     
  6. TwoTen

    TwoTen

    Joined:
    May 25, 2016
    Posts:
    1,168
    Any clues to getting the hours working? We need to somehow increment it by one everytime our minutes hit 0 right?
     
  7. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    20,026
    By using TimeSpan. This code was typed up without the aid of Unity or an IDE. There may be problems (eg permissions).

    Code (CSharp):
    1. using System;
    2. using UnityEngine;
    3. using UnityEngine.Networking;
    4.  
    5. enum TimeFormat { TwelveHour, TwentyFourHour };
    6.  
    7. public class TimeSync : NetworkBehaviour
    8. {
    9.     private TimeSpan currentTime;
    10.     public string currentTimeString;
    11.  
    12.     public TimeFormat timeFormat;
    13.  
    14.     public void SetResetTime(int hours, int minutes, int seconds)
    15.     {
    16.         resetTime = new TimeSpan(hours, minutes, seconds);
    17.     }
    18.  
    19.     public void SetResetTime(int days, int hours, int minutes, int seconds)
    20.     {
    21.         resetTime = new TimeSpan(days, hours, minutes, seconds);
    22.     }
    23.  
    24.     private void SetCurrentTime()
    25.     {
    26.         currentTime = TimeSpan.FromSeconds(Network.time);
    27.    
    28.         if (timeFormat == TimeFormat.TwentyFourHour)
    29.         {
    30.             currentTimeString = currentTime.Hours + " : " + currentTime.Minutes;
    31.         }
    32.         else
    33.         {
    34.             int hours = currentTime.Hours;
    35.             string designator = "AM";
    36.        
    37.             if (hours == 0)
    38.             {
    39.                 hours = 12;
    40.             }
    41.             else if (hours == 12)
    42.             {
    43.                 designator = "PM";
    44.             }
    45.             else if (hours > 12)
    46.             {
    47.                 hours -= 12;
    48.                 designator = "PM";
    49.             }
    50.        
    51.             currentTimeString = String.Format("{0}:{1:00}{2}", hours, currentTime.Minutes, designator);
    52.         }
    53.    
    54.         CheckResetTimer();
    55.     }
    56.  
    57.     private CheckResetTimer()
    58.     {
    59.         if (currentTime.Equals(resetTime)
    60.             // do whatever
    61.     }
    62.  
    63.     void Start()
    64.     {
    65.         InvokeRepeating("SetCurrentTime", 1, 1)
    66.     }
    67. }
     
  8. TwoTen

    TwoTen

    Joined:
    May 25, 2016
    Posts:
    1,168
    Yes, There is plenty of issues with the code. Mind trying to fix it?.And also. What is the "ResetTimer" functions? I do not want any days on record. I only want to have a clock. I have currently made 24 hours ingame to 1 real hour.

    This is my current code? Is there an easy way to convert my code to yours.
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.Networking;
    4. using UnityEngine.UI;
    5.  
    6. public class TimeSync : NetworkBehaviour
    7. {
    8.     private int _minutes;
    9.     private int _hours;
    10.     public string CurrentTime;
    11.     public int Hours;
    12.     public int Minutes;
    13.     private Text timeText;
    14.  
    15.     void Awake()
    16.     {
    17.         timeText = GameObject.Find("TimeText").GetComponent<Text>();
    18.     }
    19.  
    20.     public int minutes
    21.     {
    22.         get
    23.         {
    24.             _minutes = (System.Convert.ToInt32(Mathf.Round(System.Convert.ToSingle(Network.time) / 2.5f)) % 60);
    25.             return _minutes;
    26.         }
    27.     }
    28.     public int hours
    29.     {
    30.         get
    31.         {
    32.             _hours = (int)((minutes / 60f) % 24);
    33.             return _hours;
    34.         }
    35.     }
    36.  
    37.     public void Time()
    38.     {
    39.         CurrentTime = Mathf.Abs(hours) + ":" + Mathf.Abs(minutes);
    40.         Hours = hours;
    41.         Minutes = minutes;
    42.         timeText.text = CurrentTime;
    43.     }
    44.  
    45.     void Update()
    46.     {
    47.         Time();
    48.     }
    49. }
    50.  
    51.  
     
  9. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    20,026
    You wanted the clock to reset itself after it reached a certain time. The original idea of the script (I clearly missed putting something somewhere or copy/pasted the wrong version) was to update the current timespan with the time reported by the network code and once it reached the reset timespan it would reset to zero or something.

    I think I was pretty out of it when I typed up that script. It's more broken than I thought it might be. :p
     
  10. TwoTen

    TwoTen

    Joined:
    May 25, 2016
    Posts:
    1,168
    I never want it to reset.
    All I want is to convert a counter that increments by one every second into a clock. And I want to make 24 ingame hours into 1 real life hour. I did this like this: _minutes =(System.Convert.ToInt32(Mathf.Round(System.Convert.ToSingle(Network.time)/ 2.5f))%60); At the moment. Mind fixing the code?
     
  11. TwoTen

    TwoTen

    Joined:
    May 25, 2016
    Posts:
    1,168
    BUMB.