-
Notifications
You must be signed in to change notification settings - Fork 1
/
Task2.c
31 lines (23 loc) · 1.21 KB
/
Task2.c
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
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
int main()
{
struct tm *currentTimeinfo; // Structure contains information about current time
struct tm *birthDate = malloc(sizeof(struct tm)); // Structure contains information about user's birth time
time_t currentRawtime;
time_t birthRawtime;
time(¤tRawtime);
currentTimeinfo = localtime(¤tRawtime);
printf("Enter your date and time of birth (dd.mm.yyyy hh:mm): ");
scanf("%d.%d.%d %d:%d", &(birthDate->tm_mday), &(birthDate->tm_mon), &(birthDate->tm_year), &(birthDate->tm_hour), &(birthDate->tm_min));
// Adjusting data so that mktime function would work correctly
birthDate->tm_mon--;
birthDate->tm_year -= 1900;
birthDate->tm_sec = 0;
birthRawtime = mktime(birthDate);
printf("Current time: %d.%d.%d %d:%d\n", currentTimeinfo->tm_mday, currentTimeinfo->tm_mon + 1, currentTimeinfo->tm_year + 1900, currentTimeinfo->tm_hour, currentTimeinfo->tm_min);
printf("Your birth time: %d.%d.%d %d:%d\n", birthDate->tm_mday, birthDate->tm_mon + 1, birthDate->tm_year + 1900, birthDate->tm_hour, birthDate->tm_min);
printf("Your age in seconds = %d\n", (int)currentRawtime - (int)birthRawtime);
free(birthDate);
}