Date manipulation: Difference between revisions
Content added Content deleted
Langurmonkey (talk | contribs) |
(added Emacs Lisp code for date manipulation) |
||
Line 1,040: | Line 1,040: | ||
---- |
---- |
||
=={{header|Emacs Lisp}}== |
|||
<syntaxhighlight lang="lisp"> |
|||
(defun fix-time-string (bad-string) |
|||
"If BAD-STRING has no space between time and a.m./p.m., add a space." |
|||
(replace-regexp-in-string "\\([[:digit:]]\\)\\([ap]\\)" "\\1 \\2" bad-string)) |
|||
(defun adjust-if-pm (time-as-string) |
|||
"If TIME-AS-STRING includes pm/PM, and hour is 1 to 11, add 12 hours." |
|||
(let ((temp-time-stamp)) |
|||
(setq temp-time-stamp (parse-time-string time-as-string)) |
|||
(if (and (is-pm time-as-string) (is-hour-1-to-11 temp-time-stamp)) |
|||
(decoded-time-add temp-time-stamp (make-decoded-time :hour 12)) |
|||
temp-time-stamp))) |
|||
(adjust-if-pm "March 7 2009 7:30pm EST") |
|||
(defun is-pm (time-as-string) |
|||
"Test if TIME-AS-STRING has PM/pm in it." |
|||
(string-match-p "[Pp][Mm]" time-as-string)) |
|||
(defun is-hour-1-to-11 (a-time-stamp) |
|||
"Test if hour in A-TIME-STAMP is between 1 and 11." |
|||
(let ((hour-value)) |
|||
(setq hour-value (nth 2 a-time-stamp)) |
|||
(and (>= hour-value 1) (<= hour-value 11)))) |
|||
(defun add-hours (start-time-stamp number-of-hours) |
|||
"Add NUMBER-OF-HOURS to START-TIME-STAMP." |
|||
(decoded-time-add start-time-stamp (make-decoded-time :hour number-of-hours))) |
|||
(defun calc-future-time (string-calendar-date number-of-hours-in-future) |
|||
"Calculate future time by adding NUMBER-OF-HOURS-IN-FUTURE to STRING-CALENDAR-DATE ." |
|||
(let ((fixed-calendar-string) |
|||
(24-hour-calendar-string) |
|||
(decoded-future-time) |
|||
(coded-future-time)) |
|||
(setq fixed-calendar-string (fix-time-string string-calendar-date)) |
|||
(setq 24-hour-calendar-string (adjust-if-pm fixed-calendar-string)) |
|||
(setq decoded-future-time (add-hours 24-hour-calendar-string number-of-hours-in-future)) |
|||
(setq coded-future-time (encode-time decoded-future-time)) |
|||
(format-time-string "%B %e %Y %R %p %Z" coded-future-time))) |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
(calc-future-time "March 7 2009 7:30pm EST" 12) |
|||
<pre> |
|||
"March 8 2009 08:30 AM EDT" |
|||
</pre> |
|||
=={{header|Erlang}}== |
=={{header|Erlang}}== |