Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 8

Object Oriented Programming

(CL1004)
LABORATORY MANUAL
Spring 2023

LAB 07
ctime Header File
Engr. Nimra Fatima
SYED MUJTABA ALI SHAH 22i-6176 C
________________________________________ __________ ___
STUDENT NAME ROLL NO SEC

______________________________________
LAB ENGINEER SIGNATURE & DATE
MARKS AWARDED: ___/10

NATIONAL UNIVERSITY OF COMPUTER AND EMERGING SCIENCES (NUCES),


ISLAMABAD
LAB 07 ctime
This header file contains definitions of functions to get and manipulate date and time
information.
Some of the well-known functions are listed below along with description.

Already defined types in <ctime> header file


The header <ctime> has four time-related types: tm, clock_t, time_t, and size_t.
Each of the types, clock_t, size_t, and time_t represent the system time and date as an integer. The
structure tm holds the date and time in the form of a C structure.

The “tm” Structure


The “tm” structure is defined as follows:

Example:

Implement the asctime() and mktime() functions as present in the <ctime>


header file.
Name your functions as myasctime() and mymktime() functions
Before coding, be comfortable with the use of above functions. Explanation about
these functions is being provided below, and a sample code is also being

The asctime() function takes in a structure of type tm, and displays a string containing the date
and time information in a human-readable format, e.g.,

Mon March 26 15:28:30 2012


Practice Tasks:
Task01: Implement the asctime() function yourself and name your function as myasctime()
char* myasctime (tm* timeptr)

This function should accept the address of filled tm structure, dynamically creates a char array.
Makes up the array with date information in the following format, and then returns the address
of this array.
Mon March 26 15:28:30 2012

mktime (make time) calculate the week day on a specific date provided by user
Returns the value of type time_t that represents the local time described by the tm structure
pointed by timeptr (which may be modified).
time_t mktime (struct tm * timeptr);
CODE:
#include<iostream>
#include<ctime>
#include<string>
#include<sstream>
using namespace std;
char * myasctime(tm* t )
{
// char * time= new char[]
stringstream s1,s2,s3,s4,s5,s6,s7,s8,s9;
s1<<t->tm_hour<<endl;
s2<<t->tm_isdst<<endl;
s3<<t->tm_mday<<endl;
s4<<t->tm_min<<endl;
s5<<t->tm_mon+1<<endl;
s6<<t->tm_sec<<endl;
s7<<t->tm_wday<<endl;
s8<<t->tm_yday<<endl;
s9<<t->tm_year+1900<<endl;
string s = s + s1.str() + " ";
s = s + s2.str() + " ";
s = s + s3.str() + " ";
s = s + s4.str() + " ";
s = s + s5.str() + " ";
s = s + s6.str() + " ";
s = s + s7.str() + " ";
s = s + s8.str() + " ";
s = s + s9.str() + " ";
string
weekday[7]={"Sunday","Monday","Tuesday","Wednesday","Thursday","Frida
y","Saturday"};
s = s + weekday[t->tm_wday]+" ";
string
month[12]={"Jan","Feb","March","April","May","June","July","Aug","Sep
","Oct","Nov","Dec"};
s = s + month[t->tm_mon]+" ";
cout<<s;
}
int main()
{
time_t b=time(0);
cout<<ctime(&b);
struct tm *t= localtime(&b);
cout<<myasctime(t);

}
OUTPUT:

Task02: You will ask the user to enter any date of any year like 20-5-2000, this date will be
passed to mktime() function using built in struct tm pointer. Your function will modify the
current date by the date enter by the user and should show what day was on the date entered
by user by using the time_t. (time_t is just like an integer data type and its build in data type of
ctime library)
This function accepts the address of filled tm structure, dynamically creates a char array. Fills up
the array with date information in the following format, and then returns the address of this
array.
CODE:
#include<iostream>
#include<ctime>
#include<string>
#include<sstream>
using namespace std;
int main()
{
int day , month ,year;
cout<<"Enter the day :";
cin>>day;
cout<<endl;
cout<<"enter the month :";
cin>>month;
cout<<endl;
cout<<"Enter the year :";
cin>>year;
cout<<endl;
tm date={};
date.tm_year=year-1900;
date.tm_mon=month-1;
date.tm_mday=day;
date.tm_hour=0;
date.tm_min=0;
date.tm_sec=0;
time_t time=mktime(&date);
cout<<ctime(&time);
return 0;

}
OUTPUT:
Bonus Task:
Task03: Make your own ctime function with name as myctime()

char *myctime(const time_t *time);


CODE:
#include <iostream>
#include <ctime>

using namespace std;

string myctime() {
time_t now = time(0);
char* timeString = ctime(&now);
return string(timeString);
}

int main() {
cout << "The current local time is: " << myctime() << endl;
return 0;
}
OUTPUT:

You might also like