converting strings into dates in clojure - 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.

Related

Regex extracking information

Hope you are fine. I was trying to build a regex for extracting some strings in some documents. This is a sample of the strings I have to extract:
5003t00001fJK8EAAWFri Jan 27 2023 11:37:45 GMT-0600
0033t00004A596CAARFri Jan 27 2023 11:44:04 GMT-0600
The regex I wrote so far I have:
a) 500[\w._%+ :-]+(GMT)
b) (00).[0-9][\w._%+ :-]+(GMT)
However I am getting some observations with from 1) using the b) Regex that I build.
Any suggestion?

Get datetime using qmake

We can get datetime using qmake _DATE_ which outputs
Sat Mar 12 17:29:00 2022
Can we format this output?
By using QDateTime by passing the value of DATE (in fromString), you may be able to format the output the way you want, by using toString.
You can try things like this :
toString("ddd MMMM d hh:mm:ss yy"); //Sat Mar 12 17:29:00 2022

clojure java-time - date moves forward a year?

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.

All CSV values in column 0 are strings

For some reason a csv file I wrote (win7) with Python has all the values as a string in column 0 and cannot perform any operation.
It has no labels.
The format is (I would like to keep the last value - date - as a date format):
"Rob,Avanti,Ave,12.83,Max,4.0,Min,-21.9,analist disp:,-1.0,"" date: Feb 04, 2016 """
EDIT - When I read it with the csv module it prints it out like:
['Rob,Avanti,Ave,12.83,Max,4.0,Min,-21.9,analist disp:,-1.0," date: Feb 04, 2016\t\t\t"']
What is the best way to convert the strings into comma separated values like this?
Rob,Avanti,Ave,12.83,Max,4.0,Min,-21.9,analist disp:,-1.0, date:, Feb 04, 2016
Thanks a lot.
s="Rob,Avanti,Ave,12.83,Max,4.0,Min,-21.9,analist disp:,-1.0,"" date: Feb 04, 2016 """
print(s)
Rob,Avanti,Ave,12.83,Max,4.0,Min,-21.9,analist disp:,-1.0, date: Feb 04, 2016
to add a comma after "date:" you need to add some logic (like replace ":" with ":,"; or after first word etc.
First, your date field is quoted, which is ok (and needed) because there is a comma inside:
" date: Feb 04, 2016 "
But then the whole line also gets quoted (and thus seen as a single field). And because there are already quotes around the date field, those get escaped with another quote:
"Rob,Avanti,Ave,12.83,Max,4.0,Min,-21.9,analist disp:,-1.0,"" date: Feb 04, 2016 """
So, if you remove that last quoting, everything should be fine (but you might want to trim the date field):
Rob,Avanti,Ave,12.83,Max,4.0,Min,-21.9,analist disp:,-1.0," date: Feb 04, 2016 "
If you want it exactly like this, you need another comma after date: :
Rob,Avanti,Ave,12.83,Max,4.0,Min,-21.9,analist disp:,-1.0, date:,"Feb 04, 2016"
On the other hand, it would be better to use a header instead:
Name,Name2,Ave,Max,Min,analist disp,date
Rob,Avanti,12.83,4.0,-21.9,-1.0,"Feb 04, 2016"

javascript regular expression: how do I find date without year or date with year<2010

I need to find date without year, or date with year<2010.
basically,
Feb 15
Feb 20
Feb 20, 2009
Feb 20, 1995
should be accepted
Feb 20, 2010
Feb 20, 2011
should be rejected
How do I do it?
Thanks,
Cheng
Try this:
(Jan|Feb|Mar...Dec)\s\d{1,2},\s([1][0-9][0-9][0-9]|200[0-9])
Note: Expand the month list with proepr names. I was too lazy to spell it all out.