2011-09-03

xah lee blogs and what you like to see?

Xah Lee Blogs and What You Like to See?

Recently on g+ i asked where people know me from and what they like me to post on g+. (See: Source plus.google.com) One conversation leads to the fact that some people don't know i have webfeed of separate topics. So, here's a Reminder, and a poll.

If many subjects in my blog are not interesting to you, you can subscribe to individual topics, e.g. emacs only, programing only, math only, arts only, etc, at this page: Subscribe to XahLee.org Site Feed.

This blog at xahlee.blogspot.com is a aggregate of all, but except english vocabulary subject, which you can subscribe at wordy-english.blogspot.com.

am also on g+, twitter, facebook. In these places, i post links to new articles, and selected old articles few times a day. Also random chat (mostly on g+).

if you like my articles, please retweet or tell friends. Thanks for helping.

2011-09-02

Emacs Lisp: Writing a Date Time String Parsing Function

Perm url with updates: http://xahlee.org/emacs/elisp_parse_time.html

Emacs Lisp: Writing a Date Time String Parsing Function

Xah Lee, 2011-09-02

This page shows a example of writing a emacs lisp function that parses a date time string.

The Problem

Write a elisp function. The function will take a string argument that's any of common date time format, e.g.

  • 2011-09-02T05:29:26-07:00 (ISO 8601)
  • 2011-09-02 (ISO 8601)
  • Fri, 2 Sep 2011 11:14:11 +0200 (unixy)
  • 09/02/2011 (USA)
  • Sep 2, 2011
  • 2 Sep, 2011
  • 2 September, 2011

and output a canonical form 2011-09-02.

Solution

If you've worked with elisp for a while, or from a web search, you'll know there's a time parsing function “parse-time-string”, from the file 〔parse-time.el〕, with feature name 'parse-time (that is, you call (require 'parse-time) to load it). (See: Emacs Lisp's Library System: What's require, load, load-file, autoload, feature?.)

Here's its inline doc:

parse-time-string is a compiled Lisp function in `parse-time.el'.

(parse-time-string STRING)

Parse the time-string STRING into (SEC MIN HOUR DAY MON YEAR DOW DST TZ).
The values are identical to those of `decode-time', but any values that are
unknown are returned as nil.

However, a little test shows that this function doesn't parse some common date formats. In particular, it doesn't understand ISO 8601 nor USA custom of mm/dd/yyyy.

;; testing for supported formats for “parse-time-string”
;; As of 2011-08-15 GNU Emacs 23.2.1

(require 'parse-time)

;; unixy formats
(parse-time-string "Date: Mon, 01 Aug 2011 12:24:51 -0400") ; yes
(parse-time-string "Local: Mon, Aug 1 2011 9:24 am")        ; yes

(parse-time-string "2007, August 1")                        ; yes
(parse-time-string "August 1, 2007")                        ; yes
(parse-time-string "august 1, 2007")                        ; yes. Lowercase ok.
(parse-time-string "August 1st, 2007")                      ; no. The date is nil.
(parse-time-string "aug 1, 2007")                           ; yes. Month abbr OK.
(parse-time-string "1 aug, 2007")                           ; yes

(parse-time-string "8/1/2007")     ; no. Takes the 8 as date, 1 as nil
(parse-time-string "08/01/2007")   ; no. Takes the 8 as date, 1 as nil
(parse-time-string "8,1,2007")     ; no
(parse-time-string "2007-08-01")   ; yes
(parse-time-string "2007")         ; yes
(parse-time-string "2007-08")      ; no
(parse-time-string "2011-08-01")   ; yes
(parse-time-string "2011-08-01T11:55:37-07:00") ; no. got nothing

For me, i need it to understand the USA customary format 8/1/2007 interpreted as month/day/year. Ι also need it to understand formats such as August 1st, 2007. And i also need it to understand ISO 8601 format such as yyyy-mm, yyyy-mm-dd, yyyy-mm-ddThh:mm:ss-07:00.

The simplest solution is just do a regex match on the form. I don't need the time info, so it makes the problem slightly simpler. Here's my code:

(defun fix-timestamp-string (dateStr)
  "Returns yyyy-mm-dd format of timeStr

For examples:
 「Nov. 28, 1994」 ⇒ 「1994-11-28」
 「November 28, 1994」 ⇒ 「1994-11-28」
 「11/28/1994」 ⇒ 「1994-11-28」

Any “day of week”, or “time” info, or any other parts of the string, are discarded.

Code detail: URL `http://xahlee.org/emacs/elisp_parse_time.html'"
  (let (dateList ξyear ξmonth ξdate yyyy mm dd)
    (require 'parse-time)

    (setq dateStr (replace-regexp-in-string "^ *\\(.+\\) *$" "\\1" dateStr)) ; remove white spaces

    (cond

     ;; USA convention of mm/dd/yyyy
     ((string-match "^\\([0-9][0-9]\\)/\\([0-9][0-9]\\)/\\([0-9][0-9][0-9][0-9]\\)$" dateStr)
      (concat (match-string 3 dateStr) "-" (match-string 1 dateStr) "-" (match-string 2 dateStr))
      )
     ((string-match "^\\([0-9]\\)/\\([0-9][0-9]\\)/\\([0-9][0-9][0-9][0-9]\\)$" dateStr)
      (concat (match-string 3 dateStr) "-" (match-string 1 dateStr) "-" (match-string 2 dateStr))
      )

     ;; some ISO 8601. yyyy-mm-dd
     ((string-match "^\\([0-9][0-9][0-9][0-9]\\)-\\([0-9][0-9]\\)-\\([0-9][0-9]\\)$T[0-9][0-9]:[0-9][0-9]" dateStr)
      (concat (match-string 1 dateStr) "-" (match-string 2 dateStr) "-" (match-string 3 dateStr))
      )
     ((string-match "^\\([0-9][0-9][0-9][0-9]\\)-\\([0-9][0-9]\\)-\\([0-9][0-9]\\)$" dateStr)
      (concat (match-string 1 dateStr) "-" (match-string 2 dateStr) "-" (match-string 3 dateStr))
      )
     ((string-match "^\\([0-9][0-9][0-9][0-9]\\)-\\([0-9][0-9]\\)$" dateStr)
      (concat (match-string 1 dateStr) "-" (match-string 2 dateStr))
      )
     ((string-match "^\\([0-9][0-9][0-9][0-9]\\)$" dateStr)
      (match-string 1 dateStr)
      )

     ;; else
     (t
      (progn
        (setq dateStr (replace-regexp-in-string "January " "Jan. " dateStr))
        (setq dateStr (replace-regexp-in-string "February " "Feb. " dateStr))
        (setq dateStr (replace-regexp-in-string "March " "Mar. " dateStr))
        (setq dateStr (replace-regexp-in-string "April " "Apr. " dateStr))
        (setq dateStr (replace-regexp-in-string "May " "May. " dateStr))
        (setq dateStr (replace-regexp-in-string "June " "Jun. " dateStr))
        (setq dateStr (replace-regexp-in-string "July " "Jul. " dateStr))
        (setq dateStr (replace-regexp-in-string "August " "Aug. " dateStr))
        (setq dateStr (replace-regexp-in-string "September " "Sep. " dateStr))
        (setq dateStr (replace-regexp-in-string "October " "Oct. " dateStr))
        (setq dateStr (replace-regexp-in-string "November " "Nov. " dateStr))
        (setq dateStr (replace-regexp-in-string "December " "Dec. " dateStr))

        (setq dateStr (replace-regexp-in-string " 1st," " 1" dateStr))
        (setq dateStr (replace-regexp-in-string " 2nd," " 2" dateStr))
        (setq dateStr (replace-regexp-in-string " 3rd," " 3" dateStr))
        (setq dateStr (replace-regexp-in-string "\\([0-9]\\)th," "\\1" dateStr))

        (setq dateStr (replace-regexp-in-string " 1st " " 1 " dateStr))
        (setq dateStr (replace-regexp-in-string " 2nd " " 2 " dateStr))
        (setq dateStr (replace-regexp-in-string " 3rd " " 3 " dateStr))
        (setq dateStr (replace-regexp-in-string "\\([0-9]\\)th " "\\1 " dateStr))

        (setq dateList (parse-time-string dateStr))
        (setq ξyear (nth 5 dateList))
        (setq ξmonth (nth 4 dateList))
        (setq ξdate (nth 3 dateList))

        (setq yyyy (number-to-string ξyear))
        (setq mm (if ξmonth (format "%02d" ξmonth) "" ) )
        (setq dd (if ξdate (format "%02d" ξdate) "" ) )
        (concat yyyy "-" mm "-" dd)

        ) ) ) ))

This code is easy to understand. The function takes a string, and returns a string.

The whole code is just one giant multi-branch conditional test, known in other languages as “case” or “switch”. Elisp conditional takes this form:

(cond
 (TEST1 BODY)
 (TEST2 BODY)
 …
 )

Each of the TEST is either true (not “nil”) or false (“nil”). Emacs will go thru them in sequence. The first test that's non-nil, its body will be executed, then exit the conditional.

In my code, the first few tests are regex match of forms like nn/nn/nnnn where each “n” is a digit. When any of these match, then basically i got what i want, and the code exists. Here's one example:

 ;; USA convention of mm/dd/yyyy
 ((string-match "^\\([0-9][0-9]\\)/\\([0-9][0-9]\\)/\\([0-9][0-9][0-9][0-9]\\)$" dateStr)
  (concat (match-string 3 dateStr) "-" (match-string 1 dateStr) "-" (match-string 2 dateStr))
  )

When none of these match, then it goes to the end of the test (t BODY), where the “t” there is always true, and run a giant BODY. In the BODY, first i replace each full spelling of month names by their abbrev using “replace-regexp-in-string”, e.g.

(setq dateStr (replace-regexp-in-string "January " "Jan. " dateStr))

This is done because in emacs 22, the “parse-time-string” doesn't understand fully spelled month names. (this has been fixed.)

Then, i also replace {1st, 2nd, nth} etc by {1, 2, n}. Then, i simply feed it to “parse-time-string” and get a parsed date time as a list. After that, just extract the elements from the list and reformat the way i want.

Now, remember that my function takes a string and returns a string. It is not a interactive command. What i actually want is a interactive command, so that i can press a button, then the date on the current line will be transformed to the format i want. Here's the interactive command wrapper, which calls my “fix-timestamp-string” function to work:

(defun fix-timestamp ()
  "Change timestamp under cursor into a yyyy-mm-dd format.
If there's a text selection, use that as input, else use current line.
All other text in input are discarded.
For example:
TUESDAY, FEB 15, 2011 05:16 ET
becomes
2011-02-15
.
See `fix-timestamp-string' for detail."
  (interactive)
  (let (bds p3 p4 inputstr)
    (setq bds (get-selection-or-unit 'line))
    (setq inputstr (elt bds 0) )
    (setq p3 (elt bds 1) )
    (setq p4 (elt bds 2) )
    (delete-region p3 p4)
    (insert (fix-timestamp-string inputstr)) ))

The “get-selection-or-unit” is my custom function as replacement for “thing-at-point” function. See: Emacs Lisp: get-selection-or-unit.

The weird ξ you see in my elisp code is Greek x. I use unicode char in variable name for experimental purposes. You can just ignore it. (See: Programing Style: Variable Naming: English Words Considered Harmful.)

I ♥ Emacs.

today's Wordy English, and what makes a great essayist

today's Wordy English: gaunt, and what makes a great essayist: http://wordy-english.blogspot.com/2011/09/gaunt.html

2011-09-01

Are Googlers the Minions of Google Marketing?

Perm url with updates: http://xahlee.org/w/Googler_marketing_minion.html

Are Googlers the Minions of Google Marketing?

Xah Lee, 2011-09-01

it's somewhat disappointing. Vast majority of googlers on g+, they all seem to be doing marketing for google. It's all nice tweets, new features of g+, how great g+ is, news of another net celebrity joining g+.

maybe they just love the company they work for? But if you look closer, i don't think it's peach-n-cream like that. They seldom, basically never, tweet about actual work, or any thought that might be negative to google, in fact, vast majority don't tweet anything non-google at all (except generic crowd-pleasing cat pics or piping thru luke-warmed news).

a job is a job. Even though google caters a great and fun environment for its employees, am pretty sure there are still pressures, conflicts, or just tiredness, at least occasionally.

then, outside of g+, there are a few dedicated blogs of ex-googlers. There, it's usually always somewhat negative of google, with polite words patched over on top.

it's natural for one to carefully avoid talking about his work, his boss, especially negative parts. But the number of googlers online, and how they all seem to be doing google marketing, seems, unnatural.

just to be sure, am trying to think of how other big companies are like regarding this. Apple, Microsoft, HP, Oracle, IBM, etc. I don't really knom much blogs by these company's employees to say, except that Steve Jobs made the fan do marketing for them.

PS ok, a new thought. Maybe google is like that because they have shaped their marketing into a science, like everything else of their algorithm and scientific methodology fetish. So, instead of having dedicated marketing personnel (which they do), they spread marketing informally to be parts of work to all staff (e.g. programers and managers too). So, any worker, are encouraged to use perhaps their break time or “the 20% time” to do google marketing on their own, without being directed. May it be a simple tweet of some new Android cuddly at Google, or participate a video broadcasting some new Google feature.

PS if you need a g+ invite, click: Xah Lee's G+ Invite.

Women in Tech: Today's G+ Recommendation? You Decide.

Perm url with updates: http://xahlee.org/w/women_in_tech_TWiT.html

Women in Tech: Today's G+ Recommendation? You Decide.

Xah Lee, 2011-09-01

due to g+, recently i got acquainted with a couple of online video shows. One of them is TWiT. (it has nothing to do with Twitter, btw. It stood for This Week in Tech, started by Leo Laporte (g+) (i just looked up Leo's profile. O yeah, i remember him, a jolly comedian-like fellow, because recently i've watched one of his show, also featuring Gina Trapani (g+) , who is a coder among startup things, and she espouses Java! (that's a LOL)))

This TWiT show, was what prompted me to write about how TV Has Arrived on The Web (which, the host dully spend some 30 secs to recite a passage about the greatness of NetFlix in lively personalized detail. Catherine Fitzpatrick (g+) , the communism expert, reminded me that the proper word for it is Infomercial.)

anyhow, probably for many of you guys, that what first prompted you to watch the show is a twain Sarah Lane (g+) and Amber Mac (g+). Both got few million followers.

Sarah is this attractive animated chick. Amber is just one angelic face, attractive notwithstanding.

it is these qualities, that attracted me to the show in the first place. So far i've watched 2 episodes of their shows. Having watched them on video, i now know better who the heck are these popular women i'm following on g+. (in their last show, they talked about a new service of having online friends (aka strangers) to give you wakeup calls. Sarah & Amber, try me! I pack a dollop in the word creep.)

now you pretty females got all the breaks. You all net activists and feminists and transexual advocates and nym-cryers out there. Shouldn't society be, like, more fair, so that the public should pay more attention to a old ugly and poor and underpowered man like me?

PS if you need a g+ invite, click: Xah Lee's G+ Invite.

poem: iron ball

2011-09-01

iron ball,
rusted and scarred,
tittering on the window sill,
wondering, when
he's gonna fall

2011-08-31

2011-08-29

Perl Expert Randal Schwartz = Fun Chatty Fellow

Linux Outlaws - Interview with Randal Schwartz

Randal Schwartz, the perl expert, is caught on video in this interview.

A very fun, chatty interview. Randal tells you how much you'd make if you write a successful O'Reilly book, and on Perl 6, singing in karaoke, attending tech conferences and his record of free pass for ~15 years, his visit to Stonehenge (Randal's company is stonehenge.com) and much more.

The interview is informal, more or less random chat among 3 geeks, which you get to know Randal the person.

The hosts are Fabian A Scherschel (the German guy drinking beer) and Dan Lynch (UK guy, on voice only). They run Linux Outlaws, which publishes podcast on linux and tech.

2011-08-28

Math Mysticism: is Hurricane Shape a Fibonaci Spiral?

Perm url with updates: http://xahlee.org/math/fibonaci_spiral_hurricane.html

Math Mysticism: is Hurricane Shape a Fibonaci Spiral?

Xah Lee, 2011-08-28

Please beware of the golden ratio math mysticism trash spreading online.

Fibonacci spiral hurrican Irene
Illustration giving the impression that hurricane Irene has a shape of so-called “Fibonacci spiral” Source imgur.com

I'm seeing people sharing this image online (e.g. Reddit, Twitter, Google+). It is true that the hurricane has the general shape of a equiangular spiral (aka logarithmic spiral), which is the shape that many (but not all) natural growth takes (e.g. Seashells), but putting that golden-ratio block "spiral" there is completely baseless.

The so-called “Fibonacci spiral” you see in the pic is made up of parts of circles, stacked on blocks of rectangles who's ratio of the sides is the golden ratio. Such spiral is only a approximation of one particular equiangular spiral. (equiangular spiral is a family of spirals. See: Equiangular Spiral.)

Also, hurricane does not really have a definite shape, we can only say it's roughly that of equiangular spiral based partly on physics and appearance.

If you don't have a degree in math, you might be all confused about this Fibonacci number , the Golden ratio, and the various spirals. It is true that mathematics is mysterious, and many shapes in nature are well described by math, but if you see any online blog or math popularizing books that carelessly connect it with mysticism, about how some numbers or shapes is everything in the universe, beware, it's stretched to garbage.

For highschool-level explanation of the equiangular spiral, see: Equiangular Spiral. For other spirals, see: Archimedean Spiral, Lituus.

For debunking of the Fibonacci number and “golden” this that, see:

  • Fibonacci Flim-Flam (2008) By Donald E Simanek. @ Source www.lhup.edu
  • The Myth That Will Not Go Away (2007-05-) By Keith Devlin, Mathematics Association of America. @ Source www.maa.org
fibonacci spiral girl
When a attractive girl flips her hair, the water stream formed is a Fibonacci spiral. ☺

emacs tutorial update

Of those who bought my emacs tutorial recently, there's a new free update. (fixing a error on outbound links) Just email to xah@xahlee.org with subject “xah emacs tutorial update”, then i'll email you the download location.

If you haven't bought it, you can buy at Buy Xah Emacs Tutorial.