localtime returns null - c

localtime returns null. Why? (I'm using Visual C++ 2008)
struct tm *tb;
time_t lDate;
time(&lDate);
tb = localtime(&lDate); // tb is null everytime I try this!

Is that your exact code? I just compiled this program and it works fine:
#include <stdio.h>
#include <time.h>
int main(int argc, char **argv)
{
struct tm *tb;
time_t lDate;
time(&lDate);
if (lDate == -1) {
perror("time");
return 1;
}
tb = localtime(&lDate);
if (tb == NULL) {
fprintf(stderr, "localtime failed\n");
return 1;
}
printf("Good\n");
return 0;
}

#include <time.h>
#include <stdio.h>
int main(void)
{
// get the current time
time_t now = time(0);
struct tm* theTime = localtime(&now);
int t=(int)theTime;
printf("%d",t);
getch();
return 0;
}
it works

Define the preprocessor _USE_32BIT_TIME_T in your project and try again. Good luck:)

The code you posted in your comments works fine, up until you get to the if statement. I'm not sure what you are trying to do here, but you have a ; in if (pArea); that almost definitely should not be there (hard to tell since it's formatted horribly because you put it in a comment). You are also returning 0 all the time, is that what you intended to do?

Related

How to get current time in c

void takeTime(struct tm * timeNow)
{
time_t timeInSec;
time(&timeInSec);
timeNow = localtime(&timeInSec);
return;
}
int main()
{
struct tm* timeNow;
takeTime(timeNow);
printf("%s\n", asctime(timeNow));
return 0;
}
tried executing the code, but got Segmentation fault, can anyone explain why. i'm new to programming!
Function localtime returns a pointer to a statically allocated structure.
The code in the question modifies the local copy of the pointer timeNow in takeTime but the value is not returned to the caller.
If you want to pass the pointer to the caller you need to emulate a reference by using another level of indirection, i.e. a pointer to a pointer.
#include <stdio.h>
#include <time.h>
void takeTime(struct tm ** timeNow)
{
time_t timeInSec;
time(&timeInSec);
*timeNow = localtime(&timeInSec);
// This return at the end of a void function can be removed
// return;
}
int main(void)
{
struct tm* timeNow;
takeTime(&timeNow);
printf("%s\n", asctime(timeNow));
return 0;
}
Or you could want to get a copy of this structure. Then you need a structure variable in main and have to pass its address.
#include <stdio.h>
#include <time.h>
void takeTime(struct tm * timeNow)
{
time_t timeInSec;
time(&timeInSec);
*timeNow = *localtime(&timeInSec);
// This return at the end of a void function can be removed
// return;
}
int main(void)
{
struct tm timeNow;
takeTime(&timeNow);
printf("%s\n", asctime(&timeNow));
return 0;
}
Or you could return the pointer
#include <stdio.h>
#include <time.h>
struct tm * takeTime(void)
{
time_t timeInSec;
time(&timeInSec);
return localtime(&timeInSec);
}
int main()
{
struct tm* timeNow;
timeNow = takeTime();
printf("%s\n", asctime(timeNow));
return 0;
}
Another variant of returning a structure and a more detailed explanation were shown in an Farhod Nematov's answer, which unfortunately has been deleted.

Is there a way to code this? Im trying to code something that will display Monday Tuesday, wednesday and etc

Hello beginner coder here is there anyway to print the week days without doing it like this
#include <stdio.h>
int main()
{
int nDay;
if (nDay == 1)
printf("Sunday");
else if (nDay == 2)
printf("Monday"); // and so on
return 0;
}
this way might be lengthy if I code till the 42th day, is there anyway to code this? i tried using modulos but it doesn't work like I wanted it to be.
You can use arrays.
#include <stdio.h>
// a list of strings to print
static const char* days[] = {
"", // a dummy element to put the elements in light places
"Sunday",
"Monday",
// and so on
};
int main()
{
int nDay;
// assign something to nDay
nDay = 1; // for example
// if the string to print is defined for the value of nDay
if (0 < nDay && nDay < (int)(sizeof(days) / sizeof(*days)))
{
// print the string
fputs(days[nDay], stdout);
}
return 0;
}
The standard library function strftime can do this for you:
#include <stdio.h>
#include <string.h>
#include <time.h>
void print_weekday(int wday)
{
struct tm tm;
memset(&tm, 0, sizeof tm);
tm.tm_wday = wday;
char obuf[80];
strftime(obuf, sizeof obuf, "%A", &tm);
puts(obuf);
}
Error handling, more sensible sizing of obuf, etc. left as an exercise.
What you can do is pre-compute all values and store it.And then use it later as per your needs.
Check this code.
#include<bits/stdc++.h>
using namespace std;
int main()
{
unordered_map<int,string>mp;
mp[1]="Sunday";
mp[2]="Mon";
mp[3]="Tue";
mp[4]="Wed";
mp[5]="Thu";
mp[6]="Fri";
mp[7]="Sat";
int n;
cin>>n;
cout<<mp[n%7]<<endl;
}

