Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

This article is a Private article. Only a writer and users who know the URL can access it.
Please change open range to public in publish setting if you want to share this article with other users.

More than 5 years have passed since last update.

日付と時刻

Posted at
#include <iostream>
#include <iomanip>
#include <string>
#include <ctime>
using namespace std;

struct Time
{
	tm *access;
	time_t tm_v;

	Time(){
		access = NULL;
		tm_v = 0;
		tm_v = time( NULL );
		access = localtime( &tm_v );
	}
};

class Year : Time
{
private:
   int yy;
public:
	Year() { yy = access-> tm_year + 1900; }
	int get_year() const { return yy; }
};

class Month : Time
{
private:
	int mm;
public:
	Month() { mm = access-> tm_mon + 1; }
	int get_month() const { return mm; }
};

class Day : Time
{
private:
	int dd;
public:
	Day() { dd = access-> tm_mday; }
	int get_day() const { return dd; }
};

class Hour : Time
{
private:
	int hh;
public:
	Hour() { hh = access-> tm_hour; }
	int get_hour() const { return hh; }
};

class Miniture : Time
{
private:
	int mm;
public:
	Miniture() { mm = access-> tm_min; }
	int get_min() const { return mm; }
};

class Second : Time
{
private:
	int sec;
public:
	Second(){ sec = access-> tm_sec; }
	int get_sec() const { return sec; }
};

class Week : Time
{
private:
	int week_kind;
	enum { sun, mon, tsu, wed, thu, fri, sat };
	std::string kind;
public:
	Week()
	{
		kind = "";
		week_kind = access-> tm_wday;
		this-> toStr();
	}
	void toStr()
	{
		switch ( week_kind ) {
		case sun:
			kind = "Sun";
			break;
		case mon:
			kind = "Mon";
			break;
		case tsu:
			kind = "Tsu";
			break;
		case wed:
			kind = "Wed";
			break;
		case thu:
			kind = "Thu";
			break;
		case fri:
			kind = "Fri";
			break;
		case sat:
			kind = "Sat";
			break;
		}
	}
	const char* get_week_lable() const
	{
		return kind.c_str();
	}
	int get_week_kind() const
	{
		return week_kind;
	}

};

class Ymd : Year , Month, Day, Week
{
private:
	int y, m, d;
	const char* wk_lable;
public:
	Ymd()
	: y ( get_year() ),
	  m ( get_month() ),
	  d ( get_day() ),
	  wk_lable ( get_week_lable() )
	{}
	void get_ymd( int& set_yy, int& set_mon, int& set_dd )
	{
		set_yy = y;
		set_mon = m;
		set_dd = d;
	}
	void get_ymd( int& set_yy, int& set_mon, int& set_dd, const char* set_week_kind )
	{
		set_yy = y;
		set_mon = m;
		set_dd = d;
		set_week_kind = wk_lable;
	}
};

class Hms : Hour, Miniture, Second
{
private:
	int h, m, s;
public:
	Hms()
	: h ( get_hour() ),
	  m ( get_min() ),
	  s ( get_sec() ) { }
	void get_hms( int& set_hh, int& set_mm, int& set_ss )
	{
		set_hh = h;
		set_mm = m;
		set_ss = s;
	}
};

enum Date_order{ Days, Times };

class Date
{
private:
	int yr, mon, dy;
	int hr, min, sec;
	const char* wk;
	Ymd today;
	Hms time_now;
	Week day_w;
	Date_order dy_tm;
public:
	Date( Date_order day_or_time )
	{
	 	dy_tm = day_or_time;
		yr = mon = dy = 0;
		hr = min = sec = 0;
		wk = day_w.get_week_lable();
		today.get_ymd( yr, mon, dy );
		time_now.get_hms( hr, min, sec );
	}
	Date( const Date& tmp )
	{
		yr = mon = dy = 0;
		hr = min = sec = 0;
		today.get_ymd( yr, mon, dy );
		time_now.get_hms( hr, min, sec );
	}
	void refresh()
	{
		yr = mon = dy = 0;
		hr = min = sec = 0;

		Ymd tmp_today;
		Hms tmp_time_now;
		Week tmp_day_w;
	
		this-> wk = tmp_day_w.get_week_lable();
		tmp_today.get_ymd( this-> yr, this-> mon, this-> dy );
		time_now.get_hms( this-> hr, this-> min, this-> sec );
	}
	friend std::ostream& operator << ( std::ostream& stream, const Date& ob );
};
std::ostream& operator << ( std::ostream& stream, const Date& ob )
{
	if ( ob.dy_tm == Days ) {
		stream  << ob.yr << '/';
		stream  << std::setw(2) << std::setfill('0') << ob.mon << '/';
		stream  << std::setw(2) << std::setfill('0') << ob.dy << ' ';
		stream  << ob.wk;
	}
	else if ( ob.dy_tm == Times ) {
		stream  << std::setw(2) << std::setfill('0') << ob.hr << ':'
				<< std::setw(2) << std::setfill('0') << ob.min << ':' 
				<< std::setw(2) << std::setfill('0') << ob.sec;
	}
	return stream;
}

int main()
{
	Date t1( Days );
	Date t2( Times );
	std::cout << t1 << ' ';
	std::cout << t2 << std::endl;
	
	/*vector<int> vi(5);

	for( int i = 0; i < vi.size(); ++i ) {
		vi.at( i ) = i * 2;
		cout << vi.at( i ) << endl;
	}

	vector<int>::iterator ii;
	ii = vi.begin();
	ii++;*/

	/*cout << setlocale( LC_ALL, "" ) << endl;
	cout << setlocale( LC_ALL, "japanese_Japan.932" ) << endl;


	char cstr[] = "あいうeo"; 
	wchar_t str[] = L"あいうeo";
	wstring wstr = L"あいうえお";

	cout << sizeof cstr << endl;
	cout << sizeof str << endl;
	cout << sizeof wstr << endl;
	cout << wstr.size() << endl;*/

	std::cin.get();
	return 0;
}
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?