我通过在 C#/Mono 应用程序中将文件 /etc/locatime
替换为 /usr/share/zoneinfo
中的一个来修改时区,它几乎有效。
这是我的代码:
Console.WriteLine("a) Current Time zone: {0}", TimeZoneInfo.Local.StandardName);
Console.WriteLine("a) Now: {0}", DateTime.Now);
// Changing /etc/locatime from CET to PST ...
TimeZoneInfo.ClearCachedData();
Console.WriteLine("b) Current Time zone: {0}", TimeZoneInfo.Local.StandardName);
Console.WriteLine("b) Now: {0}", DateTime.Now);
这是输出:
a) Current Time zone: CET
a) Now: 10/20/2012 2:00:26 PM
b) Current Time zone: PST
b) Now: 10/20/2012 2:00:26 PM
如您所见,时区是正确的,但应返回本地时间的 DateTime.Now
不起作用,而是返回相同值的两倍。
这是我使用的单声道版本:
user@ubuntu:~$ mono -V
Mono JIT compiler version 2.10.8.1 (Debian 2.10.8.1-1ubuntu2.2)
TLS: __thread
SIGSEGV: altstack
Notifications: epoll
Architecture: x86
Disabled: none
Misc: softdebug
LLVM: supported, not enabled.
GC: Included Boehm (with typed GC and Parallel Mark)
有人知道这个问题吗?
最佳答案
正如 Celada 指出的那样 - 大多数 C 库只读取一次。 DateTime.Now
在 Linux 上调用 gettimeofday()
。
这在 Windows 上也是一样的,您甚至不需要任何 .NET 应用程序,只需打开 Windows Power Shell,键入 date
,然后在控制面板中更改时区并键入 date
再次出现在同一个 Power Shell 窗口中。它不会接收时区更改。只有新启动的进程才能获得新时区。
关于c# - 使用 C#/Mono 更改 Ubuntu 上的当前时区,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13142553/