help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: efficiently viewing Unix timestamps as dates


From: Ted Zlatanov
Subject: Re: efficiently viewing Unix timestamps as dates
Date: Thu, 16 Dec 2010 17:12:23 -0600
User-agent: Gnus/5.110011 (No Gnus v0.11) Emacs/24.0.50 (gnu/linux)

On Thu, 16 Dec 2010 17:23:34 -0500 Stefan Monnier <monnier@iro.umontreal.ca> 
wrote: 

>> but the actual format string should be up to the user.  Really, my
>> question is "how do I find numbers that look like 1292527019, run a
>> function on them, and then show the results of that function overlaid on
>> top of the number without actually changing it in the buffer?"

SM> The first part is rather tricky: "2" looks an awful lot like a Unix
SM> timestamp ...wait... it *is* a Unix timestamp!
SM> But assuming you know something about those time stamps, you can try
SM> something like:

SM>   (add-hook 'foo-mode-hook
SM>             (lambda ()
SM>               (font-lock-add-keywords nil
SM>                 '(("^[0-9]+"
SM>                    (0 `(face nil display
SM>                         ,(format-time-string "%F %T"
SM>                            (seconds-to-time
SM>                             (car (read-from-string
SM>                                   (concat "1292527019" ".0"))))))))))))

SM> where "^[0-9]+" is the regexp that matches your timestamps (in this
SM> case I chose to assume they're always at the beginning of a line).

I see a small bug in the last line, but I think it's fixable :)

On Thu, 16 Dec 2010 13:34:51 -0800 PJ Weisberg <pj@irregularexpressions.net> 
wrote: 

PW> Argh.  I don't actually know how to do the visual-only thing, but that
PW> problem is complicated by the fact that the string "December 16, 2010"
PW> (for example) contains TWO Unix timestamps, both in the early morning
PW> hours of January 1, 1970.

It's not so bad (I only bothered for dates after 2001 or 0-padded at the
beginning):

(add-hook 'foo-mode-hook
          (lambda ()
            (font-lock-add-keywords
             nil
             '(("[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]"
                (0 `(face nil display
                          ,(format-time-string "%F %T"
                                               (seconds-to-time
                                                (car
                                                 (read-from-string
                                                  (concat
                                                   (match-string 0)
                                                   ".0"))))))))))))

I'd rather make this a minor mode than a hook, so I can easily turn it
on in a buffer.  Is that easy or hard to do?  Any specific example I can
look at?

Ted


reply via email to

[Prev in Thread] Current Thread [Next in Thread]