【发布时间】:2019-09-01 13:20:59
【问题描述】:
我正在尝试制作一个计算器,它可以确定距离特定日期开始的秒数。
我目前能够确定当前日期和时间,但我不确定如何确定距离我想要的未来一天开始的秒数(例如星期六)。
我想我可以计算距星期六还有多少天,然后将其转换为秒,但问题是可能会增加额外的时间(我想知道午夜的时间,一天的开始)。
有人知道这怎么可能吗?
#pragma once
#include <string>
#include <ctime>
class DateTime
{
public:
DateTime();
DateTime(
const time_t& specific_datetime);
const std::string GetWeekDay() const;
const std::string GetMonth() const;
int GetDate() const;
const int GetSecondsUntilNextWeekDay(
const std::string& day_name) const;
private:
const int GetWeekDayNumberFromName(
const std::string& name) const;
tm m_time;
const time_t m_datetime;
DateTime(const DateTime &rhs);
DateTime &operator=(const DateTime &rhs);
};
#include "DateTime.h"
DateTime::DateTime() :
m_datetime(std::time(NULL))
{
localtime_s(&m_time, &m_datetime);
}
DateTime::DateTime(
const time_t& specific_datetime) :
m_datetime(specific_datetime)
{
localtime_s(&m_time, &m_datetime);
}
const std::string DateTime::GetWeekDay() const
{
switch (m_time.tm_wday)
{
case 0:
return "Sunday";
case 1:
return "Monday";
case 2:
return "Tuesday";
case 3:
return "Wednesday";
case 4:
return "Thursday";
case 5:
return "Friday";
case 6:
return "Saturday";
default:
return "Sunday";
}
}
const std::string DateTime::GetMonth() const
{
switch (m_time.tm_mon)
{
case 0:
return "January";
case 1:
return "February";
case 2:
return "March";
case 3:
return "April";
case 4:
return "May";
case 5:
return "June";
case 6:
return "July";
case 7:
return "August";
case 8:
return "September";
case 9:
return "October";
case 10:
return "November";
case 11:
return "December";
default:
return "January";
}
}
int DateTime::GetDate() const
{
return m_time.tm_mday;
}
int DateTime::GetYear() const
{
return 1900 + m_time.tm_year;
}
const int DateTime::GetSecondsUntilNextWeekDay(
const std::string& day_name) const
{
// Calculate how many seconds left for today...
const int todays_hours_left = 23 - m_time.tm_hour;
const int todays_minutes_left = (todays_hours_left * 60) + (59 - m_time.tm_min);
const int todays_seconds_left = (todays_minutes_left * 60) + (59 - m_time.tm_sec);
int overlap_seconds = 0;
// Calculate how many days until the desired date.
int current_day_number = m_time.tm_wday;
const int desired_day_number = GetWeekDayNumberFromName(day_name);
if (desired_day_number >= 0)
{
if (desired_day_number <= current_day_number)
{
// Find out how many days left in the week, add them to how many days until the today.
const int max_day = 6;
const int days_remaining = max_day - current_day_number;
overlap_seconds = (days_remaining * 24 * 60 * 60);
current_day_number = 0;
}
const int days_left = desired_day_number - current_day_number;
const int hours_left = days_left * 24;
const int minutes_left = hours_left * 60;
const int seconds_left = (minutes_left * 60) - (86400 - todays_seconds_left) + overlap_seconds;
return seconds_left;
}
return -1;
}
const int DateTime::GetWeekDayNumberFromName(
const std::string& name) const
{
if (name == "Sunday")
{
return 0;
}
else if (name == "Monday")
{
return 1;
}
else if (name == "Tuesday")
{
return 2;
}
else if (name == "Wednesday")
{
return 3;
}
else if (name == "Thursday")
{
return 4;
}
else if (name == "Friday")
{
return 5;
}
else if (name == "Saturday")
{
return 6;
}
return -1;
}
#include <iostream>
#include "DateTime.h"
int main()
{
DateTime date_time;
const int seconds = date_time.GetSecondsUntilNextWeekDay("Monday");
std::cout << "Seconds Till Date: " << seconds;
}
我希望如果我想知道从当前时间(周三晚上 9:00)到周四开始还有多长时间,它会返回 3 小时或 10800 秒(要么工作要么工作)。
【问题讨论】: