[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: how to compare 2 dates - NOW WITH TIME
- From: KHMan <keinhong@...>
- Date: Fri, 08 May 2009 00:26:00 +0800
Diego - Red Baires wrote:
[snip]
premise:
i need to represent a range or dates, and then a range of time.
i have a list of records that indicates that an event must be
executed only if:
a) today is between the fields that indicates a range of dates, and then
b) os system.clock is between the fields that indicates a range of hours.
(is today between date-A and date-B, if so, is now between 3pm and 4pm?)
so i am doing this:
-----------
between_date_from = os.time({ year="2008", month="01", day="01",
hour="00", min="00" })
between_date_to = os.time({ year="2009", month="05", day="06",
hour="00", min="00" })
On many platforms, this os.time() function which calls the
platform's equivalent will happily accept 'illegal' values like
month=13 or day=40. You'd get sorta a 'best guess' value. (Note:
You supply numbers; strings like "2009" are turned into numbers
like 2009. You might also not need to supply zero fields like
min=0, because the function does a best guess.)
Thus validating a date is non-trivial. Also, depending on user
input, dates are of various precisions. More validation, if your
application expects certain formats or precisions. Then to be
precisely precise, you'd also need to accept sec=60 for some
years, which of course, I think most OS don't deal with a leap
second in the OS layer. Then there is nntp synchronization. And so on.
[snip snip]
now the problem is how to analize a range of hours, not taking in mind
the date.
any idea ?
"os.time()" does not allow (obviously) an assignation like this:
{ year="0000", month="00", day="00", hour="17", min="30" }
os.time() on most platforms (POSIX-ish) is the number of seconds
after the Unix epoch. You can trivially compare and subtract the
times. The difference is in seconds, which you can then convert
into anything you like.
If you are in total control of an application, and need a standard
textual format for date/time, then use ISO 8601 [1](or a subset),
and make your users use it. Make your program validate ISO 8601
time formats. Once it validates correctly, common ISO 8601 formats
like YYYY-MM-DD can also be compared trivially in string form.
Using the ISO form also avoids ambiguity common with date/time
formats of different countries. More info in [1], link below.
[1] http://en.wikipedia.org/wiki/ISO_8601
--
Cheers,
Kein-Hong Man (esq.)
Kuala Lumpur, Malaysia