[Home] [Puzzles & Projects] [Delphi Techniques] [Math topics] [Library] [Utilities]
|
|
Problem DescriptionExplore date calculation algorithms by writing a program to emulate the Date Calculator included with Window 7. Background & TechniquesThere's certainly no great need for another date calculator, but the problem of performing date arithmetic based on Year, Month, and Day differences seemed like an interesting and solvable one. Given two dates, the calculator will return the difference in Y(ears), M(onths), W(eeks), and D(ays). I'll refer to this as a YMD calculation from now on because weeks are easily derived from a days value by dividing by 7 with any remainder being the new days value. YMD arithmetic is is not commutative, that is applying the operation in a different order will frequently produce a different result because of differing month lengths. For example, moving from Sept 30 to Nov 1: The difference is 1M, 2D because +1M gets us to Oct 30 and +2D to Nov 1. But in reverse order Sept 30 + 2D = Oct 2 and Oct 2 + 1M = Nov 2. Both the W7 and DFF calculators calculate and apply differences in YMD order. In testing, I found some cases where my answer does not agree with W7's answer which probably means that mine are wrong but I haven't figured out why. I'll list a couple here and I'm sure viewers will let me know who is correct:
Non-programmers are welcome to read on, but may want to jump to bottom of this page to download the executable program now. Programmer's Notes:I just counted, and my notebook has 21 pages of manual date calculation scribblings while writing this program giving you the idea that it was not a trivial project. For the date differences, I finally broke the calculation into 8 cases. based whether each of the Date1 Y, M, D values were less than or greater than the Date2 Y, M, D values. The equal YMD values are assigned to one of the groups based on whether move is forward or backward in time. Each of the cases was then debugged independently. It provided a good chance to become acquainted with some of the Delphi DateUtils unit routines such as IncYear, IncMonth, IncDay to apply year, month, or day increments forward or backward as well as EndOfTheMonth and DaysInAMonth functions. The user interface is similar to the W7 calculator interface with the exception that I kept a "Test" button used for debugging. The "Debug" dialog was added to calculate differences for random date pairs and the reapply the differences to the first date to recreate the second date. Errors between the input and the calculated Date2 greater than a specified amount are reported. There are other options in the dialog to control whether to pause on each error and whether to make the sequence repeatable or use new random set for each run. The Test button runs up to 1000 date pair calculations for each run. Running/Exploring the Program
Suggestions for Further Explorations
|
[Feedback] [Newsletters (subscribe/view)] [About me]Copyright © 2000-2018, Gary Darby All rights reserved. |