日期的C 秒
C++ Seconds in Date
本文关键字:日期 更新时间:2023-10-16
我正在尝试编写一个我想以" 03.02.2019"和一个类似4的整数以及我想要的代码的款项。要做的就是给我的结果:" 07.02.2019"。我认为我在这里遇到的问题是,我没有正确地分配指针,因为我可以看到代码做对了。它确实计算了" 07.02.2019",但我无法让我回馈结果;-)。请在下面找到代码:
#include <iostream>
#include "Calendar.h"
#include <ctime>
using namespace std;
int main()
{
Calendar MyDate("03.02.2019");
Calendar NewDate;
NewDate = MyDate + 4;
cout << endl << NewDate.print() << endl;
cin.get();
getchar();
return 0;
}
calender.h:
#pragma once
#include <iostream>
#include <vector>
#include <ctime>
#include <string>
using namespace std;
class Calendar
{
public:
Calendar();
Calendar(string thedate);
/*
Calendar operator-(const Calendar &TheDate);
Calendar operator-(const int &numberOfDays);
*/
Calendar operator+(const int &numberOfDays);
string print(void);
void set_Date(string dertag);
void set_Seconds();
~Calendar();
private:
vector<string>AllTheDates;
string TheDate;
int TheYear;
int TheMonth;
int TheDay;
unsigned int YearSeconds;
unsigned int MonthSeconds;
unsigned int DaySeconds;
unsigned int TotalSeconds;
string *theNewDate2print_ptr=new string;
//string theNewDate2print;
bool isdate;
};
calendar.cpp:
#include "Calendar.h"
#include <ctime>
#include <string>
#include <sstream>
using namespace std;
Calendar::Calender()
{
}
Calendar::Calendar(string thedate)
{
this->set_Date(thedate);
}
/*
Calendar Calendar::operator-(const Calendar &TheDate)
{
}
Calendar Calendar::operator-(const int &numberOfDays)
{
}
}*/
Calendar Calendar::operator+(const int &numberOfDays)
{
//Calendar NewDate;
unsigned int TotalSeconds_ = TotalSeconds + (numberOfDays * 24 * 60 * 60);
time_t timer = TotalSeconds_;
tm *ltm = localtime(&timer);
stringstream NewDate_;
string TheNewDate;
stringstream theDay;
stringstream theMonth;
stringstream theYear;
string theDay_;
string theMonth_;
string theYear_;
//theNewDate2print_ptr = new string;
theDay << ltm->tm_mday;
theMonth << 1+ltm->tm_mon;
theYear << 1830+ltm->tm_year;
theDay_ = theDay.str();
theMonth_ = theMonth.str();
theYear_ = theYear.str();
if (theDay_.length() == 1)
theDay_ = "0" + theDay_;
if (theMonth_.length() == 1)
theMonth_ = "0" + theMonth_;
//NewDate_ << ltm->tm_mday << "." << 1+ltm->tm_mon << "." << 1830+ltm->tm_year;
TheNewDate = theDay_ + "." + theMonth_ + "." + theYear_;
//Calendar NewDate(TheNewDate);
*theNewDate2print_ptr = TheNewDate;
return TheNewDate;
}
void Calendar::set_Date(string dertag)
{
TheDate = dertag;
TheDay = stoi(dertag.substr(0, 2));
TheMonth = stoi(dertag.substr(3,2));
TheYear = stoi(dertag.substr(6,4));
if (TheDay > 0 && TheDay < 32 && TheMonth>0 && TheMonth < 13 && TheYear>1900 && TheYear < 3000)
isdate = true;
/*if(isdate)
throw exception!*/
set_Seconds();
}
void Calendar::set_Seconds()
{
//Diese Funktion berechnet die Sekunden für ein bestimmtes Datum das im Format Calender("03.02.2019") übergeben wurde.
YearSeconds = (TheYear - 1900) * 365 * 24 * 60 * 60;
MonthSeconds = TheMonth * 30 * 24 * 60 * 60;
DaySeconds = TheDay * 24 * 60 * 60;
TotalSeconds = YearSeconds + MonthSeconds + DaySeconds;
}
string Calendar::print()
{
return (*theNewDate2print_ptr);
}
Calendar::~Calendar()
{
}
感谢您的任何帮助。非常感谢!!!
问候,Marco
Calendar Calendar::operator+(const int &numberOfDays)
{
...
string TheNewDate;
...
return TheNewDate;
}
这将从TheNewDate
字符串中构造Calendar
对象,该对象将调用构造函数:
Calendar::Calendar(string thedate)
{
this->set_Date(thedate);
}
set_Date
:
void Calendar::set_Date(string dertag)
{
TheDate = dertag;
TheDay = stoi(dertag.substr(0, 2));
TheMonth = stoi(dertag.substr(3,2));
TheYear = stoi(dertag.substr(6,4));
if (TheDay > 0 && TheDay < 32 && TheMonth>0 && TheMonth < 13 && TheYear>1900 && TheYear < 3000)
isdate = true;
/*if(isdate)
throw exception!*/
set_Seconds();
}
和set_Seconds()
:
void Calendar::set_Seconds()
{
//Diese Funktion berechnet die Sekunden für ein bestimmtes Datum das im Format Calender("03.02.2019") übergeben wurde.
YearSeconds = (TheYear - 1900) * 365 * 24 * 60 * 60;
MonthSeconds = TheMonth * 30 * 24 * 60 * 60;
DaySeconds = TheDay * 24 * 60 * 60;
TotalSeconds = YearSeconds + MonthSeconds + DaySeconds;
}
在构造期间,*theNewDate2print_ptr
未分配任何值,字符串为空 - 默认是在condort int new string
调用之前初始化的。您需要在任何地方分配它。前任。在set_seconds中:
void Calendar::set_Seconds()
{
//Diese Funktion berechnet die Sekunden für ein bestimmtes Datum das im Format Calender("03.02.2019") übergeben wurde.
YearSeconds = (TheYear - 1900) * 365 * 24 * 60 * 60;
MonthSeconds = TheMonth * 30 * 24 * 60 * 60;
DaySeconds = TheDay * 24 * 60 * 60;
TotalSeconds = YearSeconds + MonthSeconds + DaySeconds;
*theNewDate2print_ptr = std::to_string(DaySeconds) + "-" + ....;
}
您的类泄漏内存。它可以分配新对象,并且在任何地方都无法将其释放。还要编写复制构造函数以防止内存泄漏。您应该释放分配的记忆:
Calendar::~Calendar()
{
delete theNewDate2print_ptr;
}
成员:
vector<string>AllTheDates;
string TheDate;
int TheYear;
int TheMonth;
int TheDay;
unsigned int YearSeconds;
unsigned int MonthSeconds;
unsigned int DaySeconds;
unsigned int TotalSeconds;
string *theNewDate2print_ptr=new string;
//string theNewDate2print;
bool isdate;
看起来像许多重复的变量,它们以不同的格式保持相同的值。要干净。不要重复变量。不要浪费记忆。不要困惑自己。存储一个变量一次。不需要YearSeconds
和TheYear
。如果将日期存储为整数,则无需TheDate
...写转换功能,设置和Geters功能:
unsigned int TheYear;
unsigned int YearSeconds() {
// TODO: leap years
return TheYear * number_of_seconds_in_a_year;
}
void SetTheYearFromSeconds(unsigned int seconds) {
// TODO: leap years
TheYear = seconds / number_of_seconds_in_a_year;
}
// etc...
unsigned int TotalSeconds() {
return YearSeconds() + .... ;
}
相关文章:
- 查询SQLite数据库中的日期
- 获取日期异步信号安全吗?如果在信号处理程序中使用,它会导致死锁吗
- Log4cpp:以UTC/GMT时区打印日期
- 使用C++的日期库读取时间
- 日期格式为C++
- 如何在 C++20 计时中为日期添加天数?
- 如何在QTableView中排序和更改日期格式
- 如何在cpp中使用地图显示给定日期范围内(在下面的问题中)的费率?
- Gtkmm 会不断检查 Gtk::日历上的所选日期是否发生变化
- fmt 与 Howard Hinnant 的日期:为什么从 fmt::to_string "{}"?FMT 和日期的最佳实践?
- 使程序检查当前日期是否=预期日期
- 如何找到两个日期之间的时间差异(以秒和纳秒为单位)?
- JNI 日期值转换问题,在C++中获取不同的长整型值
- 确定夏令时是否适用于特定日期
- C++日期库因时区而失败
- 向日期运算符添加天数+
- 为什么在C++中获取日期和/或时间如此复杂?
- 如何验证用户输入月份的日期?
- 从编译时已知的日历日期创建"std::chrono::time_point"
- 在 MacOS 上C++:显示日期和时间问题