1// Copyright (c) 2019-2020 Alexander Medvednikov. All rights reserved.
2// Use of this source code is governed by an MIT license
3// that can be found in the LICENSE file.
4module time
5
6// format returns a date string in "YYYY-MM-DD HH:MM" format (24h).
7pub fn (t Time) format() string {
8	return t.get_fmt_str(.hyphen, .hhmm24, .yyyymmdd)
9}
10
11// format_ss returns a date string in "YYYY-MM-DD HH:MM:SS" format (24h).
12pub fn (t Time) format_ss() string {
13	return t.get_fmt_str(.hyphen, .hhmmss24, .yyyymmdd)
14}
15
16// format_ss_milli returns a date string in "YYYY-MM-DD HH:MM:SS.123" format (24h).
17pub fn (t Time) format_ss_milli() string {
18	return t.get_fmt_str(.hyphen, .hhmmss24_milli, .yyyymmdd)
19}
20
21// format_ss_micro returns a date string in "YYYY-MM-DD HH:MM:SS.123456" format (24h).
22pub fn (t Time) format_ss_micro() string {
23	return t.get_fmt_str(.hyphen, .hhmmss24_micro, .yyyymmdd)
24}
25
26// hhmm returns a date string in "HH:MM" format (24h).
27pub fn (t Time) hhmm() string {
28	return t.get_fmt_time_str(.hhmm24)
29}
30
31// hhmmss returns a date string in "HH:MM:SS" format (24h).
32pub fn (t Time) hhmmss() string {
33	return t.get_fmt_time_str(.hhmmss24)
34}
35
36// hhmm12 returns a date string in "HH:MM" format (12h).
37pub fn (t Time) hhmm12() string {
38	return t.get_fmt_time_str(.hhmm12)
39}
40
41// ymmdd returns a date string in "YYYY-MM-DD" format.
42pub fn (t Time) ymmdd() string {
43	return t.get_fmt_date_str(.hyphen, .yyyymmdd)
44}
45
46// ddmmy returns a date string in "DD.MM.YYYY" format.
47pub fn (t Time) ddmmy() string {
48	return t.get_fmt_date_str(.dot, .ddmmyyyy)
49}
50
51// md returns a date string in "MMM D" format.
52pub fn (t Time) md() string {
53	return t.get_fmt_date_str(.space, .mmmd)
54}
55
56// clean returns a date string in a following format:
57//  - a date string in "HH:MM" format (24h) for current day
58//  - a date string in "MMM D HH:MM" format (24h) for date of current year
59//  - a date string formatted with format function for other dates
60pub fn (t Time) clean() string {
61	znow := time.now()
62	// Today
63	if t.month == znow.month && t.year == znow.year && t.day == znow.day {
64		return t.get_fmt_time_str(.hhmm24)
65	}
66	// This year
67	if t.year == znow.year {
68		return t.get_fmt_str(.space, .hhmm24, .mmmd)
69	}
70	return t.format()
71}
72
73// clean12 returns a date string in a following format:
74//  - a date string in "HH:MM" format (12h) for current day
75//  - a date string in "MMM D HH:MM" format (12h) for date of current year
76//  - a date string formatted with format function for other dates
77pub fn (t Time) clean12() string {
78	znow := time.now()
79	// Today
80	if t.month == znow.month && t.year == znow.year && t.day == znow.day {
81		return t.get_fmt_time_str(.hhmm12)
82	}
83	// This year
84	if t.year == znow.year {
85		return t.get_fmt_str(.space, .hhmm12, .mmmd)
86	}
87	return t.format()
88}
89
90// get_fmt_time_str returns a date string with specified FormatTime type.
91pub fn (t Time) get_fmt_time_str(fmt_time FormatTime) string {
92	if fmt_time == .no_time {
93		return ''
94	}
95	tp := if t.hour > 11 { 'p.m.' } else { 'a.m.' }
96	hour := if t.hour > 12 { t.hour - 12 } else if t.hour == 0 { 12 } else { t.hour }
97	return match fmt_time {
98		.hhmm12{
99			'$hour:${t.minute:02d} $tp'
100		}
101		.hhmm24{
102			'${t.hour:02d}:${t.minute:02d}'
103		}
104		.hhmmss12{
105			'$hour:${t.minute:02d}:${t.second:02d} $tp'
106		}
107		.hhmmss24{
108			'${t.hour:02d}:${t.minute:02d}:${t.second:02d}'
109		}
110		.hhmmss24_milli{
111			'${t.hour:02d}:${t.minute:02d}:${t.second:02d}.${(t.microsecond/1000):03d}'
112		}
113		.hhmmss24_micro{
114			'${t.hour:02d}:${t.minute:02d}:${t.second:02d}.${t.microsecond:06d}'
115		}
116		else {
117			'unknown enumeration $fmt_time'}
118	}
119}
120
121// get_fmt_time_str returns a date string with specified
122// FormatDelimiter and FormatDate type.
123pub fn (t Time) get_fmt_date_str(fmt_dlmtr FormatDelimiter, fmt_date FormatDate) string {
124	if fmt_date == .no_date {
125		return ''
126	}
127	month := '${t.smonth()}'
128	year := t.year.str()[2..]
129mut 	res := match fmt_date {
130		.ddmmyy{
131			'${t.day:02d}|${t.month:02d}|$year'
132		}
133		.ddmmyyyy{
134			'${t.day:02d}|${t.month:02d}|${t.year}'
135		}
136		.mmddyy{
137			'${t.month:02d}|${t.day:02d}|$year'
138		}
139		.mmddyyyy{
140			'${t.month:02d}|${t.day:02d}|${t.year}'
141		}
142		.mmmd{
143			'$month|${t.day}'
144		}
145		.mmmdd{
146			'$month|${t.day:02d}'
147		}
148		.mmmddyyyy{
149			'$month|${t.day:02d}|${t.year}'
150		}
151		.yyyymmdd{
152			'${t.year}|${t.month:02d}|${t.day:02d}'
153		}
154		else {
155			'unknown enumeration $fmt_date'}}
156	res = res.replace('|', match fmt_dlmtr {
157		.dot{
158			'.'
159		}
160		.hyphen{
161			'-'
162		}
163		.slash{
164			'/'
165		}
166		.space{
167			' '
168		}
169		.no_delimiter{
170			''
171		}
172	})
173	return res
174}
175
176// get_fmt_str returns a date string with specified FormatDelimiter,
177// FormatTime type, and FormatDate type.
178pub fn (t Time) get_fmt_str(fmt_dlmtr FormatDelimiter, fmt_time FormatTime, fmt_date FormatDate) string {
179	if fmt_date == .no_date {
180		if fmt_time == .no_time {
181			// saving one function call although it's checked in
182			// t.get_fmt_time_str(fmt_time) in the beginning
183			return ''
184		}
185		else {
186			return t.get_fmt_time_str(fmt_time)
187		}
188	}
189	else {
190		if fmt_time != .no_time {
191			return t.get_fmt_date_str(fmt_dlmtr, fmt_date) + ' ' + t.get_fmt_time_str(fmt_time)
192		}
193		else {
194			return t.get_fmt_date_str(fmt_dlmtr, fmt_date)
195		}
196	}
197}
198
199// This is just a TEMPORARY function for cookies and their expire dates
200pub fn (time Time) utc_string() string {
201	day_str := time.weekday_str()
202	month_str := time.smonth()
203	utc_string := '$day_str, $time.day $month_str $time.year ${time.hour:02d}:${time.minute:02d}:${time.second:02d} UTC'
204	return utc_string
205}
206