C/C++
Программирование С++ тип даты
Опишите с помощью класса тип даты, состоящий из числа, месяца, года. Напишите метод Строка (), представляющий дату в полном сороковом формате, то есть например 22.02.2020 в виде "22 февраля 2020 года"
#include <iostream>
#include <string>
#include <ctime>
#include <iomanip>
#include <chrono>
#include <sstream>
using namespace std;
using std::chrono::system_clock;
class DataTime {
public:
static string time() {
return get_("%R");
}
static string fulltime() {
return get_("%H:%M:%S");
}
static string date() {
return get_("%d.%m.%Y");
}
static string date(int d, int m, int y) {
return to_string(d) + ' ' + months[m - 1] + ' ' + to_string(y) + " года";
}
static string rusdate() {
auto box = get_();
auto current_time = time();
return days[box.tm_wday]
+ to_string(box.tm_mday)
+ months[box.tm_mon]
+ to_string(box.tm_year + 1900) + " года. "
+ current_time;
}
static string timestamp() {
return get_("%F %T");
}
private:
static string get_(const char* fmt) {
auto tt = system_clock::to_time_t(system_clock::now());
struct tm ptm;
localtime_s(&ptm, &tt);
ostringstream oss;
oss << put_time(&ptm, fmt);
return oss.str();
}
static struct tm get_() {
auto tt = system_clock::to_time_t(system_clock::now());
struct tm ptm;
localtime_s(&ptm, &tt);
return ptm;
}
static const string months[12];
static const string days[7];
};
const string DataTime::months[12] = {
" января ",
" февраля ",
" марта ",
" апреля ",
" мая ",
" июня ",
" июля ",
" августа ",
" сентября ",
" октября ",
" ноября ",
" декабря "
};
const string DataTime::days[7] = {
"Воскресенье, ",
"Понедельник, ",
"Вторник, ",
"Среда, ",
"Четверг, ",
"Пятница, ",
"Суббота, "
};
int main() {
system("chcp 1251 > nul");
cout << DataTime::rusdate() << '\n';
cout << DataTime::date(22, 2, 2020) << '\n';
system("pause");
}
#include <string>
#include <ctime>
#include <iomanip>
#include <chrono>
#include <sstream>
using namespace std;
using std::chrono::system_clock;
class DataTime {
public:
static string time() {
return get_("%R");
}
static string fulltime() {
return get_("%H:%M:%S");
}
static string date() {
return get_("%d.%m.%Y");
}
static string date(int d, int m, int y) {
return to_string(d) + ' ' + months[m - 1] + ' ' + to_string(y) + " года";
}
static string rusdate() {
auto box = get_();
auto current_time = time();
return days[box.tm_wday]
+ to_string(box.tm_mday)
+ months[box.tm_mon]
+ to_string(box.tm_year + 1900) + " года. "
+ current_time;
}
static string timestamp() {
return get_("%F %T");
}
private:
static string get_(const char* fmt) {
auto tt = system_clock::to_time_t(system_clock::now());
struct tm ptm;
localtime_s(&ptm, &tt);
ostringstream oss;
oss << put_time(&ptm, fmt);
return oss.str();
}
static struct tm get_() {
auto tt = system_clock::to_time_t(system_clock::now());
struct tm ptm;
localtime_s(&ptm, &tt);
return ptm;
}
static const string months[12];
static const string days[7];
};
const string DataTime::months[12] = {
" января ",
" февраля ",
" марта ",
" апреля ",
" мая ",
" июня ",
" июля ",
" августа ",
" сентября ",
" октября ",
" ноября ",
" декабря "
};
const string DataTime::days[7] = {
"Воскресенье, ",
"Понедельник, ",
"Вторник, ",
"Среда, ",
"Четверг, ",
"Пятница, ",
"Суббота, "
};
int main() {
system("chcp 1251 > nul");
cout << DataTime::rusdate() << '\n';
cout << DataTime::date(22, 2, 2020) << '\n';
system("pause");
}
Похожие вопросы
- Программирование на С++
- Программирование на C++
- Как начать изучать программирование?
- Доброго дня! Хочу сменить сферу деятельности . Заинтересовал вариант программирования .
- Программирование на C++
- Книги по программированию.
- Может ли новичок в программирование начать с c++/Gamedev
- Чем отличаются языки программирования ???
- На каком языке программирования (Assembler / С / С++) лучше будет написать компилятор для своего языка программирования?
- Указатели. Программирование с использованием динамических двухмерным масивов.