Recent

Author Topic: Is this trivial enough to create a patch for dateutils?  (Read 1604 times)

Thaddy

  • Hero Member
  • *****
  • Posts: 14367
  • Sensorship about opinions does not belong here.
Is this trivial enough to create a patch for dateutils?
« on: January 31, 2024, 06:26:28 am »
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][+][-]
  1. // uses dateutils;   // of course
  2. function DayPart(dt: TDateTime): string;inline;
  3. begin
  4.   case HourOf(dt) of
  5.   0..5   : Result := 'night';
  6.   6..11  : Result := 'morning';
  7.   12..17 : Result := 'afternoon';
  8.   18..23 : Result := 'evening';
  9.   // The Jonas part would include :
  10.   else result := 'can never reach this'; // prevents warning, even though the range is covered
  11.   end;
  12. end;

Edited after comment from 440bx  later.

 :D :)
« Last Edit: January 31, 2024, 07:17:06 am by Thaddy »
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

440bx

  • Hero Member
  • *****
  • Posts: 4023
Re: Is this trivial enough to create a patch for dateutils?
« Reply #1 on: January 31, 2024, 06:51:54 am »
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".
« Last Edit: January 31, 2024, 06:58:27 am by 440bx »
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

Thaddy

  • Hero Member
  • *****
  • Posts: 14367
  • Sensorship about opinions does not belong here.
Re: Is this trivial enough to create a patch for dateutils?
« Reply #2 on: January 31, 2024, 06:53:37 am »
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...)
« Last Edit: January 31, 2024, 06:55:50 am by Thaddy »
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

440bx

  • Hero Member
  • *****
  • Posts: 4023
Re: Is this trivial enough to create a patch for dateutils?
« Reply #3 on: January 31, 2024, 06:56:46 am »
Just noticed that I put the wrong name to the function... I meant "DayPart" not "DatePart"... oops!
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

Fibonacci

  • Sr. Member
  • ****
  • Posts: 419
Re: Is this trivial enough to create a patch for dateutils?
« Reply #4 on: January 31, 2024, 07:10:51 am »
This a contest?

Short version of yours

Code: Pascal  [Select][+][-]
  1. function PartOfDay(dt: TDateTime): string; inline;
  2. const
  3.   partofdays: array of string = ('night', 'morning', 'afternoon', 'evening');
  4. begin
  5.   result := partofdays[HourOf(dt) div 6];
  6. end;

Better version

Code: Pascal  [Select][+][-]
  1. function PartOfDay(dt: TDateTime): string; inline;
  2. var
  3.   hour: integer;
  4. begin
  5.   hour := HourOf(dt);
  6.   if (hour >= 23) or  (hour <=  5) then exit('night');
  7.   if (hour >=  6) and (hour <= 12) then exit('morning');
  8.   if (hour >= 13) and (hour <= 18) then exit('afternoon');
  9.   exit('evening');
  10. 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
« Last Edit: January 31, 2024, 07:13:11 am by Fibonacci »

Thaddy

  • Hero Member
  • *****
  • Posts: 14367
  • Sensorship about opinions does not belong here.
Re: Is this trivial enough to create a patch for dateutils?
« Reply #5 on: January 31, 2024, 07:13:23 am »
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?
« Last Edit: January 31, 2024, 07:35:15 am by Thaddy »
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

Fibonacci

  • Sr. Member
  • ****
  • Posts: 419
Re: Is this trivial enough to create a patch for dateutils?
« Reply #6 on: January 31, 2024, 07:22:31 am »
Indeed asmlist is shorter for the 'case' version, so:

Code: Pascal  [Select][+][-]
  1. function DayPart(dt: TDateTime): string; inline;
  2. begin
  3.   case HourOf(dt) of
  4.     23, 0..5: result := 'night';
  5.     6..12:    result := 'morning';
  6.     13..18:   result := 'afternoon';
  7.     else      result := 'evening';      
  8.   end;
  9. end;
« Last Edit: January 31, 2024, 07:25:03 am by Fibonacci »

Thaddy

  • Hero Member
  • *****
  • Posts: 14367
  • Sensorship about opinions does not belong here.
Re: Is this trivial enough to create a patch for dateutils?
« Reply #7 on: January 31, 2024, 07:37:12 am »
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.... :-[
« Last Edit: January 31, 2024, 07:39:18 am by Thaddy »
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

Fibonacci

  • Sr. Member
  • ****
  • Posts: 419
Re: Is this trivial enough to create a patch for dateutils?
« Reply #8 on: January 31, 2024, 07:44:49 am »
But 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

  • Hero Member
  • *****
  • Posts: 14367
  • Sensorship about opinions does not belong here.
Re: Is this trivial enough to create a patch for dateutils?
« Reply #9 on: January 31, 2024, 07:59:04 am »
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? )
« Last Edit: January 31, 2024, 09:15:06 am by Thaddy »
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

cdbc

  • Hero Member
  • *****
  • Posts: 1078
    • http://www.cdbc.dk
Re: Is this trivial enough to create a patch for dateutils?
« Reply #10 on: January 31, 2024, 06:02:35 pm »
Hi
Nifty... Reminds me of the 'Fuzzy Clock' on my KDE clock, very nice  :)
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 2.2.6 up until Jan 2024 from then on it's: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 3.0

xixixi

  • New Member
  • *
  • Posts: 25
Re: Is this trivial enough to create a patch for dateutils?
« Reply #11 on: February 20, 2024, 08:21:17 pm »
"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

  • Hero Member
  • *****
  • Posts: 2792
    • tomboy-ng, a rewrite of the classic Tomboy
Re: Is this trivial enough to create a patch for dateutils?
« Reply #12 on: February 21, 2024, 12:22:44 am »
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
Lazarus 3, Linux (and reluctantly Win10/11, OSX Monterey)
My Project - https://github.com/tomboy-notes/tomboy-ng and my github - https://github.com/davidbannon

Fibonacci

  • Sr. Member
  • ****
  • Posts: 419
Re: Is this trivial enough to create a patch for dateutils?
« Reply #13 on: February 21, 2024, 12:40:22 am »
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.

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 ;)

 

TinyPortal © 2005-2018