Get seconds until end of month in C++ - c++

I have a requirement to get time interval until end of month in C++. Are there any C++ APIs which can easily do that for me?
I need to start a timer which will expire at the first day of next month at 00:00:00 hours. For that, I need to compute the time interval i.e. number of seconds from now till end of this month.

Its rather straight foward. Get the difference in number of days between now and the last day of the month. Then get the number of seconds in a day.
Now you have the number of seconds till the end of the month, now subtract that with the number of seconds right now so far today and you will get the number of seconds until the end of the month.
#include <time.h>
#include <iostream>
using namespace std;
int main(){
int day1,month1,year1;
int day2,month2,year2;
int i,temp,DaysDiff=0;
int month[]={31,28,31,30,31,30,31,31,30,31,30,31};
int totalDiffDaysSecs=0;
int nowSeconds=0;
int expire=1000;
time_t t = time(0); // get time now
struct tm * now = localtime( & t );
cout<<"\n";
day1=now->tm_mday;
month1=(now->tm_mon + 1);
year1=(now->tm_year + 1900);
day2=31;
month2=7;
year2=2015;
temp=day1;
for(i=month1;i<month2+(year2-year1)*12;i++){
if(i>12){
i=1;
year1++;
}
if(i==2){
if(year1%4==0 && (year1%100!=0 || year1%400==0))
month[i-1]=29;
else
month[i-1]=28;
}
DaysDiff=DaysDiff+(month[i-1]-temp);
temp=0;
}
cout <<"Current Time = "<< now->tm_hour << ":" << now->tm_min << "."<<now->tm_sec<<"\n";
cout <<"Today's date "<< (now->tm_year + 1900) << '-' << (now->tm_mon + 1) << '-' << now->tm_mday << " \n";
cout <<"Target date "<< year2 << '-' << month2 << '-' << day2 << " \n";
DaysDiff=DaysDiff+day2-temp;
cout<<"Target Month = "<<i<<"\n";
cout<<"Days in Target month = "<<month[i-1]<<"\n";
cout<<"Days diff Today - Target Month = "<<DaysDiff<<" \n";
totalDiffDaysSecs=DaysDiff*24*60*60;
nowSeconds= (now->tm_hour * 3600) + (now->tm_min * 60)+ now->tm_sec;
cout<<"Total seconds in a day = "<<24*60*60<<" \n";
cout<<"current total seconds so far today = "<< nowSeconds <<"\n";
cout<<"Total number of seconds in "<< DaysDiff <<" days = "<< totalDiffDaysSecs <<"\n";
cout<<"\n\n";
while(expire>0){
t = time(0); // get time now
now = localtime( & t );
nowSeconds= (now->tm_hour * 3600) + (now->tm_min * 60)+ now->tm_sec;
expire=totalDiffDaysSecs - nowSeconds;
cout <<"Seconds until end of month = "<< expire <<" \r";
}
cout<<"\n\n";
cout<<"Countdown expired.\n\n";
return 0;
}

Related

Generating Year, Months, Weeks and Days from user input

I’m creating an application that generates Year, month, weeks and days from a user input
I’ve tried this but only the years and months work
So for example when I put in 30 days, it says 1 month, 2 weeks and 2 days instead of just 1 month
Thanks
// Description: This program prompts the user for an integer, which will represents the total
////number of days; The program then will break it apart into years, months, weeks, and days ////(each one of these will have their own local variables), and once this is done, the outcome will be //displayed on the screen.
//Enter no.of days : 1234Years : 3Months : 4Weeks : 2Days : 5
#include <iostream>
#include <cmath>
using namespace std;
const int daysInWeek = 7;
const int days_in_month = 30;
const int days_in_year = 365;
const int days_in_days = 1;
int main()
{
//Local variables
int totalDays;
int years;
int months;
int weeks;
int days;
//program info/intro
cout << "My name is Diana\n";
cout << "Program 1: Convert Number of Days to Years, Months, Weeks, and Days" << endl;
cout << "---------------------------------------------------------------- ----- \n";
//get numbers and develop math progress
cout << "Enter the total number of days : ";
cin >> totalDays;
years = totalDays / days_in_year;
months = (totalDays % days_in_year) / days_in_month;
weeks = (days_in_month % daysInWeek);
/*weeks = (totalDays%days_in_year) / daysInWeek;*/
days = (totalDays% days_in_year) % daysInWeek;
// Display it in the screen
cout << " " <<
cout << "Years = " << years <<
cout << "Months = " << months << endl;
cout << "Weeks = " << weeks << endl;
cout << "Days = " << days << endl;
system("pause");
return 0;
}
As already suggested a better approach would be to keep track of the remaining days:
years = totalDays / days_in_year;
totalDays %= days_in_year;
months = totalDays / days_in_month;
totalDays %= days_in_month;
weeks = totalDays / days_in_week;
totalDays %= days_in_week;
days = totalDays;

