asctime time output error - c

#include<time.h>
#include<stdio.h>
int main(void)
{
time_t timer;
int i;
char mon[4];
char *ti=0;
ti=asctime(localtime(&timer));
ti=ti+4;
for(i=0;i<3;i++)
{
mon[i]=*ti++;
}
mon[3]='\0';
timer=time(NULL);
printf("The current time is %s\n",mon);
return 0;
}
Hi, Can anyone expalin why asctime returns wrong time-pointer sometimes.Iam using 'mon' to store the month from asctime string and when printing it on the console,it displays sometimes 'Mar' and sometimes some other month.

You are using timer without initializing it. Before calling localtime, try:
time(&timer);
Also, a simpler way to do it would be:
ctime(&timer);

Related

How to change a variable from a structure from integer to string?

The question assume
typedef struct {
int day;
int month;
int year;
} Date;
Date mfgDate = {17, 10, 2016}, expiryDate;
printf("%d-%d-%d", mfgDate.day, mfgDate.month, mfgDate.year);
By doing this i can display the date to the form of 17-10-2016, the question requires me to display the date in the form of DD-MMM-YYYY which is 17-OCT-2016, how can i do that? the question provides a tip which is to use switch statement but i can't seems to find out how, thanks a lot for help.
You could use strftime() for that:
char buffer[BUFFER_SIZE];
errno=0; //clear errno since we need it to detect errors in strftime()
//tm_year is years since 1900
//%d prints the day of the month, %B or %b the month name according to the locale
//and %Y prints the year, results in the format DD-MMMM-YYYY
int r = strftime
(buffer, sizeof buffer, "%d-%b-%Y", &(struct tm){.tm_year=2016-1900, .tm_mon=9, .tm_mday=17} );
if(!r && errno)
{
//do some error handling here
}
printf("%s\n",buffer);
If you are just starting with C and have not learned about arrays, a switch statement is a viable choice, just for the execise
const char* monthname(int month)
{
switch (month) {
case 1: return "JAN";
case 2: return "FEB";
// you can fill the remaining month yourself here
case 12: return "DEC";
// handle the error case
default: return "???";
}
}
to print your date then use:
printf("%d-%s-%d", mfgDate.day, monthname(mfgDate.month), mfgDate.year);
Simply have months names in the table.
char *months[] = {"JAN", "FEB","MAR", ......};
printf("%d-%s-%d", mfgDate.day, months[mfgDate.month - 1], mfgDate.year);

How can I create an OID from time

In pymongo you can do something like this to create an OID from time:
dummy_id = ObjectId.from_datetime(time)
Is there something like that in mongoc?
I saw that there's a "bson_oid_get_time_t()" function, but is there a reverse function of this, and if not, How can it be implemented in C?
I don't believe there is a reverse function, but it should be easy for you to generate your own using the default constructor and "fixing" the time.
Here is an example where I create an object ID.
Then I create a timestamp for December 25, 2014 and modify the OID to that date.
#include <time.h>
#include <bson.h>
int
main (int argc,
char *argv[])
{
time_t oid_thinks_time; //what time does the OID think it is
bson_oid_t oid;
bson_oid_t *oid_pointer = &oid;
bson_oid_init (&oid, NULL); // get a standard ObjectId
oid_thinks_time = bson_oid_get_time_t (&oid); //It was just made
printf ("The OID was generated at %u\n", (unsigned) oid_thinks_time); //prove it
time_t ts = time(NULL); //make a new time
struct tm * timeinfo = localtime(&ts);
timeinfo->tm_year = 2014-1900; //-1900 because time.h
timeinfo->tm_mon = 12 - 1; // time.h off by one (starts at 0)
timeinfo->tm_mday = 25;
ts = mktime(timeinfo); // create the time
u_int32_t ts_uint = (uint32_t)ts;
ts_uint = BSON_UINT32_TO_BE (ts_uint); //BSON wants big endian time
memcpy (&oid_pointer->bytes[0], &ts_uint, sizeof (ts_uint)); //overwrite the first 4 bytes with user selected time
oid_thinks_time = bson_oid_get_time_t (&oid);
printf ("The OID was fixed to time %u\n", (unsigned) oid_thinks_time);//prove it
}
The output of this code is:
The OID was generated at 1491238015
The OID was fixed to time 1419526015

error: too few arguments to function ‘ptr’ , need to eval_time from a function

I have a small problem with my program.
I want to eval the time from a function and the compiler gives me an error.
I know were the problem is, but I just dont know how to fix it :/
#include <stdio.h>
#include <time.h>
int isPerfect(int zahl){
int zaehler1, zaehler2, summe;
for(zaehler1=1;zaehler1<=zahl;zaehler1++){
summe=0;
for(zaehler2=1;zaehler2<=zaehler1/2;zaehler2++){
if(zaehler1%zaehler2==0){
summe=summe+zaehler2;
}
}
}
return summe;
}
double eval_time(int(*ptr)(int)){
time_t begin,end;
begin=time(NULL);
(*ptr)(); //compiler shows error here!
end=time(NULL);
return difftime(end,begin);
}
int main(void){
int zahl;
for(zahl=1;zahl<=500;zahl++){
if(isPerfect(zahl)==zahl){
printf("%d ist eine perfekte Zahl!\n", zahl);
}
}
printf("Die Zeit die gebraucht wurde: %.2lf s\n",eval_time(isPerfect));
return 0;
}
So my question is what do i need to change there so it evals the time from the funtion "isPerfect" ?
Sorry my variables are in german, I hope that's not a problem ;)
You have to pass a parameter, perhaps like this:
double eval_time(int(*ptr)(int), int zahl){
time_t begin,end;
begin=time(NULL);
(*ptr)(zahl);
-------^
end=time(NULL);
return difftime(end,begin);
}
Another problem is that you might have to call the functions lots and lots of times between begin and end to actually get a measurable difference in the time() values.

