Date fromString: '6-Jan-10'

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
25 messages Options
12
Reply | Threaded
Open this post in threaded view
|

Re: Date fromString: '6-Jan-10'

Schwab,Wilhelm K
80/20 is probably not unreasonable, but it cannot be based on the current date - it has to be based on the date the entry occurred.  In short, it has to be specified separately; even then, it's still just a guess.




-----Original Message-----
From: [hidden email] [mailto:[hidden email]] On Behalf Of [hidden email]
Sent: Sunday, January 17, 2010 5:30 PM
To: [hidden email]
Subject: Re: [Pharo-project] Date fromString: '6-Jan-10'

Quoting [hidden email]:

> ...
> > Python goes this way, for example.
>
> >  - the  date is assumed to  be between 80  years in the past  and 20
> > years   in    the   future   (eg    Java's   SimpleDateFormat,   see
> > http://java.sun.com/javase/7/docs/api/java/text/SimpleDateFormat.htm
> > l)
> >  - allow the  100 year-period in which 2-digit  years will be placed
> > to be specified (eg the Java SimpleDateFormat also allows this)
> >
>
> This an interesting advantage (at first) that uses some moving epoch
> to compute the century, but has IMNHO a terrific disadvantage: it will
> break unit tests as soon your code crosses the boundary of ten years
> period (like some code done last year and tested in 2011).

I think this is a *sliding* period: 80 years before the *current* year, and 20 years into the future from the *current* year.  So, presently this would be from
1911 to 2030, next year it would be 1912-2031.  I would suspect this would cover most uses, and is easy to specify.  (Birth dates might be a common exception. )

I agree that using 4-digit years is best for user input.  

David



_______________________________________________
Pharo-project mailing list
[hidden email]
http://lists.gforge.inria.fr/cgi-bin/mailman/listinfo/pharo-project

_______________________________________________
Pharo-project mailing list
[hidden email]
http://lists.gforge.inria.fr/cgi-bin/mailman/listinfo/pharo-project
Reply | Threaded
Open this post in threaded view
|

Some code! (was Re: Date fromString: '6-Jan-10')

Göran Krampe
Hi guys!

I admit not having tracked this thread in detail BUT attached is a
changeset that I dug out from the Squeak beginners list where I posted
it waaay back. The added Date class method included below shows what it
offers.

And of course, there are TONS of more cool stuff it could handle, but it
is meant to be "smart enough" for a reasonable set of cases. ...And it
was an "hour hack" ... :)

regards, Göran
-----------------------------

readFrom: inputStream pattern: pattern
        "Read a Date from the stream based on the pattern which can include the
tokens:
       
                y = A year with 1-n digits
                yy = A year with 2 digits
                yyyy = A year with 4 digits
                m = A month with 1-n digits
                mm = A month with 2 digits
                d = A day with 1-n digits
                dd = A day with 2 digits
               
        ...and any other Strings inbetween. Representing $y, $m and $d is done
using
        \y, \m and \d and slash itself with \\. Simple example patterns:

                'yyyy-mm-dd'
                'yyyymmdd'
                'yy.mm.dd'
                'y-m-d'

        A year given using only two decimals is considered to be >2000."

        | day month year patternStream char |
        patternStream := pattern readStream.
        [patternStream atEnd] whileFalse: [
                inputStream atEnd ifTrue: [^nil].
                char := patternStream next.
                char = $\
                        ifTrue: [inputStream next = patternStream next ifFalse: [^nil]]
                        ifFalse: [
                                char = $y
                                        ifTrue: [
                                                (patternStream nextMatchAll: 'yyy')
                                                        ifTrue: [year := (inputStream next: 4) asInteger]
                                                        ifFalse: [
                                                                (patternStream peekFor: $y)
                                                                        ifTrue: [
                                                                                year := (inputStream next: 2) asInteger]
                                                                        ifFalse: [
                                                                                year := Integer readFrom: inputStream]]]
                                        ifFalse: [
                                                char = $m
                                                        ifTrue: [
                                                                (patternStream peekFor: $m)
                                                                        ifTrue: [
                                                                                month := (inputStream next: 2) asInteger]
                                                                        ifFalse: [
                                                                                month := Integer readFrom: inputStream]]
                                                        ifFalse: [
                                                                char = $d
                                                                        ifTrue: [
                                                                                (patternStream peekFor: $d)
                                                                                        ifTrue: [
                                                                                                day := (inputStream next: 2) asInteger]
                                                                                        ifFalse: [
                                                                                                day := Integer readFrom: inputStream]]
                                                                        ifFalse: [
                                                                                inputStream next = char ifFalse: [^nil]]]]]].
        (year isNil | month isNil | day isNil) ifTrue: [^nil].
        ^self year: year month: month day: day! !

_______________________________________________
Pharo-project mailing list
[hidden email]
http://lists.gforge.inria.fr/cgi-bin/mailman/listinfo/pharo-project

