C#WINDOWS和UNIX DateTime有什么不同? [英] C# WINDOWS and UNIX DateTime Are Different?

查看:124
本文介绍了C#WINDOWS和UNIX DateTime有什么不同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我正在开发一个C#项目,其中一部分是从UNIX和WIN机器读取字节数据。

某些字节数据对应于日期和时间。所以这是我需要做的任务。

1.将字节转换为长。

2.将long转换为DateTime

3。将DateTime转换为String。



问题。

转换datetime时,我需要使用两个纪元时间来能够正确地将字节数据转换为日期时间字符串。



在 UNIX系统上创建的文件的代码

Hi,

I am working on a C# project and part of which is to read a byte data from both UNIX and WIN machines.
Some byte data correspond to date and time. So here is the task that I need I do.
1. Convert byte to long.
2. Convert long to DateTime
3. Convert DateTime to String.

Problem.
When converting datetime, I need to use two epoch time to be able to correctly convert the byte data to datetime string.

Code that works for files created on UNIX System

public readonly DateTime Epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).ToLocalTime();





代码可以工作对于在 WINDOWS系统上创建的文件



Code that works for files created on WINDOWS System

public readonly DateTime Epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Local).ToLocalTime();





问题

是否有一个适用于UNIX和WINDOWS的纪元日期时间格式?



请帮助。



Question.
Is there a single epoch date time format that will work for both UNIX and WINDOWS?

Please help.

推荐答案

这不是C#问题,它实际上是Windows vs UNIX的事情。



Unix通常使用UTC硬件时间,意味着硬件时钟以UTC(GMT,Zulu,+ 0等)存储时间,并根据计算机在需要信息时所说的时区转换为可读格式。



另一方面,Windows将硬件时钟设置为本地时间,以便根据夏令时等进行调整。



问题(以及为什么这个命令在双启动系统上无法正常工作)是系统的时钟实际上是什么格式?如果你告诉命令你正在使用UTC时间,而且它没有存储为UTC时间,你就会出错。



没有生成Epoch的方法可以使用该特定命令,而无需先检查您是否正在运行Linux,Mac或Windows ...当尝试获取非POSIX度量单位时,必须要有这种方法POSIX系统。



顺便说一句,告诉Linux将时间存储为本地时间要比将Windows存储时间作为UTC时间更容易(除非你告诉它你住在格林威治)
It''s not a C# problem, it''s actually a Windows vs UNIX thing.

Unix generally uses UTC hardware time, meaning that the hardware clock stores the time in UTC (GMT, Zulu, +0, etc) and is converted to a readable format according to which time zone the computer says it''s in once the information is needed.

On the other hand, Windows sets the hardware clock to Local time adjusted for daylight savings time and the like.

The question (and why this command will not work correctly on a dual boot system) is what format is the system''s clock actually in? If you tell the command that you''re using UTC time, and it''s not stored as UTC time, you will have an error.

There is no way to generate an Epoch do with that particular command without first checking to see if you''re running Linux, Mac, or Windows... It kind of has to be expected when trying to get a POSIX unit of measure on a non-POSIX system.

As an aside, it''s much easier to tell Linux to store time as Local time than it is to trick Windows into storing time as UTC time (unless you tell it that you live in Greenwich)


这里 [< a href =http://support.microsoft.com/kb/188768target =_ blanktitle =New Window> ^ ]有关Windows中文件时间的信息。
See here[^] for information on times for files in Windows.


这篇关于C#WINDOWS和UNIX DateTime有什么不同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