10000 "Timezone mismatch in .NET applications running inside Docker containers · Issue #6444 · dotnet/dotnet-docker · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

"Timezone mismatch in .NET applications running inside Docker containers #6444

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
newpost opened this issue May 13, 2025 · 7 comments
Closed

Comments

@newpost
Copy link
newpost commented May 13, 2025

Describe the bug

I ran a .NET program and a Go program inside a Docker container, both designed to display the current timezone. I also mounted the host's /etc/localtime into the container. Here’s the output:

package main

import (
	"fmt"
	"time"
)

func main() {
	// 获取当前时间
	now := time.Now()
	
	// 获取当前时区信息
	zoneName, zoneOffset := now.Zone()
	
	// 计算时区偏移量的小时和分钟表示
	hours := zoneOffset / 3600
	minutes := (zoneOffset % 3600) / 60
	
	// 输出时区信息
	fmt.Printf("当前时间: %v\n", now)
	fmt.Printf("时区名称: %s\n", zoneName)
	fmt.Printf("时区偏移: UTC%+03d:%02d\n", hours, minutes)
	fmt.Printf("完整时区信息: %s\n", now.Location())
}
$ ./go-test 
当前时间: 2025-05-13 09:16:21.541617727 +0800 CST m=+0.000060379
时区名称: CST
时区偏移: UTC+08:00
完整时区信息: Local
Console.WriteLine(TimeZoneInfo.Local.DisplayName);
Console.WriteLine(DateTimeOffset.Now);
$ dotnet run 
(UTC) Coordinated Universal Time
5/13/2025 1:16:58 AM +00:00

Which .NET image(s) are you using?

No response

Steps to reproduce

No response

Other information

No response

Output of docker version

Output of docker info

@newpost
Copy link
Author
newpost commented May 13, 2025
docker run --rm -v /etc/localtime:/etc/localtime:ro -v $(pwd):/app -w /app mcr.microsoft.com/dotnet/sdk:9.0 dotnet run
(UTC) Coordinated Universal Time
05/13/2025 01:45:58 +00:00   <<<--
 docker run --rm -v /etc/localtime:/etc/localtime:ro -v $(pwd):/app -w /app mcr.microsoft.com/dotnet/sdk:9.0 date
Tue May 13 09:46:17 CST 2025  <<<--

@richlander
Copy link
Member

This is what I see.

rich@vancouver:~/test$ cat Program.cs 
Console.WriteLine(TimeZoneInfo.Local.DisplayName);
Console.WriteLine(DateTimeOffset.Now);
rich@vancouver:~/test$ cat /etc/timezone 
America/Los_Angeles
rich@vancouver:~/test$ docker run --rm -v $(pwd):/app -w /app mcr.microsoft.com/dotnet/sdk:9.0-noble dotnet run
(UTC) Coordinated Universal Time
05/13/2025 01:55:17 +00:00
rich@vancouver:~/test$ docker run --rm -v $(pwd):/app -v /etc/timezone:/etc/timezone -w /app mcr.microsoft.com/dotnet/sdk:9.0-noble dotnet run
(UTC) Coordinated Universal Time
05/13/2025 01:55:33 +00:00
rich@vancouver:~/test$ docker run --rm -v $(pwd):/app -e TZ=$(cat /etc/timezone) -w /app mcr.microsoft.com/dotnet/sdk:9.0-noble dotnet run
(UTC-08:00) Pacific Time (Los Angeles)
05/12/2025 18:55:52 -07:00
rich@vancouver:~/test$ docker run --rm -v $(pwd):/app -e TZ="America/Los_Angeles" -w /app mcr.microsoft.com/dotnet/sdk:9.0-noble dotnet run
(UTC-08:00) Pacific Time (Los Angeles)
05/12/2025 18:55:43 -07:00

@newpost
Copy link
Author
newpost commented May 13, 2025

The environment variable approach works as expected. I’ve verified this in my tests. Appreciate it! @richlander

@richlander
Copy link
Member

@newpost
Copy link
Author
newpost commented May 13, 2025

While setting the TZ environment variable resolved the issue, I'm still curious about the root cause of the problem when this variable is not used. Would you kindly elaborate on why the timezone behaves incorrectly without explicit configuration? I appreciate your time in advance. @richlander

@richlander
Copy link
Member

@tarekgh

@tarekgh
Copy link
Member
tarekgh commented May 13, 2025

Please look at dotnet/runtime#62545 (comment). To fix it, apply the suggestion dotnet/runtime#62545 (comment).

@newpost newpost closed this as completed May 14, 2025
@github-project-automation github-project-automation bot moved this from Backlog to Done in .NET Docker May 14, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Done
Development

No branches or pull requests

3 participants
0