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));
I just stumbled on an interesting problem when trying to calculate birthday from age. Basically the idea is to substract a number of years, months and days from the current date. However depending on the order of subtraction eg smaller to larger units or vice versa the result is different.
For example we need to substract 55 years 3 months and 14 days from 2021-01-13
If I subtract the years first the result will be 1965-09-29
If I subtract the days first the result will be 1965-09-30
It all comes from the difference between the number of days in a month. Now I'm wondering which is the generally accepted order.
Data shared in Google sheet found here:
I am trying to solve a quick issue to calculate the number of training days and working shifts days spent in a small range of calendar week based on a given 'training start date' and the below criteria:
Weeks are Sunday through Saturday
Each new agency staff should complete 4 days of training before being planned to work for a max of 6 days in and one calendar week
When less than 4 training days are completed during the first calendar week, the remaining roll over to the following, immediately followed by working days but not exceeding 6 days of work or training
To exclude Christmas day
Calculated from the given date for "Training start' calculate how many training shifts and working shifts for a period of calendar weeks
Calendar Weeks based on start/finish dates in the "dates' tab
Any advice, feedback or assistance in any way is greatly appreciated.
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.
I am trying create a report that calcualtes the Overtime someone has worked in a week, my understanding of the calulation would be.
My Columns are :
Extra Hours Worked Per Week
Total Hour Worked Per Week
Calculation
Extra Hours Worked Per Week / (Extra Hours Worked Per Week + Total Hour Worked Per Week) * 100
If I represent All the columns as minutes sum them up and do the calculation I get one figure but if I sum up all the minutes in to hours and minutes and do the same calculation I get a different figure. What I want to know is, is the Calculation correct and if so, should I be doing the calculation just using minutes or using hours and minutes.
Hope someone can help.
Assuming you want to calculate the percentage of Extra hours, your calculation should be:
((TotalHours * 100)/(TotalHours - ExtraHours))-100
And my advise to you is to keep the calculation in minutes, and in the end convert it to hours (and minutes).