How can i get server's time through perl?

well i am  having a unix based server. and in one of my web page i want to display a clock that would display the server time in 12 hrs.format(i.e. in AM/PM, not 24 hrs.). i have got the asp equivalant script for this but since on unix server it is of no use. i also downloaded 2 perl scripts for the same purpose but couldn't use them (because i don't know how to cutomise them according to my need "AM/PM") .now if anybody could give me the perl code and  simple html code that calls this script,  for the same.

                      thanxs in advance to everybody who spares a few moments
                      thinking over this.( and please don't mind i am offering 25 pointss
                      only but i am running out of them.)

                      sandeep
sandeeptyagiAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

vaksCommented:
#!/usr/bin/perl

$h = (localtime(time))[2];
$m = (localtime(time))[1];
$s = (localtime(time))[0];

$p = ($h<12?"AM":"PM");

print "Content-type: text/html\n\n";
print "Time: $h ($p) $m:$s\n<br>";


-----
Regards
Vaks
0
sandeeptyagiAuthor Commented:
thanxs Vaks

for your code. but let me tell you that since i am not very familiar with perl, i don't know what to name this cgi file and how to call it from my html file. so if you could be more specific.

and how can we add 5:30 hrs. to it to get the GMT time( i don't think this would be any difficult for you) .

thanxs yet again.

sandeep
0
vaksCommented:
#!/usr/bin/perl

$h = (gmtime(time))[2];
$m = (gmtime(time))[1];
$s = (gmtime(time))[0];

$p = ($h<12?"AM":"PM");

print "Content-type: text/html\n\n";
print "Time: $h ($p) $m:$s\n<br>";

-----------
Changed localtime to gmtime. :)

The time will come on its own page.

If you want the time inside an html page, do as follows:
1 Make a file called name.pl in your cgi-bin dir on the web server with the contents below(name is your choise).
2 Type "which perl" at the unix prompt and see that the path to perl is correct on the first line in the file. (if not, change the file)
3 Add the tag <time> in your html file
4 Look at the file on http://your_server/cgi-bin/name.pl

Regards
Vaks
-----------
#!/usr/bin/perl

print "Content-type: text/html\n\n";

$f = "/full_path_to_your_html_file/file.html";

open (FILE,$f) || die "error";
$file = join("",<FILE>);
close(FILE);

$h = (gmtime(time))[2];
$m = (gmtime(time))[1];
$s = (gmtime(time))[0];

$p = ($h<12?"AM":"PM");

$t = "$h ($p) $m:$s";
$file =~ s/\<time\>/$t/ig;

print $file;


0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

vaksCommented:
Same code as above, but with comments.

With asp you have html code with programs inside. You call the asp page to see it.

With cgi you have program with html code inside. You call the cgi program to see the html page.

(All this could be done with "server side includes" instead. See the specifications of your server for more details.)

Regards
Vaks
-------
#!/usr/bin/perl

#Inform browser of file type
#Do NOT change
print "Content-type: text/html\n\n";

#The name of your html file.
#(relative or full path)
$f = "/full_path_to_your_html_file/file.html";

#Open and read in the html file
open (FILE,$f) || die "Can't open html file";
$file = join("",<FILE>);
close(FILE);

#get time
$h = (gmtime(time))[2];
$m = (gmtime(time))[1];
$s = (gmtime(time))[0];
$p = ($h<12?"AM":"PM");

#format time
#change as you wish
# $h hour
# $p AM or PM
# $m minutes
# $s seconds
#most other chars are visible
$t = "$h ($p) $m:$s";

#replace tag with time as above
$file =~ s/\<time\>/$t/ig;

#output html code to browser
print $file;
0
sandeeptyagiAuthor Commented:
thanx vaks

your code is working perfectly.... just one more thing.. before i "accept".  how can i add 5.30 hrs. to this time.

0
sandeeptyagiAuthor Commented:
i know i have got the GMT , but 5.30 hrs. i want to add to get the IST

sandeep
0
vaksCommented:
Before the line:
-----
$p = ($h<12?"AM":"PM");
-----
Add the following lines:
-----
$h += 5 + ($m>29?1:0);
$m = ($m + 30) % 60;
----
It will look like:
----
#get time
$h = (gmtime(time))[2];
$m = (gmtime(time))[1];
$s = (gmtime(time))[0];
$h += 5 + ($m>29?1:0);
$m = ($m + 30) % 60;
$p = ($h<12?"AM":"PM");
----
....somewhere in the middle of the program.

If you want the minutes and seconds to start with a zero when < 10 (04:07 etc) add the following two lines to the code:
----
$m = ($m<10?"0":"").$m;
$s = ($s<10?"0":"").$s;
----
The final program will be:
-----------
#!/usr/bin/perl

#Inform browser of file type
#Do NOT change
print "Content-type: text/html\n\n";

#The name of your html file.
#(relative or full path)
$f = "/full_path_to_your_html_file/file.html";

#Open and read in the html file
open (FILE,$f) || die "Can't open html file";
$file = join("",<FILE>);
close(FILE);

#get time
###ALL CHANGES ARE BETWEEN HERE
$h = (gmtime(time))[2];
$m = (gmtime(time))[1];
$s = (gmtime(time))[0];
$h += 5 + ($m>29?1:0);
$m = ($m + 30) % 60;
$m = ($m<10?"0":"").$m;
$s = ($s<10?"0":"").$s;
$p = ($h<12?"AM":"PM");
###AND HERE

#format time
#change as you wish
# $h hour
# $p AM or PM
# $m minutes
# $s seconds
#most other chars are visible
$t = "$h ($p) $m:$s";

#replace tag with time as above
$file =~ s/\<time\>/$t/ig;

#output html code to browser
print $file;
--------------

Regards
Vaks
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ozoCommented:
0
sandeeptyagiAuthor Commented:
hhhhmmmmm...   vaks  it works fine..


thanxs a lot for your code and precious time you gave me .

see you with another question.

sandeep
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Perl

From novice to tech pro — start learning today.