How can I get approximate time passed since some timestamp? - perl

Is there any useful module on CPAN that returns number of biggest fractions of date interval, i.e. return integer number of years/months/days/hours/minutes/seconds between two dates, to use in sentence like "N days ago" or "M months ago"

DateTime and DateTime::Duration are the best modules:
use strict;
use warnings;
use DateTime;
use DateTime::Duration;
use Data::Dumper;
my $dt1 = DateTime->now;
my $dt2 = DateTime->new(
year => 2001,
month => 01,
day => 01
);
my $duration = $dt1 - $dt2;
print Dumper($duration);
produces:
$VAR1 = bless( {
'seconds' => 38,
'minutes' => 181,
'end_of_month' => 'wrap',
'nanoseconds' => 0,
'days' => 20,
'months' => 110
}, 'DateTime::Duration' );
You can format absolute times and durations using the sister modules DateTime::Format and DateTime::Format::Duration.

Well, not very difficult to write your own.
Calculate relative time in C#

Time::Duration seems to be a natural choice for what you try to accomplish. I haven't tried it though. Only caveat seems to be that it already gives you phrases like "M months ago", so you might need to parse its output for non-English output.

You might take a look at the Time::Ago module on CPAN (I'm the author). It's a Perl port of the time_ago_in_words() helper from Rails.
Given a duration, it returns a readable string approximation. From the Rails docs:
0 29 secs
less than a minute
30 secs 1 min, 29 secs
1 minute
1 min, 30 secs 44 mins, 29 secs
[2..44] minutes
44 mins, 30 secs 89 mins, 29 secs
about 1 hour
89 mins, 30 secs 23 hrs, 59 mins, 29 secs
about [2..24] hours
23 hrs, 59 mins, 30 secs 41 hrs, 59 mins, 29 secs
1 day
41 hrs, 59 mins, 30 secs 29 days, 23 hrs, 59 mins, 29 secs
[2..29] days
29 days, 23 hrs, 59 mins, 30 secs 44 days, 23 hrs, 59 mins, 29 secs
about 1 month
44 days, 23 hrs, 59 mins, 30 secs 59 days, 23 hrs, 59 mins, 29 secs
about 2 months
59 days, 23 hrs, 59 mins, 30 secs 1 yr minus 1 sec
[2..12] months
1 yr 1 yr, 3 months
about 1 year
1 yr, 3 months 1 yr, 9 months
over 1 year
1 yr, 9 months 2 yr minus 1 sec
almost 2 years
2 yrs max time or date
(same rules as 1 yr)
CPAN module
Repository

[This isn't really an answer, but I think it's relevant.]
If you're going to get into date/time manipulation and calculation, you're best off keeping to the DateTime family of modules. Not only do they have the best handling of timezones that I've found, but they all work with each other. I've never had a problem in this domain which I couldn't find a module in DateTime to solve it.

Related

Convert seconds into 0 years 0 mons 0 days 0 hours 0 mins 0 secs

I'm trying to convert my seconds into the numbers of hours, days,... contained into my time.
I have tried the following request (fiddle):
SELECT TO_CHAR((1670857661 || ' second')::interval, 'YYYY" years" MM" mons "DD" days "HH24" hours "MI" mins "SS" secs"')
Current Output:
0000 years 00 mons 00 days 464127 hours 07 mins 41 secs
Expected output:
54 years 8 mons 12 days 10 hours 51 mins 12 secs
Use justify_interval()
select justify_interval(make_interval(secs => 1670857661));
Online example

Filling a calendar using Arrayformula or LOOKUP

I've made a calendar sheet and would like to fill it using an Arrayformula or some kind of Lookup.
The problem is, the code in each cell is different, do I need it all to be the same code or is it possible to do an Arrayformula that does a different formula for each line?
I spent ages getting the calendar code working but would now like to simplify the code and I'm not sure what my next step should be:
https://docs.google.com/spreadsheets/d/1u_J7bmOFyDlYXhcL5dW3CHFJ1esySAKK_yPc6nFTdLA/edit?usp=sharing
Any advice would be much appreciated.
I've added a new sheet in your file called 'Aresvik'.
The green cells have new formula.
Cell B3 can be =date(B1,1,1)
Then each successive month can be =eomonth(B3,0)+1, =eomonth(J3,0)+1 etc.
The date formula in cell B5 is:
=arrayformula(iferror(vlookup(sequence(7,7,1),{array_constrain(sequence(40,1),day(eomonth(B3,0))+weekday(B3,3),1),query({flatten(split(rept(",",day(eomonth(B3,0))-1),",",0,0));sequence(day(eomonth(B3,0)),1,1)},"offset "&day(eomonth(B3,0))-weekday(B3,3)&" ",0)},2,false),))
It can be copied to each other cell below Mo, so B5 will change to J5, R5, Z5 etc.
Notes
The concept revolves around using the SEQUENCE function to generate a grid of numbers, 6 rows, 7 columns:
sequence(6,7)
which looks like this:
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31 32 33 34 35
36 37 38 39 40 41 42
Then using these numbers in a VLOOKUP to get a corresponding date for the calendar. If the first of the month falls on a Thursday (April 2021), the vlookup range needs 3 gaps at the top of the list of dates. player0 has a more elegant solution than my original query using offset, so I've incorporated it below. Cell Z3 is the date 1/4/2021:
=arrayformula(
iferror(
vlookup(sequence(6,7),
{sequence(day(eomonth(Z3,0))+weekday(Z3,2),1,0),
{iferror(sequence(weekday(Z3,2),1)/0,);sequence(day(eomonth(Z3,0)),1,Z3)}},
2,false)
,))
The first column in the vlookup range is:
sequence(day(eomonth(Z3,0))+weekday(Z3,2),1,0)
which is an array of numbers from 0, corresponding with the number of days in the month plus the number of gaps before the 1st day.
The second column in the vlookup range is:
{iferror(sequence(weekday(Z3,2),1)/0,);sequence(day(eomonth(Z3,0)),1,Z3)}},
It is an array of 2 columns in this format: {x;y}, where y sits below x because of the ;.
These are the gaps: iferror(sequence(weekday(Z3,2),1)/0,), followed by the date numbers: sequence(day(eomonth(Z3,0)),1,Z3)
(Example below is April 2021):
0
1
2
3
4
5
6 44317
7 44318
8 44319
9 44320
10 44321
11 44322
12 44323
13 44324
14 44325
15 44326
16 44327
17 44328
18 44329
19 44330
20 44331
21 44332
22 44333
23 44334
24 44335
25 44336
26 44337
27 44338
28 44339
29 44340
30 44341
31 44342
32 44343
33 44344
34 44345
35 44346
36 44347
The vlookup takes each number in the initial sequence (6x7 layout), and brings back the corresponding date from col2 in the range, based on a match in col1.
When the first day of the month is a Monday, iferror(sequence(weekday(BB1,2),1)/0,) generates a gap in col2 of the vlookup range. This is why col1 in the vlookup range has to start with 0.
I've updated the sheet at https://docs.google.com/spreadsheets/d/1u_J7bmOFyDlYXhcL5dW3CHFJ1esySAKK_yPc6nFTdLA/edit#gid=68642071
Values on the calendar are dates so the formatting has to be d.
If you want numbers, then use:
=arrayformula(
iferror(
vlookup(sequence(6,7),
{sequence(day(eomonth(Z3,0))+weekday(Z3,2),1,0),
{iferror(sequence(weekday(Z3,2),1)/0,);sequence(day(eomonth(Z3,0)),1)}},
2,false)
,))
shorter solution:
=INDEX(IFNA(VLOOKUP(SEQUENCE(6, 7), {SEQUENCE(DAY(EOMONTH(B3, ))+WEEKDAY(B3, 2), 1, ),
{IFERROR(ROW(INDIRECT("1:"&WEEKDAY(B3, 2)))/0); SEQUENCE(DAY(EOMONTH(B3, )), 1, B3)}}, 2, )))

