unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
* convert time into HH:MM:SS.sss
@ 2024-04-09  3:49 Emanuel Berg
  0 siblings, 0 replies; only message in thread
From: Emanuel Berg @ 2024-04-09  3:49 UTC (permalink / raw)
  To: emacs-devel

HH:MM:SS, MM:SS.sss and MM:SS are converted into HH:MM:SS.sss
in the below code.

But for example HH:MM:SS:ss isn't given an extra zero at
the end.

That can be dealt with as well using the same method, just
more cases are needed. It is absolutely doable, it is just
that too many cases is often a sign one should think in a new
way instead of just adding more and more.

One can also think - should one do anything about for example
H:M:S.sss - which is also easy, just left-pad zeroes to
incomplete data - but, maybe one shouldn't bother trying to
rescue what is likely incorrect input from the user to
begin with?

Another thing one maybe would like is to warn for bogus time,
that is more than 59 minutes and the same for seconds seconds.

Maybe one shouldn't do this with regexps to begin with,
instead read data as data as one goes along, warn for bogus
data, and print it back with the desire format when assessed?

This is what happens right now:

;; 11:22:33.444 -> 11:22:33.444  (complete so no change)
;; 11:22:33     -> 11:22:33.000  (add milliseconds)
;; 22:33.444    -> 00:22:33.444  (add hours)
;; 22:33        -> 00:22:33.000  (add hours and milliseconds)

;;; -*- lexical-binding: t -*-

(defun convert-time ()
  "Convert incomplete time data,
that is all of either HH:MM:SS, MM:SS.sss or MM:SS,
into HH:MM:SS.sss.
When converting, pad with zero hours (00:) and/or zero milliseconds (.000)."
  (interactive)
  (goto-char (point-min))
  (while (re-search-forward "\\([[:digit:]]\\{2\\}:[[:digit:]]\\{2\\}\\)" nil t)
    (goto-char (match-beginning 0))
    (if (looking-at "[[:digit:]]\\{2\\}:[[:digit:]]\\{2\\}:[[:digit:]]\\{2\\}.[[:digit:]]\\{3\\}")
        (goto-char (match-end 0))
      (if (looking-at "\\([[:digit:]]\\{2\\}:[[:digit:]]\\{2\\}:[[:digit:]]\\{2\\}\\)")
          (replace-match "\\1.000")
        (if (looking-at "\\([[:digit:]]\\{2\\}:[[:digit:]]\\{2\\}.[[:digit:]]\\{3\\}\\)")
            (replace-match "00:\\1")
          (replace-match "00:\\1.000") )))))

(defalias 'ct #'convert-time)

;; 11:22:33.444 -> 11:22:33.444  (complete so no change)
;; 11:22:33     -> 11:22:33.000  (add milliseconds)
;; 22:33.444    -> 00:22:33.444  (add hours)
;; 22:33        -> 00:22:33.000  (add hours and milliseconds)

-- 
underground experts united
https://dataswamp.org/~incal




^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2024-04-09  3:49 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-04-09  3:49 convert time into HH:MM:SS.sss Emanuel Berg

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

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