views:

90

answers:

2

I'm looking for the Delphi eqivalent to the following Ruby Code

if Time.now.hour > 12  then
  # Statement; 
else
  # Alternative Statement;
end

Thanks Stefan

+8  A: 

Use

if HourOf(Now) > 12 then
  // Statement
else
  // Alternative Statement;

after you've added DateUtils to your uses clause.

Andreas Rejbrand
+1, but can this be done without DateUtils or adding any external lib?
NixNinja
DateUtils isn't an external lib; it's part of the RTL. If you want to do it without using DateUtils, you've got the source available; just look at how HourOf implements it.
Mason Wheeler
@Stefan Liebenberg: Why would you want that? `DateUtils` is a standard part of the Delphi run-time library. But of course, generally, if you have access to the RTL source code, you can just copy this function (and the ones it relies on) to any local unit you want. In this case, however, you can just use the code given by Serg. (But notice that you then *have* to use `Time`, and not `Now`.) But when it comes to future compatibility and readability, I would definitely use the `HourOf` standard function, and not Serg's manual trick.
Andreas Rejbrand
@Andreas: Merely educational. I need to indicate the working of IF, without someone going "yes, but what does HourOf( Now ) mean?"
NixNinja
@Stefan, anyone who can read basic English and knows the function-call syntax of Delphi should be able to figure out what `HourOf(Now)` means. Such a person would probably find it easier to understand than Serg's code (which requires knowing the storage format of `TTime` values) and easier to use than `DecodeTime` (which requires three extra dummy variables and a separate statement).
Rob Kennedy
+1 @Rob Kennedy
NixNinja
+2  A: 

For example,

if Trunc (Time * 24) > 12 then ...
Serg