clojure java-time - date moves forward a year? - clojure

I'm confused on usage of the clojure.java-time library usage that wraps Java 8's java.time api.
I want a function that translates a date in string format from
"yyyy-MM-dd'T'HH:mm:ss" format to "MM/dd/YYYY hh:mm:ss a" format.
Here's my function:
(require '[java-time :as jt])
(defn change-ds-format [in-ds]
{:pre [string? in-ds ]}
(let [input-format "yyyy-MM-dd'T'HH:mm:ss"
output-format "MM/dd/YYYY hh:mm:ss a"]
(->> in-ds
(jt/local-date-time input-format)
(jt/format output-format))))
This looks ok:
(change-ds-format "2019-12-28T00:00:00" )
;=> "12/28/2019 12:00:00 AM"
I have no idea why this is pushed to 2020?
(change-ds-format "2019-12-29T00:00:00" )
;=> "12/29/2020 12:00:00 AM"
;; Why is this one 2020 ???
I thought maybe it was a timezone offset issue but there is no way it should be shifting to the year 2020 I don't believe.

Because yyyy is not YYYY
From Oracle docs for DateTimeFormatter:
... snip...
u year year 2004; 04
y year-of-era year 2004; 04
...snip...
Y week-based-year year 1996; 96
w week-of-week-based-year number 27
...snip...
I'm not very proficient at datetime arithmetic or the nuances but I believe that in week-based year counting this December's 30th and 31st are counted as year 2020 because these dates belong to week #1, not week #52 or #53.

Related

Week number calculation in Power BI

I am new to Power BI.I have one year filter (Range filter) and one week number filter (Range filter). I want to calculate values such that when i select year 2021 to 2022 and week number 42 to 10 it will first show data for 42th weeks to 52th weeks for year 2021 and for 1st week to 10 th weeks for year 2022.

How do I calculate average from June 1 to June 30 annually and make it a dynamic function using DAX?

I have two tables in PowerBI, one modified date and one fact for customer scores. The relationship will be using the "Month Num" column. Score assessments take place every June, so I would like to be able to have the scores for 12 months (June 1 to June 30) averaged. Then I will just have a card comparing the Previous year score and Current year score. Is there a way to do this dynamically, so I do not have to change the year in the function every new year? I know using the AVERAGE function will be nested into the function somehow, but I am getting confused not using a calendar year and not seasoned enough to use Time Intelligence functions yet.
Customer Score Table
Month
Month Num
Year
Score
Customer #
June
6
2020
94.9
11111
July
7
2020
97
11111
months
continue
2020
100
June
6
2021
89
22222
July
7
2021
91
22222
months
continue
2021
100
June
6
2022
93
33333
July
7
2022
94
33333
Date Table
Month
Month Num
Month Initial
january
1
J
feb
2
F
march
3
M
other
months
continued

How to generate_series of every hour of every day of 1 year from the current timestamp

I have a query that generates every day of the year(shown below). What if I want to get a series of every hour of every day of the year from the current timestamp. Example: today is July 23,2019 10:30:00 AM, the result I am hoping to get is below
2019-07-23 20:30:00
2019-07-23 20:00:00
2019-07-23 19:00:00
2019-07-23 18:00:00
.
.
.
2018-07-23 20:00:00
This is a Redshift (PostgreSQL 8.0.2) query for Eclipse Birt. Hoping to create a parameter for both date and time but seems difficult to achieve if 2 separate ranges.
select cast(convert_timezone('UTC','AEST',cast(now() as timestamp without time zone)) as date) - generate_series(0, 365) date,
to_char(cast(convert_timezone('UTC','AEST',cast(now() as timestamp without time zone)) as date) - generate_series(0, 365), 'dd/mm/yyyy') date_disp;
Example: today is July 23,2019 10:30:00 AM, the result I am hoping to get is below:
2019-07-23 20:30:00
2019-07-23 20:00:00
2019-07-23 19:00:00
2019-07-23 18:00:00
.
.
.
2018-07-23 20:00:00
This is to similar to your previous question.
Use:
SELECT date_trunc('hour', now()::timestamp) - generate_series(0, 24 * 365) * interval '1 hour'
This outputs:
2019-07-23 05:00:00
2019-07-23 04:00:00
etc
You can use the DATEADD Redshift function, using "h", "hr" or "hrs" as your first parameter. Documentation for this function can be found here and here.

RubyMotion Day Number of year for today

I want to generate the current day number using RubyMotion code. I have looked at several IOS solutions but I'm not experienced enough to translate the code successfully to RubyMotion.
I am currently at the following point:
def today
NSDate.today
end
def day_number
NSDate.from_components (day: today)
end
When I run the above it gives me an return of 3852055-06-16 00:00:00 +0100. I thought that the 3852055 part was seconds but it doesn't seem to equate to either todays date or to 16th of June - and in any case why should it be returning 06-16 instead of 02-08?? Totally confused here.
I just want to get todays day number. As I write the date is 2nd August 2014 and the day number should be 214 so I'm obviously way out somewhere.
Any help would be greatly appreciated.
cheers
This will do the trick:
daynum = NSCalendar.currentCalendar.ordinalityOfUnit(NSDayCalendarUnit, inUnit:NSYearCalendarUnit, forDate:NSDate.date)
Now suppose you have the date in the form of a string, and you want to get the day number for it:
datestr = "2014-01-01 11:08:56 +0000"
First create an NSDateFormatter to convert the String into an NSDate
df = NSDateFormatter.new
df.dateFormat = "yyyy-MM-dd HH:mm:ss z"
mydate = df.dateFromString(datestr)
daynum = NSCalendar.currentCalendar.ordinalityOfUnit(NSDayCalendarUnit, inUnit:NSYearCalendarUnit, forDate:mydate)
If your date string is simpler:
datestr = "2014-01-01"
just use a simpler dateFormat string:
df.dateFormat = "yyyy-MM-dd"
I suggest you to take a look at motion-support gem and especially at core-exttime.
You can play with dates as you want:
=> Mon, 04 Aug 2014
(main)> Time
=> Time
(main)> Time.today
=> 2014-08-04 00:00:00 +0200
(main)> Date.today.day
=> 4
(main)> Time.today.day
=> 4
and a lot more.

converting strings into dates in clojure

I have strings in the following format "2013-02-20T17:24:33Z" and "Mon Feb 25 02:42:27 +0000 2013".
Is there a quick way to convert these into date time format so that they can be tested for equality and/or sorted.
clj-time does allow me to this format (date-time 1986 10 14 4 3 27 456). However to achieve this I will have to parse the two above strings. The above strings being standard formats, is there a way to directly convert them into date time objects?
Thanks,
Murtaza
clj-time has standard formatters defined, see clj-time.format/show-formatters, but your second format is not a 'standard' format as far as clj-time is concerned (although it does look suspiciously close to rfc822). You can create a custom formatter tho...
(use 'clj-time.format)
(parse (formatters :date-time-no-ms) "2013-02-20T17:24:33Z")
#<DateTime 2013-02-20T17:24:33.000Z>
(parse (formatter "E MMM dd hh:mm:ss Z YYYY") "Mon Feb 25 02:42:27 +0000 2013" )
#<DateTime 2013-02-25T02:42:27.000Z>
(show-formatters)
:basic-date 20130228
:basic-date-time 20130228T114047.213Z
:basic-date-time-no-ms 20130228T114047Z
:basic-ordinal-date 2013059
:basic-ordinal-date-time 2013059T114047.213Z
:basic-ordinal-date-time-no-ms 2013059T114047Z
:basic-t-time T114047.213Z
:basic-t-time-no-ms T114047Z
:basic-time 114047.213Z
:basic-time-no-ms 114047Z
:basic-week-date 2013W094
:basic-week-date-time 2013W094T114047.213Z
:basic-week-date-time-no-ms 2013W094T114047Z
:date 2013-02-28
:date-hour 2013-02-28T11
:date-hour-minute 2013-02-28T11:40
:date-hour-minute-second 2013-02-28T11:40:47
:date-hour-minute-second-fraction 2013-02-28T11:40:47.213
:date-hour-minute-second-ms 2013-02-28T11:40:47.213
:date-time 2013-02-28T11:40:47.213Z
:date-time-no-ms 2013-02-28T11:40:47Z
:hour 11
:hour-minute 11:40
:hour-minute-second 11:40:47
:hour-minute-second-fraction 11:40:47.213
:hour-minute-second-ms 11:40:47.213
:ordinal-date 2013-059
:ordinal-date-time 2013-059T11:40:47.213Z
:ordinal-date-time-no-ms 2013-059T11:40:47Z
:rfc822 Thu, 28 Feb 2013 11:40:47 +0000
:t-time T11:40:47.213Z
:t-time-no-ms T11:40:47Z
:time 11:40:47.213Z
:time-no-ms 11:40:47Z
:week-date 2013-W09-4
:week-date-time 2013-W09-4T11:40:47.213Z
:week-date-time-no-ms 2013-W09-4T11:40:47Z
:weekyear 2013
:weekyear-week 2013-W09
:weekyear-week-day 2013-W09-4
:year 2013
:year-month 2013-02
:year-month-day 2013-02-28
Use SimpleDateFormat.
(let [input "Mon Feb 25 02:42:27 +0000 2013"
fmt (java.text.SimpleDateFormat. "EEE MMM dd HH:mm:ss zzz yyyy")]
(.parse fmt input))
Remember that parsing months' and weekdays' names requires appropriately set locale.
As a side note, the first date format in your question is ISO 8601.