Re: [Haskell-cafe] GHC 7.0.3 / Win32: Data.Time library?

2011-06-15 Thread Yitzchak Gale
Dmitri O.Kondratiev wrote:
> I have:
> ds1 = "10/11/2009 7:04:28 PM"
> ds2 = "10/17/2009 8:48:29 AM"
> t1 = readTime defaultTimeLocale "%m/%d/%Y %l:%M:%S %p" ds1 :: UTCTime
> t2 = readTime defaultTimeLocale "%m/%d/%Y %l:%M:%S %p" ds2 :: UTCTime
> dif = diffUTCTime t2 t1
>
> I need to:
> 1) Find how many complete days elapsed between t1 and t2

It depends what you mean by "complete days". If you just mean
how many periods of 24 hours of UTC, without caring about things
like local clock changes for summer time, then it's just this:

floor $ dif / (24 * 60 * 60)

Otherwise, see below.

> 2) Find UTCTime for a moment 6 days after t1,  in other words time equal to
> t1 + 6 * 24 hours.

addUTCTime (6 * 24 * 60 * 60) t1

> Questions:
> 1) Is there any library function that will convert (diff = diffUTCTime t2
> t1) to a data structure similar to a tuple (days, hours, mins, secs) where
> 'days' is a whole number of days in my 'diff'' interval, and similar for
> 'hours', 'mins' and 'secs' in the tuple above?

If what you mean is amounts of UTC time, then just use the
diffs as floating point numbers. Divide and floor, as above.
You don't need any special functions.

As you can see, things are easy as long as you stay in UTC.
Often that is a little sloppy, but if it is good enough for
your program, use it.

> 2) What is the 'right way' of adding days to UTCTime? Should I add just
> equivalent number of seconds or should I convert UTCTime to
> Data.Time.Calendar.Day type first and then use 'addDays' function?

If what you need is "days", "hours", etc. as concepts of local time,
and you care about things not going wrong in the unusual case of a
change of clock, then yes, you need to convert to local time and do
your calculations there.

> 3) How to convert UTCTime to Data.Time.Calendar.Day and back
> to UTCTime?

If you stay in UTC, it's easy: a UTCTime is made up
of a Day and a DiffTime.

Suppose you need "days" in a certain time zone,
not UTC. If you already know for each of your input
times whether it is standard time or summer time
in that time zone, then here is an outline of how:

1. Create a TimeZone for each of standard time
and summer time.

2. Use utcToLocalTime with the appropriate
TimeZone to convert each input UTCTime to a
LocalTime.

3. Use diffDays. If all you care about is days,
compare the TimeOfDay with < or > to see if
you need to adjust the number of days by one.
Or work with the hours, minutes, and seconds
of the TimeOfDay directly.

If you don't already know whether your input times
are in summer time, then you need the timezone-olson
and timezone-series packages. Here is an outline:

1. Get the Olson time zone file provided by your
operating system for the time zone in question.
On Linux and Mac OS X, it's in /usr/share/zoneinfo.

On Windows it's in a binary format in the registry,
Unfortunately, we don't have a parser for that format
yet, so your best bet for now is to get the Olson files
you need from a non-Windows computer.

2. Use a function in the timezone-olson package to
read the Olson file and get a TimeZoneSeries.

3. Use utcToLocalTime' from the timezone-series
package to get a LocalTime for each of your input
times. Then continue as in #3 above.

Actually, we started out with parsing a time string,
and we have been assuming all along that it
represented UTC. As always, that is the easy case.
You're also OK if the time string has an explicit
offset from UTC, like "-0400" for EDT in the U.S.

Otherwise, you'll have to parse those strings as
LocalTime, not UTCTime. If the LocalTime is
what you need, great. If not, use a TimeZone or
TimeZoneSeries, as above, to get back to UTCTime.

Hope this helps,
Yitz

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] GHC 7.0.3 / Win32: Data.Time library?

2011-06-15 Thread Dmitri O.Kondratiev
Still have trouble iterating days (
I have:
ds1 = "10/11/2009 7:04:28 PM"
ds2 = "10/17/2009 8:48:29 AM"
t1 = readTime defaultTimeLocale "%m/%d/%Y %l:%M:%S %p" ds1 :: UTCTime
t2 = readTime defaultTimeLocale "%m/%d/%Y %l:%M:%S %p" ds2 :: UTCTime
dif = diffUTCTime t2 t1

I need to:
1) Find how many complete days elapsed between t1 and t2
2) Find UTCTime for a moment 6 days after t1,  in other words time equal to
t1 + 6 * 24 hours.

