Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

parsing a date

31 views
Skip to first unread message

Eric Abrahamsen

unread,
Sep 27, 2012, 10:07:08 PM9/27/12
to help-gn...@gnu.org
I'm reading in files with dates in this format: "2011-11-25". I need to
write them out as "2011/11/25". Instead of just manhandling the strings
(I'll likely need this date information in other places) I wanted to
parse the strings into proper date objects, then format them back into
strings. `date-to-time' doesn't work because (parse-time-string
"2011-11-15") gives me:

(nil nil nil 15 11 2011 nil nil nil)

Which is not acceptable to `encode-time', because it requires integers,
not nil. I can't believe this is quite this complicated: do I really
have to replace all the nils with 0 myself?

Any pointers gratefully accepted,

Eric

--
GNU Emacs 24.2.50.1 (i686-pc-linux-gnu, GTK+ Version 3.4.4)
of 2012-09-16 on pellet


Óscar Fuentes

unread,
Sep 27, 2012, 10:50:53 PM9/27/12
to Eric Abrahamsen, help-gn...@gnu.org
Eric Abrahamsen <er...@ericabrahamsen.net> writes:

> I'm reading in files with dates in this format: "2011-11-25". I need to
> write them out as "2011/11/25". Instead of just manhandling the strings
> (I'll likely need this date information in other places) I wanted to
> parse the strings into proper date objects, then format them back into
> strings.

Why? Once you have the date components, you only need to write them
separated by /

Or just replace - with / on the original date string.

> `date-to-time' doesn't work because (parse-time-string
> "2011-11-15") gives me:
>
> (nil nil nil 15 11 2011 nil nil nil)
>
> Which is not acceptable to `encode-time', because it requires integers,
> not nil. I can't believe this is quite this complicated: do I really
> have to replace all the nils with 0 myself?

(mapcar (lambda (x) (if x x 0)) (parse-time-string "2011-11-15"))

Eric Abrahamsen

unread,
Sep 27, 2012, 11:20:48 PM9/27/12
to help-gn...@gnu.org
On Fri, Sep 28 2012, Óscar Fuentes wrote:

> Eric Abrahamsen <er...@ericabrahamsen.net> writes:
>
>> I'm reading in files with dates in this format: "2011-11-25". I need to
>> write them out as "2011/11/25". Instead of just manhandling the strings
>> (I'll likely need this date information in other places) I wanted to
>> parse the strings into proper date objects, then format them back into
>> strings.
>
> Why? Once you have the date components, you only need to write them
> separated by /
>
> Or just replace - with / on the original date string.

As I mention, this is on the first step, and I'll likely need the dates
in a few other contexts as well. I suppose you're right, though -- I can
just use the elements of the list returned by parse-time-string.

>> `date-to-time' doesn't work because (parse-time-string
>> "2011-11-15") gives me:
>>
>> (nil nil nil 15 11 2011 nil nil nil)
>>
>> Which is not acceptable to `encode-time', because it requires integers,
>> not nil. I can't believe this is quite this complicated: do I really
>> have to replace all the nils with 0 myself?
>
> (mapcar (lambda (x) (if x x 0)) (parse-time-string "2011-11-15"))

Sure, it's doable, but it just seems odd that `parse-time-string'
returns a structure that `encode-time' can't read!

E

Óscar Fuentes

unread,
Sep 28, 2012, 12:14:19 AM9/28/12
to help-gn...@gnu.org
Eric Abrahamsen <er...@ericabrahamsen.net> writes:

>>> Which is not acceptable to `encode-time', because it requires integers,
>>> not nil. I can't believe this is quite this complicated: do I really
>>> have to replace all the nils with 0 myself?
>>
>> (mapcar (lambda (x) (if x x 0)) (parse-time-string "2011-11-15"))
>
> Sure, it's doable, but it just seems odd that `parse-time-string'
> returns a structure that `encode-time' can't read!

I guess that the designers chose to differenciate among `zero' and `not
specified' on the output of parse-time-string.

As for `encode-time', it takes each date-time component as an argument,
not the output of parse-time-string, which is a list. OTOH,
`encode-time' could interpret arguments with value `nil' as `zero'.


Eric Abrahamsen

unread,
Sep 28, 2012, 2:11:19 AM9/28/12
to help-gn...@gnu.org
On Fri, Sep 28 2012, Óscar Fuentes wrote:

I'm also heard of something like this in the works:

(read-time-string "%Y-%m-%d" "2011-11-15")

Where you inform emacs of the format of your incoming strings -- that
seems like it would be more than enough for the sort of thing I'm doing.

E

--
GNU Emacs 24.2.50.1 (i686-pc-linux-gnu, GTK+ Version 3.4.4)
of 2012-09-28 on pellet


Stefan Monnier

unread,
Sep 29, 2012, 1:00:00 PM9/29/12
to
> (mapcar (lambda (x) (if x x 0)) (parse-time-string "2011-11-15"))

(if X X Y) => (or X Y)


Stefan

David Combs

unread,
Oct 13, 2012, 9:51:37 PM10/13/12
to
In article <mailman.9889.13488127...@gnu.org>,
Eric Abrahamsen <er...@ericabrahamsen.net> wrote:
>
>I'm also heard of something like this in the works:
>
>(read-time-string "%Y-%m-%d" "2011-11-15")
>
>Where you inform emacs of the format of your incoming strings -- that
>seems like it would be more than enough for the sort of thing I'm doing.
>
>E

In the works where, and when?

David


Eric Abrahamsen

unread,
Oct 14, 2012, 3:18:32 AM10/14/12
to help-gn...@gnu.org
I believe it's this: http://www.emacswiki.org/emacs-zh/StrPTime

--
GNU Emacs 24.2.50.1 (i686-pc-linux-gnu, GTK+ Version 3.4.4)
of 2012-10-10 on pellet


WJ

unread,
Dec 2, 2012, 2:25:28 AM12/2/12
to
Eric Abrahamsen wrote:

> I'm reading in files with dates in this format: "2011-11-25". I need to
> write them out as "2011/11/25". Instead of just manhandling the strings
> (I'll likely need this date information in other places) I wanted to
> parse the strings into proper date objects, then format them back into
> strings. `date-to-time' doesn't work because (parse-time-string
> "2011-11-15") gives me:
>
> (nil nil nil 15 11 2011 nil nil nil)
>
> Which is not acceptable to `encode-time', because it requires integers,
> not nil. I can't believe this is quite this complicated: do I really
> have to replace all the nils with 0 myself?
>
> Any pointers gratefully accepted,
>
> Eric

(format-time-string "%Y/%m/%d"
(apply 'encode-time 0 0 0
(nthcdr 3 (parse-time-string "2011-11-15"))))

==> "2011/11/15"

Eric Abrahamsen

unread,
Dec 23, 2012, 12:55:12 AM12/23/12
to help-gn...@gnu.org
Only just saw this -- thanks very much!


ken

unread,
Dec 23, 2012, 7:39:05 PM12/23/12
to help-gn...@gnu.org
On 12/23/2012 12:55 AM Eric Abrahamsen wrote:
> (format-time-string "%Y/%m/%d"
>> (apply 'encode-time 0 0 0
>> (nthcdr 3 (parse-time-string "2011-11-15"))))

Nice. I was wanting to do something very close to this awhile back. Of
course the above code is more useful in the context of a function,
something like the below-- which has something missing. What?

(setq ddd "2013-02-11.tail") ;Want to remove tail on string also

(defun ddd
(format-time-string "%A, %B %d, %Y"
(apply 'encode-time 0 0 0
(nthcdr 3 (parse-time-string ddd)))))


Eric Abrahamsen

unread,
Dec 23, 2012, 10:02:12 PM12/23/12
to help-gn...@gnu.org
ken <geb...@mousecar.com> writes:

> On 12/23/2012 12:55 AM Eric Abrahamsen wrote:
>> (format-time-string "%Y/%m/%d"
>>> (apply 'encode-time 0 0 0
>>> (nthcdr 3 (parse-time-string "2011-11-15"))))
>
> Nice. I was wanting to do something very close to this awhile back.
> Of course the above code is more useful in the context of a function,
> something like the below-- which has something missing. What?
>
> (setq ddd "2013-02-11.tail") ;Want to remove tail on string also
>
> (defun ddd
> (format-time-string "%A, %B %d, %Y"
> (apply 'encode-time 0 0 0
> (nthcdr 3 (parse-time-string ddd)))))

I suppose this?

--8<---------------cut here---------------start------------->8---
(defun ddd (s)
(format-time-string "%Y/%m/%d"
(apply 'encode-time 0 0 0
(nthcdr 3 (parse-time-string
(replace-regexp-in-string "\.tail" "" s))))))
--8<---------------cut here---------------end--------------->8---


ken

unread,
Dec 23, 2012, 10:54:57 PM12/23/12
to help-gn...@gnu.org
On 12/23/2012 10:02 PM Eric Abrahamsen wrote:
> ken<geb...@mousecar.com> writes:
>....
>
> --8<---------------cut here---------------start------------->8---
> (defun ddd (s)
> (format-time-string "%Y/%m/%d"
> (apply 'encode-time 0 0 0
> (nthcdr 3 (parse-time-string
> (replace-regexp-in-string "\.tail" "" s))))))
> --8<---------------cut here---------------end--------------->8---

That works. Thanks much.



0 new messages