Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

strtotime for Emacs Lisp

Is there any functionality in Emacs Lisp that behaves similar to PHP's strtotime function? (Actually AFAIK it implements relative items of the GNU date input formats.)

In PHP I can write

echo strtotime("+2 months"); //1258891352
echo strtotime("-3 months +2 weeks"); //1246952239

which return the corresponding UNIX timestamps.

like image 200
viam0Zah Avatar asked Feb 28 '23 07:02

viam0Zah


2 Answers

While not exactly what you're asking for, this may be of use. There's a command 'org-schedule in the package org-mode that has a really nice interface to choosing dates. The specific command that does it is 'org-read-date, which understands many ways of representing the date, including:

 +0            --> today
 .             --> today
 +4d           --> four days from today
 +4            --> same as above
 +2w           --> two weeks from today
 ++5           --> five days from default date
 +2tue         --> second Tuesday from now.

If you're looking for an interactive way to specify dates with that handy syntactic sugar, that routine should fit the bill nicely. If you're looking for a programmatic solution, it looks as though the above command calls 'org-read-date-analyze to do the leg work. Note: its usage is a little obscure (two of its arguments are never defined...), so if you can't figure it out directly, it might be worth sending mail to the org mailing list.

like image 134
Trey Jackson Avatar answered Mar 07 '23 02:03

Trey Jackson


(defun string-to-time (date)
  (shell-command-to-string (format "date --date='%s' +%%s" date)))
like image 35
huaiyuan Avatar answered Mar 07 '23 04:03

huaiyuan