c++ Changing from second into days,hours.minutes and second - c++

cout << "Please enter the elapsed time in seconds or (0 to end the program): " ;
cin >> totalSeconds;
days = totalSeconds / 86400 % 60;
hours = totalSeconds/ 3600 % 60;
minutes = totalSeconds/ 60 % 60;
seconds = totalSeconds % 60;
The small number works fine but once I start using bigger number it's not working anyone knows why? Here are the logs :
Please enter the elapsed time in seconds or (0 to end the program): 62
The equivalent time of 62 seconds in days:hours:minutes:seconds is: :0:0:1:2
Please enter the elapsed time in seconds or (0 to end the program): 9630
The equivalent time of 9630 seconds in days:hours:minutes:seconds is: :0:2:40:30
Please enter the elapsed time in seconds or (0 to end the program): 216000
The equivalent time of 216000 seconds in days:hours:minutes:seconds is: :2:0:0:0
both the 62 and 9630 second works fine but not the 216000s

Let's start with your formula for days:
days = totalSeconds / 86400 % 60;
If totalSeconds is 5270400 (61 days), that equation will compute 1 for days. That's probably not your only bug.
Your modulus parameter of 60 on the right of the % doesn't appear correct or it's not needed as you are using it.
This is probably what you want without being clever.
days = totalSeconds / 86400;
totalSeconds = totalSeconds % 86400;
hours = totalSeconds / 3600;
totalSeconds = totalSeconds % 3600;
minutes = totalSeconds / 60;
totalSeconds = totalSeconds % 60;
seconds = totalSeconds;

Related

c++ adding years and days using date.h

Working on calendar duration arithmetic using date.h and std::chrono, but getting an unexpected result.
Sample code is:
#include "date.h"
#include <string>
#include <chrono>
#include <iostream>
int main() {
date::sys_seconds calendarDate = {};
calendarDate = std::chrono::years(30) + date::sys_seconds(std::chrono::days(10));
std::string stringDate = date::format("%Y-%m-%d %H:%M:%S", calendarDate);
std::cout << "{} + 30 years + 10 days = " << stringDate << "\n";
return 0;
}
Actual Output:
{} + 30 years + 10 days = 2000-01-11 06:36:00
Expected Output:
{} + 30 years + 10 days = 2000-01-11 00:00:00
Using Ubuntu 22.04; g++ 11.3.0
Compiled with: gcc -g -std=c++20 main.cpp -lstdc++
Using date.h fromm here: https://raw.githubusercontent.com/HowardHinnant/date/master/include/date/date.h
Any insight into what's adding in the extra 6hours and 36minutes?
It is because of leap time. For the Gregorian calendar, the average length of the calendar year (the mean year) across the complete leap cycle of 400 years is 365.2425 days (97 out of 400 years are leap years).
This is taken into account in the definition of std::chrono::years which is defined as a duration type with Period std::ratio<31556952> (60 * 60 * 24 * 365.2425 = 31556952).
However, if you compare this to the non-leap-aware duration of a year (60 * 60 * 24 * 365 = 31536000), then you get a difference of 20952 seconds or 5 hours 49 minutes and 12 seconds. If you multiply that difference by 30 it grows to:
7 days 6 hours 36 minutes and 0 seconds - and at least the latter part should look familiar to you.
But what about the 7 days? Well, it turns out that between 1970 and 2000: 1972, 1976, 1980, 1984, 1988, 1992, and 1996 were leap years - if you count that is 7, and that is where the days went.
You can check this by yourself by changing your code and looking at the output:
calendarDate += std::chrono::seconds(60 * 60 * 24 * 365 * 30); - "1999-12-25 00:00:00"
calendarDate += std::chrono::seconds(60 * 60 * 24 * 365 * 30) + day(7); - "2000-01-01 00:00:00"
By the way, the problem is that std::chrono::years gives you the length of an average year, while the date.h library knows (and cares about) the specific years: It knows that 1970 was not a leap year (so 60 * 60 * 24 * 365 = 31536000 seconds), but 1972 was a leap year (so 60 * 60 * 24 * 366 = 31622400 seconds).
If you had added 31 years instead, then you would have added 5:49:12 more skew from the average year, but then removed 1 more day since 2000 was also a leap year - so the time it would print would still be in 2000, but ~12 hours before new-years 2001.
In addition to Frodyne's good answer (which I've upvoted), I wanted to add that you can do either chronological arithmetic or calendrical arithmetic with date.h.
You've done chronological arithmetic, where years is considered to be a uniform unit, and is equal to the average civil year.
Calendrical arithmetic follows the irregularity of calendars, and is what you expected. This is how you would accomplish that:
#include "date/date.h"
#include <string>
#include <chrono>
#include <iostream>
int main() {
auto chronologicalDateTime = date::sys_seconds(date::days(10));
auto chronologicalDate = date::floor<date::days>( chronologicalDateTime);
date::year_month_day calendarDate = chronologicalDate;
auto timeOfDay = chronologicalDateTime - chronologicalDate;
calendarDate += date::years{30};
chronologicalDateTime = date::sys_days{calendarDate} + timeOfDay;
std::string stringDate = date::format("%Y-%m-%d %H:%M:%S", chronologicalDateTime);
std::cout << "{} + 30 years + 10 days = " << stringDate << "\n";
return 0;
}
You first convert the chronological date/time to a calendrical date, and add the years to the calendrical date. Then convert that back into a chronological date/time.
{} + 30 years + 10 days = 2000-01-11 00:00:00
Here's another SO answer that goes over the same principle, except using months: https://stackoverflow.com/a/43018120/576911
Linux stores dates as UTC. My guess is that your machine is located at Central Time zone (CT) which is +6 hours from UTC.

