{"id":619,"date":"2018-04-01T22:13:04","date_gmt":"2018-04-01T22:13:04","guid":{"rendered":"http:\/\/perfectionatic.org\/?p=619"},"modified":"2022-01-30T12:45:00","modified_gmt":"2022-01-30T12:45:00","slug":"iterating-with-dates-and-time-in-julia","status":"publish","type":"post","link":"https:\/\/perfectionatic.org\/?p=619","title":{"rendered":"Iterating with Dates and Time in Julia"},"content":{"rendered":"\n<p><code>Julia<\/code> has good documentation on dealing with <a href=\"https:\/\/docs.julialang.org\/en\/v1\/stdlib\/Dates\/\" target=\"_blank\" rel=\"noreferrer noopener\">Dates and Time<\/a>, however that is often in the context constructing and Date and Time objects. In this post, I am focus on the ability to iterate over Dates and Times. This is very useful in countless application.<\/p>\n\n\n\n<p>We start of by capturing this moment and moving ahead into the future<\/p>\n\n\n\n<pre escaped=\"true\" lang=\"julia\">\njulia> using Dates\njulia> this_moment=now()\n2018-04-01T23:13:33.437\n<\/pre>\n\n\n\n<p>In one hour that will be<\/p>\n\n\n\n<pre escaped=\"true\" lang=\"julia\">\njulia> this_moment+Dates.Hour(1)\n2018-04-02T00:13:33.437\n<\/pre>\n\n\n\n<p>Notice that <code>Julia<\/code> was clever enough properly interpret that we will be on the in another day after exactly one hour. Thanks to it multiple dispatch of the <code>DateTime<\/code> type to be able to do <a href=\"https:\/\/docs.julialang.org\/en\/stable\/manual\/dates\/#TimeType-Period-Arithmetic-1\">TimeType period arithmatic<\/a>.<\/p>\n\n\n\n<p>You can then write a nice <code>for<\/code> loop that does something every four hours for the next two days.<\/p>\n\n\n\n<pre escaped=\"true\" lang=\"julia\">\njulia> for t=this_moment:Dates.Hour(4):this_moment+Dates.Day(2)\n    println(t)\n    #or somethings special with that time\nend\n2018-04-01T23:13:33.437\n2018-04-02T03:13:33.437\n2018-04-02T07:13:33.437\n2018-04-02T11:13:33.437\n2018-04-02T15:13:33.437\n2018-04-02T19:13:33.437\n2018-04-02T23:13:33.437\n2018-04-03T03:13:33.437\n2018-04-03T07:13:33.437\n2018-04-03T11:13:33.437\n2018-04-03T15:13:33.437\n2018-04-03T19:13:33.437\n2018-04-03T23:13:33.437\n<\/pre>\n\n\n\n<p>Often we are not so interested in the full dates. For example if we are reading a video file and we want to get a frame every 5 seconds while using <a href=\"https:\/\/github.com\/kmsquire\/VideoIO.jl\"><code>VideoIO.jl<\/code><\/a>. We can deal here with the simpler <code>Time<\/code> type.<\/p>\n\n\n\n<pre escaped=\"true\" lang=\"julia\">\njulia> video_start=Dates.Time(0,5,20)\n00:05:20\n\ufeff<\/pre>\n\n\n\n<p>Here we are interested in starting 5 minutes and 20 seconds into the video.<br>Now we can make a nice loop from the start to finish<\/p>\n\n\n\n<pre escaped=\"true\" lang=\"julia\">\nfor t=video_start:Dates.Second(5):video_start+Dates.Hour(2)\n    h=Dates.Hour(t).value\n    m=Dates.Minute(t).value\n    s=Dates.Second(t).value\n    ms=Dates.Millisecond(t).value\n    # Do something interesting with ffmpeg seek on the video\nend\n\ufeff<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Julia has good documentation on dealing with Dates and Time, however that is often in the context constructing and Date and Time objects. In this post, I am focus on the ability to iterate over Dates and Times. This is very useful in countless application. We start of by capturing this moment and moving ahead [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[74,73],"tags":[92],"class_list":["post-619","post","type-post","status-publish","format-standard","hentry","category-julia","category-julialang","tag-julia"],"_links":{"self":[{"href":"https:\/\/perfectionatic.org\/index.php?rest_route=\/wp\/v2\/posts\/619","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/perfectionatic.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/perfectionatic.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/perfectionatic.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/perfectionatic.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=619"}],"version-history":[{"count":5,"href":"https:\/\/perfectionatic.org\/index.php?rest_route=\/wp\/v2\/posts\/619\/revisions"}],"predecessor-version":[{"id":818,"href":"https:\/\/perfectionatic.org\/index.php?rest_route=\/wp\/v2\/posts\/619\/revisions\/818"}],"wp:attachment":[{"href":"https:\/\/perfectionatic.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=619"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/perfectionatic.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=619"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/perfectionatic.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=619"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}