DateReadFromPattern.3.cs.gz (1K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: [Pharo-project] Some code! (was Re: Date fromString: '6-Jan-10')

Igor Stasenko
Oh, i thought this comes from Pharo. So i copied this method and
proposed to include in trunk as well.
Not sure its integrated though (i placed it into inbox).

2010/1/21 Göran Krampe <[hidden email]>:

> Hi guys!
>
> I admit not having tracked this thread in detail BUT attached is a changeset
> that I dug out from the Squeak beginners list where I posted it waaay back.
> The added Date class method included below shows what it offers.
>
> And of course, there are TONS of more cool stuff it could handle, but it is
> meant to be "smart enough" for a reasonable set of cases. ...And it was an
> "hour hack" ... :)
>
> regards, Göran
> -----------------------------
>
> readFrom: inputStream pattern: pattern
>        "Read a Date from the stream based on the pattern which can include
> the tokens:
>
>                y = A year with 1-n digits
>                yy = A year with 2 digits
>                yyyy = A year with 4 digits
>                m = A month with 1-n digits
>                mm = A month with 2 digits
>                d = A day with 1-n digits
>                dd = A day with 2 digits
>
>        ...and any other Strings inbetween. Representing $y, $m and $d is
> done using
>        \y, \m and \d and slash itself with \\. Simple example patterns:
>
>                'yyyy-mm-dd'
>                'yyyymmdd'
>                'yy.mm.dd'
>                'y-m-d'
>
>        A year given using only two decimals is considered to be >2000."
>
>        | day month year patternStream char |
>        patternStream := pattern readStream.
>        [patternStream atEnd] whileFalse: [
>                inputStream atEnd ifTrue: [^nil].
>                char := patternStream next.
>                char = $\
>                        ifTrue: [inputStream next = patternStream next
> ifFalse: [^nil]]
>                        ifFalse: [
>                                char = $y
>                                        ifTrue: [
>                                                (patternStream nextMatchAll:
> 'yyy')
>                                                        ifTrue: [year :=
> (inputStream next: 4) asInteger]
>                                                        ifFalse: [
>
>  (patternStream peekFor: $y)
>
>  ifTrue: [
>
>    year := (inputStream next: 2) asInteger]
>
>  ifFalse: [
>
>    year := Integer readFrom: inputStream]]]
>                                        ifFalse: [
>                                                char = $m
>                                                        ifTrue: [
>
>  (patternStream peekFor: $m)
>
>  ifTrue: [
>
>    month := (inputStream next: 2) asInteger]
>
>  ifFalse: [
>
>    month := Integer readFrom: inputStream]]
>                                                        ifFalse: [
>                                                                char = $d
>
>  ifTrue: [
>
>    (patternStream peekFor: $d)
>
>            ifTrue: [
>
>                    day := (inputStream next: 2) asInteger]
>
>            ifFalse: [
>
>                    day := Integer readFrom: inputStream]]
>
>  ifFalse: [
>
>    inputStream next = char ifFalse: [^nil]]]]]].
>        (year isNil | month isNil | day isNil) ifTrue: [^nil].
>        ^self year: year month: month day: day! !
>
> _______________________________________________
> Pharo-project mailing list
> [hidden email]
> http://lists.gforge.inria.fr/cgi-bin/mailman/listinfo/pharo-project
>



--
Best regards,
Igor Stasenko AKA sig.
_______________________________________________
Pharo-project mailing list
[hidden email]
http://lists.gforge.inria.fr/cgi-bin/mailman/listinfo/pharo-project
Reply | Threaded
Open this post in threaded view
|

Re: [Pharo-project] Some code! (was Re: Date fromString: '6-Jan-10')

Stéphane Ducasse
In reply to this post by Göran Krampe
thanks goran

Stef

On Jan 21, 2010, at 11:12 AM, Göran Krampe wrote:

