Flutter/Dart-Convert seconds to months, days and hours using simple_moment - flutter

Assuming I have a certain amount of seconds (eg:13456789456),How can I convert this to months,days and hours using the simple_moment package in flutter.

Try with this package.
import 'package:duration/duration.dart';
import 'package:duration/locale.dart';
void main() {
// => 5 days 9 hours
printDuration(aDay * 5 + anHour * 9);
// More examples
final dur = Duration(
days: 5,
hours: 23,
minutes: 59,
seconds: 59,
milliseconds: 999,
microseconds: 999);
// => 5 days 23 hours 59 minutes 59 seconds
printDuration(dur);
// => 3.455 milliseconds
printMilliseconds(aMicrosecond * 3455);
// => 3 seconds
printDuration(aMillisecond * 3000);
// => 2 seconds
printDuration(aMillisecond * 2250);
// => 1 day 3 hours 2 minutes
printDuration(aMillisecond * 97320000);
// => 5 días 9 horas
printDuration(aDay * 5 + anHour * 9,
abbreviated: false, locale: spanishLocale);
// => 5d, 23h, 59m, 59s, 999ms, 999us
printDuration(dur, abbreviated: true, tersity: DurationTersity.all);
// => 5 whole days 9 whole hours
printDuration(aDay * 5 + anHour * 9, spacer: ' whole ');
// => 5 days, 9 hours, 10 minute
printDuration(aDay * 5 + anHour * 9 + aMinute * 10, delimiter: ', ');
// => 5 days, 9 hours and 10 minutes
printDuration(aDay * 5 + anHour * 9 + aMinute * 10,
delimiter: ', ', conjugation: ' and ');
}

Related

List the number of days in current date/month [duplicate]

This question already has answers here:
Get last day in month of time.Time
(4 answers)
Closed 5 months ago.
How to list the number of the days in the current date/month? Exp: January have 31 day so 1 2 3 4 till 31
Keep things simple.
The Go time package normalizes dates:
The month, day, hour, min, sec, and nsec values may be outside their usual ranges and will be normalized during the conversion. For example, October 32 converts to November 1.
For the number of days in a month
func DaysInMonth(t time.Time) int {
y, m, _ := t.Date()
return time.Date(y, m+1, 0, 0, 0, 0, 0, time.UTC).Day()
}
For a list of days in a month
func ListDaysInMonth(t time.Time) []int {
days := make([]int, DaysInMonth(t))
for i := range days {
days[i] = i + 1
}
return days
}
The days in a month always range from 1 to the number of days in the given month. So the main task is to determine the number of days in a given month.
The time package does not expose such functionality, but you may use the following trick:
// Max days in year y1, month M1
t := time.Date(y1, M1, 32, 0, 0, 0, 0, time.UTC)
daysInMonth := 32 - t.Day()
The logic behind this is that the day 32 is bigger than the max day in any month. It will get automatically normalized (extra days rolled to the next month and day decremented properly). And when we subtract day we have after normalization from 32, we get exactly what the last day was in the month.
This snippet is taken from the answer time.Since() with months and years.
So here's a little helper that returns the days of a month as []int for a given time.Time:
func daysInMonth(t time.Time) []int {
t = time.Date(t.Year(), t.Month(), 32, 0, 0, 0, 0, time.UTC)
daysInMonth := 32 - t.Day()
days := make([]int, daysInMonth)
for i := range days {
days[i] = i + 1
}
return days
}
Testing it:
fmt.Println(daysInMonth(time.Date(2022, 1, 1, 0, 0, 0, 0, time.UTC)))
fmt.Println(daysInMonth(time.Date(2022, 2, 1, 0, 0, 0, 0, time.UTC)))
fmt.Println(daysInMonth(time.Date(2020, 2, 1, 0, 0, 0, 0, time.UTC)))
Output (try it on the Go Playground):
[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]
[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]
[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]
Another, less peformant option is to roll back the date to the first of the month, and start adding days until the month changes. This is how it could look like:
func daysInMonth(t time.Time) []int {
var days []int
// Roll back to day 1
t = time.Date(t.Year(), t.Month(), 1, 0, 0, 0, 0, time.UTC)
m := t.Month()
for t.Month() == m {
days = append(days, t.Day())
t = t.AddDate(0, 0, 1)
}
return days
}
This will output the same. Try this one on the Go Playground.
Since all months contain at least 28 days, we can optimize the above solution to roll to day 29, and start checking and incrementing from there:
func daysInMonth(t time.Time) []int {
days := make([]int, 28, 31)
for i := range days {
days[i] = i + 1
}
m := t.Month()
// Roll to day 29
t = time.Date(t.Year(), t.Month(), 29, 0, 0, 0, 0, time.UTC)
for t.Month() == m {
days = append(days, t.Day())
t = t.AddDate(0, 0, 1)
}
return days
}
Try this one on the Go Playground.
There are many ways to get it done, as we are dealing with constants mostly, and we just need to handle February if it's a Leap Year( 29 days).
package main
import (
"fmt"
"time"
)
func main() {
fmt.Println(list_of_days())
}
func list_of_days() []int {
non_leap_year := map[string]int{
"January": 31,
"February": 28,
"March": 31,
"April": 30,
"May": 31,
"June": 30,
"July": 31,
"August": 31,
"September": 30,
"October": 31,
"November": 30,
"December": 31,
}
leap_year := map[string]int{
"January": 31,
"February": 29,
"March": 31,
"April": 30,
"May": 31,
"June": 30,
"July": 31,
"August": 31,
"September": 30,
"October": 31,
"November": 30,
"December": 31,
}
//get the current month
year, month, _ := time.Now().Date()
//handle leap year
no_of_days := 0
if year%4 == 0 && year%100 != 0 || year%400 == 0 {
no_of_days = leap_year[month.String()]
} else {
no_of_days = non_leap_year[month.String()]
}
days := make([]int, no_of_days)
for i := range days {
days[i] = i + 1
}
return days
}

