Time Function for C
/**
Code taken from
https://www.codevscolor.com/c-print-current-time-day-month-year/
**/
#include<stdio.h>
#include <time.h>
#define BUF_LEN 256
#define _XOPEN_SOURCE 700
#define LEN 150
int time_function();
int time2();
int TIME_HMS();
int main()
{
//time_function();
//time2();
TIME_HMS();
return 0;
}
int time_function()
{
time_t t;
time(&t);
printf(" ");
printf(" %s",ctime(&t));
struct tm *getdate(const char *);
printf(" s");
/*
time_t s, val = 1;
struct tm* current_time;
s = time(NULL);
current_time = localtime(&s);//declaration ends
//printing starts
printf("\n\n\n\n\n\n");
printf("\t\t%d ",current_time->tm_mday);
//printf("Day in this week = %d\n",current_time->tm_wday);
//printf("Month of this year = %d\n",(current_time->tm_mon + 1));
//printf("\nTarget\n");
if(current_time->tm_mon=4)
printf("April");
else if(current_time->tm_mon=5)
printf("May)");
else if(current_time->tm_mon=6)
printf("June");
else if(current_time->tm_mon=7)
printf("July");
else if(current_time->tm_mon=1)
printf("January");
else if(current_time->tm_mon=2)
printf("February");
else if(current_time->tm_mon=3)
printf("March");
else if(current_time->tm_mon=8)
printf("January");
else if(current_time->tm_mon=9)
printf("September");
else if(current_time->tm_mon=10)
printf("October");
else if(current_time->tm_mon=11)
printf("November");
else if(current_time->tm_mon=12)
printf("December");
else
printf("Error>>!<<");
//printf("\nTarget\n");
printf(", %d\n",(current_time->tm_year + 1900));//Year
/*printf("\tTime = %02d:%02d:%02d\n",
current_time->tm_hour,
current_time->tm_min,
current_time->tm_sec);*/ //24 Hour Format Code
/*
printf("\t\tCurrent time= %02d:%02d:%02d\n",
current_time->tm_hour%12,
current_time->tm_min,
current_time->tm_sec);
printf("\n %d",current_time->tm_wday);
printf("\t\t");
if(current_time->tm_wday=1)
printf("Monday (CSE115 & LAB)");
else if(current_time->tm_wday=5)
printf("Tuesday (No Class)");
else if(current_time->tm_wday=6)
printf("Wednesday (CSE115 & LAB)");
else if(current_time->tm_wday=7)
printf("Thursday (MAT116 & ENG103)");
else if(current_time->tm_wday=4)
printf("Friday (No Class)");
else if(current_time->tm_wday=2)
printf("Saturday (MAT116 & ENG103)");
else if(current_time->tm_wday=3)
printf("Sunday (No Class)");
else
printf("Error>>!<<");
printf("\n\n\n\n\n\n");
//screen holder;
int scr_hold;
p:
scanf("%d",scr_hold);
switch(scr_hold)
{
}
//screen holder ends;
*/
//Good Code:http://zetcode.com/articles/cdatetime/
return 0;
}
time2()
{
char buf[BUF_LEN] = {0};
time_t rawtime = time(NULL);
if (rawtime == -1) {
puts("The time() function failed");
return 1;
}
struct tm *ptm = localtime(&rawtime);
if (ptm == NULL) {
puts("The localtime() function failed");
return 1;
}
strftime(buf, BUF_LEN, " Time %I", ptm);
puts(buf);
strftime(buf, BUF_LEN, " %M", ptm);
puts(buf);
strftime(buf, BUF_LEN, " %d", ptm);
puts(buf);
strftime(buf, BUF_LEN, " %B", ptm);
puts(buf);
strftime(buf, BUF_LEN, " %A", ptm);
puts(buf);
// strftime(buf, BUF_LEN, "The timezone is %Z", ptm);
// puts(buf);
}
TIME_HMS()
{
//https://fresh2refresh.com/c-programming/c-time-related-functions/
//#define LEN 150
//how to use %something is here:http://zetcode.com/articles/cdatetime/
char buf[LEN];
time_t curtime;
struct tm *loc_time;
//Getting current time of system
curtime = time (NULL);
// Converting current time to local time
loc_time = localtime (&curtime);
// Displaying date and time in standard format
//printf("%s", asctime (loc_time));
/*strftime (buf, LEN, "Today is %A, %b %d.\n", loc_time);
fputs (buf, stdout);
strftime (buf, LEN, "Time is %I:%M %p.\n", loc_time);
fputs (buf, stdout);*/
printf("\ =======================\n");
strftime (buf, LEN, "| %d ", loc_time);
fputs (buf, stdout);
strftime (buf, LEN, "%B", loc_time);
fputs (buf, stdout);
strftime (buf, LEN, "(%m)", loc_time);
fputs (buf, stdout);
strftime (buf, LEN, ", %Y", loc_time);
fputs (buf, stdout);
printf("\n");
strftime (buf, LEN, "| %A", loc_time);
fputs (buf, stdout);
printf("\n");
strftime (buf, LEN, "| Time : %I:%M %p.\n", loc_time);
fputs (buf, stdout);
printf("\ =======================\n");
}
//////Code Ends Here
TIME_HMS(); is the working function.

Comments
Post a Comment