Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# DateTime
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();
 
Code that works for files created on WINDOWS System
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 24-Jan-13 15:16pm
Edited 24-Jan-13 19:25pm
v2
Comments
Sergey Alexandrovich Kryukov at 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
Rate this: bad
good
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)
  Permalink  
v2
Comments
ure fowei at 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
good
Please Sign up or sign in to vote.

Solution 3

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

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 458
1 Maciej Los 375
2 OriginalGriff 338
3 CHill60 205
4 Yogesh Kumar Tyagi 160
0 OriginalGriff 6,367
1 Sergey Alexandrovich Kryukov 5,296
2 Maciej Los 3,384
3 Peter Leow 3,259
4 DamithSL 2,490


Advertise | Privacy | Mobile
Web03 | 2.8.140721.1 | Last Updated 27 Jan 2013
Copyright © CodeProject, 1999-2014
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