how I delete combination rows that have the same numbers from matrix and only keeping one of the combinations?

for a=1:50; %numbers 1 through 50
for b=1:50;
c=sqrt(a^2+b^2);
if c<=50&c(rem(c,1)==0);%if display only if c<=50 and c=c/1 has remainder of 0
pyth=[a,b,c];%pythagorean matrix
disp(pyth)
else c(rem(c,1)~=0);%if remainder doesn't equal to 0, omit output
end
end
end
answer=
3 4 5
4 3 5
5 12 13
6 8 10
7 24 25
8 6 10
8 15 17
9 12 15
9 40 41
10 24 26
12 5 13
12 9 15
12 16 20
12 35 37
14 48 50
15 8 17
15 20 25
15 36 39
16 12 20
16 30 34
18 24 30
20 15 25
20 21 29
21 20 29
21 28 35
24 7 25
24 10 26
24 18 30
24 32 40
27 36 45
28 21 35
30 16 34
30 40 50
32 24 40
35 12 37
36 15 39
36 27 45
40 9 41
40 30 50
48 14 50
This problem involves the Pythagorean theorem but we cannot use the built in function so I had to write one myself. The problem is for example columns 1 & 2 from the first two rows have the same numbers. How do I code it so it only deletes one of the rows if the columns 1 and 2 have the same number combination? I've tried unique function but it doesn't really delete the combinations. I have read about deleting duplicates from previous posts but those have confused me even more. Any help on how to go about this problem will help me immensely!
Thank you
welcome to StackOverflow.
The problem in your code seems to be, that pyth only contains 3 values, [a, b, c]. The unique() funcion used in the next line has no effect in that case, because only one row is contained in pyth. another issue is, that the values idx and out are calculated in each loop cycle. This should be placed after the loops. An example code could look like this:
pyth = zeros(0,3);
for a=1:50
for b=1:50
c = sqrt(a^2 + b^2);
if c<=50 && rem(c,1)==0
abc_sorted = sort([a,b,c]);
pyth = [pyth; abc_sorted];
end
end
end
% do final sorting outside of the loop
[~,idx] = unique(pyth, 'rows', 'stable');
out = pyth(idx,:);
disp(out)
a few other tips for writing MATLAB code:
You do not need to end for or if/else stements with a semicolon
else statements cover any other case not included before, so they do not need a condition.
Some performance reommendations:
Due to the symmetry of a and b (a^2 + b^2 = b^2 + a^2) the b loop could be constrained to for b=1:a, which would roughly save you half of the loop cycles.
if you use && for contencation of scalar values, the second part is not evaluated, if the first part already fails (source).
Regards,
Chris
You can also linearize your algorithm (but we're still using bruteforce):
[X,Y] = meshgrid(1:50,1:50); %generate all the combination
C = (X(:).^2+Y(:).^2).^0.5; %sums of two square for every combination
ind = find(rem(C,1)==0 & C<=50); %get the index
res = unique([sort([X(ind),Y(ind)],2),C(ind)],'rows'); %check for uniqueness
Now you could really optimized your algorithm using math, you should read this question. It will be useful if n>>50.

How to eliminate series of values with so much variation

I got a dataset (azimuth vs time) with measure the compass of an object trough time. So I can see when the object is moving (the compass vary so much), and when it's static, without moving (compass do not vary). My question is how to program this in matlab in order to eliminate the data which show that the object is moving and just filter data that shows the object is static.
For example:
Azimuth (angle) | 30 30 30 15 10 16 19 24 24 24 17 14 12 15 16
Time (s) | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
The output would be:
Azimuth (angle) | 30 30 30 24 24 24
Time (s) | 1 2 3 8 9 10
s=diff(Azumuth)==0
%diff only would skip the values at t=1 and t=8. Modify to include them as well:
s=[s(1),s(2:end)|s(1:end-1),s(end)]
Azumuth(s)
Time(s)

Retrieve Month, Day, Hour, Minute, from a number in minute

i want create a counter that retrieve the number of month, day, hour, minute from a given number in minutes, for example i know that:
60 minutes in an hour
24 hours in a day = 60 x 24 = 1440 minutes
31 days per month
1 month = 24 x 60 x 31 = 44,640 minutes
so if i give for example the number 44640 i want have 1 month 0 day 0 hour 0 minute , or for example if i give 44700 i want have 1 month, 0 day 0 hour 60 minute or 1 month 0 day 1 hour 0 minute
any help please?
int total_minutes = 44640;
int total_hours = total_minutes / 60;
int minutes = total_minutes % 60;
int total_days = total_hours / 24;
int hours = total_hours % 24;
int months = total_days / 31;
int days = total_days % 31;
printf("%d months, %d days, %02d:%02d\n", months, days, hours, minutes);
But that's misleading, since months are not all 31 days. On average, a month in the Gregorian calendar is 30.436875 days (43829.1 minutes), so you could use that figure. For many applications, such calculations just assume that a month is always 30 days. But if your time interval is anchored to a specific point in time, it might be better to use the date at both ends to determine how many whole months there are between them.