473,385 Members | 1,736 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

how do i write basic time function

hello,
i am new to c and how do i use time function to get time.

thanks,
Sean
Nov 14 '05 #1
2 1803


Suresh wrote:
hello,
i am new to c and how do i use time function to get time.


What do you mean by "get time"? Display?

The Standard provides a time_t type that can hold a value
representing the system time. The Standard offers function
time that can get store in a time_t variable the value
representing the system time.

You can use function ctime to put this
time_t value in printable form(a char string) an then
a stdout function, ie puts, to display.

Example:

#include <time.h>
#include <stdio.h>

int main(void)
{
time_t now;

now = time(NULL);
if(now != (time_t)-1) puts(ctime(&now));
else puts("There is no system time");
return 0;
}
--
Al Bowers
Tampa, Fl USA
mailto: xa******@myrapidsys.com (remove the x to send email)
http://www.geocities.com/abowers822/

Nov 14 '05 #2
Suresh wrote:
hello,
i am new to c and how do i use time function to get time.


#include <stdio.h>
#include <time.h>
#include <string.h>
#define TMBUFSIZ 120 /* only 26 needed for localtime, ctime,
etc */

int main(void)
{
time_t now;
struct tm tyme;
char timebuf[TMBUFSIZ] = "";

now = time(0); /* get the time. A simple line of
'time(&now);' works too. */

/* the short way to show the time */
printf("The time is now (from ctime) %s", ctime(&now));

/* a longer way that sets the struct tm tyme for further use */
tyme = *localtime(&now); /* set up the broken-down time struct */
strcpy(timebuf, asctime(&tyme)); /* We could just printf the
asctime() result as with
ctime above */
printf("The longer way: %s", timebuf);

/* another way to use the struct tm tyme */
strftime(timebuf, TMBUFSIZ, "This is %A, %e %B %Y, %r", &tyme);
printf("Another form: %s\n", timebuf);
return 0;
}

The time is now (from ctime) Sun Apr 25 00:13:38 2004
The longer way: Sun Apr 25 00:13:38 2004
Another form: This is Sunday, 25 April 2004, 12:13:38 AM
Nov 14 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

33
by: Nick Evans | last post by:
Hello there, I have been on and off learning to code (with python being the second language I have worked on after a bit of BASIC). What I really want to know is, if you are going to actually...
9
by: Ricardo | last post by:
Hi. How should I pass a " (doble-quote) character to response.write() in order for it to be sent to the output ? Thanks in advance for the help.
8
by: ABC | last post by:
In VB, we can write the share function as Public Shared Function FnXXX() .... .... End Function How about on C#?
7
by: Dr. Zharkov | last post by:
We have program Visual Basic .NET 2003 for construction of 3D-Graphics as a surface z=f (x, y). From VB .NET 2003 we want to transfer coordinates of this surface as myArrayVB (2000, 1) in myArrayVó...
74
by: lovecreatesbeauty | last post by:
My small function works, but I have some questions. And I want to listen to you on How it is implemented? 1. The function does not check if parameter x is larger or smaller than parameter y. ...
10
by: sandraz444 | last post by:
I have an expression in the query under my form to autofill the date under a certain condition but it wont write to the underlying table?? The date shows in the form but not the table. Does anyone...
7
by: garyusenet | last post by:
This is the first time i've worked with openfile dialog. I'm getting a couple of errors with my very basic code. Can someone point out the errors in what i've done please....
0
AHMEDYO
by: AHMEDYO | last post by:
Hi Every one... With this visual Basic 6.0 Code you can handle more event that visual basic Support as Mouse wheel and hover or you can control event before VB IDE Default Windows proc as...
6
Atli
by: Atli | last post by:
This is an easy to digest 12 step guide on basics of using MySQL. It's a great refresher for those who need it and it work's great for first time MySQL users. Anyone should be able to get...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.