Discussion:
how do you compute date difference in emacs?
Edward M. Reingold
2010-05-04 15:44:49 UTC
Permalink
XL> computing dates. Recently i needed to compute dates in emacs.

XL> For example, i need to find out: 2010-04-16 - 215 days.

XL> how you do it in emacs? (i know various langs that have date
XL> libs... but i just need a convenient one time use in emacs)

Use the calendar mode.
--
Professor Edward M. Reingold Email: ***@iit.edu
Department of Computer Science Voice: (312) 567-3309
Illinois Institute of Technology Fax: (312) 567-5067
Stuart Building, 228F
10 West 31st Street
Chicago, IL 60616-3729 U.S.A.
José A. Romero L.
2010-05-04 15:44:49 UTC
Permalink
Post by Edward M. Reingold
computing dates.
Recently i needed to compute dates in emacs.
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.

Cheers,
--
José A. Romero L.
escherdragon at gmail
"We who cut mere stones must always be envisioning cathedrals."
(Quarry worker's creed)
Jason Rumney
2010-05-04 15:44:55 UTC
Permalink
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.
In calendar mode, you can do it interactively.
José A. Romero L.
2010-05-04 15:44:58 UTC
Permalink
On 17 Kwi, 09:38, Xah Lee <***@gmail.com> wrote:
(...)
this code results in "1999-05-30".
In 23.1.1 (x86_64-pc-linux-gnu, GTK+ Version 2.18.3) of 2010-03-16 on
crested, modified by Debian; the code above results in an error,
because the seconds are required in the argument to date-to-time.
good info.
(...)

Indeed, good to know. Looks like the Emacs team has been working
on that lately -- in 24.0.50.1 (i686-pc-linux-gnu, GTK+ Version
2.18.3) of 2010-03-22 it seems to work OK, yielding "2009-09-13" as
result.

Cheers,
--
José A. Romero L.
escherdragon at gmail
"We who cut mere stones must always be envisioning cathedrals."
(Quarry worker's creed)
Kevin Rodgers
2010-05-06 23:37:20 UTC
Permalink
Post by José A. Romero L.
(...)
this code results in "1999-05-30".
In 23.1.1 (x86_64-pc-linux-gnu, GTK+ Version 2.18.3) of 2010-03-16 on
crested, modified by Debian; the code above results in an error,
because the seconds are required in the argument to date-to-time.
good info.
(...)
Indeed, good to know. Looks like the Emacs team has been working
on that lately -- in 24.0.50.1 (i686-pc-linux-gnu, GTK+ Version
2.18.3) of 2010-03-22 it seems to work OK, yielding "2009-09-13" as
result.
In 22.3 (i386-apple-darwin8.11.1, Carbon Version 1.6.0) it returns
"2009-09-12" when the seconds are included in the argument to date-to-time.

What would explain the 1-day discrepancy, time zone? I am MDT (UTC +6).
--
Kevin Rodgers
Denver, Colorado, USA
Xah Lee
2010-05-04 15:44:58 UTC
Permalink
Post by Jason Rumney
In calendar mode, you can do it interactively.
David Kastrup
2010-05-04 15:44:57 UTC
Permalink
Post by Edward M. Reingold
Recently i needed to compute dates in emacs.
2010-04-16 - 215 days.
[...]
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.
You could do

C-x * q <2010-04-16>-215 RET

That's rather simple to use. You can yank the result using C-y after
that.
--
David Kastrup
Xah Lee
2010-05-04 15:44:54 UTC
Permalink
Post by José A. Romero L.
Post by Edward M. Reingold
Recently i needed to compute dates in emacs.
2010-04-16 - 215 days.
(...)
(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/


Xah Lee
2010-05-04 15:44:47 UTC
Permalink
computing dates.

Recently i needed to compute dates in emacs.

For example, i need to find out:
2010-04-16 - 215 days.

how you do it in emacs?
(i know various langs that have date libs... but i just need a
convenient one time use in emacs)

Thanks.

Xah
∑ http://xahlee.org/


Loading...