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

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.

Related

Total date calculation for a list of date intervals

I have an array of start-end dates (work experience). What I need is to calculate total duration of these experiences.
Example 1:
[ {"startDate": "1999-01-01", "endDate": "1999-12-31"}, {"startDate": "2000-01-01", "endDate": "2000-12-31"} ]
This data should produce this result: 2 Years, 0 Months, 0 Days
Example 2:
[ {"startDate": "1999-01-01", "endDate": "1999-02-28"}, {"startDate": "2000-03-01", "endDate": "2000-05-31"} ]
This data sould produce: 0 Years 5 Months 0 Days
My current code is like this:
int totalDays = 0;
foreach (experience in experiences){
totaldays += experience.endDate.Subtract(experience.startDate);
}
int years = totalDays / 365;
totalDays -= years * 365;
int months = totalDays / 30;
totalDays -= months * 30;
int days = totalDays;
There are 2 problems I'm struggling with:
1: I cannot omit leap days. My result for first example is: 2 Years 0 Months 1 Days (February 29th 2000)
2: I cannot calculate days in months correctly. My result for second example is: 0 Years 5 Months 1 Days (151 / 30)
Is there a correct way to do this?

How to convert a random distributed vector to datetime object in MATLAB

I have a vector with random distributed values from 0 to 10 in increasing value, e.g. [1 3 4 9 10]. How can i convert this vector to a datetime object with time values between e.g. November and December such that these numbers represent the corresponding times in between?
Example, if x = [1 2 3] and I want the time period the whole January, then the output should be [1st January, 15th January, 30th January], according to their relative values.
Example, if x = [0 0.5 9 10] and we have entire January then 0 should map to the first day in January and 10 to the last day in January. 0.5 will map to the date at part 0.5/10 = 1/20 starting from the first January to the last. That date will be approximately 30 * 1 / 20 = 1 day and a half into January. Now, the 9 will in the same way be in position 9 / 10 of 30 days. That is 30 * 9 / 10 = 27. That is the 27th day of January. So the output should be [1st January, 1.5th January, 27th January, 30th January] in datetime format.
You can use datenum and some basic arithmetic to arrive at the following solution:
formatIn = 'dd.mm.yyyy';
d1 = '01.01.2017'; % user input, should be the earlier date
d2 = '31.01.2017'; % user input, should be the later date
x = [0 0.5 5 7 10]; % user input
d1 = datenum(d1,formatIn);
d2 = datenum(d2,formatIn);
daysAfter_d1 = d2-d1;
x = x/max(x);
addDays = round(daysAfter_d1*x);
interpolatedDates = d1 + addDays;
datestr(interpolatedDates,formatIn)
ans =
01.01.2017
03.01.2017
16.01.2017
22.01.2017
31.01.2017

How to calculate the day of the week based on unix time

I know that there are functions/classes in most programming languages to do that, but I would like to know the calculation.
So: How do I get from the unix time in seconds to a day-number (e.g. 0 for Sunday, 1 for Monday etc.)?
Thanks in advance. BTW: this is my first post on Stack Overflow.
The problem you ask is reasonably easy, compared to how ridiculously complicated other date/time functions can be (e.g. Zeller's congruence).
Unix time is defined as the number of seconds elapsed after January 1, 1970, at 00:00 (midnight) UTC.
You can look up a calendar to find that 1970-01-01 was a Thursday. There are 24 * 60 * 60 = 86400 seconds in a day.
Therefore values 0 to 86399 are Thursday, 86400 to 172799 are Friday, 172800 to 259199 are Saturday, etc. These are blocks of 86400 seconds aligned at 0.
Suppose T is your Unix timestamp. Then floor(T / 86400) tells you the number of days after 1970-01-01. 0 = Thursday January 1st; 1 = Friday January 2nd; 2 = Saturday January 3rd; etc.
Add 4 and modulo 7. Now 0 → 4; 1 → 5; 2 → 6; 3 → 0; 4 → 1; 5 → 2; 6 → 3; 7 → 4; 8 → 5; 9 → 6; 10 → 0; etc. This is your final answer.
In summary: day of week = (floor(T / 86400) + 4) mod 7.
(This assumes that you want the day of week in UTC. If you want to calculate it for another time zone, you need to perform some addition or subtraction of hours and minutes on T first.)
In JavaScript, days of the week are:
0 = Sun
1 = Mon
2 = Tue
3 = Wed
4 = Thu
5 = Fri
6 = Sat
You can use built-in methods:
// Unix epoch, 4 = Thu
new Date(0).getUTCDay()
// Today, 2 = Tue
new Date().getUTCDay()
Or a custom solution (remember to divide getTime() milliseconds by 1000):
// Unix epoch, 4 = Thu
(Math.floor(new Date(0).getTime() / 86400 / 1000) + 4) % 7
// Today, 2 = Tue
(Math.floor(new Date().getTime() / 86400 / 1000) + 4) % 7
Solution (from Geek for Geeks):
function dayOfWeek(d, m, y) {
let t = [0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4];
y -= (m < 3) ? 1 : 0;
return ( y + y/4 - y/100 + y/400 + t[m-1] + d) % 7;
}
// Unix epoch, 4 = Thu
Math.floor(dayOfWeek(1, 1, 1970))
// Today, 2 = Tue
Math.floor(dayOfWeek(7, 12, 2021))
https://www.geeksforgeeks.org/find-day-of-the-week-for-a-given-date/

Which coordinate format is this?

How to split these coordinates to degrees, minutes and seconds?
E0732931.00 and N30 2025.20
Is it 73 degrees, 29 mins and 31 seconds. To convert it to decimal degree, i have to
73+39/60+31/3600?
N30 2025.20 is 30 degs, 20 minutes, 25 seconds? What is the value after the decimal?
2nd i want to ask that is there any coordinate system which uses 100 seconds in a minute, i.e.
decimal degrees = 73+39/60+31/(60*100) ???
Your asumption are correct.
Further:
What is the value after the decimal?
the fraction of seconds: so 25.20 seconds
is there any coordinate system which uses 100 seconds in a minute, i.e.
No! There are other systems like DM which uses Degrees and decimal minutes: e.g 23 deg 17.1234 minutes

How can I get approximate time passed since some timestamp?

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.