[Pharo-dev] Formatting dates

phil at highoctane.be phil at highoctane.be
Thu Oct 31 04:00:51 EDT 2013


Closest match is with the Printf package (http://www.squeaksource.com/Printf
)

example:

now := DateAndTime now.
filename := '%s-%04d%02d%02d_%02d%02d.ston' printf: { self name. now year.
now month. now dayOfMonth. now hour. now minutes.}.

I am coming from C, so, yes, I like printf. (Can't help thinking about
PRINT USING "#####.##" of old :-)
http://www.colorcomputerarchive.com/coco/Documents/Manuals/Hardware/Getting%20Started%20With%20Extended%20Color%20Basic%20(Tandy).pdf
p187
- 1983)

Phil



On Wed, Oct 30, 2013 at 11:11 PM, Sven Van Caekenberghe <sven at stfx.eu>wrote:

>
> On 30 Oct 2013, at 22:46, Nicolas Cellier <
> nicolas.cellier.aka.nice at gmail.com> wrote:
>
> > Why not a more discriminating example, like '19991231’?
>
> From the class comment:
>
>> You instanciate me by specifying the textual format by example, based on a
> #reference timetamp.
>
> Each component of the example representation is numbered from largest to
> smallest unit, 1=year, 2=month, 3=dayInMonth, 4=hour (16 in 24 hour
> format), 5=minute and 6=second, as in the ISO representation:
> 2001-02-03T16:05:06Z which is a Saterday. Example format strings can be
> found in my class accessing protocol or in the unit tests.
>>
> But of course, internally there is less magic: it are just specific
> keys/tokens that are recognised ;-) The example is kind of compiled though
> to make it more efficient should you reuse the format.
>
> > 2013/10/30 Sven Van Caekenberghe <sven at stfx.eu>
> > Hi Blake,
> >
> > On 30 Oct 2013, at 21:55, blake <dsblakewatson at gmail.com> wrote:
> >
> > > Heya, guys--
> > >
> > > Converting a Ruby program to Pharo, and one of the things this program
> does is write a file out by date. The format is "yyyymmdd" (which is my
> preferred naming convention). In Ruby I have:
> > >
> > > "%04d" % d.year + "%02d" % d.month + "%02d" % d.day
> > >
> > > In Smalltalk, the closest I can seem to get to this is:
> > >
> > > d year asString, (d monthIndex asString padLeftTo: 2 with: $0), (d
> dayOfMonth asString padLeftTo: 2 with: $0)
> > >
> > > ...which seems sort verbose. Anyone have any more elegant solutions?
> >
> > Using the ‘by example’ formatter/parser that is part of the ZTimestamp
> package (can be loaded using the Configuration Browser), you could say:
> >
> >   (ZTimestampFormat fromString: '20010203') format: Date today.
> >
> > Although it is called ZTimestampFormat, it can actually work with Date,
> Time, DateAndTime as well.
> >
> > Sven
> >
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.pharo.org/pipermail/pharo-dev_lists.pharo.org/attachments/20131031/bfa22578/attachment-0002.html>


More information about the Pharo-dev mailing list