[bash] mlength=('xx' '31' '28' '31' '30' '31' '30' '31' '31' '30' '31' '30' '31')
-
No. That's far too self-documenting and NIH. Let's do something cute instead.
Spotted while reviewing some utility I used to use, which has had - apparently - quite a few updates since I last had cause to use it....
bash
:# @params: #month #year # @deps: (none) days_of_month() { m="$1"; y="$2"; a=$(( 30+(m+m/8)%2 )) (( m==2 )) && a=$((a-2)) (( m==2 && y%4==0 && ( y<100 || y%100>0 || y%400==0) )) && a=$((a+1)) printf '%d' $a }
-
@PJH said in [bash] mlength=('xx' '31' '28' '31' '30' '31' '30' '31' '31' '30' '31' '30' '31'):
No. That's far too self-documenting and NIH. Let's do something cute instead.
Spotted while reviewing some utility I used to use, which has had - apparently - quite a few updates since I last had cause to use it....
bash
:# @params: #month #year # @deps: (none) days_of_month() { m="$1"; y="$2"; a=$(( 30+(m+m/8)%2 )) (( m==2 )) && a=$((a-2)) (( m==2 && y%4==0 && ( y<100 || y%100>0 || y%400==0) )) && a=$((a+1)) printf '%d' $a }
I've seen that done in Java. So a lot of text that made it even harder to understand what the fuck was going on, because it covered a couple of pages of code.
-
@PJH said in [bash] mlength=('xx' '31' '28' '31' '30' '31' '30' '31' '31' '30' '31' '30' '31'):
Just imagine the following line, but for this function written in Javascript:
# @deps: (none)
*shudder*
-
@cvi
leftpad
oris-promise
?
-
To be fair, the longer one does account for leap years...
What's scarier is that looking at that first line I can see what it's doing and wouldn't have thought it strange if I saw it.
-
@Watson said in [bash] mlength=('xx' '31' '28' '31' '30' '31' '30' '31' '31' '30' '31' '30' '31'):
I can see what it's doing and wouldn't have thought it strange if I saw it.
Ruined. Ruined, I tell you...
-
@PJH said in [bash] mlength=('xx' '31' '28' '31' '30' '31' '30' '31' '31' '30' '31' '30' '31'):
@Watson said in [bash] mlength=('xx' '31' '28' '31' '30' '31' '30' '31' '31' '30' '31' '30' '31'):
I can see what it's doing and wouldn't have thought it strange if I saw it.
Ruined. Ruined, I tell you...
Oh, I've known that for a long time now.
($m == 2) && a = $((a-2+!(y%(400-396*!!(y%100)))))
-
@Watson that deserves a downvote. Have a !!one instead.
-
@PJH your suggested implementation gets february wrong, which is what two of the four lines of that function handle
-
@ben_lubar said in [bash] mlength=('xx' '31' '28' '31' '30' '31' '30' '31' '31' '30' '31' '30' '31'):
what two of the four lines of that function handle
I wasn't complaining about those two lines.
-
@cvi said in [bash] mlength=('xx' '31' '28' '31' '30' '31' '30' '31' '31' '30' '31' '30' '31'):
@PJH said in [bash] mlength=('xx' '31' '28' '31' '30' '31' '30' '31' '31' '30' '31' '30' '31'):
Just imagine the following line, but for this function written in Javascript:
# @deps: (none)
*shudder*
depends on the none package