How do I get the first day of week (Sunday or Monday) for the current locale in bash?
Some exemplary locales with their first day of week: [1]
- Sunday:
en_US
,pt_BR
(Brazil) - Monday:
en_GB
,es_AR
(Argentina) - Saturday:
ar_AE
(United Arab Emirates)
If possible, I would prefer a platform-independent solution for UNIX-like systems.
I know that it's possible to get information about the current locale via the locale(1)
command, which is part of glibc.
But the documentation (locale(5)
) confuses me a little, and maybe there's a better solution than using glibc.
How do I get the first day of week (Sunday or Monday) for the current locale in bash?
Some exemplary locales with their first day of week: [1]
- Sunday:
en_US
,pt_BR
(Brazil) - Monday:
en_GB
,es_AR
(Argentina) - Saturday:
ar_AE
(United Arab Emirates)
If possible, I would prefer a platform-independent solution for UNIX-like systems.
I know that it's possible to get information about the current locale via the locale(1)
command, which is part of glibc.
But the documentation (locale(5)
) confuses me a little, and maybe there's a better solution than using glibc.
- 1 I don't know, but most sensible programs let their users decide, because lots of people don't agree with the locale value that supposedly applies to them. – xpusostomos Commented Feb 2 at 16:23
1 Answer
Reset to default 5The solution I present requires glibc >= 2.2 and GNU Coreutils (date
).
In glibc, there are two locale keywords for defining the first day of week: week-1stday
(which is the second value of the week
keyword) and first_weekday
.
The value of week-1stday
determines the meaning of first_weekday
: [2]
$(locale week-1stday) |
$(locale first_weekday) |
---|---|
19971130 , which was a Sunday |
⇒ 1=Sunday ... 7=Saturday |
19971201 , which was a Monday |
⇒ 1=Monday ... 7=Sunday |
Note that the value of week-1stday
can theoretically be any date. First I will present a solution that covers all theoretically possible cases, then a simplification.
Solution
Get the first day of week as a number: (0=Sunday, 1=Monday, ...)
num_first=$(( ( $(date -d $(locale week-1stday) +%w)
+ $(locale first_weekday) - 1
) % 7 ))
without line-breaks: $(( ( $(date -d $(locale week-1stday) +%w) +$(locale first_weekday) -1 ) % 7 ))
Get its name: (”Sunday“, ”Monday“, ...)
# localized name using the current locale (e.g. French: ”dimanche“, ”lundi“, ...)
date -d "19971130 +$num_first days" +%A
# or
date -d "$(locale week-1stday) +$(($(locale first_weekday)-1)) days" +%A
To get the name in a specific language, simply prepend LC_TIME=...
:
# English name
LC_TIME=en_US.UTF-8 date -d "19971130 +$num_first days" +%A
# or
LC_TIME=en_US.UTF-8 date -d "$(locale week-1stday) +$(($(locale first_weekday)-1)) days" +%A
However, the corresponding locale (e.g. en_US
) must be installed.
FYI: Get the the first day of week of some reference date:
reference="today"
date -d "$reference -$(date -d $reference +%w) days +$num_first days"
Simpler solution?
The notes section of locale(5)
states:
For compatibility reasons, all glibc locales should set […] [
week-1stday
] to19971130
(Sunday) […].
In fact, as far as I know, all relevant glibc locale definition files meet this criterion. I'm not sure if this is always the case, though. ”ISO 8601 conforming applications should use […] 19971201
“ [3].
If you can assume that week-1stday
is 19971130
(Sunday) on all systems you're addressing, you may simplify above commands to simple one-liners:
Get the day number: num_first=$(( $(locale first_weekday) -1 ))
Get the day's name: date -d "19971130 +$(($(locale first_weekday)-1)) days" +%A
But to be on the safe side, take the longer solution.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745299784a4621365.html
评论列表(0条)