R, 97 95 93 byte
Sử dụng các phương pháp tìm thấy ở trên trong R
c("Zzz","Good morning","Good afternoon","Good evening")[as.POSIXlt(Sys.time(),"G")$h%%20/6+1]
Giải trình:
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
Thí dụ
Lưu ý cách dòng mã này, không thêm 1, là 10 phần tử ngắn
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"
Thêm 1 đảm bảo rằng kết quả thu được lớn hơn 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"