Questions:
1) Is there any library function that will convert (diff = diffUTCTime t2
t1) to a data structure similar to a tuple (days, hours, mins, secs) where
'days' is a whole number of days in my 'diff'' interval, and similar for
'hours', 'mins' and 'secs' in the tuple above?
2) What is the 'right way' of adding days to UTCTime? Should I add just
equivalent number of seconds or should I convert UTCTime to
Data.Time.Calendar.Day type first and then use 'addDays' function?
3) How to convert UTCTime to Data.Time.Calendar.Day and back to UTCTime?

Thanks!


On Wed, Jun 15, 2011 at 12:55 PM, Dmitri O.Kondratiev wrote:

>
> Hi, Yitz!
> Your example puts scattered around pieces in place, thanks a lot!
>
> On Wed, Jun 15, 2011 at 9:49 AM, Yitzchak Gale  wrote:
>
>> Dmitri O.Kondratiev wrote:
>> > It would also help to see a simple example of parsing "10/11/2009
>> 7:04:28
>> > PM" to  time and date objects.
>>
>> Let's assume that 10/11/2009 means October 11, as in the U.S.
>> Then you can use:
>>
>> import System.Locale (defaultTimeLocale)
>> import Data.Time
>>
>> thatMoment :: Maybe UTCTime
>> thatMoment = parseTime defaultTimeLocale "%m/%d/%Y %l:%M:%S %p"
>> "10/11/2009 7:04:28 PM"
>>
>> Then use diffUTCTime to subtract two UTCTime and
>> get the amount of time between them. The resulting object
>> can then be used as if it is a regular floating point number
>> in units of seconds, or you can use the functions in Data.Time
>> that treat it specially as an amount of time.
>>
>> There are many options for the format string and locale that will
>> affect how the date is parsed - the order of month and day,
>> leading zeros or leading spaces, upper case or lower case AM or PM
>> (or 24-hour clock), etc. You can also get different behavior on
>> parsing failure by using readTime or readsTime instead of parseTime.
>>
>> For details, see:
>>
>>
>> http://www.haskell.org/ghc/docs/7.0.3/html/libraries/time-1.2.0.3/Data-Time-Format.html
>>
>> http://www.haskell.org/ghc/docs/7.0.3/html/libraries/old-locale-1.0.0.2/System-Locale.html#t:TimeLocale
>>
>> http://www.haskell.org/ghc/docs/7.0.3/html/libraries/old-locale-1.0.0.2/src/System-Locale.html#TimeLocale
>>
>> As an example of modifying the locale, let's say you want to use "a" and
>> "p"
>> instead of "AM" and "PM", as is customary in some parts of the world.
>> Then you can say:
>>
>> myLocale = defaultTimeLocale {amPm = ("a","p")}
>>
>> and then use myLocale instead of defaultTimeLocale.
>>
>> Hope this helps,
>> Yitz
>>
>
>
>
>
>
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] GHC 7.0.3 / Win32: Data.Time library?

2011-06-15 Thread Dmitri O.Kondratiev
Hi, Yitz!
Your example puts scattered around pieces in place, thanks a lot!

On Wed, Jun 15, 2011 at 9:49 AM, Yitzchak Gale  wrote:

