c++ 获取用户选择的日期和实际日期之间的年数(计算天数、月数、年数)
c++ get years between date chosen by user and actual date(counting days,months,years)
我试过这样做:
struct Den_t
{
int day, month, year;
};
int main()
{
struct Den_t* Datum = new struct Den_t;
struct Den_t* Dnes = new struct Den_t;
time_t theTime = time(NULL);
struct tm aTime;
localtime_s(&aTime, &theTime);
Dnes->day = aTime.tm_mday;
Dnes->month = aTime.tm_mon + 1;
Dnes->year = aTime.tm_yday + 1900;
cin >> Datum->day >> Datum->month >> Datum->year;
if (Dnes->year - Datum->year >= 18 )
cout << "full aged " << endl;
else
cout << "not full aged " << endl;
system("PAUSE");
return 0;
}
但我不知何故无法理解我什至应该比较和递减什么,有人能解释一下吗
what else i need to do to tell people's date for example in float by
comparing year,month and day of actual time and date user inputs in
the program?
您的代码逻辑有问题。
例如:
Datum is 31/12/1982
Dnes is 01/01/2000
年差18岁,年龄相差17岁差2天。
考虑使用标准库函数而不是重新发明轮子。
difftime 可能会有用,例如
这是一个非常肮脏的例子,但它可以完成工作:
time_t dnes;
time(&dnes);
// Set datum here ...
cin >> Datum->day >> Datum->month >> Datum->year;
datum.tm_mday = Datum->day;
datum.tm_mon = Datum->month - 1;
datum.tm_yday = Datum->year - 1900;
datum->tm_yday+=18;
if (difftime(dnes, mktime(&datum)) <0 )
cout << "not full aged " << endl;
else
cout << "full aged " << endl;
使用这些库:
http://howardhinnant.github.io/date/date.html
http://howardhinnant.github.io/date/tz.html
这就是我解决问题的方法。先上代码,再解释:
#include "tz.h"
#include "date.h"
#include <iostream>
int
main()
{
using namespace date;
using namespace std::chrono;
std::cout << "Enter birthday [day month year]:";
int di, mi, yi;
std::cin >> di >> mi >> yi;
if (std::cin.fail())
{
std::cout << "Invalid date\n";
return 1;
}
auto y = year{yi};
if (!y.ok())
{
std::cout << "Invalid year\n";
return 1;
}
auto m = month(mi);
if (!m.ok())
{
std::cout << "Invalid month\n";
return 1;
}
auto d = day(di);
if (!d.ok())
{
std::cout << "Invalid day\n";
return 1;
}
auto birthday = y/m/d;
if (!birthday.ok())
{
std::cout << "Invalid birthday\n";
return 1;
}
auto local_time = current_zone()->to_local(system_clock::now());
auto today = year_month_day{floor<days>(local_time)};
auto age = today.year() - birthday.year();
if (birthday + age > today)
--age;
if (age >= years{18})
std::cout << "full aged at " << age.count() << "\n";
else
std::cout << "not full aged at " << age.count() << "\n";
}
我会先去检查一下用户输入的有效性。我下面的内容似乎是最低限度的:
- 必须是整数输入
- 每个整数输入必须有一个合理的值(例如月份必须在 [1, 12] 范围内)。
- 组合 y/m/d 必须是有效日期。
一个更健壮的程序可能会给用户一些关于他输入的内容的反馈,并给他另一次改正错误的机会。
确定我们的生日有效后,我们需要获取当地时区的当前日期。这个:
auto local_time = current_zone()->to_local(system_clock::now());
获取当地时间。
本地时间可以转换为本地年月日:
auto today = year_month_day{floor<days>(local_time)};
此计算遵循您的生日从当地午夜开始的习俗,无论您实际出生在一天中的什么时间(以及在地球上的哪个位置)。也就是说,一旦本地year/month/day成立,这个问题就与本地时区无关,甚至与本地时间无关。
接下来,计算当前年龄:
auto age = today.year() - birthday.year();
if (birthday + age > today)
--age;
今天的年份与生日之间的差异是年龄的一级近似值。通过计算您今年生日 的日期来改进此近似值。如果今年的生日还在未来,那么按照习俗,我们会算小一岁。如果我们正在做的事情不那么倾向于法律体系,而更倾向于科学工作,我们可能会以其他方式进行计算,例如四舍五入到最近的年份(使用这个库也很容易做到)。
如果生日是 2 月 29 日,上面的代码仍然有效:birthday + age
将导致(75% 的机会)无效日期,例如:feb/29/2015。然而,这个无效日期将正确地比较大于 feb/28/2015 和小于 mar/1/2015,这正是我们需要的!无效的日期是你的朋友,而不是你的敌人——你只需要知道它们的存在以及如何处理它们。
现在报告您的发现是一件简单的事情:
if (age >= years{18})
std::cout << "full aged at " << age.count() << "\n";
else
std::cout << "not full aged at " << age.count() << "\n";
我试过这样做:
struct Den_t
{
int day, month, year;
};
int main()
{
struct Den_t* Datum = new struct Den_t;
struct Den_t* Dnes = new struct Den_t;
time_t theTime = time(NULL);
struct tm aTime;
localtime_s(&aTime, &theTime);
Dnes->day = aTime.tm_mday;
Dnes->month = aTime.tm_mon + 1;
Dnes->year = aTime.tm_yday + 1900;
cin >> Datum->day >> Datum->month >> Datum->year;
if (Dnes->year - Datum->year >= 18 )
cout << "full aged " << endl;
else
cout << "not full aged " << endl;
system("PAUSE");
return 0;
}
但我不知何故无法理解我什至应该比较和递减什么,有人能解释一下吗
what else i need to do to tell people's date for example in float by comparing year,month and day of actual time and date user inputs in the program?
您的代码逻辑有问题。 例如:
Datum is 31/12/1982
Dnes is 01/01/2000
年差18岁,年龄相差17岁差2天。
考虑使用标准库函数而不是重新发明轮子。 difftime 可能会有用,例如
这是一个非常肮脏的例子,但它可以完成工作:
time_t dnes;
time(&dnes);
// Set datum here ...
cin >> Datum->day >> Datum->month >> Datum->year;
datum.tm_mday = Datum->day;
datum.tm_mon = Datum->month - 1;
datum.tm_yday = Datum->year - 1900;
datum->tm_yday+=18;
if (difftime(dnes, mktime(&datum)) <0 )
cout << "not full aged " << endl;
else
cout << "full aged " << endl;
使用这些库:
http://howardhinnant.github.io/date/date.html
http://howardhinnant.github.io/date/tz.html
这就是我解决问题的方法。先上代码,再解释:
#include "tz.h"
#include "date.h"
#include <iostream>
int
main()
{
using namespace date;
using namespace std::chrono;
std::cout << "Enter birthday [day month year]:";
int di, mi, yi;
std::cin >> di >> mi >> yi;
if (std::cin.fail())
{
std::cout << "Invalid date\n";
return 1;
}
auto y = year{yi};
if (!y.ok())
{
std::cout << "Invalid year\n";
return 1;
}
auto m = month(mi);
if (!m.ok())
{
std::cout << "Invalid month\n";
return 1;
}
auto d = day(di);
if (!d.ok())
{
std::cout << "Invalid day\n";
return 1;
}
auto birthday = y/m/d;
if (!birthday.ok())
{
std::cout << "Invalid birthday\n";
return 1;
}
auto local_time = current_zone()->to_local(system_clock::now());
auto today = year_month_day{floor<days>(local_time)};
auto age = today.year() - birthday.year();
if (birthday + age > today)
--age;
if (age >= years{18})
std::cout << "full aged at " << age.count() << "\n";
else
std::cout << "not full aged at " << age.count() << "\n";
}
我会先去检查一下用户输入的有效性。我下面的内容似乎是最低限度的:
- 必须是整数输入
- 每个整数输入必须有一个合理的值(例如月份必须在 [1, 12] 范围内)。
- 组合 y/m/d 必须是有效日期。
一个更健壮的程序可能会给用户一些关于他输入的内容的反馈,并给他另一次改正错误的机会。
确定我们的生日有效后,我们需要获取当地时区的当前日期。这个:
auto local_time = current_zone()->to_local(system_clock::now());
获取当地时间。
本地时间可以转换为本地年月日:
auto today = year_month_day{floor<days>(local_time)};
此计算遵循您的生日从当地午夜开始的习俗,无论您实际出生在一天中的什么时间(以及在地球上的哪个位置)。也就是说,一旦本地year/month/day成立,这个问题就与本地时区无关,甚至与本地时间无关。
接下来,计算当前年龄:
auto age = today.year() - birthday.year();
if (birthday + age > today)
--age;
今天的年份与生日之间的差异是年龄的一级近似值。通过计算您今年生日 的日期来改进此近似值。如果今年的生日还在未来,那么按照习俗,我们会算小一岁。如果我们正在做的事情不那么倾向于法律体系,而更倾向于科学工作,我们可能会以其他方式进行计算,例如四舍五入到最近的年份(使用这个库也很容易做到)。
如果生日是 2 月 29 日,上面的代码仍然有效:birthday + age
将导致(75% 的机会)无效日期,例如:feb/29/2015。然而,这个无效日期将正确地比较大于 feb/28/2015 和小于 mar/1/2015,这正是我们需要的!无效的日期是你的朋友,而不是你的敌人——你只需要知道它们的存在以及如何处理它们。
现在报告您的发现是一件简单的事情:
if (age >= years{18})
std::cout << "full aged at " << age.count() << "\n";
else
std::cout << "not full aged at " << age.count() << "\n";