How to calculate height based on item count in swift

How do I archive this result using a simple mathematical formula.
I have an initial offset = 100, and initial count = 0, that i want to increase the offset based on count value. I tried using the below code but it doesn't function correctly.
Example
When count is 0 to 3, then offset should be 100.
When count is
4 to 6, then offset should be 200.
When count is 7 to 9,
then offset should be 300.
When count is 10 to 12, then offset
should be 400.
Attempt
func getHeight(count: Int) ->CGFloat {
var index = 0
for i in 0..<count{
if(i % 2 != 0){
index += 1
}
}
return CGFloat(100 * index)
//return CGFloat(count + 3 / 9 * 100)
}
Testing
print("0 to 3 = \(self.getHeight(count: 0)), expected = 100")
print("0 to 3 = \(self.getHeight(count: 2)), expected = 100")
print("4 to 6 = \(self.getHeight(count: 4)), expected = 200")
print("7 to 9 = \(self.getHeight(count: 7)), expected = 300")
print("10 to 12 = \(self.getHeight(count: 12)), expected = 400")
Results
0 to 3 = 0.0, expected = 100
0 to 3 = 100.0, expected = 100
4 to 6 = 200.0, expected = 200
7 to 9 = 300.0, expected = 300
10 to 12 = 600.0, expected = 400
Formula with integer division:
let cnt = count != 0 ? count : 1
result = 100 * ((cnt + 2) / 3)

MATLAB create constant spacing between numbers on same line

Good morning,
I'm sure there's a built in function but I can't find it. I want to create static positioning for information being sent to a text document in MATLAB. For example:
height weight age favorite number
------------------------------------------------------------
60 140 24 9
30 45 3 10000000
48 100 9 19
9 7 1 1
currently, i'm just doing an fprint call with padded spaces to get it lined up, but the issue arises where having different length numbers causes the alignment to be off, like so:
height weight age favorite number
------------------------------------------------------------
60 140 24 9
30 45 3 10000000
48 100 9 19
1 7 1 1
Thanks in advance.
here's an example script that'll show what I mean:
fid1 = fopen('stackoverflowtest', 'w');
if fid1 < 3,
error('ERROR');
end;
fprintf(fid1, 'height weight age favorite number \n');
fprintf(fid1, '------------------------------------------------------------ \n');
height = 0;
weight = 10;
age = 100;
number = 3;
for i = 1:100
fprintf(fid1, "%d ', height);
fprintf(fid1, "%d ', weight);
fprintf(fid1, "%d ', age);
fprintf(fid1, "%d \n", number);
height = height + 3;
weight = weight + 6;
age = age - 1;
number = number + 23;
end
You can do this with the fprintf format specification, for example %-15d.
Here, the - is a flag which specifies left justification, and the 15 specifies how much space to leave around the representation.
We can reproduce your example with
A = [60 140 24 9
30 45 3 10000000
48 100 9 19
9 7 1 1];
fprintf('height weight age favorite number \n'),...
fprintf('------------------------------------------------------------ \n'),...
fprintf('%-15d %-15d %-12d %-15d \n',A')
which displays
height weight age favorite number
------------------------------------------------------------
60 140 24 9
30 45 3 10000000
48 100 9 19
9 7 1 1
EDIT: You can store this data as a table:
height = A(:,1)
weight = A(:,2);
age = A(:,3);
favourite_number = A(:,4);
tab1 = table(height, weight, age, favourite_number);
disp(tab1);
This prints to screen
height weight age favourite_number
______ ______ ___ ________________
60 140 24 9
30 45 3 1e+07
48 100 9 19
9 7 1 1
but I'm not sure how to save this representation to a file.

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/

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.