SRL Archive - Research & Development Lounge

GetHour


Smartzkid

2007-08-24 16:26:58

I made this for a friend who said that I should release it because it could be helpful.

function GetHour(GMToffset: integer): integer;
var
timestring, tempstring, resultstring: string;
begin
timestring:= GetPage('http://www.worldtimeserver.com/current_time_in_UTC.aspx');
tempstring := Between('<font size="7">', ':', timestring);
resultstring := tempstring[length(tempstring)];
if(tempstring[length(tempstring) - 1] = '1')then
resultstring := '1' + resultstring;
result := StrToIntDef(resultstring, -1);
if(result = -1)then
writeln('ERROR GETTING TIME')
else
result := result + GMToffset;
end;

And here it is all commented for those who want to learn something:
function GetHour(GMToffset: integer): integer;
var
timestring, tempstring, resultstring: string;
begin
timestring:= GetPage('http://www.worldtimeserver.com/current_time_in_UTC.aspx'); //get the page's source
tempstring := Between('<font size="7">', ':', timestring); //I just looked through the source and found strings that were both near the time, and wouldn't change
resultstring := tempstring[length(tempstring)]; //Get the last character in the string resulting from the above operation
if(tempstring[length(tempstring) - 1] = '1')then //For 10, 11, and 12 o'clock. The above operation will leave out the 1's, so this checks to see if there's a 1, and if there is, adds it onto the result
resultstring := '1' + resultstring; //Adds a 1 to the beginning of the result for 10, 11, and 12 o'clock, as mentioned above
result := StrToIntDef(resultstring, -1); //Turns the above strings into an integer; I used StrToIntDef instead of the regular StrToInt because StrToIntDef will not cause errors if an invalid string is handed to it. Instead, it will return the Default value, which, in this case, is -1.
if(result = -1)then //if resultstring is not a valid integer, pass an error on to the user
writeln('ERROR GETTING TIME') //the error
else //if resultstring is valid, offset the time to the user's timezone
result := result + GMToffset; //the offset
end;

Timer

2007-08-24 18:21:50

Nice thanks.

bullzeye95

2007-08-24 18:40:34

What is the advantage of that over
function GetHour: Integer;
var
Hour, Min, Sec, MSec: Word;
begin
DecodeTime(Now, Hour, Min, Sec, MSec);
Result:= Hour;
end;

Bobarkinator

2007-08-25 00:42:03

From: bullzeye95
What is the advantage of that over
function GetHour: Integer;
var
Hour, Min, Sec, MSec: Word;
begin
DecodeTime(Now, Hour, Min, Sec, MSec);
Result:= Hour;
end;


I was going to post that lol.

Santa_Clause

2007-08-25 01:17:19

From: bullzeye95
What is the advantage of that over
function GetHour: Integer;
var
Hour, Min, Sec, MSec: Word;
begin
DecodeTime(Now, Hour, Min, Sec, MSec);
Result:= Hour;
end;


His looks more 1337 :p