summaryrefslogtreecommitdiff
blob: 0d3d18f185991f8f48294c7c9663248d9f7d767e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <curses.h>

void print_current_activity();
void start_new_activity();
void stop_current_activity();
void print_activities();
void save_to_file();
void load_file();

typedef struct activities
{
	time_t start_time;
	time_t end_time;
	char description[100];
} activity;

activity current_activity;
activity activities_list[100];

int main()
{
	strcpy(current_activity.description,"N/A");
	current_activity.start_time = time(NULL);
	load_file();
	char command;

	while(command != 'q')
	{
		system("clear");
		print_current_activity();
		print_activities();
		printf("\n\n[s]tart tracking new activity | sto[p] current activity | sa[v]e to file | [q]uit: ");		
		fgets(&command, 256, stdin);
		switch(command)
		{
			case 's':
				start_new_activity();
				break;

			case 'p':
				stop_current_activity();
				break;

			case 'v':
				save_to_file();
				break;

			case 'q':
				break;

			default :
				break;
		}
	}
	return 0;
}

void print_current_activity()
{
	time_t time_now = time(NULL);
	char buf[80];

	strftime(buf, sizeof(buf), "%H:%M:%S", localtime(&current_activity.start_time));

	printf("=================================================="
			"==================================================\n"
			"Current activity: %s\nStart time: %s\nDuration: %ld mins.\n"
			"=================================================="
			"==================================================\n",
			current_activity.description, buf, (time_now - current_activity.start_time)/60);
}

void print_activities()
{
	char buf1[80], buf2[80];

	printf("\nPast activities:\n"
			"--------------------------------------------------"
			"--------------------------------------------------"); 
	for(int i = 0; activities_list[i].start_time; i++)
	{
		strftime(buf1, sizeof(buf1), "%H:%M:%S", localtime(&activities_list[i].start_time));
		strftime(buf2, sizeof(buf2), "%H:%M:%S", localtime(&activities_list[i].end_time));

		printf("\nStart time: %s\nEnd time: %s\nActivity: %s\n"
				"--------------------------------------------------"
				"--------------------------------------------------", 
				buf1, buf2, activities_list[i].description);
	}
}

void start_new_activity()
{
	current_activity.start_time = time(NULL);

	printf("\nWhat are you doing: ");
	fgets(current_activity.description, 256, stdin);
	current_activity.description[strcspn(current_activity.description, "\n")] = 0;
}

void stop_current_activity()
{
	current_activity.end_time = time(NULL);

	for(int i = 0; i < 100; i++)
	{
		if(!activities_list[i].start_time)
		{	
			activities_list[i] = current_activity;
			strcpy(current_activity.description, "N/A");
			break;
		}
	}

}

void save_to_file()
{
	FILE *fp;
	time_t rawtime;
    struct tm *info;
    char buffer[80];

    time(&rawtime);

    strftime(buffer, 80, "%Y-%m-%d", localtime(&rawtime));

	char filename[80];
	snprintf(filename, sizeof(filename), "%s/Timesheets/timesheet-%s.txt", getenv("HOME"), buffer);

	fp = fopen(filename, "w");
	
	for(int i = 0; activities_list[i].start_time; i++)
	{
		fprintf(fp, "%ld;%ld;%s\n", activities_list[i].start_time, activities_list[i].end_time, 
				activities_list[i].description);
	}

	fclose(fp);
}

void load_file()
{
	FILE *fp;
	time_t rawtime;
    struct tm *info;
    char buffer[80];

    time(&rawtime);

    strftime(buffer, 80, "%Y-%m-%d", localtime(&rawtime));

	char filename[80];
	snprintf(filename, sizeof(filename), "%s/Timesheets/timesheet-%s.txt", getenv("HOME"), buffer);
	fp = fopen(filename, "r");

	int i = 0;
	
	while (EOF != fscanf(fp, "%ld;%ld;%s", &activities_list[i].start_time, &activities_list[i].end_time, 
				activities_list[i].description)) i++;
}