asctime
#include <time.h>
#include <stdio.h>
int main(int argc, char *argv[])
{
struct tm *newtime;
time_t aclock;
/* Get time in seconds */
time( &aclock );
/* Convert time to struct tm form */
newtime = localtime( &aclock );
/* Print local time as a string */
printf( "The current date and time are:\n\t\t%s",
asctime( newtime ) );
return 0;
}
OUTPUT :
// The current date and time are:
// Sat Feb 22 18:20:25 2003
asctime_r
#include <stdio.h>
#include <time.h>
#include <string.h>
int main (int argc, char *argv[])
{
char buff[256];
char *p;
struct tm *ttime;
time_t t;
time(&t);
ttime = localtime(&t);
puts(asctime(ttime));
/* asctime_r returns ptr to string */
p = (asctime_r(ttime,buff));
/* ptr */
puts(p);
/* buff */
puts(buff);
return 0;
}
OUTPUT :
// Sat Feb 22 18:41:08 2003
//
// Sat Feb 22 18:41:08 2003
//
// Sat Feb 22 18:41:08 2003
clock
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(int argc, char *argv[])
{
int i;
long milisec = 500000000;
clock_t start, finish, seconds;
double duration;
/* CLOCKS_PER_SEC is the number of processor clock
ticks per second. */
printf("CLOCKS_PER_SEC is %lu\n",
(unsigned long)CLOCKS_PER_SEC);
/* Pause for 5 seconds. */
printf("pause 5 seconds\n");
for ( i = 5; i > 0; i-- )
{
printf("%d ",i);
fflush(stdout);
seconds = 1 * CLOCKS_PER_SEC + clock();
while ( seconds > clock() )
;
}
printf("\nDone!\n\n");
/* Measure the duration of an event. */
printf("Duration of empty loop %lu times\n",
milisec);
start = clock();
while( milisec-- )
;
finish = clock();
duration = (double)(finish - start) / CLOCKS_PER_SEC;
printf("Start is %lu\n",start);
printf("Finish is %lu\n",finish);
printf("Duration time is %2.2f seconds\n", duration );
return 0;
}
OUTPUT :
// CLOCKS_PER_SEC is 1000000
// pause 5 seconds
// 5 4 3 2 1
// Done!
//
// Duration of empty loop 500000000 times
// Start is 5010000
// Finish is 9090000
// Duration time is 4.08 seconds
ctime
#include <time.h>
#include <stdio.h>
int main(int argc, char *argv[])
{
time_t ltime;
/* time in seconds */
time( <ime );
/* print current time */
printf( "The time is %s",
ctime( <ime ) );
return 0;
}
OUTPUT :
// The time is Sat Feb 22 18:22:55 2003
difftime
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(int argc, char *argv[])
{
time_t start, finish;
unsigned long i = 500000000;
double elapsed_time;
printf( "Empty loop %lu times\n",i);
time( &start );
while ( i-- )
;
time( &finish );
elapsed_time = (double)difftime( finish, start );
printf("\nStart is %s",(ctime(&start)));
printf("Finish is %s",(ctime(&finish)));
printf("Duration took %6.2f seconds.\n",
elapsed_time );
return 0;
}
OUTPUT :
// Empty loop 500000000 times
//
// Start is Sat Feb 22 15:13:17 2003
// Finish is Sat Feb 22 15:13:21 2003
// Duration took 4.00 seconds.
dysize
#include <stdio.h>
#include <time.h>
int main (int argc, char *argv[])
{
int days_in_year;
int year1 = 2004;
int year2 = 2003;
days_in_year = dysize(year1);
printf("%d year consists %d days\n",
year1,days_in_year);
days_in_year = dysize(year2);
printf("%d year consists %d days\n",
year2,days_in_year);
return 0;
}
OUTPUT :
// 2004 year consists 366 days
// 2003 year consists 365 days
ftime
/*
ftime function uses to obtain the current
time and then stores this time in timebuffer.
*/
#include <stdio.h>
#include <sys/timeb.h>
#include <time.h>
int main(int argc, char *argv[])
{
struct timeb timebuffer;
char *timeline;
ftime( &timebuffer );
timeline = ctime( & ( timebuffer.time ) );
printf( "The time is %.19s.%hu %s",
timeline,
timebuffer.millitm,
&timeline[20] );
return 0;
}
OUTPUT :
// The time is Sat Feb 22 20:01:32.417 2003
gmtime
#include <time.h>
#include <stdio.h>
int main(int argc, char *argv[])
{
struct tm *newtime;
long lt;
time( < );
puts(ctime(<));
/* Obtain coordinated universal time: */
newtime = gmtime( < );
printf( "Coordinated universal time :\n\t\t%s",
asctime( newtime ) );
return 0;
}
OUTPUT :
// Sat Feb 22 18:16:11 2003
//
// Coordinated universal time :
// Sat Feb 22 23:16:11 2003
leap_year
#include <stdio.h>
int main (int argc, char *argv[])
{
int i;
for ( i = 999; i < 1013; i++ )
{
if ( leap_year(i) )
printf("%4d year is leap year\n",i);
else
printf("%4d year is common year\n",i);
}
return 0;
}//
//------------------------------------------------
int leap_year(int year)
//------------------------------------------------
{
if ( (year % 4 == 0) && ( year % 100 != 0 ||
year % 400 == 0) )
return 1;
else
return 0;
}
OUTPUT :
// 999 year is common year
// 1000 year is common year
// 1001 year is common year
// 1002 year is common year
// 1003 year is common year
// 1004 year is leap year
// 1005 year is common year
// 1006 year is common year
// 1007 year is common year
// 1008 year is leap year
// 1009 year is common year
// 1010 year is common year
// 1011 year is common year
// 1012 year is leap year
localtime
#include <stdio.h>
#include <string.h>
#include <time.h>
int main(int argc, char *argv[])
{
struct tm *newtime;
char am_pm[] = "AM";
time_t long_time;
/* Get time as long integer. */
time( &long_time );
/* Convert to local time. */
newtime = localtime( &long_time );
if( newtime->tm_hour > 12 )
{
/* set up extension. */
strcpy( am_pm, "PM" );
/* to 12-hour clock. */
newtime->tm_hour -= 12;
}
/* Set hour to 12 if midnight. */
if( newtime->tm_hour == 0 )
newtime->tm_hour = 12;
printf( "%.24s %s\n", asctime(newtime), am_pm );
return 0;
}
OUTPUT :
// Sat Feb 22 06:10:11 2003 PM
mktime
#include <stdio.h>
#include <time.h>
int main (int argc, char *argv[])
{
struct tm init = { 30, // seconds
10, // minutes
12, // hours
3, // date
5, // month (0-jan)
103, // year+1900,
2 }; // date (0-Sun)
struct tm *tpt = &init;
if ( mktime(&init) != (time_t)-1 )
printf("30,10,12,3,5,103,2 is %s",
asctime(&init));
tpt->tm_sec = 8;
tpt->tm_min = 22;
tpt->tm_hour = 1;
tpt->tm_mday = 15;
tpt->tm_mon = 11;
tpt->tm_year = 100;
tpt->tm_wday = 6;
printf("Changed time is %s",
asctime(&init));
return 0;
}
OUTPUT :
// 30,10,12,3,5,103,2 is Tue Jun 3 13:10:30 2003
// Changed time is Sat Dec 15 01:22:08 2000
my_difftime
/*
converts number of seconds in
year(s), days(s), hours, minutes, seconds
*/
#include <stdio.h>
#include <time.h>
short int leap_year(int*);
void convert_from_sec(size_t, unsigned int*,
unsigned int*, unsigned short int*,
unsigned short int*, unsigned short int*);
//------------------------------------------------
int main (int argc, char *argv[])
//------------------------------------------------
{
size_t t = 1200000009;
unsigned short int sec, min, hour;
unsigned int days, year;
sec = min = hour = days = year = 0;
convert_from_sec(t,&year,&days,
&hour,&min,&sec);
printf("%lu seconds = ",(unsigned long) t);
printf("\n\t%d years, ",year);
printf("%d days, ",days);
printf("%d hours,",hour);
printf("\n\t%d minutes, and ",min);
printf("%d seconds.\n",sec);
return 0;
}
//------------------------------------------------
void convert_from_sec(size_t t,
//------------------------------------------------
unsigned int* y,
unsigned int *d,
unsigned short int *h,
unsigned short int *m,
unsigned short int *s)
//------------------------------------------------
{
unsigned int sec_per_day = 60*60*24;
unsigned int sec_per_hou = 60*60;
unsigned int sec_per_leap = 3600*24*365;
unsigned int sec_per_comm = 3600*24*366;
unsigned int leap;
while ( t >= sec_per_leap )
{
(*y)++;
if ( leap_year(y) )
t -= sec_per_leap;
else
t -= sec_per_comm;
leap = (*y)+1;
if ( t == sec_per_leap &&
!leap_year(&leap) )
break;
}
while ( t >= sec_per_day )
{
(*d)++;
t -= sec_per_day;
}
while ( t >= sec_per_hou )
{
(*h)++;
t -= sec_per_hou;
}
while ( t >= 60 )
{
(*m)++;
t -= 60;
}
*s = (unsigned short)t;
}
//------------------------------------------------
short int leap_year(int *year)
//------------------------------------------------
{
if ( (*year % 4 == 0) && ( *year % 100 != 0 ||
*year % 400 == 0) )
return 1;
else
return 0;
}
OUTPUT :
// 1200000009 seconds =
// 37 years, 355 days, 21 hours,
// 20 minutes, and 9 seconds.
stime
#include <stdio.h>
#include <time.h>
int main (int argc, char *argv[])
{
struct tm *tt
time_t init;
time_t correction = 1*60*60*24*365;
time(&init);
tt = localtime(&init);
printf("Current time : %s",asctime(tt));
tt->tm_year = 100;
/* function for a supperuser */
/* change time - 1 year */
init = init - correction;
stime(&init);
tt = localtime(&init);
printf("Current time : %s",asctime(tt));
/* return to old time */
init = init + correction;
stime(&init);
tt = localtime(&init);
printf("Current time : %s",asctime(tt));
return 0;
}
OUTPUT :
// Current time : Sat Feb 22 18:57:34 2003
// Current time : Fri Feb 22 18:57:34 2002
// Current time : Sat Feb 22 18:57:34 2003
strftime
#include <stdio.h>
#include <time.h>
int main (int argc, char *argv[])
{
char buff[401];
time_t t;
struct tm *today;
time(&t);
today = localtime(&t);
/* strftime builds a customized time string. */
strftime(buff,400,"%%a %a The abbreviated weekday namen\n\t\t"
"according to the current locale.",today);
puts(buff);
strftime(buff,400,"%%A %A The full weekday\n\t\t"
"name according to the current locale.",today);
puts(buff);
strftime(buff,400,"%%b %b The abbreviated month\n\t\t"
"name according to the current locale.",today);
puts(buff);
strftime(buff,400,"%%B %B The full month name according\n\t\t"
"to the current locale.",today);
puts(buff);
strftime(buff,400,"%%c %c The preferred date and time\n\t\t"
"representation for the current locale.",today);
puts(buff);
strftime(buff,400,"%%C %C The century number\n\t\t"
"(year/100) as a 2-digit integer.(SU)",today);
puts(buff);
strftime(buff,400,"%%d %d The day of the month as a\n\t\t"
"decimal number (range 01 to 31).",today);
puts(buff);
strftime(buff,400,"%%D %D Equivalent to %%m/%%d/%%y.\n\t\t"
"(Yecch - for Americans only. Americans should\n\t\t"
"note that in other countries %%d/%%m/%%y is\n\t\t"
"rather common. This means that in international\n\t\t"
"context this format is ambiguous and should not\n\t\t"
"be used.) (SU)",today);
puts(buff);
strftime(buff,400,"%%e %e Like %%d, the day of the\n\t\t"
"month as a decimal number, but a leading zero\n\t\t"
"is replaced by a space. (SU)",today);
puts(buff);
strftime(buff,400,"%%E %E Modifier: use alternative\n\t\t"
"format, see below. (SU)",today);
puts(buff);
strftime(buff,400,"%%G %G The ISO 8601 year with\n\t\t"
"century as a decimal number. The 4-digit year\n\t\t"
"corresponding to the ISO week number (see %%V).\n\t\t"
"This has the same format and value as %%y,\n\t\t"
"except that if the ISO week number belongs\n\t\t"
"to the previous or next year, that year is\n\t\t"
"used instead. (TZ)",today);
puts(buff);
strftime(buff,400,"%%g %g Like %%G, but without\n\t\t"
"century, i.e., with a 2-digit year (00-99). (TZ)",
today);
puts(buff);
strftime(buff,400,"%%h %h Equivalent to %%b. (SU)",today);
puts(buff);
strftime(buff,400,"%%H %H The hour as a decimal\n\t\t"
"number using a 24-hour clock (range 00 to 23).",
today);
puts(buff);
strftime(buff,400,"%%I %I The hour as a decimal\n\t\t"
"number using a 12-hour clock (range 01 to 12).",
today);
puts(buff);
strftime(buff,400,"%%j %j The day of the year as a\n\t\t"
"decimal number (range 001 to 366).",today);
puts(buff);
strftime(buff,400,"%%k %k The hour (24-hour clock)\n\t\t"
"as a decimal number (range 0 to 23); single\n\t\t"
"digits are preceded by a blank.\n\t\t"
"(See also %%H.) (TZ)",today);
puts(buff);
strftime(buff,400,"%%l %l The hour (12-hour clock)\n\t\t"
"as a decimal number (range 1 to 12); single\n\t\t"
"digits are preceded by a blank.\n\t\t"
"(See also %I.) (TZ)",today);
puts(buff);
strftime(buff,400,"%%m %m The month as a decimal\n\t\t"
"number (range 01 to 12).",today);
puts(buff);
strftime(buff,400,"%%M %M The minute as a decima\n\t\t"
"number (range 00 to 59).",today);
puts(buff);
strftime(buff,400,"%%n %n A newline character. (SU)",
today);
puts(buff);
strftime(buff,400,"%%O %O Modifier: use alternative\n\t\t"
"format, see below. (SU)",today);
puts(buff);
strftime(buff,400,"%%p %p Either `AM' or `PM'\n\t\t"
"according to the given time value, or the\n\t\t"
"corresponding strings for the current\n\t\t"
"locale. Noon is treated as `pm' and\n\t\t"
"midnight as `am'.",today);
puts(buff);
strftime(buff,400,"%%P %P Like %%p but in\n\t\t"
"lowercase: `am' or `pm' or a corresponding\n\t\t"
"string for the current locale. (GNU)",today);
puts(buff);
strftime(buff,400,"%%r %r The time in a.m.\n\t\t"
"or p.m. notation. In the POSIX locale this\n\t\t"
"is equivalent to `%%I:%%M:%%S %%p'. (SU)",today);
puts(buff);
strftime(buff,400,"%%R %R The time in 24-hour\n\t\t"
"notation (%%H:%%M). (SU) For a version\n\t\t"
"including the seconds, see %%T below.",today);
puts(buff);
strftime(buff,400,"%%s %s The number of seconds\n\t\t"
"since the Epoch, i.e., since\n\t\t"
"1970-01-01 00:00:00 UTC. (TZ)",today);
puts(buff);
strftime(buff,400,"%%S %S The second as a\n\t\t"
"decimal number (range 00 to 61).",today);
puts(buff);
strftime(buff,400,"%%t %t A tab character. (SU)",
today);
puts(buff);
strftime(buff,400,"%%T %T The time in 24-hour\n\t\t"
"notation (%%H:%%M:%%S). (SU)",today);
puts(buff);
strftime(buff,400,"%%u %u The day of the week\n\t\t"
"as a decimal, range 1 to 7, Monday being 1.\n\t\t"
"See also %w. (SU)",today);
puts(buff);
strftime(buff,400,"%%U %U The week number of\n\t\t"
"the current year as a decimal number, range\n\t\t"
"00 to 53, starting with the first Sunday\n\t\t"
"as the first day of week 01. See also\n\t\t"
"%%V and %%W.",today);
puts(buff);
strftime(buff,400,"%%V %V The ISO 8601:1988\n\t\t"
"week number of the current year as a\n\t\t"
"decimal number, range 01 to 53, where week 1\n\t\t"
"is the first week that has at least 4 days in\n\t\t"
"the current year, and with Monday as the\n\t\t"
"first day of the week.\n\t\t"
"See also %%U and %%W. (SU)",today);
puts(buff);
strftime(buff,400,"%%w %w The day of the week\n\t\t"
"as a decimal, range 0 to 6, Sunday\n\t\t"
"being 0. See also %%u.",today);
puts(buff);
strftime(buff,400,"%%W %W The week number of\n\t\t"
"the current year as a decimal number,\n\t\t"
"range 00 to 53, starting with the first\n\t\t"
"Monday as the first day of week 01.",today);
puts(buff);
strftime(buff,400,"%%x %x The preferred date\n\t\t"
"representation for the current locale\n\t\t"
"without the time.",today);
puts(buff);
strftime(buff,400,"%%X %X The preferred time\n\t\t"
"representation for the current locale\n\t\t"
"without the date.",today);
puts(buff);
strftime(buff,400,"%%y %y The year as a decimali\n\t\t"
"number without a century (range 00 to 99).",
today);
puts(buff);
strftime(buff,400,"%%Y %Y The year as a decimal\n\t\t"
"number including the century.",today);
puts(buff);
strftime(buff,400,"%%z %z The time-zone as hour\n\t\t"
"offset from GMT. Required to emit\n\t\t"
"RFC822-conformant dates\n\t\t"
"(using \"%%a, %%d %%b %%Y %%H:%%M:%%S %%z\").\n\t\t"
"(GNU)",today);
puts(buff);
strftime(buff,400,"%%Z %Z The time zone or name or\n\t\t"
"abbreviation.",today);
puts(buff);
strftime(buff,400,"%%+ %+ The date and time\n\t\t"
"in date(1) format. (TZ)",today);
puts(buff);
return 0;
}
OUTPUT :
// %a Sat The abbreviated weekday namen
// according to the current locale.
// %A Saturday The full weekday
// name according to the current locale.
// %b Feb The abbreviated month
// name according to the current locale.
// %B February The full month name according
// to the current locale.
// %c Sat Feb 22 16:41:16 2003 The preferred
// date and time representation for the current
// locale.
// %C 20 The century number
// (year/100) as a 2-digit integer.(SU)
// %d 22 The day of the month as a
// decimal number (range 01 to 31).
// %D 02/22/03 Equivalent to %m/%d/%y.
// (Yecch - for Americans only. Americans should
// note that in other countries %d/%m/%y is
// rather common. This means that in international
// context this format is ambiguous and should not
// be used.) (SU)
// %e 22 Like %d, the day of the
// month as a decimal number, but a leading zero
// is replaced by a space. (SU)
// %E %E Modifier: use alternative
// format, see below. (SU)
// %G 2003 The ISO 8601 year with
// century as a decimal number. The 4-digit year
// corresponding to the ISO week number (see %V).
// This has the same format and value as %y,
// except that if the ISO week number belongs
// to the previous or next year, that year is
// used instead. (TZ)
// %g 03 Like %G, but without
// century, i.e., with a 2-digit year (00-99). (TZ)
// %h Feb Equivalent to %b. (SU)
// %H 16 The hour as a decimal
// number using a 24-hour clock (range 00 to 23).
// %I 04 The hour as a decimal
// number using a 12-hour clock (range 01 to 12).
// %j 053 The day of the year as a
// decimal number (range 001 to 366).
// %k 16 The hour (24-hour clock)
// as a decimal number (range 0 to 23); single
// digits are preceded by a blank.
// (See also %H.) (TZ)
// %l 4 The hour (12-hour clock)
// as a decimal number (range 1 to 12); single
// digits are preceded by a blank.
// (See also 04.) (TZ)
// %m 02 The month as a decimal
// number (range 01 to 12).
// %M 41 The minute as a decima
// number (range 00 to 59).
// %n
// A newline character. (SU)
// %O %O Modifier: use alternative
// format, see below. (SU)
// %p PM Either `AM' or `PM'
// according to the given time value, or the
// corresponding strings for the current
// locale. Noon is treated as `pm' and
// midnight as `am'.
// %P pm Like %p but in
// lowercase: `am' or `pm' or a corresponding
// string for the current locale. (GNU)
// %r 04:41:16 PM The time in a.m.
// or p.m. notation. In the POSIX locale this
// is equivalent to `%I:%M:%S %p'. (SU)
// %R 16:41 The time in 24-hour
// notation (%H:%M). (SU) For a version
// including the seconds, see %T below.
// %s 1045950076 The number of seconds
// since the Epoch, i.e., since
// 1970-01-01 00:00:00 UTC. (TZ)
// %S 16 The second as a
// decimal number (range 00 to 61).
// %t A tab character. (SU)
// %T 16:41:16 The time in 24-hour
// notation (%H:%M:%S). (SU)
// %u 6 The day of the week
// as a decimal, range 1 to 7, Monday being 1.
// See also 6. (SU)
// %U 07 The week number of
// the current year as a decimal number, range
// 00 to 53, starting with the first Sunday
// as the first day of week 01. See also
// %V and %W.
// %V 08 The ISO 8601:1988
// week number of the current year as a
// decimal number, range 01 to 53, where week 1
// is the first week that has at least 4 days in
// the current year, and with Monday as the
// first day of the week.
// See also %U and %W. (SU)
// %w 6 The day of the week
// as a decimal, range 0 to 6, Sunday
// being 0. See also %u.
// %W 07 The week number of
// the current year as a decimal number,
// range 00 to 53, starting with the first
// Monday as the first day of week 01.
// %x 02/22/03 The preferred date
// representation for the current locale
// without the time.
// %X 16:41:16 The preferred time
// representation for the current locale
// without the date.
// %y 03 The year as a decimali
// number without a century (range 00 to 99).
// %Y 2003 The year as a decimal
// number including the century.
// %z -0500 The time-zone as hour
// offset from GMT. Required to emit
// RFC822-conformant dates
// (using "%a, %d %b %Y %H:%M:%S %z").
// (GNU)
// %Z EST The time zone or name or
// abbreviation.
// %+ %+ The date and time
// in date(1) format. (TZ)
time
#include <stdio.h>
#include <time.h>
int main (int argc, char *argv[])
{
char buff[51];
time_t cur_time;
struct tm* local_time;
/* initit cur_time */
time(&cur_time);
/* genereate pointer for formatting time */
local_time = localtime(&cur_time);
/* formatting output to 'buff' */
strftime(buff,50,"%A %b %e %T %p %G",
local_time);
puts(buff);
/* convert binary data for formatting output */
puts(ctime(&cur_time));
return 0;
}
OUTPUT :
// Friday Feb 21 00:42:18 AM 2003
// Fri Feb 21 00:42:18 2003
tzset
#include <time.h>
#include <stdlib.h>
#include <stdio.h>
int main(int argc, char *argv[])
{
if( putenv( "TZ=EST5EDT" ) == -1 )
{
printf( "Unable to set TZ\n" );
exit( 1 );
}
else
{
tzset();
printf( "daylight = %d\n", daylight );
printf( "timezone = %ld\n", timezone );
printf( "tzname[0] = %s\n", tzname[0] );
}
return 0;
}
OUTPUT :
// daylight = 1
// timezone = 18000
// tzname[0] = EST