Hi, I'd like to write a function that extracts the date from a DateTime object returns a new DateTime object with that date but time set to 00:00:00. The input will be in unixtime due to other requirements. When I use the following
func test(unixTime: int64): bool =
let t1 = local(fromUnix(unixTime))
# let t = initDateTime(t1.monthday, t1.month, t1.year, 0, 0, 0).toTime.toUnix
local() leads to side effects. Using instead
parse(format(fromUnix(unixTime), "yyyy-MM-dd"), "yyyy-MM-dd").toTime.toUnix
also leads to side effects and is slower anyways.
Is there a way to circumvent these side effects such that I can use a func instead of proc? Thanks!
I think it leads to side effects because it uses clib's time functions. I have a pure nim implementation of times, calendars and timezones here: https://github.com/treeform/chrono
import chrono
proc test1(unixTime: int64): bool =
var
t = Timestamp(unixTime) # in seconds?
c = t.calendar
c.toStartOf(Day)
echo c.ts
echo c.formatIso
func test(unixTime: int64): bool =
var c = Timestamp(unixTime).calendar # convert to calendar
c.toStartOf(Day) # rewind calendar to start of day
c.ts.float == 0 # convert calendar to timestmap, then to float
discard test1(1233456789)
discard test(1233456789)
But honestly it might be easier to just quantize your thing by a day if you don't care about timezones and daylight savings.
unixTime - unixTime mod (24*60*60)
To circumvent the effect system, you can use this (since 0.20.0):
func test(unixTime: int64): bool =
{.noSideEffect.}:
let t1 = local(fromUnix(unixTime))
"moon rise and set time" oh I have another library for you:
https://github.com/treeform/orbits
You can see adapt JPL Horizon api to get the exact "moon rise and set time" times directly from Nasa.
You right about chrono and miniz. Miniz is just there to make timezone data smaller. I should make it optional.