首页 > 系统相关 >DOTNET CORE DATETIME在LINUX与WINDOWS时间不一致

DOTNET CORE DATETIME在LINUX与WINDOWS时间不一致

时间:2023-03-09 20:45:28浏览次数:46  
标签:CORE zone RTC WINDOWS DateTime Windows time 时区 DATETIME

.net core项目,部署到CentOS上的时候,发现DateTime.Now获取的时间与Windows不一致,主要是时区不一致。

static void Main(string[] args)
{
    Console.WriteLine(DateTime.Now);
}

CentOS的时区配置如下:

复制代码
[root@localhost ~]# timedatectl status
      Local time: 五 2019-04-26 13:01:02 CST
  Universal time: 五 2019-04-26 05:01:02 UTC
        RTC time: 五 2019-04-26 13:01:01
       Time zone: Asia/Shanghai (CST, +0800)
     NTP enabled: n/a
NTP synchronized: no
 RTC in local TZ: yes
      DST active: n/a

Warning: The system is configured to read the RTC time in the local time zone.
         This mode can not be fully supported. It will create various problems
         with time zone changes and daylight saving time adjustments. The RTC
         time is never updated, it relies on external facilities to maintain it.
         If at all possible, use RTC in UTC by calling
         'timedatectl set-local-rtc 0'.
复制代码 CentOS上的本地时间也是北京时间,为什么dotnet core程序获取到的时间却相对北京时间少了8个小时? 猜测问题可能是dotnet core程序的DateTime在Linux平台获取到错误的时区了。 google发现,dotnet core在Windows和Linux上使用的时区不同,在Windows上使用的是Windows time zone IDs,但是在*nix系统上使用的是IANA时区。 那么解决办法是不管什么系统,统一使用IANA时区,可以通过一个第三方库NodaTime来实现。 添加依赖包:NodaTime 将系统的当前时间换算成CST标准时间的工具方法 复制代码
public class TimeUtil
{
    public static DateTime GetCstDateTime()
    {
        Instant now = SystemClock.Instance.GetCurrentInstant();
        var shanghaiZone = DateTimeZoneProviders.Tzdb["Asia/Shanghai"];
        return now.InZone(shanghaiZone).ToDateTimeUnspecified();
    }
}
复制代码

然后写一个DateTime的扩展方法:

复制代码
public static class DateTimeExtentions
{
    public static DateTime ToCstTime(this DateTime time)
    {
        return TimeUtil.GetCstDateTime();
    }
}
复制代码

所有系统里面获取时间都通过如下方法,即可实现在Windows和Linux系统上都获取到同样的北京时间:

DateTime.Now.ToCstTime()

 转 https://www.cnblogs.com/asd14828/p/10773737.html

标签:CORE,zone,RTC,WINDOWS,DateTime,Windows,time,时区,DATETIME
From: https://www.cnblogs.com/wl-blog/p/17201328.html

相关文章

  • AspNet Core: Jwt身份认证
    目录AspNetCore:Jwt身份认证资源服务器创建项目依赖包添加APIProgram认证服务器创建项目依赖包AspNetCore:Jwt身份认证资源服务器创建项目新建一个“AspNetCoreWe......
  • .netcore 静态文件地址访问
    首先在StartUp.cs文件里面注册管道如:需要在网站里面的files的文件对外访问app.UseStaticFiles();app.UseFileServer(newFileServerOptions{......
  • ASP.NET Core Web API 接口限流
    前言ASP.NETCoreWebAPI接口限流、限制接口并发数量,我也不知道自己写的有没有问题,抛砖引玉、欢迎来喷!需求写了一个接口,参数可以传多个人员,也可以传单个人员,时间范围......
  • Jenkins使用 web Deploy 发布 .Net Core
    0.MSDeploy安装MSDeploy下载地址deploy默认监听80端口,当80端口被其他应用占用时,服务将会开启失败!所以需要使用微软自带的命令行安装工具:msiexec进行手动指定安......
  • SQL Server 自定义DateTime格式化显示内容
    SQLServer的Convert函数没有想要的格式类型,需要自定义显示格式。CASTandCONVERT(Transact-SQL)Thesefunctionsconvertanexpressionofonedatatypetoanothe......
  • asp.net core 3.1 模拟数据库,假数据步骤
         1.Model usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Threading.Tasks;namespaceWebgentle.BookStore.......
  • Windows Docker Desktop 安装 Nacos
    前言以前都是在Linux虚拟机上的Docker安装应用,这次使用Windows10系统的DockerDesktop安装Nacos,所以用挂载文件就不是很方便了,这次采用启动参数的方式对配......
  • linux运行 netcore,linux 下netcore程序开机自动启动服务
    [Unit]Description=aixiezuo守护进程[Service]WorkingDirectory=/www/wwwroot/fuwu/linux-x64ExecStart=/usr/bin/dotnet/www/wwwroot/fuwu/linux-x64/AIXieZuoNetC......
  • prometheus 监控windows
    通过ansible批量操作windows机器,部署windows_exporter-0.21.0-amd64.exe1、需要检查的点:1)、ansible登录用户名必须与系统组记录的成员名一致,要不然会一直报错,"ntlm:the......
  • cdc-file-transfer 谷歌开源的windows 到linux 同步工具
    cdc-file-transfer是基于contentdefinedchunking以及fastcdc技术,cdc-file-transfer目前提供了两种工具cdc_rsync类似rsync的同步能力,进行文件拷贝,但是性能相比rsync......