all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Emanuel Berg <incal@dataswamp.org>
To: emacs-devel@gnu.org
Subject: convert time into HH:MM:SS.sss
Date: Tue, 09 Apr 2024 05:49:31 +0200	[thread overview]
Message-ID: <87pluz19h0.fsf@dataswamp.org> (raw)

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




                 reply	other threads:[~2024-04-09  3:49 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87pluz19h0.fsf@dataswamp.org \
    --to=incal@dataswamp.org \
    --cc=emacs-devel@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

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

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.