Click here to Skip to main content
13,139,254 members (76,162 online)
Rate this:
Please Sign up or sign in to vote.
See more:

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.

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();

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

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

Please help.
Posted 24-Jan-13 15:16pm
Updated 24-Jan-13 19:25pm
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?

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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)
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?
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

See here[^] for information on times for files in Windows.

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web02 | 2.8.170915.1 | Last Updated 27 Jan 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100