Getting current date and time in C++

Hi guys i am doing a school project.

The project basically is that it records the in and out time of an employee(of an particular company).The employee while checking in or out should enter a unique key generated specially for him so that no one else can check in and out for him.Then referring to the employees position( A worker or a manager or something like that) his total working time each day , for a week and a month is calculated. The company starts at 8 am and ends at 5 pm for 1st shift and for second shift from 3.30 pm to 2.30 am.Plus Saturday and Sunday off.

Then after a month the program refers to the working time date stored in a file and calculates his pay(For workers its per hour salary and for managers it aint). If an employee is consistently late the details are forwarded to the HR account( so that necessary steps are taken).This will help the company log the details of their employees duty time plus give enough detail to take action if someones always late.

I'm doing this as a school project and it needn't be of enterprise class and all.. But i want the coding to perform as it should.Also i'm forced to use the old Turbo C++.

Now i'm struck in the place where the time of the employees in and out time is logged.

This coding does the work

1
2
3
4
5
6
7
8
9
10
void main( )
{
clrscr();
char dateStr [9],timeStr [9];
_strdate( dateStr);
cout<<" The current date is "<<dateStr<<'\n';
_strtime( timeStr );
cout<<" The current time is "<<timeStr<<'\n';
getch();
}

I saw it somewhere on the web but can someone help me understand how it works.

I also saw another coding

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
typedef struct _SYSTEMTIME {
WORD wYear;
WORD wMonth;
WORD wDayOfWeek;
WORD wDay;
WORD wHour;
WORD wMinute;
WORD wSecond;
WORD wMilliseconds;
} SYSTEMTIME;

#include <Windows.h>
#include <stdio.h>

void main()
{
SYSTEMTIME st;
GetSystemTime(&st);
printf("Year:%dnMonth:%dnDate:%dnHour:%dnMin:%dnSecond:% dn" 
st.wYear,st.wMonth,st.wDay,st.wHour,st.wMinute,st.wSecond);                                                                                                        
}

I think the second one is better as it not only gives me date but also gives me the day so i can check easily for the weekends.

SO help me understand how these time functions work. Also if you have any suggestions for my project they are welcome.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// http://www.cplusplus.com/reference/ctime/strftime/
// strftime example
#include <iostream>      /* puts */
#include <time.h>       /* time_t, struct tm, time, localtime, strftime */

int main ()
{
  time_t rawtime;
  struct tm * timeinfo;
  char st [80];
	
  time (&rawtime);
  timeinfo = localtime (&rawtime);

  strftime (st,80,"Date: %y-%m-%d  Time: %I:%M%p",timeinfo);
  
  puts (st);
  return 0;
}
Topic archived. No new replies allowed.