iterating over a range of dates
Hi, I'm trying to iterate over a range of dates and print them out. Here's how I am doing it in Ruby: 'require 'date' now = Date.parse('2011-07-11') end_date = Date.parse('2011-12-31') (now..end_date).each do |d| # print out a date that looks like: # Monday, July 11, 2011 puts "#{d.strftime('%A, %B %d, %Y')}" end I've been looking at the docs for Data.Time.Format and Data.Time.Calendar and am a bit puzzled at where to begin. Anyone have ideas? I've not found many (any?) examples of time and date manipulation in Haskell. RW
On 9 July 2011 09:33, Rolf Hanson <rolf.hanson@gmail.com> wrote:
Hi, I'm trying to iterate over a range of dates and print them out. Here's how I am doing it in Ruby:
'require 'date'
now = Date.parse('2011-07-11') end_date = Date.parse('2011-12-31')
(now..end_date).each do |d| # print out a date that looks like: # Monday, July 11, 2011 puts "#{d.strftime('%A, %B %d, %Y')}" end
I've been looking at the docs for Data.Time.Format and Data.Time.Calendar and am a bit puzzled at where to begin. Anyone have ideas? I've not found many (any?) examples of time and date manipulation in Haskell.
RW _______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners
A good starting point would be the Enum instance of Day in Data.Time.Calendar, which lets you use the [a..b] syntactic sugar to do a range. fromGregorian will get you from a year, month, and day to the Day type: Prelude Data.Time.Calendar> [fromGregorian 2011 07 09..fromGregorian 2011 07 20] [2011-07-09,2011-07-10,2011-07-11,2011-07-12,2011-07-13,2011-07-14,2011-07-15,2011-07-16,2011-07-17,2011-07-18,2011-07-19,2011-07-20] This is equivalent to enumFromTo (fromGregorian 2011 07 09) (fromGregorian 2011 07 20) formatTime in Data.Time.Format will probably get you the rest of the way. Hope that helps, Alex
import Data.Time.LocalTime import Data.Time.Format import System.Locale now :: Maybe LocalTime -- Change this to get different types of values (days, etc). now = parseTime defaultTimeLocale "%F" "2011-07-11" main = putStrLn $ case now of Nothing -> "Failed Parse" Just x -> (formatTime defaultTimeLocale "%F" x) On Sat, Jul 9, 2011 at 12:33 PM, Rolf Hanson <rolf.hanson@gmail.com> wrote:
Hi, I'm trying to iterate over a range of dates and print them out. Here's how I am doing it in Ruby:
'require 'date'
now = Date.parse('2011-07-11') end_date = Date.parse('2011-12-31')
(now..end_date).each do |d| # print out a date that looks like: # Monday, July 11, 2011 puts "#{d.strftime('%A, %B %d, %Y')}" end
I've been looking at the docs for Data.Time.Format and Data.Time.Calendar and am a bit puzzled at where to begin. Anyone have ideas? I've not found many (any?) examples of time and date manipulation in Haskell.
RW _______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners
Sorry, I misunderstood your ruby. The Day datatype has an ordinal instance for this purpose and also a convenient show instance in case you wanted it. import Data.Time.Calendar import Data.Time.Format import System.Locale main = let (Just now) = (parseTime defaultTimeLocale "%F" "2011-07-11" :: Maybe Day) (Just end) = (parseTime defaultTimeLocale "%F" "2011-07-15" :: Maybe Day) in mapM_ print [now..end] On Sat, Jul 9, 2011 at 1:14 PM, David McBride <dmcbride@neondsl.com> wrote:
import Data.Time.LocalTime import Data.Time.Format import System.Locale
now :: Maybe LocalTime -- Change this to get different types of values (days, etc). now = parseTime defaultTimeLocale "%F" "2011-07-11"
main = putStrLn $ case now of Nothing -> "Failed Parse" Just x -> (formatTime defaultTimeLocale "%F" x)
On Sat, Jul 9, 2011 at 12:33 PM, Rolf Hanson <rolf.hanson@gmail.com> wrote:
Hi, I'm trying to iterate over a range of dates and print them out. Here's how I am doing it in Ruby:
'require 'date'
now = Date.parse('2011-07-11') end_date = Date.parse('2011-12-31')
(now..end_date).each do |d| # print out a date that looks like: # Monday, July 11, 2011 puts "#{d.strftime('%A, %B %d, %Y')}" end
I've been looking at the docs for Data.Time.Format and Data.Time.Calendar and am a bit puzzled at where to begin. Anyone have ideas? I've not found many (any?) examples of time and date manipulation in Haskell.
RW _______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners
participants (3)
-
Alexander Dunlap -
David McBride -
Rolf Hanson