> Hi guys!
>
> I admit not having tracked this thread in detail BUT attached is a changeset that I dug out from the Squeak beginners list where I posted it waaay back. The added Date class method included below shows what it offers.
>
> And of course, there are TONS of more cool stuff it could handle, but it is meant to be "smart enough" for a reasonable set of cases. ...And it was an "hour hack" ... :)
>
> regards, Göran
> -----------------------------
>
> readFrom: inputStream pattern: pattern
> "Read a Date from the stream based on the pattern which can include the tokens:
>
> y = A year with 1-n digits
> yy = A year with 2 digits
> yyyy = A year with 4 digits
> m = A month with 1-n digits
> mm = A month with 2 digits
> d = A day with 1-n digits
> dd = A day with 2 digits
>
> ...and any other Strings inbetween. Representing $y, $m and $d is done using
> \y, \m and \d and slash itself with \\. Simple example patterns:
>
> 'yyyy-mm-dd'
> 'yyyymmdd'
> 'yy.mm.dd'
> 'y-m-d'
>
> A year given using only two decimals is considered to be >2000."
>
> | day month year patternStream char |
> patternStream := pattern readStream.
> [patternStream atEnd] whileFalse: [
> inputStream atEnd ifTrue: [^nil].
> char := patternStream next.
> char = $\
> ifTrue: [inputStream next = patternStream next ifFalse: [^nil]]
> ifFalse: [
> char = $y
> ifTrue: [
> (patternStream nextMatchAll: 'yyy')
> ifTrue: [year := (inputStream next: 4) asInteger]
> ifFalse: [
> (patternStream peekFor: $y)
> ifTrue: [
> year := (inputStream next: 2) asInteger]
> ifFalse: [
> year := Integer readFrom: inputStream]]]
> ifFalse: [
> char = $m
> ifTrue: [
> (patternStream peekFor: $m)
> ifTrue: [
> month := (inputStream next: 2) asInteger]
> ifFalse: [
> month := Integer readFrom: inputStream]]
> ifFalse: [
> char = $d
> ifTrue: [
> (patternStream peekFor: $d)
> ifTrue: [
> day := (inputStream next: 2) asInteger]
> ifFalse: [
> day := Integer readFrom: inputStream]]
> ifFalse: [
> inputStream next = char ifFalse: [^nil]]]]]].
> (year isNil | month isNil | day isNil) ifTrue: [^nil].
> ^self year: year month: month day: day! !
> <DateReadFromPattern.3.cs.gz>_______________________________________________
> Pharo-project mailing list
> [hidden email]
> http://lists.gforge.inria.fr/cgi-bin/mailman/listinfo/pharo-project


_______________________________________________
Pharo-project mailing list
[hidden email]
http://lists.gforge.inria.fr/cgi-bin/mailman/listinfo/pharo-project
Reply | Threaded
Open this post in threaded view
|

Re: Some code! (was Re: Date fromString: '6-Jan-10')

Stéphane Ducasse
In reply to this post by Göran Krampe
Thanks goran

        Issue 1900: Date reading from goran
        http://code.google.com/p/pharo/issues/detail?id=1900

Stef


On Jan 21, 2010, at 11:12 AM, Göran Krampe wrote:

> Hi guys!
>
> I admit not having tracked this thread in detail BUT attached is a changeset that I dug out from the Squeak beginners list where I posted it waaay back. The added Date class method included below shows what it offers.
>
> And of course, there are TONS of more cool stuff it could handle, but it is meant to be "smart enough" for a reasonable set of cases. ...And it was an "hour hack" ... :)
>
> regards, Göran
> -----------------------------
>
> readFrom: inputStream pattern: pattern
> "Read a Date from the stream based on the pattern which can include the tokens:
>
> y = A year with 1-n digits
> yy = A year with 2 digits
> yyyy = A year with 4 digits
> m = A month with 1-n digits
> mm = A month with 2 digits
> d = A day with 1-n digits
> dd = A day with 2 digits
>
> ...and any other Strings inbetween. Representing $y, $m and $d is done using
> \y, \m and \d and slash itself with \\. Simple example patterns:
>
> 'yyyy-mm-dd'
> 'yyyymmdd'
> 'yy.mm.dd'
> 'y-m-d'
>
> A year given using only two decimals is considered to be >2000."
>
> | day month year patternStream char |
> patternStream := pattern readStream.
> [patternStream atEnd] whileFalse: [
> inputStream atEnd ifTrue: [^nil].
> char := patternStream next.
> char = $\
> ifTrue: [inputStream next = patternStream next ifFalse: [^nil]]
> ifFalse: [
> char = $y
> ifTrue: [
> (patternStream nextMatchAll: 'yyy')
> ifTrue: [year := (inputStream next: 4) asInteger]
> ifFalse: [
> (patternStream peekFor: $y)
> ifTrue: [
> year := (inputStream next: 2) asInteger]
> ifFalse: [
> year := Integer readFrom: inputStream]]]
> ifFalse: [
> char = $m
> ifTrue: [
> (patternStream peekFor: $m)
> ifTrue: [
> month := (inputStream next: 2) asInteger]
> ifFalse: [
> month := Integer readFrom: inputStream]]
> ifFalse: [
> char = $d
> ifTrue: [
> (patternStream peekFor: $d)
> ifTrue: [
> day := (inputStream next: 2) asInteger]
> ifFalse: [
> day := Integer readFrom: inputStream]]
> ifFalse: [
> inputStream next = char ifFalse: [^nil]]]]]].
> (year isNil | month isNil | day isNil) ifTrue: [^nil].
> ^self year: year month: month day: day! !
> <DateReadFromPattern.3.cs.gz>_______________________________________________
> Pharo-project mailing list
> [hidden email]
> http://lists.gforge.inria.fr/cgi-bin/mailman/listinfo/pharo-project


_______________________________________________
Pharo-project mailing list
[hidden email]
http://lists.gforge.inria.fr/cgi-bin/mailman/listinfo/pharo-project
12