Documente online.
Zona de administrare documente. Fisierele tale
Am uitat parola x Creaza cont nou
 HomeExploreaza
upload
Upload




Time and Date functions

visual c en


Time and Date functions

The C library offers a lot of functions for working with dates and time. The first of them is the time function that returns the number of seconds that have passed since January first 1970, at midnight.



Several structures are defined that hold time information. The most important from them are 151e45b the "tm" structure and the "timeb" structure.

struct tm

The fields are self-explanatory. The structure "timeb" is defined in the directory include\sys, as follows:

struct timeb ;

We show here a small program that displays the different time settings.

#include <time.h>

#include <stdio.h>

#include <sys/types.h>

#include <sys/timeb.h>

#include <string.h>

void main()

/* Display operating system-style date and time.

strtime( tmpbuf );

printf( "OS time:\t\t\t\t%s\n", tmpbuf );

strdate( tmpbuf );

printf( "OS date:\t\t\t\t%s\n", tmpbuf );

/* Get UNIX-style time and display as number and string. */

time( &ltime );

printf( "Time in seconds since UTC 1/1/70:\t%ld\n", ltime );

printf( "UNIX time and date:\t\t\t%s", ctime( &ltime ) );

/* Display UTC. See note (1) in text */

gmt = gmtime( &ltime );

printf( "Coordinated universal time:\t\t%s", asctime( gmt ) );

/* Convert to time structure and adjust for PM if necessary. */

today = localtime( &ltime );

if( today->tm_hour > 12 )

if( today->tm_hour == 0 ) /* Adjust if midnight hour. */

today->tm_hour = 12;

/* See note (2) in text */

printf( "12-hour time:\t\t\t\t%.8s %s\n",

asctime( today ) + 11, ampm );

/* Print additional time information.

ftime( &tstruct );

printf( "Plus milliseconds:\t\t\t%u\n", tstruct.millitm );

printf( "Zone difference in seconds from UTC:\t%u\n",

tstruct.timezone );

printf( "Time zone name:\t\t\t\t%s\n", _tzname[0] );

printf( "Daylight savings:\t\t\t%s\n", // See note (3) in text

tstruct.dstflag ? "YES" : "NO" );

/* Make time for noon on Christmas, 1993. */

if( mktime( &xmas ) != (time_t)-1 )

printf( "Christmas\t\t\t\t%s\n", asctime( &xmas ) );

/* Use time structure to build a customized time string. */

today = localtime( &ltime );

/* Use strftime to build a customized time string. */

strftime( tmpbuf, 128,

"Today is %A, day %d of the month of %B in the year %Y.\n",

today );

printf( tmpbuf );

We use this opportunity for introducing new C constructs.

We see the function call gmtime(&ltime);. What does this mean? The function gmtime requires a pointer to a variable of type time_t. We do not have a pointer, so we make one "on the fly" by using the "address-of" operator.

The printf statement uses pointer addition to skip the first 11 characters of the result of asctime. That function returns a pointer to a character string. To skip characters we just add up a constant (11) to that pointer, effectively skipping those characters. Since we want to display the 8 following characters only, we pass a width argument to the %s directive of printf. As you know, "%s" is a directive that instructs printf to display a character string. Since we give it a maximum width of 8, only the first 8 chars will be displayed.

We see here the construct (expr) ? val1 : val2; This construct evaluates first the expression, in this case "tstruct.dstflag". If the value is different than zero, the return value of the expression will be the first value, in this case the character string "YES". If the expression evaluates to zero, the second value will be choosen , in this case the character string "NO". The result of this is passed to printf as an argument.

The Windows system too has a lot of time-related functions. Here is a handy list of the most important. Note that file times are kept using 64 bits in modern versions of windows, i.e. the numbers represent the number of 100 nanosecond intervals since January first, 1601.

Function

Purpose

CompareFileTime

Compares two 64-bit file times

DosDateTimeToFileTime

Converts MS-DOS date and time values to a 64-bit file time.

FileTimeToDosDateTime

Converts a 64-bit file time to MS-DOS date and time values.

FileTimeToLocalFileTime

Converts a file time based on the Coordinated Universal Time (UTC) to a local file time.

FileTimeToSystemTime

Converts a 64-bit file time to system time format

GetFileTime

Retrieves the date and time that a file was created, last accessed, and last modified.

GetLocalTime

Retrieves the current local date and time.

GetSystemTime

Retrieves the current system date and time.

GetSystemTimeAdjustment

Determines whether the system is applying periodic time adjustments to its time-of-day clock at each clock interrupt, along with the value and period of any such adjustments.

GetSystemTimeAsFileTime

Obtains the current system date and time. The information is in Coordinated Universal Time (UTC) format.

GetTickCount

Retrieves the number of milliseconds that have elapsed since the system was started. It is limited to the resolution of the system timer.

GetTimeZoneInformation

Retrieves the current time-zone parameters. These parameters control the translations between Coordinated Universal Time (UTC) and local time.

LocalFileTimeToFileTime

Converts a local file time to a file time based on the Coordinated Universal Time (UTC).

SetFileTime

Sets the date and time that a file was created, last accessed, or last modified.

SetLocalTime

Sets the current local time and date.

SetSystemTime

Sets the current system time and date. The system time is expressed in Coordinated Universal Time (UTC).

SetSystemTimeAdjustment

Tells the system to enable or disable periodic time adjustments to its time of day clock.

SetTimeZoneInformation

Sets the current time-zone parameters. These parameters control translations from Coordinated Universal Time (UTC) to local time.

SystemTimeToFileTime

Converts a system time to a file time.

SystemTimeToTzSpecificLocalTime

Converts a Coordinated Universal Time (UTC) to a specified time zone's corresponding local time.



Since this is stored in a 32 bit integer, the counter will overflow somewhere in year 2038. I hope I will be around to celebrate that event.

This clock will overflow in something like 2.000 years so be prepared for windows 4.000!


Document Info


Accesari: 1420
Apreciat: hand-up

Comenteaza documentul:

Nu esti inregistrat
Trebuie sa fii utilizator inregistrat pentru a putea comenta


Creaza cont nou

A fost util?

Daca documentul a fost util si crezi ca merita
sa adaugi un link catre el la tine in site


in pagina web a site-ului tau.




eCoduri.com - coduri postale, contabile, CAEN sau bancare

Politica de confidentialitate | Termenii si conditii de utilizare




Copyright © Contact (SCRIGROUP Int. 2024 )