1 /*
2  *  Copyright (C) 2020 Ingo Brückl
3  *
4  *  This program is free software; you can redistribute it and/or modify
5  *  it under the terms of the GNU General Public License as published by
6  *  the Free Software Foundation; either version 2 of the License, or
7  *  (at your option) any later version.
8  *
9  *  This program is distributed in the hope that it will be useful,
10  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  *  GNU General Public License for more details.
13  *
14  *  You should have received a copy of the GNU General Public License
15  *  along with this program; if not, write to the Free Software
16  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
17  *  MA 02110-1301 USA.
18  */
19 
20 #include <stdio.h>
21 #include <string.h>
22 #include <time.h>
23 #include "date_utils.h"
24 
month(const gchar * date)25 static guint month (const gchar *date)
26 {
27 	if (strncmp(date, "Jan", 3) == 0) return 1;
28 	if (strncmp(date, "Feb", 3) == 0) return 2;
29 	if (strncmp(date, "Mar", 3) == 0) return 3;
30 	if (strncmp(date, "Apr", 3) == 0) return 4;
31 	if (strncmp(date, "May", 3) == 0) return 5;
32 	if (strncmp(date, "Jun", 3) == 0) return 6;
33 	if (strncmp(date, "Jul", 3) == 0) return 7;
34 	if (strncmp(date, "Aug", 3) == 0) return 8;
35 	if (strncmp(date, "Sep", 3) == 0) return 9;
36 	if (strncmp(date, "Oct", 3) == 0) return 10;
37 	if (strncmp(date, "Nov", 3) == 0) return 11;
38 	if (strncmp(date, "Dec", 3) == 0) return 12;
39 
40 	return 0;
41 }
42 
date_MMM_dD_HourYear(const gchar * date)43 gchar *date_MMM_dD_HourYear (const gchar *date)
44 {
45 	static gchar iso8601[11];
46 	gchar mm[3];
47 
48 	iso8601[4] = '-';
49 	iso8601[7] = '-';
50 	iso8601[10] = 0;
51 
52 	sprintf(mm,"%02u", month(date));
53 
54 	memcpy(iso8601 + 5, mm, 2);
55 	memcpy(iso8601 + 8, date + 4, 2);
56 
57 	if (iso8601[8] == ' ')
58 		iso8601[8] = '0';
59 
60 	if (date[9] == ':')
61 	{
62 		time_t now;
63 		struct tm *current;
64 		gchar mm_dd[6], yyyy[5];
65 
66 		time(&now);
67 		current = localtime(&now);
68 		sprintf(mm_dd, "%02u-%02u", current->tm_mon + 1, current->tm_mday);
69 
70 		if (strcmp(iso8601 + 5, mm_dd) > 0)
71 			current->tm_year--;
72 
73 		sprintf(yyyy,"%04u", current->tm_year + 1900);
74 		memcpy(iso8601, yyyy, 4);
75 	}
76 	else
77 		memcpy(iso8601, date + (date[7] == ' ' ? 8 : 7), 4);
78 
79 	return iso8601;
80 }
81 
date_YY_MM_DD(const gchar * date)82 gchar *date_YY_MM_DD (const gchar *date)
83 {
84 	static gchar iso8601[11];
85 	guint yy;
86 
87 	memcpy(iso8601 + 2, date, 8);
88 	iso8601[10] = 0;
89 
90 	yy = 10 * (date[0] - '0') + (date[1] - '0');
91 
92 	if (yy >= 70 && yy <= 99)
93 		memcpy(iso8601, "19", 2);
94 	else
95 		memcpy(iso8601, "20", 2);
96 
97 	return iso8601;
98 }
99 
date_DD_MM_YY(const gchar * date)100 gchar *date_DD_MM_YY (const gchar *date)
101 {
102 	gchar yy_mm_dd[9];
103 
104 	yy_mm_dd[0] = date[6];
105 	yy_mm_dd[1] = date[7];
106 	yy_mm_dd[2] = '-';
107 	yy_mm_dd[3] = date[3];
108 	yy_mm_dd[4] = date[4];
109 	yy_mm_dd[5] = '-';
110 	yy_mm_dd[6] = date[0];
111 	yy_mm_dd[7] = date[1];
112 	yy_mm_dd[8] = 0;
113 
114 	return date_YY_MM_DD(yy_mm_dd);
115 }
116 
date_YY_MMM_DD(const gchar * date)117 gchar *date_YY_MMM_DD (const gchar *date)
118 {
119 	gchar yy_mm_dd[9], mm[3];
120 
121 	yy_mm_dd[0] = date[0];
122 	yy_mm_dd[1] = date[1];
123 	yy_mm_dd[2] = '-';
124 
125 	sprintf(mm,"%02u", month(date + 3));
126 
127 	yy_mm_dd[3] = mm[0];
128 	yy_mm_dd[4] = mm[1];
129 	yy_mm_dd[5] = '-';
130 	yy_mm_dd[6] = date[7];
131 	yy_mm_dd[7] = date[8];
132 	yy_mm_dd[8] = 0;
133 
134 	return date_YY_MM_DD(yy_mm_dd);
135 }
136