> Dmitri O.Kondratiev wrote:
> > It would also help to see a simple example of parsing "10/11/2009 7:04:28
> > PM" to  time and date objects.
>
> Let's assume that 10/11/2009 means October 11, as in the U.S.
> Then you can use:
>
> import System.Locale (defaultTimeLocale)
> import Data.Time
>
> thatMoment :: Maybe UTCTime
> thatMoment = parseTime defaultTimeLocale "%m/%d/%Y %l:%M:%S %p"
> "10/11/2009 7:04:28 PM"
>
> Then use diffUTCTime to subtract two UTCTime and
> get the amount of time between them. The resulting object
> can then be used as if it is a regular floating point number
> in units of seconds, or you can use the functions in Data.Time
> that treat it specially as an amount of time.
>
> There are many options for the format string and locale that will
> affect how the date is parsed - the order of month and day,
> leading zeros or leading spaces, upper case or lower case AM or PM
> (or 24-hour clock), etc. You can also get different behavior on
> parsing failure by using readTime or readsTime instead of parseTime.
>
> For details, see:
>
>
> http://www.haskell.org/ghc/docs/7.0.3/html/libraries/time-1.2.0.3/Data-Time-Format.html
>
> http://www.haskell.org/ghc/docs/7.0.3/html/libraries/old-locale-1.0.0.2/System-Locale.html#t:TimeLocale
>
> http://www.haskell.org/ghc/docs/7.0.3/html/libraries/old-locale-1.0.0.2/src/System-Locale.html#TimeLocale
>
> As an example of modifying the locale, let's say you want to use "a" and
> "p"
> instead of "AM" and "PM", as is customary in some parts of the world.
> Then you can say:
>
> myLocale = defaultTimeLocale {amPm = ("a","p")}
>
> and then use myLocale instead of defaultTimeLocale.
>
> Hope this helps,
> Yitz
>
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] GHC 7.0.3 / Win32: Data.Time library?

2011-06-14 Thread Yitzchak Gale
Dmitri O.Kondratiev wrote:
> It would also help to see a simple example of parsing "10/11/2009 7:04:28
> PM" to  time and date objects.

Let's assume that 10/11/2009 means October 11, as in the U.S.
Then you can use:

import System.Locale (defaultTimeLocale)
import Data.Time

thatMoment :: Maybe UTCTime
thatMoment = parseTime defaultTimeLocale "%m/%d/%Y %l:%M:%S %p"
"10/11/2009 7:04:28 PM"

Then use diffUTCTime to subtract two UTCTime and
get the amount of time between them. The resulting object
can then be used as if it is a regular floating point number
in units of seconds, or you can use the functions in Data.Time
that treat it specially as an amount of time.

There are many options for the format string and locale that will
affect how the date is parsed - the order of month and day,
leading zeros or leading spaces, upper case or lower case AM or PM
(or 24-hour clock), etc. You can also get different behavior on
parsing failure by using readTime or readsTime instead of parseTime.

For details, see:

http://www.haskell.org/ghc/docs/7.0.3/html/libraries/time-1.2.0.3/Data-Time-Format.html
http://www.haskell.org/ghc/docs/7.0.3/html/libraries/old-locale-1.0.0.2/System-Locale.html#t:TimeLocale
http://www.haskell.org/ghc/docs/7.0.3/html/libraries/old-locale-1.0.0.2/src/System-Locale.html#TimeLocale

As an example of modifying the locale, let's say you want to use "a" and "p"
instead of "AM" and "PM", as is customary in some parts of the world.
Then you can say:

myLocale = defaultTimeLocale {amPm = ("a","p")}

and then use myLocale instead of defaultTimeLocale.

Hope this helps,
Yitz

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] GHC 7.0.3 / Win32: Data.Time library?

2011-06-14 Thread Andrew Coppin

On 14/06/2011 01:41 PM, Dmitri O.Kondratiev wrote:

Yes, thanks. I just wonder why documentation for this particular module
(Data.Time) is missing from GHC, 7.0.3 "Haskell Hierarchical Libraries"
documentation generated for Win32. Really strange ...


There are several packages that come with GHC, but don't show up in the 
Win32 library index. I don't know why, but it's been happening for a 
long time now. Apparently this is supposed to be fixed soon though.


___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] GHC 7.0.3 / Win32: Data.Time library?

2011-06-14 Thread Dmitri O.Kondratiev
It would also help to see a simple example of parsing "10/11/2009 7:04:28
PM" to  time and date objects.

On Tue, Jun 14, 2011 at 4:41 PM, Dmitri O.Kondratiev wrote:

> Yes, thanks. I just wonder why documentation for this particular module
> (Data.Time) is missing from GHC, 7.0.3 "Haskell Hierarchical Libraries"
> documentation generated for Win32. Really strange ...
>
> On Tue, Jun 14, 2011 at 4:10 PM, Dmitry Olshansky 
> wrote:
>
>> Could you look at
>> http://hackage.haskell.org/packages/archive/time/1.2.0.5/doc/html/Data-Time-Format.html
>> ?
>> Is it enough?
>>
>>
>>
>> 2011/6/14 Dmitri O.Kondratiev 
>>
>>>  Sorry for typo - I need subtract dates (no 'abstracting') :
>>>
>>>
>>> On Tue, Jun 14, 2011 at 3:49 PM, Dmitri O.Kondratiev 
>>> wrote:
>>>
 It looks like GHC, 7.0.3 "Haskell Hierarchical Libraries" documentation
 generated for Win32, that goes with Haskell Platform installation package,
 does not have a section on Data.Time module.
 How  can that be?

 I need to convert a string of the form ",10/11/2009 7:04:28 PM" to
 Haskell type that can be used for comparing and abstracting dates.
 What is the simplest way to day that?

 Thanks!