Converting seconds into days, hours, minutes, seconds format (C++)

I was doing the following programming exercise:
Write a program that asks the user to enter the number of seconds as an integer
value (use type long, or, if available, long long) and that then displays the equivalent
time in days, hours, minutes, and seconds. Use symbolic constants to represent
the number of hours in the day, the number of minutes in an hour, and the number
of seconds in a minute. The output should look like this:
Enter the number of seconds: 31600000
31600000 seconds = 365 days, 17 hours, 46 minutes, 40 seconds
So I wrote this (in Microsoft Visual Studio 2015):
#include "stdafx.h"
#include iostream
int main()
{
using namespace std;
const int sec_per_min = 60;
const int min_per_hr = 60;
const int hr_per_day = 24;
cout << "Enter numbers of second: ";
long long seconds;
cin >> seconds;
int day, hr, min, sec;
day = seconds / (sec_per_min * min_per_hr * hr_per_day);
hr = (seconds - day * hr_per_day * min_per_hr * sec_per_min) / (sec_per_min * min_per_hr);
sec = seconds % sec_per_min;
min = (seconds - sec) / sec_per_min % min_per_hr;
cout << seconds << " seconds = ";
cout << day << " days, ";
cout << hr << " hours, ";
cout << min << " minutes, ";
cout << sec << " seconds.";
return 0;
}
It produced the correct outcome.
But I was wondering if there's better statements for day, hr, min, sec?
I would've done the following. I think it's much clearer:
auto n=seconds;
sec = n % sec_per_min;
n /= sec_per_min;
min = n % min_per_hr;
n /= min_per_hr;
hr = n % hr_per_day;
n /= hr_per_day;
day = n;

How convert string to datetime c++?

string s = v[2];
string result = s.substr(s.find_last_of("modify=") + 1, 14);
cout << result;//output MDTM boost.txt 20150911115551
i want convert like this YYYY-MM-DD HH:MM
To access date and time related functions and structures, you would need to include header file in your C++ program.
There are four time-related types: clock_t, time_t, size_t, and tm. The types clock_t, size_t and time_t are capable of representing the system time and date as some sort of integer.
The structure type tm holds the date and time in the form of a C structure having the following elements:
struct tm {
int tm_sec; // seconds of minutes from 0 to 61
int tm_min; // minutes of hour from 0 to 59
int tm_hour; // hours of day from 0 to 24
int tm_mday; // day of month from 1 to 31
int tm_mon; // month of year from 0 to 11
int tm_year; // year since 1900
int tm_wday; // days since sunday
int tm_yday; // days since January 1st
int tm_isdst; // hours of daylight savings time
}
Considering that you have the data string in the following format "YYY-MM-DD HH:MM:SS", the subsequent code below show how to convert the string in a date structure:
#include <iostream>
#include <ctime>
#include <string.h>
#include <cstdlib>
using std::cout;
using std::endl;
int main(int argc, char* argv[])
{
char date[] = "2012-05-06 21:47:59";
tm *ltm = new tm;
char* pch;
pch = strtok(date, " ,.-:");
ltm->tm_year = atoi(pch); //get the year value
ltm->tm_mon = atoi(strtok(NULL, " ,.-:")); //get the month value
ltm->tm_mday = atoi(strtok(NULL, " ,.-:")); //get the day value
ltm->tm_hour = atoi(strtok(NULL, " ,.-:")); //get the hour value
ltm->tm_min = atoi(strtok(NULL, " ,.-:")); //get the min value
ltm->tm_sec = atoi(strtok(NULL, " ,.-:")); //get the sec value
// print various components of tm structure.
cout << "Year: "<< ltm->tm_year << endl;
cout << "Month: "<< ltm->tm_mon<< endl;
cout << "Day: "<< ltm->tm_mday << endl;
cout << "Time: "<< ltm->tm_hour << ":";
cout << ltm->tm_min << ":";
cout << ltm->tm_sec << endl;
delete ltm;
return 0;
}
Output:
Year: 2012
Month: 5
Day: 6
Time: 21:47:59

