Erlang - How Can I Parse RFC1123 Dates Into An Erlang Term? - parsing

Without using a third party module, what steps would I need to take to convert this:
<<"Mon, 17 Feb 2014 11:07:53 GMT">>
Into this?:
[17, 2, 2014, 10, 07, 53]
Most of the answers I've Googled suggest using a library.
So far, I suspect I'd get somewhere by pattern matching the formatted date string.
Something like:
<<_:5/binary, Date:2/binary>> = <<"Mon, 17 Feb 2014 11:07:53 GMT">>...
Which I think should produce the following 'match'
Date = 17...
That's based on an idea found here - https://groups.google.com/forum/#!topic/erlang-programming/OpXSqt3U86c - Is this a good approach?
Are there any BIF's or modules that can help with this?
And furthermore, how would I convert/map "Feb" to an integer?

Let's try that in the shell:
1> <<_:5/binary, Date:2/binary>> = <<"Mon, 17 Feb 2014 11:07:53 GMT">>.
** exception error: no match of right hand side value <<"Mon, 17 Feb 2014 11:07:53 GMT">>
Right, we need to match the rest of the binary at the same time:
2> <<_:5/binary, Date:2/binary, Rest/binary>> = <<"Mon, 17 Feb 2014 11:07:53 GMT">>.
<<"Mon, 17 Feb 2014 11:07:53 GMT">>
3> Date.
<<"17">>
So now Date is a binary containing the bytes for the ASCII digits 1 and 7. We can convert that to a number with binary_to_integer:
4> binary_to_integer(Date).
17
As for the conversion of month names to integers, the usual way to do something like that is with a function:
month_name_to_integer("Jan") -> 1;
month_name_to_integer("Feb") -> 2;
...
month_name_to_integer("Dec") -> 12.

You may use tempo library for datetime formatting and parsing.

Bin = <<"Mon, 17 Feb 2014 11:07:53 GMT">>,
L = binary_to_list(Bin),
{match, Res} =
re:run(L, "[0-9]+", [global, {capture, all, list}]),
[list_to_integer(X) || [X] <- Res].
the output is:
[17,2014,11,7,53]

Why suffering? Why not third party module?
I am use erlware_commons
ec_date:parse("Mon, 17 Feb 2014 11:07:53 GMT").

Related

Date Extraction from a specific dataset - Google sheets

I've tried all types of date extraction from this timestamp but nothing works.
Data samples:
Mon 2021 Jul 26 2021 8:26 PM
Wed May 19 2021 22:54:00 GMT+0800 (Hong Kong Standard Time)
Tried MOD, = Time,Minute, and Timevalue
Does anyone have any idea?
Tried MOD, = Time,Minute, and Timevalue. Expected to extract the date but it doesn't.
try:
=INDEX(TEXT(IFNA(1*REGEXEXTRACT(TO_TEXT(A1:A),
"(\w+ \d+ \d{4})" ), "​"), "dd/mm/e"))
Use regexextract(), like this:
=to_date( value( regexextract( to_text(A2), "^\w+ (\w+ \w+ \w+)" ) ) )

Stop ...skipping... SUMMARY OF LESS COMMANDS Commands marked with * may be preceded by a number

When copying some code (about 50 lines) from a rake task to the rails console, I want it to just run the code in the rails console, but instead this happens. The first part is some results printing to screen, but I have no idea what happens next):
#<Appointment:0x00007fb83eec5358
id: "0f0e14a6-1645-4a7b-ad61-f799e60ac570",
doctor_id: 1,
patient_id: 1,
start_time: Sun, 24 Jan 2021 13:25:45 UTC +00:00,
end_time: Sun, 24 Jan 2021 14:25:45 UTC +00:00,
created_at: Sun, 24 Jan 2021 12:50:45 UTC +00:00,
updated_at: Sun, 24 Jan 2021 13:10:45 UTC +00:00]
...skipping...
SUMMARY OF LESS COMMANDS
Commands marked with * may be preceded by a number, N.
Notes in parentheses indicate the behavior if N is given.
A key preceded by a caret indicates the Ctrl key; thus ^K is ctrl-K.
h H Display this help.
q :q Q :Q ZZ Exit.
---------------------------------------------------------------------------
MOVING
e ^E j ^N CR * Forward one line (or N lines).
y ^Y k ^K ^P * Backward one line (or N lines).
What is going on and how do I stop it?
Notes:
Here is the full text of what appears
I tried running Pry.config.pager = false as provided here, but the problem happens despite that.