How would you get the epoch time and convert it to local time in arm assembly on a raspberry pi?

So I am programming in arm assembly on raspbian and I am trying to convert the epoch time using c/c++ libraries because that is what I am allowed to do, but I am confused as to how to do it. If I simply bl time it will give me the epoch time, but I am confused as to how I would get the return value in r0, then convert that into the local time in assembly using C or C++ libraries. I know localtime/gmtime and strftime exist, but its not as easy as getting the epoch and just bl localtime or bl strftime. Then I want to format it where I only get the local time and maybe am/pm. I am not interested in the date. I just need some helpful code, or some direction to be pushed into. Thanks
Edit: If its easier to just convert using math that would also be helpful
So to not do your homework for you, here is an example. All of this is basic grade school math, no magic.
If I have 345678 pennies how does that brake down into various dollar and coin amounts.
There are 100 pennies in a dollar and 100 dollars in a 100 dollar bill, so
345678 / (100*100) = 34 remainder 5678
Looking at units
pennies / (pennies/dollar * dollars/hundred) =
(pennies * dollars * hundreds) / (pennies * dollars )
pennies and dollars cancel out left with hundreds which is correct
so 34 hundred dollar bills with a remainder of 5678 pennies
repeat for 20 dollar bills
5678 / (100 * 20) = 2 remainder 1678
10 dollar bills
1678 / (100 * 10) = 1 remainder 678
5 dollar bills
678 / (100 * 5) = 1 remainder 178
one dollar bills
178 / (100 * 1) = 1 remainder 78
50 cent pieces
78 / 50 = 1 remainder 28
quarters (25 cents)
28 / 25 = 1 remainder 3
dimes (10 cents) 0 remainder 3
nickles (5 cents) 0 remainder 3
pennies the remainder 3
so 345678 pennies is equal to
34 100 dollar bills +
2 20 dollar bills +
1 10 dollar bill +
1 5 dollar bill +
1 1 dollar bill +
1 half dollar coin +
1 quarter +
3 pennies
check the work
34 * 100 * 100 = 340000
2 * 100 * 20 = 4000
1 * 100 * 10 = 1000
1 * 100 * 5 = 500
1 * 100 = 100
1 * 50 = 50
1 * 25 = 25
1 * 3 = 3
add that up you get 345678
If I simply wanted to know how many quarters
345678 / (25 * 1) = 13827 quarters with a remainder of 3.
it all works the same with 60 seconds per minute 60 minutes per hour 24 hours per day. 365 days for 1970, 365 days for 1971, 366 days for 1972 365 days for 1973 and so on
31 days for january, 28 days for february 2019, 31 days for march and so on
easier to adjust for timezone first 60 seconds * 60 minutes * hours of adjustment
add or subtract that off as needed, then work that number either through the years/months/days or if you simply care about time of day then you only need to divide by seconds per day. or you can divide by seconds per day and get total days as a result with fraction of a day as a remainder, the fraction of a day is the time of day today and the total days you can then later subtract off the years then months to find the date.
Extra credit, what year will computers with 32 bit time counters using the 1970 epoch have a Y2K like roll over event (causing crashes and death and destruction across the planet just like Y2K)?
The programming language is irrelevant until the algorithm is understood and ideally coded in a favorite high level language, to confirm/prove the algorithm. Then port that knowledge to some other programming language.
Shortcutting the steps will sometimes get you there faster but when the shortcut fails it fails in spectacular fashion.

