Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change default timezone in ASP.NET Core 2.2 on Docker for 24h time format

I want to use German date formatting as default for my ASP.NET Core 2.2 application. On my Win10 machine with German language/layout it works, so I assumed that the timezone has to be set in my Dockerfile. According to the Alpine wiki, I did this:

FROM mcr.microsoft.com/dotnet/core/sdk:2.2-alpine3.9 AS build-env
ENV TZ=Europe/Berlin
WORKDIR /app

COPY *.csproj ./
RUN dotnet restore

RUN apk add tzdata \
        && cp /usr/share/zoneinfo/${TZ} /etc/localtime \
        && echo "${TZ}" > /etc/timezone

COPY . ./
RUN dotnet publish -c Production -o dist
ENTRYPOINT ["dotnet", "./dist/MyApp.dll"]

According to the date command, this worked:

/app # date
Sun Apr  7 13:50:42 CEST 2019
/app # date -u
Sun Apr  7 11:50:40 UTC 2019

But having a DateTime object in my Model from the Database

<td>@article.PublishedTime.ToString("g")</td>

I get 4/7/19 12:16 AM where my German Win10 machine shows 07.04.2019 00:16. Why isn't this working? Since ASP.NET Core uses the system timezone, it should now use the 24h time format as set in linux.

like image 626
Lion Avatar asked Apr 07 '19 11:04

Lion


1 Answers

Found out that we need to set the language, since this is used for the formatting:

ENV TZ=Europe/Berlin
ENV LANG de_DE.UTF-8
ENV LANGUAGE ${LANG}
ENV LC_ALL ${LANG}

This works and produces German 24h DateTime formattings.

I agree that in most cases this should be controlled by the application, which use different formattings on e.g. user specified settings. Since this is a simple application only for me, it's the easiest way to set the server settings to my localisation. Passing a culture info from any kind of settings would produce overhead without advantage.

But as I said this is only suiteable for my case. In a productive environment you may want to specify the colture and allow different cultures for international users.

like image 114
Lion Avatar answered Sep 20 '22 12:09

Lion