unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* how do you compute date difference in emacs?
@ 2010-04-16 18:23 Xah Lee
  2010-04-16 20:45 ` Edward M. Reingold
  2010-04-16 20:50 ` José A. Romero L.
  0 siblings, 2 replies; 9+ messages in thread
From: Xah Lee @ 2010-04-16 18:23 UTC (permalink / raw)
  To: help-gnu-emacs

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/^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: how do you compute date difference in emacs?
  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.
  1 sibling, 0 replies; 9+ messages in thread
From: Edward M. Reingold @ 2010-04-16 20:45 UTC (permalink / raw)
  To: help-gnu-emacs

>>>>> "XL" == Xah Lee <xahlee@gmail.com> writes:

    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: reingold@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.


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: how do you compute date difference in emacs?
  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
  1 sibling, 1 reply; 9+ messages in thread
From: José A. Romero L. @ 2010-04-16 20:50 UTC (permalink / raw)
  To: help-gnu-emacs

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.

Cheers,
--
José A. Romero L.
escherdragon at gmail
"We who cut mere stones must always be envisioning cathedrals."
(Quarry worker's creed)



^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: how do you compute date difference in emacs?
  2010-04-16 20:50 ` José A. Romero L.
@ 2010-04-17  0:39   ` Xah Lee
  2010-04-17  4:42     ` Jason Rumney
  2010-04-17  7:19     ` David Kastrup
  0 siblings, 2 replies; 9+ messages in thread
From: Xah Lee @ 2010-04-17  0:39 UTC (permalink / raw)
  To: help-gnu-emacs

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/^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: how do you compute date difference in emacs?
  2010-04-17  0:39   ` Xah Lee
@ 2010-04-17  4:42     ` Jason Rumney
  2010-04-17  7:38       ` Xah Lee
  2010-04-17  7:19     ` David Kastrup
  1 sibling, 1 reply; 9+ messages in thread
From: Jason Rumney @ 2010-04-17  4:42 UTC (permalink / raw)
  To: help-gnu-emacs

On Apr 17, 8:39 am, Xah Lee <xah...@gmail.com> wrote:

> 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.

From the "Goto" menu, you can find "Other date" which has the keyboard
shortcut "g d" (in your example case you don't need this step, since
the date was yesterdays date). Then you can use C-u NUMBER_OF_DAYS
<left> to go back a number of days.


> 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".

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.



^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: how do you compute date difference in emacs?
  2010-04-17  0:39   ` Xah Lee
  2010-04-17  4:42     ` Jason Rumney
@ 2010-04-17  7:19     ` David Kastrup
  1 sibling, 0 replies; 9+ messages in thread
From: David Kastrup @ 2010-04-17  7:19 UTC (permalink / raw)
  To: help-gnu-emacs

Xah Lee <xahlee@gmail.com> writes:

> 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.

[...]

> 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


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: how do you compute date difference in emacs?
  2010-04-17  4:42     ` Jason Rumney
@ 2010-04-17  7:38       ` Xah Lee
  2010-04-17  9:13         ` José A. Romero L.
  0 siblings, 1 reply; 9+ messages in thread
From: Xah Lee @ 2010-04-17  7:38 UTC (permalink / raw)
  To: help-gnu-emacs

On Apr 16, 9:42 pm, Jason Rumney <jasonrum...@gmail.com> wrote:
> In calendar mode, you can do it interactively.
>
> From the "Goto" menu, you can find "Other date" which has the keyboard
> shortcut "g d" (in your example case you don't need this step, since
> the date was yesterdays date). Then you can use C-u NUMBER_OF_DAYS
> <left> to go back a number of days.

thanks a lot Jason. That does it. Pretty easy too.

> > 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".
>
> 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.

  Xah
∑ http://xahlee.org/^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: how do you compute date difference in emacs?
  2010-04-17  7:38       ` Xah Lee
@ 2010-04-17  9:13         ` José A. Romero L.
  2010-05-06 23:37           ` Kevin Rodgers
  0 siblings, 1 reply; 9+ messages in thread
From: José A. Romero L. @ 2010-04-17  9:13 UTC (permalink / raw)
  To: help-gnu-emacs

On 17 Kwi, 09:38, Xah Lee <xah...@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)


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: how do you compute date difference in emacs?
  2010-04-17  9:13         ` José A. Romero L.
@ 2010-05-06 23:37           ` Kevin Rodgers
  0 siblings, 0 replies; 9+ messages in thread
From: Kevin Rodgers @ 2010-05-06 23:37 UTC (permalink / raw)
  To: help-gnu-emacs

José A. Romero L. wrote:
> On 17 Kwi, 09:38, Xah Lee <xah...@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.

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





^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2010-05-06 23:37 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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
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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).