Solved How do I make the week start on Monday?

I'm running FreeBSD 13.0-RELEASE.

Like the title says, how can I make Monday be the first day of the week? I have found some (quite old) threads that say I should set my locale to en_GB, in particular, LC_TIME, but this doesn't seem to make any difference on my system.

For instance, here is my current output of locale:
Code:
LANG=C.UTF-8
LC_CTYPE="C.UTF-8"
LC_COLLATE="C.UTF-8"
LC_TIME=en_GB.UTF-8
LC_NUMERIC="C.UTF-8"
LC_MONETARY="C.UTF-8"
LC_MESSAGES="C.UTF-8"
LC_ALL=

And this is the output of cal:
Code:
    August 2021       
Su Mo Tu We Th Fr Sa  
 1  2  3  4  5  6  7  
 8  9 10 11 12 13 14  
15 16 17 18 19 20 21  
22 23 24 25 26 27 28  
29 30 31

So, what do I need to tweak to get a correct week?
 
Most of the noise will probably be over what to name the switch.
Name it -iso8601, like the international standard ISO 8601 after which the week starts with Mondays.

Week date representations are in the formats as shown in the adjacent box. [YYYY] indicates the ISO week-numbering year which is slightly different from the traditional Gregorian calendar year (see below). [Www] is the week number prefixed by the letter W, from W01 through W53. [D] is the weekday number, from 1 through 7, beginning with Monday and ending with Sunday.
 
When I wrote ad-spotting software for radio stations in the early '80s, all our radio schedules had to conform to the broadcast calendar which always starts the week on Mondays.

Writing custom calendar logic for business software was fairly commonplace in those days, since a lot of stuff had to be written from scratch. When I later applied for a programming job at a VAR in '87, one of the entry-level tests was to write a quick calendar display program in BUSINESS BASIC for a Point 4 or Nixdorf minicomputer using PRINT statements and dumb terminal output. Easy for me because of my earlier experience.
 
I suppose it is possible to make shell script to move Sunday columns at right and use default cal.
 
Or to just display the desired calendar format outright.
Code:
(wasat@mate ~)$ mcal 8 2021
    August 2021
Mo Tu We Th Fr Sa Su
                   1
 2  3  4  5  6  7  8
 9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31

(wasat@mate ~)$ cat $b/mcal
#!/bin/sh
# mcal - display one month calendar columnwise in "Mo Tu We Th Fr Sa Su" style
# 2021-8-15 n Vull

#    D e c i n t
#
Decint() { #--- $1 = string to check if it is valid decimal integer
#--- returns same string stripped of leading zeros only if validated
  local inp ck
  inp=$1
  if [ "$inp" ]; then
    #--- trim leading zeroes, to avoid octal evaluation, since we want decimal
    while [ ${#inp} -gt 1 ]&&[ "$(echo $inp | cut -c -1)" = "0" ]; do
      inp=$(echo $inp | cut -c 2-)
    done
    ck=$(echo $inp | tr -d 0-9)
    if [ "$ck" ]; then inp= ; fi #--- contains characters not in the set [0..9]
    if [ "$inp" ]; then inp=$(($inp)); fi
    echo $inp
  fi
}

#    M a i n   l o g i c

myname=${0##*/} #--- this program's name
osflavor=
check=
mo=
title=
subhdg=
pad=
x=
y=
column=
days=
day=

#--- housekeeping
if [ "$(which freebsd-version)" ]; then
  osflavor=freebsd
elif [ "$(which lsb_release)" ]; then
  osflavor=linux
else
  echo "Unsupported operating system" 1>&2
  exit 1
fi

#--- command line arguments
month=$1
year=$2
if [ "$month" ]; then
  check=$(Decint $month)
  if [ ! "$check" ]; then check=0; fi
  if [ $check -lt 1 ]||[ $check -gt 12 ]; then
    echo $myname": month '"$month"' out of range 1..12" 1>&2
    exit 1
  else
    month=$check
  fi
fi
if [ "$year" ]; then
  check=$(Decint $year)
  if [ ! "$check" ]; then check=0; fi
  if [ $check -lt 1970 ]||[ $check -gt 9999 ]; then
    echo $myname": year '"$year"' out of range 1970..9999" 1>&2
    exit 1
  else
    year=$check
  fi
fi
if [ ! "$month" ]||[ ! "$year" ]; then
  echo $myname": Month and year must be specified. For example:" 1>&2
  echo $myname" 08 2021" 1>&2
  exit 1
fi

#--- get day of week & name of month from system date utility
case "$osflavor" in
  ("freebsd")
 
    #--- day of week in 1..7 (Monday..Sunday)
    dow=$(date -j -f '%m-%d-%Y' $month"-1-"$year +'%u')

    #--- name of month in January..December
    mo=$(date -j -f '%m-%d-%Y' $month"-1-"$year +'%B')
    ;;
  ("linux")

    x=$(printf "%02d" $month)
    dow=$(date -d $year$x"01" +'%u')
    mo=$(date -d $year$x"01" +'%B')
    ;;
esac

#--- display calendar title and subheading
title=$mo" "$year
subhdg="Mo Tu We Th Fr Sa Su"
pad=$((${#subhdg}-${#title}))
pad=$(($pad/2))
x=0; while [ $x -lt $pad ]; do echo -n " "; x=$(($x+1)); done; echo $title
echo $subhdg

#--- number of days this month
#            J  F  M  A  M  J  J  A  S  O  N  D
days=$(echo "31 28 31 30 31 30 31 31 30 31 30 31" | \
  awk 'BEGIN{FS=" "}{print $'$month'}')
if [ $days -eq 28 ]; then #--- leap year?
  x=$(($year/4))
  if [ $(($x*4)) -eq $year ]; then #--- gregorian leap year?
    days=29
    x=$(($year/100)); y=$(($year/400))
    if [ $(($x*100)) -eq $year ]&&[ $(($y*400)) -ne $year ]; then days=28; fi
  fi
fi

#--- display calendar
column=1
while [ $column -lt $dow ]; do echo -n "   "; column=$(($column+1)); done
day=1
while [ $day -le $days ]; do
  while [ $column -le 7 ]&&[ $day -le $days ]; do
    printf "%2d" $day
    day=$(($day+1))
    column=$(($column+1))
    if [ $column -le 7 ]; then echo -n " "; fi
  done
  column=1
  echo
done
echo
exit 0
(wasat@mate ~)$
 
Back
Top