Unix Time stamp

Hi guys,

I have come across this script in the Unify wiki that sets the position of the sun depending on the time of day, and it seems to do all that I need…with one exception!

import System;
var date = DateTime.Now;
var timeDisplay : GUIText;
 
function Start() {
	InvokeRepeating("Increment", 1.0, 1.0);
}
function Update () {
	var seconds : float = date.TimeOfDay.Ticks / 10000000;
	transform.rotation = Quaternion.LookRotation(Vector3.up);
	transform.rotation *= Quaternion.AngleAxis(seconds/86400*360,Vector3.down);
	if (timeDisplay) timeDisplay.text = date.ToString("f");
}
 
function Increment() {
	date += TimeSpan(0,0,0, 1);
}

Basically I need to be able to get my time from the server (not a network game I must add), I have a php script that returns the time as a Unix timestamp, and just wondered if anyone knows if its possible to replace the

var date = DateTime.Now;
with a timestamp returned from the server?

The DateTime.Now is no good to me as my project is a simulator of a certain place so i need the time to reflect the time at the location and not on the users machine.[/code]

Use the WWW class to call a php file and grab the data returned from that. Then simply echo the timestamp in the php script.

Hmmm… ok tried this but no dice!

Bascially the WWW call will only return it as a string so I get errors telling me that I cannot access the methods of dateTime - is there a way of casting this data from WWW to DateTime?

string wwwResult;

// EX:
// wwwResult = "2008-04-09 17:40 PM";

DateTime dateTime;
dateTime = new DateTime();
dateTime = DateTime.ParseExact( wwwResult, "yyyy-MM-dd HH:mm tt", null)

Cheers AngryAnt - sorry for the delay in replying! Very busy at the moment :frowning:

After a lot of playing around (and swearing!) I realized that Unity, when reading the value in seconds returned by a php script using the time() method, automatically converts that value to the date format in your example.

I do however have another question - in my script i want to be able to compare the current date time with a set value (say 8pm), this will in turn set the sun to an appropriate value. I have the methods in place to get the current time from the server, but how would I go about comparing these values?

The only thing I can think of is to set a date like so:

var refDate = new DateTime();
	
	refDate = 2008-01-30 20:00;

The only issue with this is that I don’t really want the date to be compared, just the time. Any ideas?