help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: how do you compute date difference in emacs?


From: Xah Lee
Subject: Re: how do you compute date difference in emacs?
Date: Tue, 04 May 2010 15:44:54 -0000
User-agent: G2/1.0

On Apr 16, 1:50 pm, José A. Romero L. <escherdra...@gmail.com> wrote:
> On 16 Kwi, 20:23, Xah Lee <xah...@gmail.com> wrote:> computing dates.
>
> > Recently i needed to compute dates in emacs.
>
> > For example, i need to find out:
> > 2010-04-16 - 215 days.
>
> (...)
>
> Something like this should do the job:
>
> (format-time-string
>  "%Y-%m-%d"
>  (time-subtract (date-to-time "2010-04-16 00:00") (days-to-time 215)))
>
> Notice that you *must* specify at least hour and minute (e.g. 00:00)
> in order
> for the date string to be parseable.
>
> Have a look at time-date.el for some useful time-related functions.

Thanks for both replies. After spending about 10 min on each suggested
solution, still can't solve it, i end up google for a date calculator
and and in 10 sec found
http://www.timeanddate.com/date/dateadd.html
and finished doing what i needed for 5 calculations of dates.

here's some complaints about the emacs solutions in case some emacs
dev is interested.

The calender mode... so i type Alt+x calendar. Been using it for 10
years, but mostly only just to look at today's date and day of the
week.

After Edward's suggestion, i thought yeah why didn't i thought of it,
cause i knew the emacs calender does all sort of esoteric calender
systems and thus must contain ways to do simple day substraction.  But
while in calender, am not sure how to use it to computer yyyy-mm-dd
minus days. So i typed C-h m to read the online doc. It has close to
200 commands. I searched for string “diff” or “substr” but no
result. Then i looked up its info doc by typing ? while in the
mode. Quickly got to the node on Counting Days. Learned that i can
find the number of dates by selecting text in the calender. But other
than that, still not sure how to use it to solve my problem at
hand. At this point, i dropped this potential solution because i can
probably find other solutions faster than investing further time on
this one.

For Jose Romero's suggestion:

(format-time-string
 "%Y-%m-%d"
 (time-subtract (date-to-time "2010-04-16 00:00") (days-to-time 215)))

this code results in "1999-05-30". Apparantly a wrong result.  Both
date-to-time and days-to-time function returns a weird result of a
list of 2 integers.  e.g.

(date-to-time "2010-04-16 00:00") returns (14445 17280)

(days-to-time 215) return (283 29312)

looking up the inline doc of days-to-time, all it says is: “Convert
days into a time value.”. The inline doc of date-to-time is similarly
not helpful.

So, i looked up their package time-date.el, thinking that the header
should provide some info on how to use this. Unfortunately, it
doesn't.

The closest in this cryptic paragraph:

;; Time values come in three formats.  The oldest format is a cons
;; cell of the form (HIGH . LOW).  This format is obsolete, but still
;; supported.  The two other formats are the lists (HIGH LOW) and
;; (HIGH LOW MICRO).  The first two formats specify HIGH * 2^16 + LOW
;; seconds; the third format specifies HIGH * 2^16 + LOW + MICRO /
;; 1000000 seconds.  We should have 0 <= MICRO < 1000000 and 0 <= LOW
;; < 2^16.  If the time value represents a point in time, then HIGH is
;; nonnegative.  If the time value is a time difference, then HIGH can
;; be negative as well.  The macro `with-decoded-time-value' and the
;; function `encode-time-value' make it easier to deal with these
;; three formats.  See `time-subtract' for an example of how to use
;; them.

according to this, i can see examples from the inline doc of the
function
time-subtract.

But the inline doc of that just says:

 Subtract two time values, t1 minus t2.
 Return the difference in the format of a time value.

no info about what the format really means, what's the high low thing,
or general outline of what are the major functions or explanation of
the time format. How info about what this package is really for. Of
course, it has to do with computing date and time, but how or why?
what it adds to emacs default date time functions? how to use it? does
it do the unix epoch seconds? Why is this package created? ...

At this point, i also stopped looking further with this potential
solution.

Of course, if i persist, 30 min, 1 hour, 2 hours, or more, am sure
will find out how to do the simple calculation i needed. But with cost
considerations, both of these failed, even though i think they could
easily be the best solution.

I think the above illustrate some emacs problems. Bad interface and or
bad documentation. (i've been using emacs daily for about 12 years and
coding elisp for 5 years)

... i've already done what i needed, but in retrospect, i can probably
easily do it by converting my date into unix epoch seconds, minues the
num of days i want to substract as expressed in seconds, then convert
the unix epoch seconds back to yyyy-mm-dd. I can probably do this in
elisp easily using builtin function, or i can do it with unix shell,
or perl...

  Xah
∑ http://xahlee.org/

reply via email to

[Prev in Thread] Current Thread [Next in Thread]