Is this trivial enough to create a patch for dateutils? lazarus / 64-FPC development ---------------------------------------------------------------------- Thaddy · 2024-01-31 05:26 And who can come up with a better name? ( I could not find this, and literally dreamt it up in my sleep ) Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i // uses dateutils;   // of coursefunction DayPart(dt: TDateTime): string;inline;begin  case HourOf(dt) of  0..5   : Result := 'night';  6..11  : Result := 'morning';  12..17 : Result := 'afternoon';  18..23 : Result := 'evening';  // The Jonas part would include :  else result := 'can never reach this'; // prevents warning, even though the range is covered  end;end; Edited after comment from 440bx  later. ---------------------------------------------------------------------- 440bx · 2024-01-31 05:51 I'd name the function "DatePart" "DayPart", seems just as descriptive yet more succinct. Also, there may be cultural nuances to the function.  I am used to evening ending at around 10:00 p.m, therefore night goes from 10:00 p.m to 5:59 a.m. I have no idea if that is universal or not.  Maybe it's something that should be selectable (maybe based on some enumeration ?) ETA: Changed the suggested function name from "DatePart" to "DayPart". ---------------------------------------------------------------------- Thaddy · 2024-01-31 05:53 An enumeration is a good idea, because that would allow for cultural differences indeed. But only when taken into account the customs for a certain time zone. (See, it becomes more complex already: do not write code in your sleep...) ---------------------------------------------------------------------- 440bx · 2024-01-31 05:56 Just noticed that I put the wrong name to the function... I meant "DayPart" not "DatePart"... oops! ---------------------------------------------------------------------- Fibonacci · 2024-01-31 06:10 This a contest? Short version of yours Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function PartOfDay(dt: TDateTime): string; inline;const  partofdays: array of string = ('night', 'morning', 'afternoon', 'evening');begin  result := partofdays[HourOf(dt) div 6];end; Better version Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function PartOfDay(dt: TDateTime): string; inline;var  hour: integer;begin  hour := HourOf(dt);  if (hour >= 23) or  (hour <=  5) then exit('night');  if (hour >=  6) and (hour <= 12) then exit('morning');  if (hour >= 13) and (hour <= 18) then exit('afternoon');  exit('evening');end; Quote  0 = night   1 = night   2 = night   3 = night   4 = night   5 = night   6 = morning   7 = morning   8 = morning   9 = morning  10 = morning  11 = morning  12 = morning  13 = afternoon  14 = afternoon  15 = afternoon  16 = afternoon  17 = afternoon  18 = afternoon  19 = evening  20 = evening  21 = evening  22 = evening  23 = night ---------------------------------------------------------------------- Thaddy · 2024-01-31 06:13 if then is not optimal.... case is. But I like the suggestion from 440bx, so I changed the name... The array part is a good idea however., and possibly more efficient. But does it belong in DateUtils? Simple code but amiss? ---------------------------------------------------------------------- Fibonacci · 2024-01-31 06:22 Indeed asmlist is shorter for the 'case' version, so: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function DayPart(dt: TDateTime): string; inline;begin  case HourOf(dt) of    23, 0..5: result := 'night';    6..12:    result := 'morning';    13..18:   result := 'afternoon';    else      result := 'evening';      end;end; ---------------------------------------------------------------------- Thaddy · 2024-01-31 06:37 The numbers do not match for the most efficient case. Mine generates more efficient assembler, at least on arm and intel. But more in general, should we add it? I do not care about the implementation we add, I was asleep.... ---------------------------------------------------------------------- Fibonacci · 2024-01-31 06:44 Quote from: Thaddy on January 31, 2024, 07:37:12 amBut more in general, should we add it? I do not care about the implementation we add, I was asleep.... I dont think its necessary in DateUtils, especially of the reason mentioned by @440bx - cultural nuances. Perhaps you can add a poll to the topic. ---------------------------------------------------------------------- Thaddy · 2024-01-31 06:59 Given your array idea that would be covered by translation. Need more coffee. Tnx both for giving this your attention. Sometimes trivial can be valuable. I would suggest that the name is well covered by 440bx and the implementation part is covered with the array solution by you.. ( I like one liners) ( Oh, btw, different subject: https://www.youtube.com/watch?v=0yq- Fw7C26Y enjoy. And who noticed the shift to 13/12 in the solo? ) ---------------------------------------------------------------------- cdbc · 2024-01-31 17:02 Hi Nifty... Reminds me of the 'Fuzzy Clock' on my KDE clock, very nice  Regards Benny ---------------------------------------------------------------------- xixixi · 2024-02-20 19:21 "DayPeriod" would be a better name, and in agreement with Unicode nomenclature. It's a lot of locale-dependent data to carry though, so I don't thing it's appropriate for the base type. https://www.unicode.org/cldr/charts/44/supplemental/day_periods.html ---------------------------------------------------------------------- dbannon · 2024-02-20 23:22 I think its too cultural dependent. And not at a national level, down to the individual.  Just about everyone as differing views about these terms, people eat at different times of the day on weekend compared to work days for example. If someone needs this sort of data, its easy enough to generate it, in a context appropriate way, in their own code. Davo ---------------------------------------------------------------------- Fibonacci · 2024-02-20 23:40 Quote from: dbannon on February 21, 2024, 12:22:44 amI think its too cultural dependent. And not at a national level, down to the individual.  Just about everyone as differing views about these terms, people eat at different times of the day on weekend compared to work days for example. Yes, I for one just woke up, its 00:40 AM, Im about to make a coffee. The day is starting for me, but first lets take a peek at the FPC forum