Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
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
C#
public readonly DateTime Epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).ToLocalTime();


Code that works for files created on WINDOWS System
C#
public readonly DateTime Epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Local).ToLocalTime();


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

Please help.
Posted
Updated 24-Jan-13 19:25pm
v2
Comments
Sergey Alexandrovich Kryukov 24-Jan-13 23:07pm    
What do you mean "work for"?

File formats are certainly identical, because this is CLR format. But are you saying that if you call identical constructors on Mono for Unix and .NET for Windows, the results will be different?!

What if you also try on Mono for Windows?

—SA

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)
 
Share this answer
 
v2
Comments
ure fowei 25-Jan-13 0:55am    
Thank you for the response. Very well said. So there is no way to create a generic Epoch time format usable for files encoded from UNIX and WINDOWS.

My Proposed Solution:
I guess I will need a separate algorithm to determine whether the encoded datetime matches that of the time reflected on the filename(through a timestamp YYMMDD-HHmmSS)

Do you think this would be passable?
See here[^] for information on times for files in Windows.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900