R, 97 95 93 Bytes
Using methods found above in R
c("Zzz","Good morning","Good afternoon","Good evening")[as.POSIXlt(Sys.time(),"G")$h%%20/6+1]
Explanation:
c("Zzz","Good morning","Good afternoon","Good evening") # Creates a vector with the greetings
[ # Open bracket. The number in the bracket will extract the corresponding greeting from the vector below
as.POSIXlt( # as.POSIXlt converts the object to one of the two classes used to represent date/times
Sys.time(), # Retrieves the current time on the OS
"G") # Converts the time to the desired time zone. Does output a warning, but still converts properly to GMT
$h # Extracts the hour from the object created by as.POSIXlt
%%20/6 # Methodology as used by other golfers
+1] # Vectors in R start from 1, and not 0 like in other languages, so adding 1 to the value ensures values range from 1 to 4, not 0 to 3
Example
Notice how this line of code, without adding 1, is short 10 elements
c('Zzz','Good morning','Good afternoon','Good evening')[0:23%%20/6]
[1] "Zzz" "Zzz" "Zzz" "Zzz" "Zzz" "Zzz"
[7] "Good morning" "Good morning" "Good morning" "Good morning" "Good morning" "Good morning"
[13] "Good afternoon" "Good afternoon"
Adding 1 ensures that the result obtained is greater than 0
c('Zzz','Good morning','Good afternoon','Good evening')[as.integer(0:23)%%20/6+1]
[1] "Zzz" "Zzz" "Zzz" "Zzz" "Zzz" "Zzz"
[7] "Good morning" "Good morning" "Good morning" "Good morning" "Good morning" "Good morning"
[13] "Good afternoon" "Good afternoon" "Good afternoon" "Good afternoon" "Good afternoon" "Good afternoon"
[19] "Good evening" "Good evening" "Zzz" "Zzz" "Zzz" "Zzz"
1Without a testing environment in which one can manipulate the current time, how would you test this? Wouldn't it be better for the code to receive the current time as input instead? – Skidsdev – 2018-08-08T12:57:34.540
2If not taking input should we be using UTC / local timezone according to some OS spec / whatever we want (needs some restriction though as it's always morning somewhere in the world) – Jonathan Allan – 2018-08-08T13:04:04.737
I think you forgot about
Good night
... – mbomb007 – 2018-08-08T13:55:05.2601@mbomb007 the program is sleeping at night – lolad – 2018-08-08T14:19:44.780
2Well, there goes my Novell login script solution - its
%GREETING_TIME
variable doesn't have "Zzz", it just switches from "evening" to "morning" at midnight. – Neil – 2018-08-08T15:34:56.8209I recommend against restricting to a specific time zone, but am in favor of restricting to your own system time instead. Many answers are currently invalid because of it. – Erik the Outgolfer – 2018-08-08T18:16:05.670