3

I need to retrieve the month of a date (dd.month.year) and transform it into the digit YYYY-MM

the source month is in french, so i try :


# date --date="$(printf "01 %s" "January 2020")" +"%Y-%m"
2020-01

# LC_TIME=fr_FR date --date="$(printf "01 %s" "Janvier 2020")" +"%Y-%m"
date: invalid date '01 Janvier 2020'

# LANG=fr_FR date --date="$(printf "01 %s" "Janvier 2020")" +"%Y-%m"
date: invalid date '01 Janvier 2020'

it works when I use the month in English.
But with the month in French even by forcing the language or local environment parameters it does not recognize the month correctly.

even with date simply it still announces in us :

# LANG=fr_FR date
Sun May 17 23:45:40 CEST 2020

# LC_TIME=fr_FR date
Sun May 17 23:45:48 CEST 2020

# date
Sun 17 May 2020 11:45:53 PM CEST

any idea ?

info :
Operating System: Debian GNU/Linux 10 (buster)
Kernel: Linux 4.19.0-8-amd64
Architecture: x86-64
Aserre
  • 4,916
  • 5
  • 33
  • 56
zignet
  • 31
  • 3

1 Answers1

2

Although the parsing done by date is flexible and amazing at times, it doesn't handle localization (here and the array is static here). You have to build an translation array that would translate Janvier to January yourself, and then pass the result to date.

It will be easy to use an associative bash array for that:

arr=([Janvier]=January etc)
echo "${arr[Janvier]}"
KamilCuk
  • 120,984
  • 8
  • 59
  • 111