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?
Date today yyyymmdd. "prints '2013-10-30'" :) On Wed, Oct 30, 2013 at 3:55 PM, blake <dsblakewatson@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?
On 30 October 2013 20:55, blake <dsblakewatson@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
Why not "%04d%02d%02d" % [d.year, d.month, 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?
Chris has already given a near solution. You just need to "select: #isDigit" on the end of his answer to get exactly what you want. frank
Or: Date today printFormat: #(3 2 1 0 1 1 2) On Wed, Oct 30, 2013 at 2:13 PM, Frank Shearar <frank.shearar@gmail.com>wrote:
On 30 October 2013 20:55, blake <dsblakewatson@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
Why not "%04d%02d%02d" % [d.year, d.month, 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?
Chris has already given a near solution. You just need to "select: #isDigit" on the end of his answer to get exactly what you want.
frank
Date today printFormat: #(3 2 1 0 1 1 2)
Wait, that's what I was trying for, initially (although it's not super-clear). I did not get that I could use 0 (or apparently a bunch of other characters) as a separator. I didn't see this code at the bottom of printOn:format: (formatArray at: 4) ~= 0
Why not "%04d%02d%02d" % [d.year, d.month, d.day] ?
Probably because I'm not super-comfortable with all of Ruby's many, many, many features.
Chris has already given a near solution. You just need to "select: #isDigit" on the end of his answer to get exactly what you want.<<
I figured there was a good way to use "select:" but couldn't think of it, thanks. Date today yyyymmdd select: #isDigit is not bad, though I'd think you should be able to set the separator somehow (or remove it, as in my case).
Hi Blake, On 30 Oct 2013, at 21:55, blake <dsblakewatson@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
Why not a more discriminating example, like '19991231'? 2013/10/30 Sven Van Caekenberghe <sven@stfx.eu>
Hi Blake,
On 30 Oct 2013, at 21:55, blake <dsblakewatson@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
On 30 Oct 2013, at 22:46, Nicolas Cellier <nicolas.cellier.aka.nice@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@stfx.eu> Hi Blake,
On 30 Oct 2013, at 21:55, blake <dsblakewatson@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
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%... p187 - 1983) Phil On Wed, Oct 30, 2013 at 11:11 PM, Sven Van Caekenberghe <sven@stfx.eu>wrote:
On 30 Oct 2013, at 22:46, Nicolas Cellier < nicolas.cellier.aka.nice@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@stfx.eu> Hi Blake,
On 30 Oct 2013, at 21:55, blake <dsblakewatson@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
Thanks, everybody! On Thu, Oct 31, 2013 at 1:00 AM, phil@highoctane.be <phil@highoctane.be>wrote:
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%... p187 - 1983)
Phil
On Wed, Oct 30, 2013 at 11:11 PM, Sven Van Caekenberghe <sven@stfx.eu>wrote:
On 30 Oct 2013, at 22:46, Nicolas Cellier < nicolas.cellier.aka.nice@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@stfx.eu> Hi Blake,
On 30 Oct 2013, at 21:55, blake <dsblakewatson@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
Good to know, thanks. That looks cool. On Wed, Oct 30, 2013 at 2:39 PM, Sven Van Caekenberghe <sven@stfx.eu> wrote:
Hi Blake,
On 30 Oct 2013, at 21:55, blake <dsblakewatson@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
participants (8)
-
blake -
Chris Cunningham -
Chris Muller -
David T. Lewis -
Frank Shearar -
Nicolas Cellier -
phil@highoctane.be -
Sven Van Caekenberghe