c# - 使用 C#/Mono 更改 Ubuntu 上的当前时区

标签 c# datetime mono

我通过在 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/

相关文章:

ruby - 在 Ruby 中将字符串解析为 DateTime 对象并添加分钟

mysql - Mono、mysql、fluent nhibernate 和 TdsInternalException

c# - 如何使用 AutoMapper 将可空属性映射到 DTO?

c# - 你能通过windows连接到Linux上的MySQL数据库吗

c# - 将用户添加到现有角色时任务被取消

c# - 如何使用大于或等于编程 iOS AutoLayout 约束而不会使布局不明确?

c# - 检查 WebException 中的证书验证错误

c# - 如何使用 C# 以编程方式将 x509 证书添加到本地机器存储

php - JavaScript 到 PHP 时间戳失败,时间增加了大约一个半月

c# - 如何格式化 DateTime?在 C# 中?