From: Xah Lee <xahlee@gmail.com>
To: help-gnu-emacs@gnu.org
Subject: Re: how do you compute date difference in emacs?
Date: Fri, 16 Apr 2010 17:39:57 -0700 (PDT) [thread overview]
Message-ID: <c0a692af-79f5-4700-bd1d-807c190b5893@y14g2000yqm.googlegroups.com> (raw)
In-Reply-To: fdef3a54-e80f-4724-9a03-5c74c3b58b5b@j21g2000yqh.googlegroups.com
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/
☄
next prev parent reply other threads:[~2010-04-17 0:39 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2010-04-16 18:23 how do you compute date difference in emacs? Xah Lee
2010-04-16 20:45 ` Edward M. Reingold
2010-04-16 20:50 ` José A. Romero L.
2010-04-17 0:39 ` Xah Lee [this message]
2010-04-17 4:42 ` Jason Rumney
2010-04-17 7:38 ` Xah Lee
2010-04-17 9:13 ` José A. Romero L.
2010-05-06 23:37 ` Kevin Rodgers
2010-04-17 7:19 ` David Kastrup
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=c0a692af-79f5-4700-bd1d-807c190b5893@y14g2000yqm.googlegroups.com \
--to=xahlee@gmail.com \
--cc=help-gnu-emacs@gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this external index
https://git.savannah.gnu.org/cgit/emacs.git
https://git.savannah.gnu.org/cgit/emacs/org-mode.git
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.