Date manipulation: Difference between revisions
Content deleted Content added
m re-ordered part of code |
improved variable names, added comments, added second example |
||
Line 1,043:
<syntaxhighlight lang="lisp">
(defun fix-time-string (
"If
Return string with space between time and am/a.m./pm/p.m."
(replace-regexp-in-string "\\([[:digit:]]\\)\\([ap]\\)" "\\1 \\2"
(defun is-pm (
"Test if
(string-match-p "[Pp][.]?[Mm]"
(defun is-hour-1-to-11 (a-
"Test if hour in A-
(let ((hour-value))
(setq hour-value (nth 2 a-
(and (>= hour-value 1) (<= hour-value 11))))
(defun adjust-if-pm (time-as-string)
"If TIME-AS-STRING includes pm/PM, and hour is 1 to 11, add 12 hours.
Return CALENDAR-LIST modified if date is pm and hour is 1-11; otherwise
(let ((temp-time-stamp))▼
return CALENDAR-LIST of original TIME-AS-STRING."
(setq temp-time-stamp (parse-time-string time-as-string))▼
(let ((calendar-list))
(if (and (is-pm time-as-string) (is-hour-1-to-11 temp-time-stamp)) ▼
(setq
(if (and
(decoded-time-add calendar-list (make-decoded-time :hour 12))
calendar-list)))
(defun add-hours (
"Add NUMBER-OF-HOURS to
(decoded-time-add
(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-
(
(coded-future-time))
(setq fixed-calendar-string (fix-time-string string-calendar-date))
(setq 24-hour-calendar-
(setq
(setq coded-future-time (encode-time
(format-time-string "%B %e %Y %R %p %Z" coded-future-time)))
Line 1,089 ⟶ 1,092:
"March 8 2009 08:30 AM EDT"
</pre>
And a second solution, which modifies the ELisp timestamp to calculate 12 hours in the future
<syntaxhighlight lang="lisp">
(defun fix-time-string (calendar-string)
"If CALENDAR-STRING has no space between time and a.m./p.m., add a space."
(replace-regexp-in-string "\\([[:digit:]]\\)\\([ap]\\)" "\\1 \\2" calendar-string))
(defun is-pm (calendar-string)
"Test if CALENDAR-STRING has PM/pm in it."
(string-match-p "[Pp][Mm]" time-as-string))
(defun is-hour-1-to-11 (a-calendar-list)
"Test if hour in A-CALENDAR-LIST is between 1 and 11."
(let ((hour-value))
(setq hour-value (nth 2 a-calendar-list))
(and (>= hour-value 1) (<= hour-value 11))))
(defun adjust-if-pm (time-as-string)
"If TIME-AS-STRING includes pm/PM, and hour is 1 to 11, add 12 hours.
Return time as integer of seconds past the epoch."
(let ((calendar-list)
(time-stamp-as-integer))
(setq temp-time-stamp (encode-time calendar-list))
(setq time-stamp-as-integer (time-convert temp-time-stamp 'integer))
(+ time-stamp-as-integer (* 12 60 60)) ; return time + 12 hours, so that hour is 13-23
time-stamp-as-integer))) ; return time unchanged, leaving hour 0-12
(defun add-seconds (start-time-stamp-integer number-of-seconds)
"Add NUMBER-OF-HOURS to START-TIME-STAMP-INTEGER."
(+ start-time-stamp-integer number-of-seconds))
(defun calc-future-time (string-calendar-date number-of-seconds-in-future)
"Calculate future time by adding NUMBER-OF-SECONDS-IN-FUTURE to STRING-CALENDAR-DATE ."
(let ((fixed-calendar-string)
(time-stamp-as-integer)
(coded-current-time)
(future-time-as-integer))
(setq fixed-calendar-string (fix-time-string string-calendar-date))
(setq time-stamp-as-integer (adjust-if-pm fixed-calendar-string))
(setq future-time-as-integer (add-seconds time-stamp-as-integer number-of-seconds-in-future))
(format-time-string "%B %e %Y %R %p %Z" future-time-as-integer)))
</syntaxhighlight>
{{out}}
(calc-future-time "March 7 2009 7:30pm EST" (* 12 60 60))
<pre>
"March 8 2009 08:30 AM EDT"
</pre>
=={{header|Erlang}}==
|