1 #include "filezilla.h"
2 #include "timeformatting.h"
3 #include "Options.h"
4 
5 #include "option_change_event_handler.h"
6 
7 namespace {
8 
9 class Impl final : public wxEvtHandler, public COptionChangeEventHandler
10 {
11 public:
Impl()12 	Impl()
13 		: COptionChangeEventHandler(this)
14 	{
15 		InitFormat();
16 
17 		COptions::Get()->watch(OPTION_DATE_FORMAT, this);
18 		COptions::Get()->watch(OPTION_TIME_FORMAT, this);
19 	}
20 
~Impl()21 	~Impl()
22 	{
23 		//COptions::Get()->unwatch_all(this);
24 	}
25 
InitFormat()26 	void InitFormat()
27 	{
28 		std::wstring dateFormat = COptions::Get()->get_string(OPTION_DATE_FORMAT);
29 		std::wstring timeFormat = COptions::Get()->get_string(OPTION_TIME_FORMAT);
30 
31 		if (dateFormat == L"1") {
32 			m_dateFormat = L"%Y-%m-%d";
33 		}
34 		else if (!dateFormat.empty() && dateFormat[0] == '2') {
35 			dateFormat = dateFormat.substr(1);
36 			if (fz::datetime::verify_format(dateFormat)) {
37 				m_dateFormat = dateFormat;
38 			}
39 			else {
40 				m_dateFormat = L"%x";
41 			}
42 		}
43 		else {
44 			m_dateFormat = L"%x";
45 		}
46 
47 		m_dateTimeFormat = m_dateFormat;
48 		m_dateTimeFormat += ' ';
49 
50 		if (timeFormat == L"1") {
51 			m_dateTimeFormat += L"%H:%M";
52 		}
53 		else if (!timeFormat.empty() && timeFormat[0] == '2') {
54 			timeFormat = timeFormat.substr(1);
55 			if (fz::datetime::verify_format(timeFormat)) {
56 				m_dateTimeFormat += timeFormat;
57 			}
58 			else {
59 				m_dateTimeFormat += L"%X";
60 			}
61 		}
62 		else {
63 			m_dateTimeFormat += L"%X";
64 		}
65 	}
66 
OnOptionsChanged(watched_options const &)67 	virtual void OnOptionsChanged(watched_options const&)
68 	{
69 		InitFormat();
70 	}
71 
72 	std::wstring m_dateFormat;
73 	std::wstring m_dateTimeFormat;
74 };
75 
GetImpl()76 Impl& GetImpl()
77 {
78 	static Impl impl;
79 	return impl;
80 }
81 }
82 
Format(fz::datetime const & time)83 wxString CTimeFormat::Format(fz::datetime const& time)
84 {
85 	wxString ret;
86 	if (!time.empty()) {
87 		if (time.get_accuracy() > fz::datetime::days) {
88 			ret = FormatDateTime(time);
89 		}
90 		else {
91 			ret = FormatDate(time);
92 		}
93 	}
94 	return ret;
95 }
96 
FormatDateTime(fz::datetime const & time)97 wxString CTimeFormat::FormatDateTime(fz::datetime const& time)
98 {
99 	Impl& impl = GetImpl();
100 
101 	return time.format(impl.m_dateTimeFormat, fz::datetime::local);
102 }
103 
FormatDate(fz::datetime const & time)104 wxString CTimeFormat::FormatDate(fz::datetime const& time)
105 {
106 	Impl& impl = GetImpl();
107 
108 	return time.format(impl.m_dateFormat, fz::datetime::local);
109 }
110