Recipe 14.1.
Finding the Current Date and Time
Problem
You
want to know the current
date and time.
Solution
Create a new date by using the Date( )
constructor with no parameters. Alternatively, use a CGI script or any other server-side
program to return the server time and create a new Date
object from that value.
Discussion
The date and time that ActionScript calculates
on its own is based on the client computer's date and time
settings. Therefore, if the user's computer has the incorrect time,
so will the Date object. With that caveat in mind, you can
retrieve the current client-side date and time by creating a new
Date object by using a constructor without parameters, as
follows:
// Create a new Date object.
var current:Date = new Date( );
// Displays client-side date and time.
trace(current);
If you have an Internet connection, the Flash
movie attempts to retrieve the date and time from a server. This
technique can ensure more accurate dates and times. Although the
server's time settings might be inaccurate, at least the time will
be consistent for all clients.
The basic process when reading the time from a
server is as follows:
-
Create a CGI script on the web server that
outputs the number of seconds since midnight of January 1, 1970
(the epoch).
-
Use a
flash.net.URLLoader object from ActionScript to load the
epoch seconds.
-
Convert the loaded seconds from a string to a
number, multiply by 1,000, and construct a new Date object
by passing the value to the constructor.
PHP is a
scripting language that can be found on a large number of web
hosts. It is quite simple to create a PHP page to output the
current time and date as the number of seconds since the epoch. All
you need to do is create a PHP document with the following content
and upload it to the server:
<?php echo time( );?>
If you don't have PHP on your server, or if you
are simply more comfortable with Perl (another language that is
almost universally available on web servers), then here is a Perl
script that outputs the number of seconds since the epoch:
#!/usr/local/bin/perl
print "Content-type:text/plain\n\n";
print time;
There are a few tips to keep in mind when
setting up this script on your server:
-
The first line indicates where the Perl
interpreter can be found. The value given in the example is fairly
universal. However, contact your web server administrator if you
encounter problems.
-
Many servers disable remote script execution
without particular file extensions; however, the .cgi
extension is commonly allowed. Try renaming the script
getDate.cgi.
-
Most web servers limit CGI access to specific
directories. These directories are normally found in the account's
root directory (or within the web root of the account), and are
named either cgi or cgi-bin. Make sure you save the
script in the correct directory.
-
On Unix servers, your CGI script must have its
permissions set to 755. Most
FTP programs allow you to change permissions. If you are working
from a shell script, use the following command:
-
chmod 755 filename
Regardless of what server-side language you use,
you need to load the time value from the server using ActionScript.
You can accomplish that by using a flash.net.URLLoader
object.
If you pass the Date constructor a single
value, ActionScript interprets it as the number of milliseconds
since the epoch and creates a new Date object that
corresponds to that value. Therefore, you must multiply the value
returned by the script (which is in seconds) by 1,000; for
example:
package {
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.events.Event;
public class ServerDateTimeExample {
public function ServerDateTimeExample( ) {
// The following code constructs a URLLoader object, adds
// an event listener so that the onDateTimeLoad( ) method is
// called when the data loads, and then it makes the request
// to the server script.
var loader:URLLoader = new URLLoader( );
loader.addEventListener(Event.COMPLETE, onDateTimeLoad);
loader.load(new URLRequest("script.cgi"));
}
private function onDateTimeLoad(event:Event):void {
var loader:URLLoader = URLLoader(event.target);
var data:int = parseInt(loader.data);
var current:Date = new Date(data * 1000);
trace(current);
}
}
The date is always stored in ActionScript as the
milliseconds since the epoch, and it is always displayed with
proper offsets, based on the user's local time zone setting (unless
you specifically use the UTC methods). So, if the user's computer
has the incorrect time zone setting, the display might be
incorrect. However, the actual date (as stored in epoch
milliseconds) is still correct.
See Also
Recipe 14.9; if your server's time is not reliable or accurate
enough for your needs, there are many existing date and time
servers on the Internet from which you can retrieve reasonably
accurate date and time information. See, for example, http://tycho.usno.navy.mil. For
details on synchronizing time via the Network Time Protocol, see
http://www.ntp.org.
|