How tm structures (from time.h) works?

I need to create a struct where is set a date. I googled something and i found the tm structure from the library time.h, but I'm having some troubles;
I need to print some dates on a log file, here an example:
typedef struct tm* tm_;
...
void NEW_Job()
{
time_t t;
tm_ secs;
t=time(NULL);
secs=localtime(&t);
add_QUEUEnode(generate_job());
fprintf(f, "\n%d:%d.%d : New job created.", secs->tm_hour, secs->tm_min, secs->tm_sec);
}
I really don't know where i'm wrong.
Thanks in advance for the help :)
The exact error wasn't there, but in another line of the code, exactly here:
void PCunload(int b)
{
time_t t;
tm_ secs;
int hh, mm, ss;
hh=(time(NULL)-n[b].start_time)/3600;
mm=((time(NULL)-n[b].start_time)%3600)/60;
ss=((time(NULL)-n[b].start_time)%3600)%60;
t=time(NULL);
secs=localtime(&t);
n[b].job.priority=-1;
-->>fprintf(f, "\n%d:%d.%d : PC number %d unloaded; elapsed time: %d:%d.%d", secs->tm_hour, secs->tm_min, secs->tm_sec, hh, mm, ss);
}
There I tried to do the conversion inside the printf functions, but something goes wrong...
My apologies!
strftime() can help you printing date and time at your favorite format. Please take a look at man strftime.

can't get C method icalrecur_expand_recurrence to work

This is a bit frustrating. I've been working on this for a while now, and I can't seem to get this method to work like it says it does.
#include "icalrecur.h"
#include <time.h> /* for time() */
#include <stdio.h>
int get_occurrences(char* rrule, time_t start, int count)
{
//char* rule; /* rule string */
// *rule = PG_GETARG_CHAR(0);
time_t *result[count]; /* output array */
icalrecur_expand_recurrence(rrule, start, count, *result);
return (time_t) *result;
}
//time_t *output[5*8];
void main() {
time_t right_now = time(0);
char *_rrule = "FREQ=WEEKLY;INTERVAL=2;COUNT=8;WKST=SU;BYDAY=TU,TH";
get_occurrences(_rrule, right_now, 5);
}
I save this test file as ical_recur.h. Then I type in bash:
gcc -I/usr/local/libical/include/libical -L/usr/local/libical/lib/ -lical -o hello ical_recur.c
To include the libical.a libraries. The include/libical directory has icalrecur.h in it, so I really don't even need to be including the whole ical library.
~: ./hello
Segmentation fault
Anytime I change around any pointers, it starts complaining about something during compilation. Can anyone get this to work?? Source files are from Marketcircle on github.
Looking at the documentation it seems that you have an unwanted extra level of indirection - you need to change:
time_t *result[count]; /* output array */
icalrecur_expand_recurrence(rrule, start, count, *result);
to:
time_t result[count]; /* output array */
icalrecur_expand_recurrence(rrule, start, count, result);
Also you're passing a read-only string literal to a function which expects a char * - this should at least give you a compiler warning (hint: always use gcc -Wall ..., read the warnings carefully, understand them and fix them). main() should look more like this:
int main() {
time_t right_now = time(0);
char _rrule[] = "FREQ=WEEKLY;INTERVAL=2;COUNT=8;WKST=SU;BYDAY=TU,TH";
get_occurrences(_rrule, right_now, 5);
return 0;
}
Some more problems:
this line doesn't do anything useful, but it's not clear what you're trying to achieve:
char _size = (char)(((int)'0') + sizeof(result));
all those casts are a "code smell" and this should tell you that you're doing something very wrong here
your function is defined as returning an int, but you're trying to cast your array to a time_t and return that, which also makes no sense - again, turn on compiler warnings - let the compiler help you find and fix your mistakes.
Now you can use this extension for postgresql.
Example usage:
SELECT * FROM
unnest(
rrule_get_occurrences('FREQ=WEEKLY;INTERVAL=1;WKST=MO;UNTIL=20200101T045102Z;BYDAY=SA;BYHOUR=10;BYMINUTE=51;BYSECOND=2'::rrule,
'2019-12-07 10:51:02+00'::timestamp with time zone)
);
unnest
------------------------
2019-12-07 10:51:02+00
2019-12-14 10:51:02+00
2019-12-21 10:51:02+00
2019-12-28 10:51:02+00
(4 rows)

Resources