Click here to Skip to main content
15,867,568 members
Home / Discussions / C#
   

C#

 
GeneralRe: Sense of making my own Timestamp class? Pin
Marco Bertschi4-Feb-14 4:03
protectorMarco Bertschi4-Feb-14 4:03 
AnswerRe: Sense of making my own Timestamp class? Pin
jschell4-Feb-14 13:20
jschell4-Feb-14 13:20 
GeneralRe: Sense of making my own Timestamp class? Pin
Marco Bertschi4-Feb-14 20:07
protectorMarco Bertschi4-Feb-14 20:07 
AnswerRe: Sense of making my own Timestamp class? Pin
TnTinMn6-Feb-14 12:37
TnTinMn6-Feb-14 12:37 
GeneralRe: Sense of making my own Timestamp class? Pin
Marco Bertschi6-Feb-14 21:12
protectorMarco Bertschi6-Feb-14 21:12 
GeneralRe: Sense of making my own Timestamp class? Pin
TnTinMn9-Feb-14 10:29
TnTinMn9-Feb-14 10:29 
GeneralRe: Sense of making my own Timestamp class? Pin
Marco Bertschi9-Feb-14 19:56
protectorMarco Bertschi9-Feb-14 19:56 
GeneralRe: Sense of making my own Timestamp class? Pin
TnTinMn10-Feb-14 13:12
TnTinMn10-Feb-14 13:12 
Your showing of the "fff" format characters prompted me to see if you can expand them out to six characters and use the TryParseExact overload that excepts an array of formats. It does. A little more playing around with the formats and it all becomes so simple after all.

This is real simple now especially after I noticed the other overload on TryParseExact that takes an array of formats to test. Smile | :)
string[] formats = { "yyyy-MM-dd\\THH:mm:ss\\Z", 
                     "yyyy-MM-dd\\THH:mm:ss.f\\Z", 
                     "yyyy-MM-dd\\THH:mm:ss.ff\\Z", 
                     "yyyy-MM-dd\\THH:mm:ss.fff\\Z", 
                     "yyyy-MM-dd\\THH:mm:ss.ffff\\Z", 
                     "yyyy-MM-dd\\THH:mm:ss.fffff\\Z", 
                     "yyyy-MM-dd\\THH:mm:ss.ffffff\\Z", 
                     "yyyy-MM-dd\\THH:mm:sszzz", 
                     "yyyy-MM-dd\\THH:mm:ss.fzzz", 
                     "yyyy-MM-dd\\THH:mm:ss.ffzzz", 
                     "yyyy-MM-dd\\THH:mm:ss.fffzzz", 
                     "yyyy-MM-dd\\THH:mm:ss.ffffzzz", 
                     "yyyy-MM-dd\\THH:mm:ss.fffffzzz", 
                     "yyyy-MM-dd\\THH:mm:ss.ffffffzzz" 
                   };

DateTime dt1 = default(DateTime);
bool res1 = System.DateTime.TryParseExact("1985-04-12T20:20:59.100000-00:15", 
                                           formats, System.Globalization.CultureInfo.InvariantCulture, 
                                           System.Globalization.DateTimeStyles.AdjustToUniversal, 
                                           out dt1);

DateTime dt2 = default(DateTime);
bool res2 = System.DateTime.TryParseExact("1985-04-12T20:20:59.100001-00:15", 
                                          formats, System.Globalization.CultureInfo.InvariantCulture, 
                                          System.Globalization.DateTimeStyles.AdjustToUniversal,
                                          out dt2);

Console.WriteLine(dt1.Kind.ToString()); // yep UTC
Console.WriteLine(dt1.ToString("u"));
Console.WriteLine((dt1 < dt2).ToString()); // 1 µsec less

This code works, but has the drawback of loosing the original offsets as everything gets adjusted to UTC.

You can maintain the original offset date by using the DateTimeOffset [^]structure (this structure has all the functionality that I so foolishly recreated by custom parsing). D'Oh! | :doh: Oh, well it was fun to see if I could do it. Blush | :O

DateTimeOffset dto1 = new DateTimeOffset();
bool res1b = System.DateTimeOffset.TryParseExact("1985-04-12T20:20:59.100000-00:15", 
                                                 formats, 
                                                 System.Globalization.CultureInfo.InvariantCulture, 
                                                 System.Globalization.DateTimeStyles.AssumeLocal, 
                                                 out dto1);
Notice the change to AssumeLocal. The DateTime and Offset properties can then be used to recreate the original string.
QuestionSSIS for Fixed Length line Pin
nitin_ion3-Feb-14 20:26
nitin_ion3-Feb-14 20:26 
AnswerRe: SSIS for Fixed Length line Pin
Bernhard Hiller3-Feb-14 20:52
Bernhard Hiller3-Feb-14 20:52 
GeneralRe: SSIS for Fixed Length line Pin
Marco Bertschi3-Feb-14 20:56
protectorMarco Bertschi3-Feb-14 20:56 
GeneralRe: SSIS for Fixed Length line Pin
nitin_ion3-Feb-14 21:46
nitin_ion3-Feb-14 21:46 
QuestionCreating barcode of type ean-13 in c# windows forms with sql server 2008 using StrokeScribe control of version 4.3.2 Pin
Member 102487683-Feb-14 19:00
Member 102487683-Feb-14 19:00 
SuggestionRe: Creating barcode of type ean-13 in c# windows forms with sql server 2008 using StrokeScribe control of version 4.3.2 Pin
Richard MacCutchan3-Feb-14 21:53
mveRichard MacCutchan3-Feb-14 21:53 
QuestionNorth wind Database Pin
Sandhya Bandar3-Feb-14 9:46
Sandhya Bandar3-Feb-14 9:46 
AnswerRe: North wind Database Pin
Kornfeld Eliyahu Peter3-Feb-14 9:54
professionalKornfeld Eliyahu Peter3-Feb-14 9:54 
AnswerRe: North wind Database Pin
Dave Kreskowiak3-Feb-14 12:49
mveDave Kreskowiak3-Feb-14 12:49 
JokeRe: North wind Database Pin
Kornfeld Eliyahu Peter3-Feb-14 20:19
professionalKornfeld Eliyahu Peter3-Feb-14 20:19 
AnswerRe: North wind Database Pin
Marco Bertschi3-Feb-14 21:04
protectorMarco Bertschi3-Feb-14 21:04 
QuestionWorking with Bitmaps Pin
BBatts3-Feb-14 7:45
BBatts3-Feb-14 7:45 
AnswerRe: Working with Bitmaps Pin
TnTinMn4-Feb-14 6:56
TnTinMn4-Feb-14 6:56 
QuestionProject Question Pin
reaper21913-Feb-14 5:15
reaper21913-Feb-14 5:15 
AnswerRe: Project Question Pin
Alan Balkany3-Feb-14 5:20
Alan Balkany3-Feb-14 5:20 
AnswerRe: Project Question Pin
Pete O'Hanlon3-Feb-14 5:48
subeditorPete O'Hanlon3-Feb-14 5:48 
AnswerRe: Project Question Pin
OriginalGriff3-Feb-14 6:11
mveOriginalGriff3-Feb-14 6:11 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.