How to create a indicator that loops through date and unique client ID?

The data structure is as follows:
Cli_ID M_YR trans-Date First_Trans
1004525 Oct 17 30-Oct-17 20-Sep-17
1004570 Oct 17 02-Oct-17 30-Aug-17
1004570 Oct 17 05-Oct-17 30-Aug-17
1004570 Oct 17 10-Oct-17 30-Aug-17
1004570 Oct 17 11-Oct-17 30-Aug-17
1004570 Oct 17 12-Oct-17 30-Aug-17
1004570 Oct 17 13-Oct-17 30-Aug-17
1004570 Oct 17 17-Oct-17 30-Aug-17
1004570 Oct 17 19-Oct-17 30-Aug-17
1004570 Oct 17 23-Oct-17 30-Aug-17
1004570 Oct 17 24-Oct-17 30-Aug-17
1004570 Oct 17 25-Oct-17 30-Aug-17
1143578 Oct 17 13-Oct-17 07-Sep-17
1143578 Oct 17 18-Oct-17 07-Sep-17
1143578 Oct 17 19-Oct-17 07-Sep-17
I need to identify all clients who have had at least one transaction per month. I am thinking, based on their first transaction date, calculate how many month they should have had at least one transaction for (Count_month) (eg., if client first transaction is Sept 2017, so from October 2017 to June 2019, the number of month they need to have at least one transaction in is 21).
And then create another counter to loop through each M_YR (month, year), and add 1 if a unique client had a transaction in a given Month-year (Count_client)...(eg., if client had at least one transaction a month from October 2017 to June 2019, the counter would be 21)
If I compare these two variables (Count_client and Count_month), if Count_client < Count_month then i will know this client did not have at least one transaction per month.
Does this method work? is there an easier way to do this? I feel this is probably not the simplest solution but I am currently out of ideas..
To calculate the number of months between first transaction and present date you can use:
compute Months_since_1st = datediff($time, First_Trans, "months").
(the $Time expression will use present date in calculation - you can replace that with any other date).
To calculate the number of months that had any transaction:
dataset name orig.
* identifying every month/year that had transactions - per client.
dataset declare agg1.
aggregate out=agg1 /break=Cli_ID M_YR /n=n.
dataset activate agg1.
* counting them for each client.
dataset declare agg2.
aggregate out=agg2 /break=Cli_ID /Nmonths_with_transactions=n.
* attaching the results back to original data.
dataset activate orig.
match files /file = * /table = agg2 / by Cli_ID.

Rails: Grouping Hashes with same date and getting their difference

I have 2 hases:
x = {Sun, 01 Oct 2017=>10, Wed, 01 Nov 2017=>4, Fri, 01 Dec 2017=>2}
y = {Sun, 01 Oct 2017=>7, Wed, 01 Nov 2017=>2, Fri, 01 Dec 2017=>1}
I wanted to group them and get their difference, which should produce this result:
z = {Sun, 01 Oct 2017=>3, Wed, 01 Nov 2017=>2, Fri, 01 Dec 2017=>1}
You can use reduce to achieve this, as follows:
z = x.reduce({}) do |z_hash, (key, val)|
next z_hash unless y[key]
z_hash[key] = val - y[key]
z_hash
end
Alternatively, and more built to purpose, you could use:
z = x.merge(y) { |key, v1, v2| v1 - v2 }
The first approach allows you to easily skip keys that don't appear in both hashes, while the second is what I'd recommend - merge takes an optional block for cases like this.
It's readable and gets exactly the output you'd expect.
Hope that helps - give me a shout if you've any questions :)
ar = x , y
z = p ar.inject{|memo, el| memo.merge( el ){|k, old_v, new_v| old_v - new_v}}

Rails - Time arithmetic

I was playing time arithmetic in rails console. I have subtracted two different times and got back a long number, please see below my code.
a = User.find(1).updated_at # returns Mon, 03 Mar 2014 11:07:43 UTC +00:00
b = User.find(1).created_at # returns Mon, 03 Mar 2014 08:36:50 UTC +00:00
a - b # returns 9053.699814796448
My question is. What is this number 9053.699814796448? Is it time? What is it's unit? and how is that calculated? Is it possible to change the default unit of the outcome?
Thanks
a - b gives you the time in seconds. Check out the Time#- .

Resources