Converting seconds to hours and minutes and seconds

#include <cstdio>
#include <iostream>
using namespace std;
int main ()
{
int seconds, hours, minutes;
cin >> seconds;
hours = seconds/3600;
cout << seconds << " seconds is equivalent to " << int(hours) << " hours " << seconds%(hours*60)
<< " minutes " << (seconds%(hours*3600))-((seconds%(hours*60))*60) << " seconds.";
}
For some reason, this program works with only numbers above 3600. Does anyone know how to fix this problem? Whenever I do a number below 3600, the screen shows up with a message from Windows saying that the program has stopped working.
Try this out instead, tested and works:
int seconds, hours, minutes;
cin >> seconds;
minutes = seconds / 60;
hours = minutes / 60;
cout << seconds << " seconds is equivalent to " << int(hours) << " hours " << int(minutes%60)
<< " minutes " << int(seconds%60) << " seconds.";
Because minutes is seconds/60, dividing it by 60 again is equivalent to diving seconds by 3600, which is why it works.
seconds/3600 is integer division, so for seconds < 3600, hours is 0, then things like seconds%(hours*3600) becomes seconds % 0, causing a division-by-zero.
Let's first get the logic right. Suppose you want to write 5000 seconds as x hours y minutes z seconds, such that all three are integers and neither y nor z is greater than 59. What do you do?
Well, you can first write it as q minutes z seconds, such that both are integers and z is not greater than 59. That's easy:
q = 5000 / 60 = 83 // integer division
z = 5000 % 60 = 20
So 5000 seconds is 83 minutes 20 seconds. Now how do you write 83 minutes into x hours y minutes, such that both are integers and y is no greater than 59? You do the same thing:
x = 83 / 60 = 1
y = 83 % 60 = 23
OK, let's generalize this:
int total, seconds, hours, minutes;
cin >> total;
minutes = total / 60;
seconds = total % 60;
hours = minutes / 60;
minutes = minutes % 60;
cout << total << " seconds is equivalent to " << hours << " hours " << minutes
<< " minutes " << seconds << " seconds.\n" ;
You've got a divide-by-zero problem here:
seconds % (hours*60);
hours is 0 by virtue of integer division.
hours = seconds/3600;
From what you're trying to do, you should consider conditional logic to print the minutes if the total number of seconds is greater than 3600. You'll also want to investigate similar logic in the next part of your print stream.
My C++ is rusty, so forgive if this isn't exactly valid syntax:
cout << (seconds > 3600 ? seconds % (hours*60) : seconds) << endl;
Try this:
int totalSecond;
cin >> totalSecond;
int hour = totalSecond / 3600;
int minute = (totalSecond % 3600) / 60;
int second = totalSecond % 60;
cout << hour << ":" << minute << ":" << second << endl;
Assuming that totalSeconds is the number of seconds since midnight and is less than 86400
With c++20 this gets pretty easy using std::chrono::hh_mm_ss:
#include <iostream>
#include <chrono>
int main ()
{
int seconds;
std::cin >> seconds;
std::chrono::hh_mm_ss time{std::chrono::seconds(seconds)};
std::cout << seconds << " seconds is equivalent to " << time.hours().count() << " hours " << time.minutes().count()
<< " minutes " << time.seconds().count() << " seconds.";
}
by using function;
#include<iostream>
using namespace std;
int hour(int h)
{
int second;
//second=(h/3600);
if (h>3600)
second=h/3600;
else
second=(h/3600);
return (second);
}
int minute(int m)
{
int second2;
second2=( );
return(second2);
}
int second(int s)
{
int second3;
second3=((s-3600)%60);
return (second3);
}
void main()
{
int convert;
cout<<"please enter seconed to convert it to hour\b";
cin>>convert;
cout<<"hr : min : sec \n";
cout<<hour(convert)<<":"<<minute(convert)<<":"<<second(convert)<<endl;
system("pause");
}
Until now none of the presented solution have in count the day duration. I had to write a clock to use in arduino, the millis() function return milliseconds since turn on, to get seconds just divide by 1000.
void showTime(unsigned long seconds){
printf( "seconds : %lu ", seconds);
int hh = (seconds/3600) %24;
int mm = (seconds/60) %60;
int ss = seconds%60;
printf( " Time : %2d:%02d:%02d\n", hh, mm,ss);
}
showTime( 0*24*3600 + 4*3600+35*60+25); // 0d+4hs+35m+25s;
showTime( 2*24*3600 + 1*3600+45*60+59); // 2d+1hs+45m+59s;
showTime(40*24*3600 + 10*3600+ 5*60+10); // 40d+10hs+5m+10s;
wath print:
seconds : 189325 Time : 4:35:25
seconds : 6359 Time : 1:45:59
seconds : 3492360 Time : 10:06:00
Using std::chrono::duration_cast, one can simply write (refer std::chrono::duration for more options):
#include <chrono>
#include <iostream>
using namespace std::chrono;
int main() {
int s; std::cin >> s;
seconds sec(s);
std::cout << duration_cast<hours>(sec).count() << ':'
<< duration_cast<minutes>(sec).count() % 60 << ':'
<< sec.count() % 60;
}
One string convertion from seconds to hh:mm:ss format:
sprintf(tempBuffer, "%02u:%02u:%02u", _time / 3600, (_time % 3600) / 60, _time % 60);
Try this
int h,m,s;
cout << "Time in Seconds = ";
cin >> s;
cout << "H:M:S - " << s / 3600 << ":" << (s % 3600) / 60 << ":" << s % 60;
Input:
Time in Seconds = 25300
Output:
H:M:S - 7:1:40
Have a look at this code:
#include <iostream>
using namespace std;
int main()
{
int seconds;
cout << "Enter an integer for seconds: ";
cin >> seconds;
int minutes = seconds / 60;
int remainingSeconds = seconds % 60;
cout << seconds << " seconds is " << minutes <<
" minutes and " << remainingSeconds << " seconds " << endl;
return 0;
}