>>>
>>>
>>>
>>>
>
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] GHC 7.0.3 / Win32: Data.Time library?

2011-06-14 Thread Dmitri O.Kondratiev
Yes, thanks. I just wonder why documentation for this particular module
(Data.Time) is missing from GHC, 7.0.3 "Haskell Hierarchical Libraries"
documentation generated for Win32. Really strange ...

On Tue, Jun 14, 2011 at 4:10 PM, Dmitry Olshansky wrote:

> Could you look at
> http://hackage.haskell.org/packages/archive/time/1.2.0.5/doc/html/Data-Time-Format.html
> ?
> Is it enough?
>
>
>
> 2011/6/14 Dmitri O.Kondratiev 
>
>> Sorry for typo - I need subtract dates (no 'abstracting') :
>>
>>
>> On Tue, Jun 14, 2011 at 3:49 PM, Dmitri O.Kondratiev 
>> wrote:
>>
>>> It looks like GHC, 7.0.3 "Haskell Hierarchical Libraries" documentation
>>> generated for Win32, that goes with Haskell Platform installation package,
>>> does not have a section on Data.Time module.
>>> How  can that be?
>>>
>>> I need to convert a string of the form ",10/11/2009 7:04:28 PM" to
>>> Haskell type that can be used for comparing and abstracting dates.
>>> What is the simplest way to day that?
>>>
>>> Thanks!
>>>
>>>
>>
>>
>>
>>
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] GHC 7.0.3 / Win32: Data.Time library?

2011-06-14 Thread Dmitry Olshansky
Could you look at
http://hackage.haskell.org/packages/archive/time/1.2.0.5/doc/html/Data-Time-Format.html
?
Is it enough?



2011/6/14 Dmitri O.Kondratiev 

> Sorry for typo - I need subtract dates (no 'abstracting') :
>
>
> On Tue, Jun 14, 2011 at 3:49 PM, Dmitri O.Kondratiev wrote:
>
>> It looks like GHC, 7.0.3 "Haskell Hierarchical Libraries" documentation
>> generated for Win32, that goes with Haskell Platform installation package,
>> does not have a section on Data.Time module.
>> How  can that be?
>>
>> I need to convert a string of the form ",10/11/2009 7:04:28 PM" to Haskell
>> type that can be used for comparing and abstracting dates.
>> What is the simplest way to day that?
>>
>> Thanks!
>>
>>
>
>
>
> ___
> Haskell-Cafe mailing list
> Haskell-Cafe@haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>
>
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] GHC 7.0.3 / Win32: Data.Time library?

2011-06-14 Thread Dmitri O.Kondratiev
Sorry for typo - I need subtract dates (no 'abstracting') :

On Tue, Jun 14, 2011 at 3:49 PM, Dmitri O.Kondratiev wrote:

> It looks like GHC, 7.0.3 "Haskell Hierarchical Libraries" documentation
> generated for Win32, that goes with Haskell Platform installation package,
> does not have a section on Data.Time module.
> How  can that be?
>
> I need to convert a string of the form ",10/11/2009 7:04:28 PM" to Haskell
> type that can be used for comparing and abstracting dates.
> What is the simplest way to day that?
>
> Thanks!
>
>
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


[Haskell-cafe] GHC 7.0.3 / Win32: Data.Time library?

2011-06-14 Thread Dmitri O.Kondratiev
It looks like GHC, 7.0.3 "Haskell Hierarchical Libraries" documentation
generated for Win32, that goes with Haskell Platform installation package,
does not have a section on Data.Time module.
How  can that be?

I need to convert a string of the form ",10/11/2009 7:04:28 PM" to Haskell
type that can be used for comparing and abstracting dates.
What is the simplest way to day that?

Thanks!
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe