QReferate - referate pentru educatia ta.
Referatele noastre - sursa ta de inspiratie! Referate oferite gratuit, lucrari si proiecte cu imagini si grafice. Fiecare referat, proiect sau comentariu il poti downloada rapid si il poti folosi pentru temele tale de acasa.



AdministratieAlimentatieArta culturaAsistenta socialaAstronomie
BiologieChimieComunicareConstructiiCosmetica
DesenDiverseDreptEconomieEngleza
FilozofieFizicaFrancezaGeografieGermana
InformaticaIstorieLatinaManagementMarketing
MatematicaMecanicaMedicinaPedagogiePsihologie
RomanaStiinte politiceTransporturiTurism
Esti aici: Qreferat » Referate informatica

Functii de timp si data



Functii de timp si data


Prototipurile functiilor de timp si data se gasesc in fisierul antet time.h. Tot in acest fisier sunt definite trei tipuri de reprezentare a timpului si date si doua macrocomenzi.

Tipuri pentru reprezentarea timpului si datei

Cele trei structuri pentru reprezentarea datei si timpului sunt: tm, time_t si clock_t.

Tipul tm este definit astfel:



struct tm


tm este o structura care defineste timpul broken-down. Aceasta structura este utilizata de functiile: asctime, gmtime, localtime, mktime si strftime.

Tipul time_t reprezinta timpul si data sistemului ca un intreg. Acesta este denumit calendar time.

Tipul clock_t este returnat de functia clock si contine timpul scurs din momentul lansarii in executie a programului. Timpul este masurat in impulsuri de ceas. Numarul de impulsuri pe secunda este definit de constanta CLK_TCK.

Functii pentru conversia date si timpului in sir de caractere

Pentru conversia timpului si datei intr-un sir de caractere exista doua functii si anume:

asctime - care converteste in zona indicata sub forma unui sir de 26 caractere timpul si data stocat intr-o structura. Prototipul functiei este:

char *asctime(const struct tm *tblock);

unde: tblock este zona in care se executa conversia datei si timpului.

ctime - care convertestevaloarea timpului intr-un sir de 26 caractere. Prototipul functiei este:

char *ctime(const time_t *time

unde : time este zona in care se obtine sirul prin conversia valorii timpului.

Sirul de 26 caractere obtinut prin conversie este terminat cu caracterul newline (n) si un caracter null (0) si are urmatorul format:

DDD MMM dd hh:mm:ss YYYY

unde:

DDD = Ziua (Mon, Tue, Wed, etc.)

MMM = Luna (Jan, Feb, Mar, etc.)

dd = Data (1, 2, , 31)

hh = Ora (1, 2, , 24)

mm = Minutele (1, , 59)

ss = Secundele (1, , 59)

YYYY = An

Spre exemplu:

Mon Oct 20 11:31:54 1952

Toate campurile au lungime constanta

Valori returnate

Ambele functii returneaza un pointer spre sirul de caractere care contine data si timpul. Acest sir este o variabila statica, care este rescrisa de fiecare data cand se apeleaza functia.

Exemplu de utilizare a functiei asctime

#include <string.h>

#include <time.h>

#include <stdio.h>

int main(void)


Prin rularea acestui program se va afisa:

Thu Dec 22 09:30:01 2002

Exemplu de utilizare a functiei ctime

#include <stdio.h>

#include <time.h>

int main(void)


Prin rularea acestui program, rezultatul afisat apare sub forma:

Data si timpul sistemului este: Fri Jun 21 10:18:59 2002

Functii pentru obtinerea si stabilirea timpului si datei

Pentru a obtine timpul scurs de la 01/01/1970 se foloseste functia time, iar pentru a seta data si timpul sistemului se utilizeaza functia stime. Prototipurile functiilor sunt:

time_t time(time_t *timer

int stime(time_t *tp

Observatii

Functia time furnizeaza timpul, in secunde, scurs de la 1 ianuarie 19700 (00:00:00 GMT). Aceasta valoare este stocata in zona referita de *timer,

Functia stime seteaza data si timpul sistemului, masurat in secunde de la 01.01.197 (00:00:00 GMT). Variabila tp refera valoarea timpului exprimat in secunde.

Valori returnate

Functia time returneaza timpul scurs in secunde.

Functia stime returneaza 0.

Exemple

Exemplu pentru functia time

#include <time.h>

#include <stdio.h>

#include <dos.h>

int main(void)


La rularea acestui program, pe ecran se va afisa:

Numarul de secunde de la 1 Ianuarie, 1970 este 1024761177

Exemplu pentru utilizarea functiei stime

#include <stdio.h>

#include <time.h>

int main(void)


La rularea acestui program, pe ecran se obtine:

Data curenta este: Sat Jun 22 11:57:55 2002

Noua data este Fri Jun 21 11:57:55 2002

Functii pentru conversia datei si timpului la o structura

gmtime, localtime  <TIME.H>


gmtime converts date and time to Greenwich Mean Time (GMT)

localtime converts date and time to a structure


Declarations


struct tm *gmtime(const time_t *timer);

struct tm *localtime(const time_t *timer);


Remarks


Both functions accept the address of a value returned by time and return a pointer to a tm structure containing the broken-down time.


gmtime converts directly to GMT.


localtime corrects for the time zone and possible daylight saving time.



Arg What It Is/Does


timer Points to location of time_t value returned by time



Set timezone to the difference in seconds between GMT and local standard time.

In PST, timezone = 8(hrs) * 60(min/hr) * 60(sec/min)

timezone  <TIME.H>

Difference in seconds between Greenwich Mean Time and local time.

Declaration: extern long timezone;

Remarks

Used by the time-and-date functions.

Set daylight to non-zero if and only if the standard U.S. daylight saving time conversion should be applied.

daylight (global variable) <TIME.H>

Indicates whether Daylight Savings Time adjustments will occur

Declaration:  extern int daylight;

Remarks

The time-and-date functions use daylight.

The ftime, localtime, and tzset functions set daylight to 1 or 0:

Value   Meaning

Make Daylight Savings Time adjustments

Do not adjust for Daylight Savings Time

Return Value

gmtime and localtime return a pointer to the tm structure containing the broken-down time.

This structure is a static that is overwritten with each call.


/* gmtime example */


#include <stdio.h>

#include <stdlib.h>

#include <time.h>

#include <dos.h>


/* Pacific Standard Time & Daylight Savings */

char *tzstr = 'TZ=PST8PDT';


int main(void)



/* localtime example */


#include <time.h>

#include <stdio.h>

#include <dos.h>


int main(void)


Functii pentru conversia timpului in format calendar

mktime <TIME.H>


Converts time to calendar format


Declaration


time_t mktime(struct tm *t);


Remarks


Converts the time in the structure *t into a calendar time with the same format used by the time function.


The original values of the fields tm_sec, tm_min, tm_hour, tm_mday, and tm_mon are not restricted to the ranges described in the tm structure.


If the fields are not in their proper ranges, mktime adjusts them.


mktime computes values for the fields tm_wday and tm_yday after the other fields have been adjusted.


Return Value  See Remarks.


/* mktime example */


#include <stdio.h>

#include <time.h>


char *wday[] = ;


int main(void)



Functii pentru formatarea timpului

strftime <TIME.H>


Formats time for output


Declaration


size_t _cdecl strftime(char *s, size_t maxsize, const char *fmt, const struct tm *t);


Remarks


strftime formats the time in *t into the array *s according to the format specifications (a format string) in *fmt.


All ordinary characters are copied unchanged. No more than maxsize characters are placed in s.


Return Value


On success, strftime returns the number of characters placed into s.

On error (if the number of characters required > maxsize), returns 0.


/* strftime example */


#include <stdio.h>

#include <time.h>

#include <dos.h>


int main(void)



Functii pentru impulsuri ceasului

clock <TIME.H>


Returns number of clock ticks since program start


Declaration


clock_t clock(void);


Remarks


clock can be used to determine the time interval between two events.


To determine the time in seconds, divide the value returned by clock by the value of the macro CLK_TCK.


Return Value


On success, returns the processor time elapsed since the beginning of the program invocation.


On failure (the processor time is not available, or its value can't be represented), returns -1.


/* clock example */


#include <time.h>

#include <stdio.h>

#include <dos.h>


int main(void)



Functii pentru calculul diferentei intre doua date si timpi

difftime <TIME.H>


Computes difference between 2 times


Declaration


double difftime(time_t time2, time_t time1);


Remarks


difftime calculates the elapsed time in seconds, from time1 to time2.


Return Value


Returns the result of its calculation, as a double.


/* difftime example */


#include <time.h>

#include <stdio.h>

#include <dos.h>

#include <conio.h>


int main(void)



Functia _strdate

Converts current date to string.

Declaration:    char *_strdate(char *buf);

Remarks

_strdate converts the current date to a string and stores the string in the buffer *buf.

The buffer must be at least 9 characters long.

The date string is terminated by a null character and has the form

MM/DD/YY

where

MM = month

DD = day

YY = year

MM, DD, and YY are all two-digit numbers.

Return Value: Returns buf, the address of the date string.


/* _strdate example */


#include <time.h>

#include <stdio.h>

void main(void)



Functia _strtime

Converts current time to string

Declaration:    char *_strtime(char *buf);

Remarks

_strtime converts the current time to a string and stores the string in the buffer *buf.


The buffer must be at least 9 characters long.


The time string, terminated by a null character, has the form HH:MM:SS

where HH = hour MM = minute SS = seconds HH, MM, and SS are all two-digit numbers.

Return Value: Returns buf, the address of the time string.

/* __strtime example */


#include <time.h>

#include <stdio.h>

void main(void)


Nu se poate descarca referatul
Acest referat nu se poate descarca

E posibil sa te intereseze alte referate despre:


Copyright © 2024 - Toate drepturile rezervate QReferat.com Folositi referatele, proiectele sau lucrarile afisate ca sursa de inspiratie. Va recomandam sa nu copiati textul, ci sa compuneti propriul referat pe baza referatelor de pe site.
{ Home } { Contact } { Termeni si conditii }