C++ Output error

I have a problem with my program and I would appreciate help.
It's supposed to allow you to enter an account number, a service code, and the number of minutes the service was used. The program then calculates the bill and it varies depending on your service. When I execute the program, it doesn't allow you to enter anything.
Regular service:$10.00 plus first 50 minutes free. Charges for over 50 minutes are $0.20 per minute.
Premium service:
$25.00 plus:
a)
For calls made from 6:00 am to 6:00 pm, the first 75 minutes are free; charges for over
75 minutes are $0.10 per minute.
b)For calls made from 6:00 pm to 6:00 am, the first 100 minutes are free; charges for over 100 minutes are $0.05 per minute.
Here is the program that I've typed.
#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
int main()
{
int minutes = 0,
day_minutes = 0, //minutes used during the day
night_minutes = 0; //minutes used during the night
string service_code,
account_number;
double final_amount,
final_damount, //final amount for day minutes
final_namount = 0; //final amount for night minutes
cout << "Please enter your account number: ";
cin >> account_number;
cout << "Please enter your service code (r or R for regular service and p or P for premium service): ";
cin >> service_code;
if (service_code == "r")
{
cout << "Please enter the amount of minutes used: " << minutes << endl;
}
if (minutes <= 50)
{
final_amount = 10;
cout << "Your final amount is $: " << final_amount << endl;
}
if (minutes > 50)
{
final_amount = (minutes * 0.20) + 10;
cout << "Your final amount is $: " << final_amount << endl;
}
else if (service_code == "p")
{
cout << "Please enter the amount of minutes used during the day: " << day_minutes << endl;
cout << "Please enter the amount of minutes used during the night: " << night_minutes << endl;
}
if (day_minutes <=75)
{
final_damount = 0;
final_amount = final_damount + final_namount + 20;
}
if (day_minutes > 75)
{
final_damount = day_minutes * 0.10;
final_amount = final_damount + final_namount + 20;
}
if (night_minutes <= 100)
{
final_namount = 0;
final_amount = final_damount + final_namount + 20;
}
if (night_minutes > 100)
{
final_namount = night_minutes * 0.05;
final_amount = final_damount + final_namount + 20;
cout << "Your final amount is: $ " << final_amount << endl;
}
else
cout << "Error, this program does not accept negative numbers.\n";
return 0;
}
Does anyone the problem to my program? Thank you.
In your code you never specify to ask for user input (read from any input streams or files), hence it doesn't ask for any input.
You will probably want to somewhere do something such as cin or cin.readline or any of several other various methods to read the user's input from stdin.
To avoid duplicating other questions I'm not putting further details here.