How to get hours value greater than 24 hours in python 2.7?

time1 = timedelta(days=2, hours=6.20)
time2 = timedelta(hours=20.10)
sum_time = time1 + time2
print str(sum_time)
print sum_time.total_seconds() / 3600
Output:
3 days, 2:18:00
74.3
How to get output 74:18:00 ?
With total_Seconds / 3600 you only get the hours in decimal format.
You can use divmod to break down the seconds into full hours, minutes and seconds:
divmod(a, b)
Take two (non complex) numbers as arguments and return a pair of numbers consisting of their quotient and remainder when using long division
The code would look like this (I added 34 seconds in time2 to check if the seconds part is correct):
from datetime import timedelta
time1 = timedelta(days=2, hours=6.20)
time2 = timedelta(hours=20.10, seconds=34)
sum_time = time1 + time2
print str(sum_time)
hours, seconds = divmod(sum_time.total_seconds(), 3600)
minutes, seconds = divmod(seconds, 60)
print "%d:%02d:%02d" % (hours, minutes, seconds)
and the output will be:
3 days, 2:18:34
74:18:34
The result of the first divmod is 74 hours (quotient) and a remainder of 1114 seconds.
The second divmod is feeded with the remaining seconds from the line before (1114) and gives a result of 18 minutes and 34 seconds.

double if then in sas

I want to write a code like this:
DATA TEST;
SET original;
IF hour = 30 AND minutes = . or 0 THEN new_minutes = 30;
ELSE if hour = 60 AND minutes = . or 0 then new_minutes = 60;
RUN;
If I used
IF hour = 30 and minutes = . OR minutes = 0 THEN new_minutes = 30; THEN even if the new value should be 60 it changes to 30. I there a way to get around this besides changing all 0 to . (or all . to 0)
It sounds like you need ( ).
IF hour = 30 AND (minutes = . or minutes = 0) THEN new_minutes = 30;
Or you could use the in statement
IF hour = 30 AND minutes in ( . , 0) THEN new_minutes = 30;
You could use coalesce for this; that returns the first nonmissing result.
if hour = 30 and coalesce(minutes,0) = 0 then minutes=30;
I would also suggest that you could write it this way to simplify the code:
if hour in (30,60) and coalesce(minutes,0) = 0 then minutes=hour;
And you could even generalize that first part potentially, depending on what are otherwise legal value for hour. If hour can't be over 24 then
if hour > 24 and coalesce(minutes,0) = 0 then minutes=hour;
Would be one possibility, as well as checking to see if hour is divisible by 15 or 10 or something like that.
You could even write it without any if statements, providing you can accept new_minutes being zero if the criteria are not satisfied :
data want ;
set have ;
new_minutes = (hour in(30,60) and minutes in(.,0)) * hour ;
run ;

What is the meaning of using "days*24*60*60*1000" in cookie?

What is the meaning of using days * 24 * 60 * 60 * 1000 in a cookie?
I've seen it quite a lot and I don't understand what it means.
I need to create a function that reads the cookie and puts the value in a text box while uploading the page.
<body onload="readCookie()">
Basically, 1000 is used here just for converting seconds to milliseconds.
Number of seconds in a day = 24 * 60 * 60 = 86400 seconds.
1 second = 1000 milliseconds.
So after calculating the expression, the result is in milliseconds.
days * 24 * 60 * 60 * 1000 = days * 86400000 ms
Cookies can be set with an “expiry date”, and that is given in milliseconds.
Days * 24 * 60 * 60 * 1000 is therefore the milliseconds in a day – 1000 milliseconds * 60 seconds * 60 minutes * 24 hours * days