Получить текущее время
#include <sys/timeb.h>int ftime( struct timeb *timeptr );
libc
Функция ftime() сохраняет текущее время в структуре timeptr ( struct timeb).
#include <stdio.h>#include <time.h>#include <sys/timeb.h>#include <stdlib.h>int main( void ){struct timeb timebuf;char *now;ftime( &timebuf );now = ctime( &timebuf.time );/** Note that we're cutting "now" off after 19 characters to* avoid the \n that ctime() appends to the formatted time* string.*/printf( "The time is %.19s.%hu\n", now, timebuf.millitm );return (EXIT_SUCCESS);}
Код генерирует следующий вывод:
$ ./a.out The time is Mon Jul 05 15:58:42.870
POSIX 1003.1 X/Open Systems Interfaces Extension
struct timeb, asctime(), clock(), ctime(), difftime(), gmtime(), localtime(), mktime(), strftime(), time(), tzset()
Предыдущий раздел: Описание API системной библиотеки