Adde Nilsson | 18 Aug 19:27 2012
Picon

Adding to / subtracting from a LocalTime?

Hi.
How do you add to or subtract from a LocalTime? 
I'm trying to subtract a second from a LocalTime value but the only API's I can find act only on the TimeOfDay part.

subSec :: LocalTime -> LocalTime
subSec (LocalTime d t) = LocalTime d $ timeToTimeOfDay ((timeOfDayToTime t) - (secondsToDiffTime 1))

I'm obviously on the wrong track as this looks way too complex for what it's trying to achieve.
Calling with '2012-08-18 00:00:00' gives '2012-08-18 -01:59:59' which isn't very helpful. How do I get to '2012-08-17 23:59:59'?

Thanks,
Adde
_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe <at> haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe
Roman Cheplyaka | 19 Aug 19:42 2012

Re: Adding to / subtracting from a LocalTime?

On Sat, Aug 18, 2012 at 8:27 PM, Adde Nilsson <trialcodr <at> gmail.com> wrote:
Hi.
How do you add to or subtract from a LocalTime? 
I'm trying to subtract a second from a LocalTime value but the only API's I can find act only on the TimeOfDay part.

subSec :: LocalTime -> LocalTime
subSec (LocalTime d t) = LocalTime d $ timeToTimeOfDay ((timeOfDayToTime t) - (secondsToDiffTime 1))

I'm obviously on the wrong track as this looks way too complex for what it's trying to achieve.
Calling with '2012-08-18 00:00:00' gives '2012-08-18 -01:59:59' which isn't very helpful. How do I get to '2012-08-17 23:59:59'?

The time-lens [1] package should do what you need.

[1]: http://hackage.haskell.org/package/time-lens

Roman
_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe <at> haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe
Paolino | 18 Aug 19:53 2012
Picon

Re: Adding to / subtracting from a LocalTime?

Hello.

This is a part of a solution, with explicit TimeZone

Prelude Data.Time> :t \z s t -> utcToLocalTime z $ addUTCTime (fromIntegral s) (localTimeToUTC z t)
\z s t -> utcToLocalTime z $ addUTCTime (fromIntegral s) (localTimeToUTC z t)
  :: Integral a => TimeZone -> a -> LocalTime -> LocalTime

where the Integral arguments is in seconds, by docs

regards
paolino



2012/8/18 Adde Nilsson <trialcodr <at> gmail.com>
Hi.
How do you add to or subtract from a LocalTime? 
I'm trying to subtract a second from a LocalTime value but the only API's I can find act only on the TimeOfDay part.

subSec :: LocalTime -> LocalTime
subSec (LocalTime d t) = LocalTime d $ timeToTimeOfDay ((timeOfDayToTime t) - (secondsToDiffTime 1))

I'm obviously on the wrong track as this looks way too complex for what it's trying to achieve.
Calling with '2012-08-18 00:00:00' gives '2012-08-18 -01:59:59' which isn't very helpful. How do I get to '2012-08-17 23:59:59'?

Thanks,
Adde

_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe <at> haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe <at> haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe
Adde Nilsson | 19 Aug 00:05 2012
Picon

Re: Adding to / subtracting from a LocalTime?

Ok, do you know of any way to add/subtract without converting to UTC and back?


On Sat, Aug 18, 2012 at 7:53 PM, Paolino <paolo.veronelli <at> gmail.com> wrote:
Hello.

This is a part of a solution, with explicit TimeZone

Prelude Data.Time> :t \z s t -> utcToLocalTime z $ addUTCTime (fromIntegral s) (localTimeToUTC z t)
\z s t -> utcToLocalTime z $ addUTCTime (fromIntegral s) (localTimeToUTC z t)
  :: Integral a => TimeZone -> a -> LocalTime -> LocalTime

where the Integral arguments is in seconds, by docs

regards
paolino



2012/8/18 Adde Nilsson <trialcodr <at> gmail.com>
Hi.
How do you add to or subtract from a LocalTime? 
I'm trying to subtract a second from a LocalTime value but the only API's I can find act only on the TimeOfDay part.

subSec :: LocalTime -> LocalTime
subSec (LocalTime d t) = LocalTime d $ timeToTimeOfDay ((timeOfDayToTime t) - (secondsToDiffTime 1))

I'm obviously on the wrong track as this looks way too complex for what it's trying to achieve.
Calling with '2012-08-18 00:00:00' gives '2012-08-18 -01:59:59' which isn't very helpful. How do I get to '2012-08-17 23:59:59'?

Thanks,
Adde

_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe <at> haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe



_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe <at> haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe
Brandon Allbery | 19 Aug 00:11 2012
Picon

Re: Adding to / subtracting from a LocalTime?

On Sat, Aug 18, 2012 at 6:05 PM, Adde Nilsson <trialcodr <at> gmail.com> wrote:
Ok, do you know of any way to add/subtract without converting to UTC and back?

I'm not sure I'd do that in any environment, since usually libraries don't deal with the result crossing a daylight/summer time change (and those that do, surprise surprise, normalize to UTC).

--
brandon s allbery                                      allbery.b <at> gmail.com
wandering unix systems administrator (available)     (412) 475-9364 vm/sms

_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe <at> haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe
Adde Nilsson | 19 Aug 01:14 2012
Picon

Re: Adding to / subtracting from a LocalTime?

Didn't know, thanks.
Back in Haskell land my conclusion is that if you're planning on doing pure date calculations you have to pass a TimeZone as well.

/Adde

On Sun, Aug 19, 2012 at 12:11 AM, Brandon Allbery <allbery.b <at> gmail.com> wrote:
On Sat, Aug 18, 2012 at 6:05 PM, Adde Nilsson <trialcodr <at> gmail.com> wrote:
Ok, do you know of any way to add/subtract without converting to UTC and back?

I'm not sure I'd do that in any environment, since usually libraries don't deal with the result crossing a daylight/summer time change (and those that do, surprise surprise, normalize to UTC).

--
brandon s allbery                                      allbery.b <at> gmail.com
wandering unix systems administrator (available)     (412) 475-9364 vm/sms


_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe <at> haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe
Yitzchak Gale | 19 Aug 16:32 2012

Re: Adding to / subtracting from a LocalTime?

Adde Nilsson wrote:
>>> Ok, do you know of any way to add/subtract without converting to UTC and
>>> back?

Brandon Allbery wrote:
>> I'm not sure I'd do that in any environment, since usually libraries don't
>> deal with the result crossing a daylight/summer time change (and those that
>> do, surprise surprise, normalize to UTC).

> Didn't know, thanks.
> Back in Haskell land my conclusion is that if you're planning on doing pure
> date calculations you have to pass a TimeZone as well.

For that to work near clock changes, you need to
have the latest summer time/DST policies coded
in your app so that you can pass the correct TimeZone.
You also need hand-coded logic to handle clock
times that occur twice, and clock times that never occur at
all, due to changes of clock.

<shameless-plug>

An alternative is to use the timezone-series package.
Your timezone series knows about the latest
clock change policies if you create it from an
up-to-date Olson timezone file using the
timezone-olson package.

http://hackage.haskell.org/package/timezone-series
http://hackage.haskell.org/package/timezone-olson

</shameless-plug>

Regards,
Yitz

Gmane