Professional Documents
Culture Documents
Lab07 Ctime 22i-6176 C
Lab07 Ctime 22i-6176 C
(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
Example:
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.,
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()
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: