Flutter/Dart-How to display moths using the Duration package - flutter

Assuming I have a specific no.of seconds as the input and I use the flutter duration package.
printDuration(aSecond * 4466000);
then the output will be
7 weeks 2 days 16 hours 33 minutes 20 seconds
what I want is for the output to include months like
1 month 3 weeks 2 days 16 hours 33 minutes 20 seconds
is there any way I can achieve this?

Get second: duration.inSeconds
Convert to milliseconds
Use DateTime.fromMillisecondsSinceEpoch(millisecondsSinceEpoch)

Related

How does rust chrono Duration get number of years or months?

I'm quite new to Rust and chrono library.
I checked the https://docs.rs/chrono/0.4.19/chrono/struct.Duration.html#method.num_weeks, but there's no num_years() or num_months() API in chrono::Duration.
Is there any work around solution for this ?
chrono::Duration provides date and time duration in "ISO 8601 time duration with nanosecond precision", which implies it is representing the duration internally as a number of nanoseconds, and then providing convenience methods to convert into other duration units such as days, weeks, milliseconds, etc.
This is a little at odds with the actual ISO 8601 duration standard, which is a standard of representations and formats. The standard represents durations by the format P[n]Y[n]M[n]DT[n]H[n]M[n]S - which might give you what you want. But this is not what chronos::Duration was designed to provide.
The problem is that in order to represent a duration of months or years, more information than the number of nanoseconds is needed. Durations define the amount of intervening time in a time interval: the time between two points. The start or end time is important, because months or years are not standard durations. There are months of 28, 29 30, 31 days, and years of 365 and 366 days.
If you were to write your own algorithm to format durations in terms of years, months, days, hours, minutes, seconds, etc.. you would have to know the start date. In addition, time zone is important, because daylight savings needs to be taken into account. You would also have to make decisions about how to represent parts of months or years. For example the month of January has 31 days, and February 28, say. What would it mean to represent a duration of 1.75 months from January 1? Would that mean 31 days for January then 0.75 * 28 days in February?
Or you could represent the duration from a start date in a cascading unit format: e.g., 5 years, 4 months and 3 days, 2 hours, 12 minutes and 3 seconds from 1 Jan 1970 12:00Z. Just like the ISO 8601 standard.
So, its not an easy solution, and it all depends what your requirements are. I can understand why the developers of chronos:Duration left off providing num_months() and num_years()!
The crate chronoutil provides RelativeDuration which is "extending Chrono’s Duration to add months and years".
Example from docs.rs:
let one_day = RelativeDuration::days(1);
let one_month = RelativeDuration::months(1);
let delta = one_month + one_day;
let start = NaiveDate::from_ymd(2020, 1, 30);
assert_eq!(start + delta, NaiveDate::from_ymd(2020, 3, 1));

How to round out time to specific nearest given interval in postgreSQL?

I have separate data like below in PostgreSQL. I want to group by data based in rounding date and time with X minute. Cost column will has aggregate function like min and max.
Date
Time
Cost
2021-05-14
10:18:00
10
2021-05-14
10:20:00
20
2021-05-14
10:21:00
30
2021-05-14
10:22:00
40
2021-05-14
10:31:00
50
2021-05-14
10:48:00
60
2021-05-14
10:59:00
70
2021-05-14
11:00:00
80
2021-05-14
11:14:00
90
2021-05-14
11:15:00
100
2021-05-14
11:17:00
110
I want it round out to nearest 3, 5,10,15,30,60,120,240 minutes.
But what specific i want to do is, when it is rounding out with round figure digits like 10,30,60,120,240 it should start with 10:15:00. ( means it should with minute 15.)
For example,
for 5 minutes, above data from 10:15:00 to 10:19:00 should be round out to 10:15:00
(Total 5 minutes considered, 10:15, 10:16, 10:17, 10:18, 10:19)
for 10 minutes, above data from 10:15:00 to 10:24:00 should be round out to 10:15:00
for 15 minutes, above data from 10:15:00 to 10:29:00 should be round out to 10:15:00
for 30 minutes, above data from 10:15:00 to 10:44:00 should be round out to 10:15:00
for 60 minutes, above data from 10:15:00 to 11:14:00 should be round out to 10:15:00
for 120 minutes, above data from 10:15:00 to 12:14:00 should be round out to 10:15:00
for 240 minutes, above data from 10:15:00 to 14:14:00 should be round out to 10:15:00
I have tried to go through many stackoverflow answer but my specific case i couldn't found.
Whichever answers i have tried of similar question either they round out to 10:10:00 for 10 minutes or for higher minutes, it starts with 10:00:00.
We have huge data set so performance is also a point to consider here.
Can anyone help for this specific case?

PostgreSQL: Difference between 2 timestamps in days, hours and minutes

I have two timestamps and I need to calculate the difference between them in Days:Hours:Minutes.
I have done timestamp2-timestamp1 and the result is in months, days, hours and minutes. How can I convert the months in days knowing that some months have 31 days and the rest 30 without losing precision.

How to calculate a year to date subtotal in kdb

I have a table like this:
2019.03m Bolts 100
2019.03m Nuts 50
2019.02m Bolts 10
2019.02m Nuts 100
2019.01m Bolts 50
2019.01m Nuts 10
2018.12m Bolts 10
2018.12m Nuts 10
2018.11m Bolts 20
2018.11m Nuts 30
I would like to introduce a new column called the year to date column
2019.03m Bolts 100 160
2019.03m Nuts 50 160
2019.02m Bolts 10 60
2019.02m Nuts 100 110
2019.01m Bolts 50 50
2019.01m Nuts 10 10
2018.12m Bolts 10 30
2018.12m Nuts 10 40
2018.11m Bolts 20 20
2018.11m Nuts 30 30
This sums the previous year-to-date row and resets when it reaches a new year.
I have an idea of using sums but how can I reset when I get to a new year?
I believe the below is what you are after. Note I have reversed the table in order put in time ascending order initially.
reverse update YTD:sums Number by tool,date.year from reverse t
date tool Number YTD
------------------------
2019.03 Bolts 100 160
2019.03 Nuts 50 160
2019.02 Bolts 10 60
2019.02 Nuts 100 110
2019.01 Bolts 50 50
2019.01 Nuts 10 10
2018.12 Bolts 10 30
2018.12 Nuts 10 40
2018.11 Bolts 20 20
2018.11 Nuts 30 30
If your table is ordered by date(descending in your example) you can use below query. Else you can just order it using date xdesc before running the query.
q) update ytd:reverse sums reverse num by date.year,name from t

Checking days which are closest

This is going to be a bit hard to explain but
I have 2 dates, i'm interested in the day and month unless the 2 dates have the same year
2015/12/30 and 2019/01/04
ignoring the year part (kind of) the result i'm expecting 6 days difference for the 2 dates above
if i was to use date.DayOfYear i would get 364 (2015/12/30) and 4 (2019/01/04) respectively
however if the years are same then
2019/01/04 2019/12/30 then the result i'm expecting is 359 days difference
is there a clever way of doing this without peppering the code with if statements?
If the year difference was just 0 or 1, then you could just convert both dates to an absolute value (in seconds, ms, unix time or whatever), subtract the 2 values and convert it to full days. But if you have more than 1 year diff (as 2015 and 2019), then you have to have an "if" an think about what are you actually trying to do there. You shouldn't use DayOfYear, because some years have 366 days, so you can't be sure that 364 corresponds to 1 or 2 days left in that year.