重学c#系列——datetime 和 datetimeoffset[二十一]
前言
简单介绍一下datetime和 datetimeoffset.
正文
了解一个国家的文化,就要了解一个国家的历史。
要了解datetimeoffset,那么很有必要了解一下datetime。
表示时间上的一刻,通常以日期和当天的时间表示。
继承
Object-> ValueType-> DateTime
那么可以看到DateTime 是值类型了。
实际上了解Datetime 有两个重要的参数,一个是:ticks 另一个是:kind。
ticks
Int64
一个日期和时间,以公历 0001 年 1 月 1 日 00:00:00.000 以来所经历的以 100 纳秒为间隔的间隔数来表示。
kind
DateTimeKind
枚举值之一,该值指示 ticks 是指定了本地时间、协调世界时 (UTC),还是两者皆未指定。
这里有一个值得注意的是这个ticks 是以公历 0001 年 1 月 1 日 00:00:00.000 开始计算的,且单位是100纳秒。
比如说,我这个ticks 是200,那么就是20000纳秒了。
和秒的计算公式为:1 纳秒=0.000000001 秒。
DateTimeKind 指定是本地,还是utc,或者不指定。
初始化如下:
public DateTime(long ticks, DateTimeKind kind)
{
if (ticks < 0L || ticks > 3155378975999999999L)
throw new ArgumentOutOfRangeException(nameof (ticks), SR.ArgumentOutOfRange_DateTimeBadTicks);
if (kind < DateTimeKind.Unspecified || kind > DateTimeKind.Local)
throw new ArgumentException(SR.Argument_InvalidDateTimeKind, nameof (kind));
this._dateData = (ulong) (ticks | (long) kind << 62);
}
而其他年月日其实也就是转换为tickes。
public DateTime(int year, int month, int day)
{
this._dateData = (ulong) DateTime.DateToTicks(year, month, day);
}
然后DateToTicks:
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static long DateToTicks(int year, int month, int day)
{
if (year < 1 || year > 9999 || (month < 1 || month > 12) || day < 1)
ThrowHelper.ThrowArgumentOutOfRange_BadYearMonthDay();
int[] numArray = DateTime.IsLeapYear(year) ? DateTime.s_daysToMonth366 : DateTime.s_daysToMonth365;
if (day > numArray[month] - numArray[month - 1])
ThrowHelper.ThrowArgumentOutOfRange_BadYearMonthDay();
int num = year - 1;
return (long) (num * 365 + num / 4 - num / 100 + num / 400 + numArray[month - 1] + day - 1) * 864000000000L;
}
由上面可值,其他的转换都是通过_dateData 来转换。
那么datetime 有什么问题呢? 其实可以想象一个问题,就是这个设计的时候呢。
有一个local 还有 一个 UTC,那么可能local就是UTC呢?完全可能,从这里开始概念就开始出现偏差了。
public static DateTime Now
{
get
{
DateTime utc = UtcNow;
long offset = TimeZoneInfo.GetDateTimeNowUtcOffsetFromUtc(utc, out bool isAmbiguousLocalDst).Ticks;
long tick = utc.Ticks + offset;
if (tick > DateTime.MaxTicks)
{
return new DateTime(DateTime.MaxTicks, DateTimeKind.Local);
}
if (tick < DateTime.MinTicks)
{
return new DateTime(DateTime.MinTicks, DateTimeKind.Local);
}
return new DateTime(tick, DateTimeKind.Local, isAmbiguousLocalDst);
}
}
DateTime.Now,那么就是本地时间了,然后看下面这个东西。
static void Main(string[] args)
{
DateTime d = DateTime.Now;
DateTime d2 = d.ToUniversalTime();
Console.WriteLine(d.Equals(d2));
Console.Read();
}

返回为false,理论上他们应该是同一个时间。
那么这个equal 是干什么呢?
public bool Equals(DateTime value)
{
return InternalTicks == value.InternalTicks;
}
查看一下:
internal long InternalTicks => (long)(_dateData & TicksMask);
这个时候只要_dateData 不相等,那么就不相等, 是的因为时区不同,这个自然不同。
// Returns the tick count for this DateTime. The returned value is
// the number of 100-nanosecond intervals that have elapsed since 1/1/0001
// 12:00am.
//
public long Ticks => InternalTicks;
static void Main(string[] args)
{
DateTime d = DateTime.Now;
DateTime d2 = d.ToUniversalTime();
Console.WriteLine(d2.Ticks);
Console.WriteLine(d.Ticks);
Console.WriteLine(d.Equals(d2));
Console.Read();
}

计算一下相差时间:
static void Main(string[] args)
{
DateTime d = DateTime.Now;
DateTime d2 = d.ToUniversalTime();
Console.WriteLine((d.Ticks - d2.Ticks)/60/60/10000000);
Console.WriteLine(d.Equals(d2));
Console.Read();
}

那么把本地时间调成协调世界时会发生什么呢?

这时候就是true了,所以一套代码在不同时区的机器上有不同的效果了。
当然,对于datetime 我们就需要做兼容了,就是要判断其时区做处理,这里就不演示了,
DateTimeOffset是什么呢? 和datetime 有什么关系呢。
首先来看时间相等情况:
static void Main(string[] args)
{
DateTimeOffset d = DateTimeOffset.Now;
DateTimeOffset d2 = d.ToUniversalTime();
Console.WriteLine(d.Equals(d2));
Console.Read();
}

