The ftime64_s() and ctime_s() functions: Displaying current date and time



Compiler: Visual C++ Express Edition 2005

Compiled on Platform: Windows XP Pro SP2

Target platform: none, just for learning

Header file: Standard and Windows

Additional library: Windows Platform SDK

Additional project setting: Set project to be compiled as C

Project -> your_project_name Properties -> Configuration Properties -> C/C++ -> Advanced -> Compiled As: Compiled as C Code (/TC)

Other info: non-CLR or unmanaged

To do: Displaying the current date and time

To show: Using ftime64_s() and ctime_s() C functions



// ftime64_s() usage. This program uses _ftime64_s() to obtain the current time and then stores this time in timebuffer.

#include <stdio.h>

#include <sys.h>

#include <time.h>


int main(void)


struct _timeb timebuffer;

char timeline[26];

errno_t err;

time_t time1;

unsigned short millitm1;

short timezone1;

short dstflag1;



time1 = timebuffer.time;

millitm1 = timebuffer.millitm;

timezone1 = timebuffer.timezone;

dstflag1 = timebuffer.dstflag;


printf("Seconds since midnight, January 1, 1970 (UTC): %I64d\n", time1);

printf("Milliseconds: %d\n", millitm1);

printf("Minutes between UTC and local time: %d\n", timezone1);

printf("Daylight savings time flag (1 means Daylight time is in effect): %d\n", dstflag1);


err = ctime_s(timeline, 26, & (timebuffer.time));

if (err)


printf("Invalid argument to ctime_s().");


printf("The time is %.19s.%hu %s", timeline, timebuffer.millitm, &timeline[20]);



Output example:


Seconds since midnight, January 1, 1970 (UTC): 1134811376

Milliseconds: 578

Minutes between UTC and local time: -480

Daylight savings time flag (1 means Daylight time is in effect): 0

The time is Sat Dec 17 17:22:56.578 2005

Press any key to continue . . .



C and C++ Programming Resources | C & C++ Code Example Index