我要求在C ++中获得到月末的时间间隔。是否有任何C ++ API可以轻松地为我做到这一点?
我需要启动一个计时器,它将在下个月的第一天00:00:00到期。为此,我需要计算时间间隔,即从现在到本月底的秒数。
答案 0 :(得分:0)
它相当直接的前锋。在{em> now 和当月的最后一天之间获得number of days
的差异。然后得到the number of seconds in a day
。
现在您已the number of seconds till the end of the month
,现在用the number of seconds right now so far today
减去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;
}
。
public class DBHelper extends SQLiteOpenHelper {
public DBHelper(Context context){
super(context, DATABASE_NAME , null, 1);
}
private static final String DATABASE_TABLE_QUOTES = "quotes";
public void onCreate(SQLiteDatabase db) {
String CREATE_TABLE_QUOTES = "CREATE TABLE IF NOT EXISTS `" + DATABASE_TABLE_QUOTES + "` (\n" +
"\t`quote_id`\tINTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,\n" +
"\t`quote`\tTEXT NOT NULL\n" +
");";
db.execSQL(CREATE_TABLE_QUOTES);
db.execSQL("INSERT INTO " + DATABASE_TABLE_QUOTES + "VALUES (1, 'test 1')");
db.execSQL("INSERT INTO " + DATABASE_TABLE_QUOTES + "VALUES (2, 'test 2')");
db.execSQL("INSERT INTO " + DATABASE_TABLE_QUOTES + "VALUES (3, 'test 3')");
db.execSQL("INSERT INTO " + DATABASE_TABLE_QUOTES + "VALUES (4, 'test 4')");
// ........
}
}