x


Save current time on quit, then on relaunch compare to new current time and get difference?

Hi all!

Bit of a hefty question today, I'm looking to get the difference between two different times, one time being when the application was last quit, and when it was opened again. I've tried a few different methods but with no real success.

My current method involves recording the binary time (a long int) into player prefs (which requires converting into a string) and calling that back out on opening the application as a string, and converting it back to a long. This works up until a point, which is when converting it back yields the error "Cannot implicitly convert type string to long"

Here's the current code all nice and commented and stripped of irrelevant lines

   using UnityEngine;
using System.Collections;

public class DataMaster : MonoBehaviour {

    public long sysTime;
    public long lastTime;
    public long difTime;


    void Start () {

       //This line yields the "Cannot implicitly convert type string to long". 
       //It should recall the the binary time out of the player prefs and hold it as the long variable 'lastTime'
       long lastTime = long.Parse(PlayerPrefs.GetString ("sysString"));

       long difTime = (lastTime-sysTime);

       }

    void Update () 

    {
    //This fetches the time, turns it into its binary form and sends it to the sysTime long
    sysTime = System.DateTime.Now.ToBinary();

    }  

    void OnApplicationQuit ()
{
       PlayerPrefs.SetString("sysString", sysTime.ToString());

}

}

If anyone has a method to solve this, or a whole new method it would be greatly appreciated. I'm open to totally different ways of doing this, as long difTime ends up as a number I can use in further operations I'm a happy man ^_^

Thanks everyone!!

more ▼

asked Sep 28 '11 at 02:05 PM

HeyItsDaijoubu gravatar image

HeyItsDaijoubu
33 6 6 8

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

You're going to want to read up on the DateTime class as Jason mentioned. It has a ToBinary method which you have used to convert the time to a long. Do the reverse of this when you want to convert your binary data back to a long. Which is 'FromBinary'.

C# has a nice class called "Convert", which has a few methods to convert one data type to another. In this case you want to convert the string from player prefs to a long (Int64). Now you can use the FromBinary to get the DateTime.

Once you have your old DateTime, you can use it's 'Subtract' method to subtract the new from the old. This gets stored as a TimeSpan variable. So check that out, and use it how you wish. See the below example.

Be careful though! As you can see, you're grabbing the old date from player prefs and then trying to calculate the difference. But what if this is the first playthrough? You need to take this factor into account. Perhaps set the old date to current date if there is none? Resulting in a difference of 00:00:00.

Enjoy!

using UnityEngine;
using System.Collections;
using System;

public class DataMaster : MonoBehaviour
{
    DateTime currentDate;
    DateTime oldDate;
    void Start()
    {
        //Store the current time when it starts
        currentDate = System.DateTime.Now;

        //Grab the old time from the player prefs as a long
        long temp = Convert.ToInt64(PlayerPrefs.GetString("sysString"));

        //Convert the old time from binary to a DataTime variable
        DateTime oldDate = DateTime.FromBinary(temp);
        print("oldDate: " + oldDate);

        //Use the Subtract method and store the result as a timespan variable
        TimeSpan difference = currentDate.Subtract(oldDate);
        print("Difference: " + difference);

    }

    void OnApplicationQuit()
    {
        //Savee the current system time as a string in the player prefs class
        PlayerPrefs.SetString("sysString", System.DateTime.Now.ToBinary().ToString());

        print("Saving this date to prefs: " + System.DateTime.Now);
    }

}
more ▼

answered Sep 28 '11 at 03:13 PM

Default117 gravatar image

Default117
331 26 29 33

This has solved by problem, thank you very much!!

Sep 28 '11 at 03:16 PM HeyItsDaijoubu
(comments are locked)
10|3000 characters needed characters left

Add using System; to the top.

From there, you can start easily using DateTime objects to store, restore, and compare dates.

For instance:

DateTime currentDate;
currentDate = DateTime.now;

Would get today's date and store it in variable currentDate.

And these DateTime objects have all sorts of yummy things to play with, including a "DateTime.CompareTo" function. So you could do something like currentDate.CompareTo(lastDate); and get something useful out of that.

I'd recommend doing more reading about DateTime on other C#-related sites, but this is what I've been using and it works well.

more ▼

answered Sep 28 '11 at 02:09 PM

Jason B gravatar image

Jason B
1.7k 29 32 44

Thanks for the input! The above answer ended up solving it but you make a good point regardless, there's lots of trawling of MSDN and such to be done for C# :D

Sep 28 '11 at 03:18 PM HeyItsDaijoubu
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x4165
x572
x25
x17
x9

asked: Sep 28 '11 at 02:05 PM

Seen: 2029 times

Last Updated: Sep 28 '11 at 03:18 PM