I am learning C program. When try to run the code I am getting error as : [Error] ld returned 1 exit status
#include <stdio.h>
#include <time.h>
void main()
{
time_t t;
time(&t);
clrscr();
printf("Today's date and time : %s",ctime(&t));
getch();
}
Can someone explain me What I am doing wrong here?
I tried this code :
int main()
{
printf("Today's date and time : %s \n", gettime());
return 0;
}
char ** gettime() {
char * result;
time_t current_time;
current_time = time(NULL);
result = ctime(¤t_time);
return &result;
}
but still shows me error as : error: called object ‘1’ is not a function
in current_time = time(NULL); line. What is wrong with the code
I think your looking for something like this:
#include <time.h>
#include <stdlib.h>
#include <stdio.h>
int main() {
time_t current_time;
char* c_time_string;
current_time = time(NULL);
/* Convert to local time format. */
c_time_string = ctime(¤t_time);
printf("Current time is %s", c_time_string);
return 0;
}
you need to change clrscr(); to system(clear).Below is the working version of your code:
#include<stdio.h>
#include<time.h>
void main()
{
time_t t;
time(&t);
system("clear");
printf("Today's date and time : %s",ctime(&t));
}
Related
I'm trying to pause my program for 1 second, and check the system time after that (I'm on Linux)
This is my testing program:
#include <stdio.h>
#include <unistd.h>
#include <time.h>
int main() {
time_t now = time(NULL);
struct tm *time_now = localtime(&now);
printf("now: %d-%d\n", time_now->tm_min, time_now->tm_sec);
int i = 0;
for (; i < 5; i++) {
sleep(1);
}
// This printf result is not as expected
printf("now: %d-%d\n", time_now->tm_min, time_now->tm_sec);
return 0;
}
The expected result is that the second printf would print +5 seconds. Instead, it prints the same time/seconds as the first printf.
I've found (maybe) the same problem posted here, but it doesn't seem to work:
sleep() and time() not functioning as expected inside for loop.
Sorry for my bad english, and thank you for your time.
Code is printing the original *time_now values - as expected.
Simply read time again to use new values.
time_t now = time(NULL);
struct tm *time_now = localtime(&now);
printf("now: %d-%d\n", time_now->tm_min, time_now->tm_sec);
int i = 0;
for (; i < 5; i++) {
sleep(1);
}
// Add
time_t now = time(NULL);
struct tm *time_now = localtime(&now);
printf("now: %d-%d\n", time_now->tm_min, time_now->tm_sec);
As mentioned in title, how can I execute the specific threads at specific time accurately?
Is there any library help to do it?
For example, [00:00:00.00, 00:05:00.00, 00:10:00.00..04:00:00.00, 04:05:00.00...]
Here is my current approach to do it, is there any better way to do it?
#include <stdio.h>
#include <unistd.h>
#include <time.h>
unsigned interval = 5*60;
void until_next_tick(time_t *last_tick){
time_t now = time(NULL);
time_t next_tick = now / interval * interval + interval;
time_t diff = next_tick - now;
usleep(diff * 1000 * 1000);
*last_tick = next_tick;
}
void print_current_time(char *str){
time_t raw = time(NULL);
struct tm *curr = localtime(&raw);
sprintf(str, "%04d/%02d/%02d %02d:%02d:%02d",
curr->tm_year+1900, curr->tm_mon+1, curr->tm_mday,
curr->tm_hour, curr->tm_min, curr->tm_sec);
}
int main(int argc, char **argv)
{
time_t last_tick = time(NULL);
char str[30];
while (1) {
print_current_time(str);
printf("%s\n", str);
until_next_tick(&last_tick);
}
return 0;
}
Use timer_create with SIGEV_THREAD and set repeating time in timer_settime to start a new thread at a repeated time interval.
One simple way is to have a while(true) loop which calls sleep(1); and then in the loop checks what time it is with time(NULL) and if the time for a thread is past due, start the corresponding thread.
One simple way is using time() to get the time:
#include <stdio.h>
#include <time.h>
void *get_sys_stat(void* arg)
{
// Do somthing hrear
}
int main()
{
hour = 5;
min = 30;
int status = 0;
time_t t = time(NULL);
pthread_t sys_stat_thread;
while(1) {
/* Get time*/
struct tm tm = *localtime(&t);
/* Trigger another process or thread */
if ((tm.tm_hour == hour) && (tm.tm_min == min))
pthread_create(&sys_stat_thread, NULL, get_sys_stat, NULL);
}
}
Which function can return current datetime with d/m/y format in C language?
EDIT:
here is my code:
#include <stdio.h>
#include <time.h>
int main()
{
time_t tmp_time;
struct tm * info;
time ( &tmp_time );
info = localtime ( &tmp_time );
printf ( "%s", asctime (info) );
}
this returns to me something like that Thu Jan 26 13:08:01 2017 and i would like to return 26/01/17 or 26/01/2017
Like this:
int main ()
{
time_t rawtime;
struct tm * currentTime;
time ( &rawtime );
currentTime = localtime ( &rawtime );
printf ( "%d/%d/%d", currentTime->tm_mday, currentTime->tm_mon+1, currentTime->tm_year+1900);
return 0;
}
Be careful, months are indexed since 0, and year is since 1900 in tm struct.
Perhaps like this:
#include <stdio.h>
#include <time.h>
int main()
{
time_t t = time(0);
if((time_t)-1 == t){
perror(0);
exit(1);
}
char buf[64];
struct tm tdata;
//I believe the 2 calls below should always succeed
//in this context
localtime_r(&t, &tdata);
strftime(buf, sizeof(buf), "%d/%m/%y", &tdata);
puts(buf);
}
The localtime(3) manpage says strftime is the recommended way to do it, and the strftime(3) manpage provides a similar example.
You can do it like this
#include <time.h>
#include <stdio.h>
int main(void)
{
time_t mytime = time(NULL);
struct tm date = *localtime(&mytime);
printf("now: %d/%d/%d\n", date.tm_mday,date.tm_mon + 1,date.tm_year +1900 );
return 0;
}
if you want to make it a function send the date as a parameter and return a int array holds day month and year
I have a string that contains microseconds since the epoch. How could I convert it to a time structure?
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
int main ()
{
struct tm tm;
char buffer [80];
char *str ="1435687921000000";
if(strptime (str, "%s", &tm) == NULL)
exit(EXIT_FAILURE);
if(strftime (buffer,80,"%Y-%m-%d",&tm) == 0)
exit(EXIT_FAILURE);
printf("%s\n", buffer);
return 0;
}
Portable solution (assuming 32+ bit int). The following does not assume anything about time_t.
Use mktime() which does not need to have fields limited to their primary range.
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
int main(void) {
char buffer[80];
char *str = "1435687921000000";
// Set the epoch: assume Jan 1, 0:00:00 UTC.
struct tm tm = { 0 };
tm.tm_year = 1970 - 1900;
tm.tm_mday = 1;
// Adjust the second's field.
tm.tm_sec = atoll(str) / 1000000;
tm.tm_isdst = -1;
if (mktime(&tm) == -1)
exit(EXIT_FAILURE);
if (strftime(buffer, 80, "%Y-%m-%d", &tm) == 0)
exit(EXIT_FAILURE);
printf("%s\n", buffer);
return 0;
}
Edit: You could simply truncate the string, since struct tm does not store less than 1 second accuracy.
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main ()
{
struct tm now;
time_t secs;
char buffer [80];
char str[] ="1435687921000000";
int len = strlen(str);
if (len < 7)
return 1;
str[len-6] = 0; // divide by 1000000
secs = (time_t)atol(str);
now = *localtime(&secs);
strftime(buffer, 80, "%Y-%m-%d", &now);
printf("%s\n", buffer);
printf("%s\n", asctime(&now));
return 0;
}
Program output:
2015-06-30
Tue Jun 30 19:12:01 2015
You can convert the microseconds to seconds, and use localtime() like this
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
int main (void)
{
struct tm *tm;
char buffer[80];
char *str = "1435687921000000";
time_t ms = strtol(str, NULL, 10);
/* convert to seconds */
ms = (time_t) ms / 1E6;
tm = localtime(&ms);
if (strftime(buffer, 80, "%Y-%m-%d", tm) == 0)
return EXIT_FAILURE;
printf("%s\n", buffer);
return EXIT_SUCCESS;
}
Note that in the printed date, the microseconds are not present, so you can ignore that part.
Convert the string to a time_t, then use gmtime(3) or localtime(3).
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
int main () {
struct tm *tm;
char buffer [80];
char *str ="1435687921000000";
time_t t;
/* or strtoull */
t = (time_t)(atoll(str)/1000000);
tm = gmtime(&t);
strftime(buffer,80,"%Y-%m-%d",tm);
printf("%s\n", buffer);
return 0;
}
I am able to get the current date but the output is like 9/1/2010,but my requirement is to get the current day like"Wednesday" not in form of integer value like 1.
My code is here.
#include <dos.h>
#include <stdio.h>
#include<conio.h>
int main(void)
{
struct date d;
getdate(&d);
printf("The current year is: %d\n", d.da_year);
printf("The current day is: %d\n", d.da_day);
printf("The current month is: %d\n", d.da_mon);
getch();
return 0;
}
Please help me to find the current day as Sunday,Monday.........
Thanks
Are you really writing for 16-bit DOS, or just using some weird outdated tutorial?
strftime is available in any modern C library:
#include <time.h>
#include <stdio.h>
int main(void) {
char buffer[32];
struct tm *ts;
size_t last;
time_t timestamp = time(NULL);
ts = localtime(×tamp);
last = strftime(buffer, 32, "%A", ts);
buffer[last] = '\0';
printf("%s\n", buffer);
return 0;
}
http://ideone.com/DYSyT
The headers you are using are nonstandard. Use functions from the standard:
#include <time.h>
struct tm *localtime_r(const time_t *timep, struct tm *result);
After you call the function above, you can get the weekday from:
tm->tm_wday
Check out this tutorial/example.
There's more documentation with examples here.
As others have pointed out, you can use strftime to get the weekday name once you have a tm. There's a good example here:
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
int
main(int argc, char *argv[])
{
char outstr[200];
time_t t;
struct tm *tmp;
t = time(NULL);
tmp = localtime(&t);
if (tmp == NULL) {
perror("localtime");
exit(EXIT_FAILURE);
}
if (strftime(outstr, sizeof(outstr), "%A", tmp) == 0) {
fprintf(stderr, "strftime returned 0");
exit(EXIT_FAILURE);
}
printf("Result string is \"%s\"\n", outstr);
exit(EXIT_SUCCESS);
}
Alternatively, if you insist on using your outdated compiler, there's a dosdate_t struct in <dos.h>:
struct dosdate_t {
unsigned char day; /* 1-31 */
unsigned char month; /* 1-12 */
unsigned short year; /* 1980-2099 */
unsigned char dayofweek; /* 0-6, 0=Sunday */
};
You fill it with:
void _dos_getdate(struct dosdate_t *date);
Use struct tm Example
strftime is certainly the right way to go. Of course you could do
char * weekday[] = { "Sunday", "Monday",
"Tuesday", "Wednesday",
"Thursday", "Friday", "Saturday"};
char *day = weekday[d.da_day];
I'm of course assuming that the value getdate() puts in the date struct is 0-indexed, with Sunday as the first day of the week. (I don't have a DOS box to test on.)