Get current time in C, function

I want to get current time (without a current date) in C. The main problem is when I want to do it with functions. When I dont use them, evertyhing is just fine. Can anybody tell me, why my code shows only an hour? (take a look at the attached image). Thanks in advance.
#include <stdio.h>
#include <time.h>
#include <string.h>
char* get_time_string()
{
struct tm *tm;
time_t t;
char *str_time = (char *) malloc(100*sizeof(char));
t = time(NULL);
tm = localtime(&t);
strftime(str_time, sizeof(str_time), "%H:%M:%S", tm);
return str_time;
}
int main(int argc, char **argv)
{
char *t = get_time_string();
printf("%s\n", t);
return 0;
}
sizeof(str_time) gives you the size of char*. You want the size of the buffer str_time points to instead. Try
strftime(str_time, 100, "%H:%M:%S", tm);
// ^ size of buffer allocated for str_time
Other minor points - you should include <stdlib.h> to pick up a definition of malloc and should free(t) after printing its content in main.
The sizeof operator returns the length of the variable str_time which is a pointer to char. It doesn't returns the length of your dynamic array.
Replace sizeof(str_time) by 100 and it will go fine.
try this...
int main ()
{
time_t rawtime;
struct tm * timeinfo;
time ( &rawtime );
timeinfo = localtime ( &rawtime );
printf ( "Current local time and date: %s", asctime (timeinfo) );
return 0;
}
Use This Concept Getting System Time and Updating it. I have used this in my project many years before. you can change it as per your requirements.
updtime() /* FUNCTION FOR UPDATION OF TIME */
{
struct time tt;
char str[3];
gettime(&tt);
itoa(tt.ti_hour,str,10);
setfillstyle(1,7);
bar(getmaxx()-70,getmaxy()-18,getmaxx()-30,getmaxy()-10);
setcolor(0);
outtextxy(getmaxx()-70,getmaxy()-18,str);
outtextxy(getmaxx()-55,getmaxy()-18,":");
itoa(tt.ti_min,str,10);
outtextxy(getmaxx()-45,getmaxy()-18,str);
return(0);
}
The previous function will update time whenever you will call it like
and this will give you time
int temp;
struct time tt;
gettime(&tt); /*Get current time*/
temp = tt.ti_min;
If you want to update time the you can use the following code.
gettime(&tt);
if(tt.ti_min != temp) /*Check for any time update */
{
temp = tt.ti_min;
updtime();
}
This is complex code but if you understand it then it will solve your all problems.
Enjoy :)
In getting the time, you can try this one:
#include <stdio.h>
int main(void)
{
printf("Time: %s\n", __TIME__);
return 0;
}
Result:
Time: 10:49:49

sleep function in while statement

I can't understand why this code does not print current time in every one second.
What is the problem here ?
#include <stdio.h>
#include <unistd.h>
#include <time.h>
int main(int argc, const char * argv[])
{
while (1) {
sleep(1);
time_t tm;
struct tm *t_struct;
tm = time(NULL);
t_struct = localtime(&tm);
printf("%.2d:%.2d:%.2d", t_struct->tm_hour, t_struct->tm_min, t_struct->tm_sec);
}
return 0;
}
stdout may be line buffered, so you might need to either fflush it after outputting text, or print a newline to make changes visible.

Cross platform method of getting abbreviated weekday name

#include <langinfo.h>
#include <stdio.h>
int main(int argc, char **argv){
char *firstDayAb;
firstDayAb = nl_langinfo(ABDAY_1);
printf("\nFirst day ab is %s\n", firstDayAb);
return 0;
}
This code works fine on Mac and Linux but it doesn't work on windows due to absence of langinfo.h. How to avoid using langinfo.h? Or maybe there is another way of getting abbreviated weekday name?
#include <stdio.h>
#include <time.h>
int main ()
{
struct tm timeinfo = {0};
char buffer [80];
timeinfo.tm_wday = 1;
strftime (buffer, 80, "First day ab is %a", &timeinfo);
puts (buffer);
return 0;
}
I have found a link Code for header file is given here. It uses KDE32 on windows. I hope this helps you.

Resources