它似乎解决了我们前面的问题,那么其实怎么做的呢?
DateTimeOffset 有两个重要的参数:
ticks
Int64
一个日期和时间,以 0001 年 1 月 1 日午夜 12:00:00 以来所经历的以 100 纳秒为间隔的间隔数来表示。
offset
TimeSpan
与协调世界时 (UTC) 之间的时间偏移量。
实例化:
// Constructs a DateTimeOffset from a tick count and offset
public DateTimeOffset(long ticks, TimeSpan offset)
{
_offsetMinutes = ValidateOffset(offset);
// Let the DateTime constructor do the range checks
DateTime dateTime = new DateTime(ticks);
_dateTime = ValidateDate(dateTime, offset);
}
这里可以看到DateTimeOffset 就没有了kind 这个概念年了,而是直接指名了这个offset概念,就是和utc到底相差多少时差。
而其相等,那么也是用UTc来计算的。
public bool Equals(DateTimeOffset other) =>
UtcDateTime.Equals(other.UtcDateTime);
这样,其实就解决了这个时区概念了。
结
至此datetime 和 datetimeoffset 概念和比较到此结束,那么遇到具体问题也可以根据其特征来分析了,该系列持续更新。
重学c#系列——datetime 和 datetimeoffset[二十一]的更多相关文章
- 重学c#系列——c#运行原理(二)
前言 c# 是怎么运行的呢?是否和java一样运行在像jvm的虚拟机上呢?其实差不多,但是更广泛. c# 运行环境不仅c#可以运行,符合.net framework 开发规范的都可以运行. c# 程序 ...
- 重学c#系列——字典(十一)
前言 重学c#系列继续更新,简单看一下字典的源码. 看源码主要是解释一下江湖中的两个传言: 字典foreach 顺序是字典添加的顺序 字典删除元素后,字典顺序将会改变 正文 那么就从实例化开始看起,这 ...
- 重学c#系列——string.empty 和 "" 还有null[二十]
前言 简单整理一下string.empty 和 "" 还有 null的区别. 正文 首先null 和 string.empty 还有 "" 是不一样的. nul ...
- 重学c#系列——对c#粗浅的认识(一)
前言 什么是c#呢? 首先你是如何读c#的呢?c sharp?或者c 井? 官方读法是:see sharp. 有没有发现开发多年,然后感觉名字不对. tip:为个人重新整理,如学习还是看官网,c# 文 ...
- 重学c#系列——list(十二)
前言 简单介绍一下list. 正文 这里以list为介绍. private static readonly T[] s_emptyArray = new T[0]; public List() { t ...
- 重学c#系列——盛派自定义异常源码分析(八)
前言 接着异常七后,因为以前看过盛派这块代码,正好重新整理一下. 正文 BaseException 首先看下BaseException 类: 继承:public class BaseException ...
- 重学Golang系列(一): 深入理解 interface和reflect
前言 interface(即接口),是Go语言中一个重要的概念和知识点,而功能强大的reflect正是基于interface.本文即是对Go语言中的interface和reflect基础概念和用法的一 ...
- 重学c#系列——c# 托管和非托管资源(三)
前言 c# 托管和非托管比较重要,因为这涉及到资源的释放. 现在只要在计算机上运行的,无论玩出什么花来,整个什么概念,逃不过输入数据修改数据输出数据(计算机本质),这里面有个数据的输入,那么我们的内存 ...
- 重学c#系列——异常续[异常注意事项](七)
前言 对上节异常的补充,也可以说是异常使用的注意事项. 正文 减少try catch的使用 前面提及到,如果一个方法没有实现该方法的效果,那么就应该抛出异常. 如果有约定那么可以按照约定,如果约定有歧 ...
随机推荐
- R语言中的正则表达式(转载:http://blog.csdn.net/duqi_yc/article/details/9817243)
转载:http://blog.csdn.net/duqi_yc/article/details/9817243 目录 Table of Contents 1 正则表达式简介 2 字符数统计和字符翻译 ...
- Unity——Js和Unity互相调用
Unity项目可以打包成WebGl,打包后的项目文件: Build中是打包后的Js代码: Index.html是web项目的入口,里面可以调整web的自适应,也可以拿去嵌套: TemplateData ...
- IO流的字节输入输出流(InputStream,OutputStream)
字节输出流与文件字节输出流 文件存储原理和记事本打开文件原理 OutputStream及FileOutputStream import java.io.FileOutputStream; import ...
- 使用systemd将iptables规则在docker启动后自动导入
编写systemd文件 $ sudo vi /etc/systemd/system/iptables-import.service # /etc/systemd/system/iptables-imp ...
- 《手把手教你》系列技巧篇(四十七)-java+ selenium自动化测试-判断元素是否显示(详解教程)
1.简介 webdriver有三种判断元素状态的方法,分别是isEnabled,isSelected 和 isDisplayed,其中isSelected在前面的内容中已经简单的介绍了,isSelec ...
- Shell 管道指令pipe
目录 管道命令pipe 选取命令 cut.grep cut 取出需要的信息 grep 取出需要行.过滤不需要的行 排序命令 sort.wc.uniq sort 排序 假设三位数,按十位数从小到大,个位 ...
- Spark中的分区方法详解
转自:https://blog.csdn.net/dmy1115143060/article/details/82620715 一.Spark数据分区方式简要 在Spark中,RDD(Resilien ...
- css系列,选择器权重计算方式
CSS选择器分基本选择器(元素选择器,类选择器,通配符选择器,ID选择器,关系选择器), 属性选择器,伪类选择器,伪元素选择器,以及一些特殊选择器,如has,not等. 在CSS中,权重决定了哪些CS ...
- C++ 数字分类
1012 数字分类 (20分) 输入格式: 每个输入包含 1 个测试用例.每个测试用例先给出一个不超过 1000 的正整数 N,随后给出 N 个不超过 1000 的待分类的正整数.数字间 ...
- oracle to_char处理日期
select to_char(sysdate,'d') from dual;--本周第几天 select to_char(sysdate,'dd') from dual;--本月第几天 select ...