Click here to Skip to main content
15,904,288 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Deer all,

I'm having problem with DateTime structure. Namely, it does not support year 10000 and later (or I'm wrong in which case I would appreciate correction) and I can't find method which checks whether particular DateTime value is valid or not. Furthermore, GregorianCalendar recognizes current era only.
There are more features that I need and can not get, but I think you get the point.

Is there some other classes and/or structures which represent the same thing (dates, time, time zones, and so on) but implement more features, or I need to implement them myself?

Thanks in advance.
Posted

Year 10000?? Is it a joke ah ah...

OK seriously, DateTime structure can't give impossible dates. There is no method to check them because you can't instanciate invalid DateTime objects.

But for the other points, you will probably need to do them by yourself or search the web...

If you want to deal with far future or past, you probably don't need to use DateTime structure.
 
Share this answer
 
v3
Comments
Espen Harlinn 19-Feb-11 12:48pm    
Good advice, a 5
Valid? If you try to set a DateTime to an invalid date, it will throw an exception. If you need to add functionality, you can use extension methods. Nothing you do, though, will allow you to specify a date that exceeds the max possible DateTime. There are a number of articles and tips here on CP about extension methods.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 19-Feb-11 21:34pm    
Make sense, my 5, but there are better thoughts.
Please see some funniest time facts, in my comment to the Answer by Espen!
--SA
dsnlkc 23-Feb-11 5:34am    
Yes, you are right. I know I can't create DateTime object which holds 44th of June or January 1st 2010. 12:123:-2, but take a look at this code:

DaylightTime dlt = TimeZone.CurrentTimeZone.GetDaylightChanges(2010);
DateTime shortDate = dlt.Start;
DateTime invalidDateTime = new DateTime(shortDate.Year, shortDate.Month, shortDate.Day,
shortDate.Hour, shortDate.Minute +1, 0,
DateTimeKind.Local);
Console.WriteLine("\nDaylight saving time for year {0}:", 2010);
Console.WriteLine("{0:yyyy-MM-dd HH:mm} to " + "{1:yyyy-MM-dd HH:mm}, delta: {2}",
dlt.Start, dlt.End, dlt.Delta);
Console.WriteLine("\nDate and time held by invalidDateTime object: {0:yyyy-MM-dd HH:mm}",
invalidDateTime);

In my time zone it will produce this output:

Daylight saving time for year 2010:
2010-03-28 02:00 to 2010-10-31 03:00, delta: 01:00:00

Date and time held by invalidDateTime object: 2010-03-28 02:01

shortDate holds date and time which is shown first in the output. That is the date when clocks moved from 02:00 to 03:00 immediately, so there was no time 02:01 on that date, yet DateTime constructor allowed me to create that last date and time held by invalidDateTime object: 2010-03-28 02:01. So if DateTimeKind of some DateTime object is set to Local, that DateTime object might be invalid in some time zones.

If I'm wrong about anything I said, please correct me.
Also, thanks for the advice to use extension methods. As I could see on MSDN they are great when some type can't be derived, whether it is a structure or sealed class. I think they alone will not solve my problem, though.
Sergey Alexandrovich Kryukov 23-Feb-11 5:44am    
Pretty complex, but is it related to too long time in future like year 10000?
--SA
dsnlkc 23-Feb-11 14:02pm    
I'm sorry, I don't understand your question. If you are asking whether my previous reply is related to disability of DateTime structure to represent years 10000 and later, then no, it has nothing to do with that. But that disability, as I wrote in my original question, is just one thing of those bother me. Among others are the fact that GregorianCalendar recognizes only current era and possibility of creating DateTime structure which in some timezones might be invalid, as I showed above (or I'm somehow wrong).
Sergey Alexandrovich Kryukov 23-Feb-11 15:15pm    
Thank you, I got it.

I feel the conclusion is this:
You really need to develop your own data structure to represent time, to cover much extended range of time period and satisfy all your requirements. You should be able to convert DateTime to your structure but not always to convert your structure to DataTime
(or not at all).

--SA
The human race doesn't have the "experience" to design such a calendar. In 10 000 years our current way of measuring time is probably outdated.
http://en.wikipedia.org/wiki/Earth's_rotation[^].

I'd use a decimal or a double if I wanted to deal with anything on that scale.

You may find this entertaining:
The Curious History of the Gregorian Calendar[^]

Here is some more: History of the Calendar[^]

Have fun :)

Anyway - dates are obviously problematic, ever heard of the 30th of February[^]? I don't think you'll come across that one in any of the widely used libraries dealing with dates :)

Regards
Espen Harlinn
 
Share this answer
 
v2
Comments
Olivier Levrey 19-Feb-11 12:57pm    
My 5. I didn't expect to learn about history on CodeProject but this is very interesting :)
Espen Harlinn 19-Feb-11 17:47pm    
There is also this: http://en.wikipedia.org/wiki/Gregorian_calendar
Note that Sweeden had even more problems than other countries when it came to keep up with the times :)
Nish Nishant 19-Feb-11 19:36pm    
Voted 5, useful and interesting response!
Espen Harlinn 20-Feb-11 5:15am    
Thank you Nishant :)
Sergey Alexandrovich Kryukov 19-Feb-11 21:32pm    
That is correct and interesting response! My 5.

I would add that the calendar time issues haunt whole humankind again and again. I know little issues that attracted so much of people's stupidity as much as that. Double hit was 2000, so called "Problem 2000" which was never a real technical problem despite the claims and the mere fact that most people celebrated the beginning of XXI century in wrong year! How about pointless "summer saving time"?

In our PhysTech University department's Theater of Miniatures, people coined this:

"Time is money. After shifting to summer saving time, winter money goes out of circulation!"

People at the concert LOLed like crazy. We already knew though that our jokes tends be dangerous. Another coincidence came up quickly: in a short period of time the government decreed value denomination!

Espen, what's that Sweeden problem? I'm really intrigued! Did you know that before 1918 Russia still used Julian, not Gregorian calendar? So, in text books the historical events around the Revolution were given in two variants. The common Soviet joke was: celebrating "Great _October_ Socialist Revolution" in November! So, what's up with Sweeden time?

--SA
Are you working on some kind of futuristic game? If so I understand why you may want to show an year greater than 9999. The Windows SYSTEMTIME structure supports up till year 30,827. I don't know if that limit will satisfy your requirements, but it's certainly an improvement over DateTime.MaxValue. You should be able to write a wrapper around the native struct and then use that.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 19-Feb-11 21:33pm    
Best answer so far, 5.
Please see some funniest time facts, in my comment to the Answer by Espen!
--SA
Espen Harlinn 20-Feb-11 5:17am    
Good proposal, my 5
dsnlkc 20-Feb-11 12:46pm    
Best answer so far, indeed.

Well, it is not a game, but it might be futuristic. My application should be able to keep information of various dates (and times), both in future and in past. And I will not prevent its users from setting some date to, for example October 24th 23456. or January 1st 100. B.C. no matter how unlikely it is that someone will actually use those dates.

So, I will probably write class which has DateTime struct as its field plus 2 more fields to hold those extra years, and information about the era, or something like that.

Thanks again.
Nish Nishant 20-Feb-11 17:55pm    
You're welcome.

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