DOTNET CORE DATETIME在LINUX与WINDOWS时间不一致
.net core项目,部署到CentOS上的时候,发现DateTime.Now获取的时间与Windows不一致,主要是时区不一致。
static void Main(string[] args)
{
Console.WriteLine(DateTime.Now);
}
CentOS的时区配置如下:
[root@localhost ~]# timedatectl status
Local time: 五 -- :: CST
Universal time: 五 -- :: UTC
RTC time: 五 -- ::
Time zone: Asia/Shanghai (CST, +)
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'.
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()
DOTNET CORE DATETIME在LINUX与WINDOWS时间不一致的更多相关文章
- Windows与Linux/Mac系统时间不一致的解决方法
		
Windows与Linux/Mac系统时间不一致的解决方法 分类: linux2012-02-12 14:25 1691人阅读 评论(1) 收藏 举报 windowsubuntusystemlinux ...
 - .net Core 中DateTime在Linux Docker中与Windows时间不一致
		
最近写了一个.net core项目,部署到CentOS并在docker上运行的时候,发现DateTime.Now获取的时间与Windows不一致(定时执行的任务,晚了8个小时),在Windows中可以 ...
 - dotnet core 3.0 linux 部署小贴士
		
dotnet core 3.0 目前还是测试版,在linux下安装 sdk 需要有一些注意事项 1.下载url https://dotnet.microsoft.com/download/thank- ...
 - linux时间与Windows时间不一致的解决
		
一.首先要弄清几个概念:1. “系统时间”与“硬件时间” 系统时间: 一般说来就是我们执行 date命令看到的时间,linux系统下所有的时间调用(除了直接访问硬件时间的命令)都是使用的这个时间. ...
 - django 获取系统当前时间 和linux 系统当前时间不一致 问题处理。
		
问题场景: 在django admin models 实体对象添加一个属性最后修改时间,用户在添加.修改是系统自动修改操作时间. UpdateTime自动获取系统时间.并且自动修改. 代码设置如下. ...
 - Django 获取时间 和Linux 本地 系统时间 不一致
		
问题描述 Django 中获取的本地时间 ,和系统时间不一致 错误原因 Django在配置文件settings.py 中 默认配置 UTC世界标准时间,而北京时间是东八区,比UTC时间早8个小时. T ...
 - 解决Elementary OS和Windows时间不一致
		
解决方案一:把Windows从硬件时间修改成修改成UTC时间,管理员身份运行 Reg add HKLM\SYSTEM\CurrentControlSet\Control\TimeZoneInforma ...
 - ubuntu上部署windows开发的dotnet core程序
		
目标:完成windows上开发的dotnet core程序部署至linux服务器上(Ubuntu 14.04) windows上开发dotnet core很简单,安装好VS2017,建立相关类型的项目 ...
 - dotnet core多平台开发体验(mac os x 、windows、linux)
		
前言 随着net core rc2的发布,园子里面关于net core的入门文章也也多了起来,但是大多数都是在一个平台上面来写几个简单的例子,或者是在解释代码本身,并没有体现说在一个平台上面创建一个项 ...
 
随机推荐
- Python爬虫突破封禁的6种常见方法
			
转 Python爬虫突破封禁的6种常见方法 2016年08月17日 22:36:59 阅读数:37936 在互联网上进行自动数据采集(抓取)这件事和互联网存在的时间差不多一样长.今天大众好像更倾向于用 ...
 - github push出错(1)You can't push to git://     Use https://
			
fatal: remote error: You can't push to git://github.com/niexiaobo/remote.git Use https://github.com/ ...
 - 【b303】加分二叉树
			
[题目链接]:https://vijos.org/p/1100 [题意] [题解] 因为已经确定了最后中序遍历的结果为1..n; 所以对于每一个区间[l..r] 你需要确定这个区间里面哪一个是这个子树 ...
 - 阿里云centos7.2自己安装mysql5.7远程不能访问解决方案
			
版权声明:转载也行 https://blog.csdn.net/u010955892/article/details/72774920 最近,无意中看到阿里云服务器降价,所以一时手痒,买了一年的服务器 ...
 - springboot内置tomcat验证授权回调页面域名
			
springboot内置tomcat验证公众号授权回调页面域名 解决方法: 网上下载一个tomcat,在server.xml文件中修改端口为springboot内置tomcat的端口号,复制验证文件到 ...
 - linux的开机启动过程:
			
简单视图 按下电源开关 开机自检(BIOS)弹笔记本logo的时候,检查cpu 硬盘 这些硬件问题 MBR引导 一般是通过硬盘启动系统 GRUB的菜单 黑底白字有个倒数计时 可以选择内核 yum命令可 ...
 - 《80x86汇编语言程序设计》保护模式第一个例题
			
<80x86汇编语言程序设计>保护模式第一个例题的一些个人理解和注视 ; 16位偏移的段间直接转移指令的宏定义 jump macro selector, offsetv db 0eah ...
 - XSS攻击之基础篇:HTML标签与字符串的渲染
			
<body> XSS攻击之基础篇:HTML标签与字符串的渲染 测试 <div class="a1"> </div> <div class= ...
 - Qt实用技巧:界面切换使用Dialog全屏切换
			
需求 在做应用程序的过程中,需要使用界面切换,界面切换到下一个界面使用new一个界面并显示,如系统设置,相关信息展示等等. (注意:本技巧适用的条件是,主界面不需要相关的信号与槽做消息循环,因为主界面 ...
 - Shell Step by Step (3) —— Stdin & if
			
4.输入输出 #! /bin/bash # Read users input and then get his name read -p "Please input your first n ...