How to get current day of month properly in Tizen c program - c

the following program works with c compiler and returns correct date. But on tizen app code it returns 0:
time_t my_time;
struct tm * timeinfo;
time (&my_time);
timeinfo = localtime (&my_time);
int y = timeinfo->tm_year+1900;
int m = timeinfo->tm_mon+1;
int d = timeinfo->tm_mday;
char day[10];
dlog_print(DLOG_DEBUG, "GG", "Current time: %d %d %d", y,m,d);
required screenshot:
it shows 0 only for date(d) but other m and y is printed correctly. Also similar code prints correct date on other c complier program.
How to get current day of the month properly?

You can use Watch Application API on the wearable profile.
You can also find sample code in Tizen Studio sample application(Classic Watch) like as below.
/**
* #brief Called at each second. This callback is not called while the app is paused or the device is in ambient mode.
* #param[in] watch_time The watch time handle. watch_time will not be available after returning this callback. It will be freed by the framework.
* #param[in] user_data The user data to be passed to the callback functions
*/
void app_time_tick(watch_time_h watch_time, void* user_data)
{
int hour = 0;
int min = 0;
int sec = 0;
int year = 0;
int month = 0;
int day = 0;
int day_of_week = 0;
watch_time_get_hour(watch_time, &hour);
watch_time_get_minute(watch_time, &min);
watch_time_get_second(watch_time, &sec);
watch_time_get_day(watch_time, &day);
watch_time_get_month(watch_time, &month);
watch_time_get_year(watch_time, &year);
watch_time_get_day_of_week(watch_time, &day_of_week);
_set_time(hour, min, sec);
_set_date(day, month, day_of_week);
_set_moonphase(day, month, year);
}

Related

How do I manipulate the values to a pointer to a struct

It's a function that makes events based off the time values the user enters, it then converts the value to Unix Epoch using mktime and stores them to a file. I've tested and found that everything else works well until we reach the section where the user enters the values seems like I'm not using the pointer event to the struct tm well, so I need help in doing it right.
int EventCreator(){
FILE *fptr = fopen("Events_Calendar.txt", "a+");
struct tm *event;
printf("Enter the values of the hour minute day month year in digits and this specific format meaning using spaces to seperate them");
printf("\n hr min day mon yr\n>");
scanf("%d %d %d %d %d", event->tm_hour, event->tm_min, event->tm_mday, event->tm_mon, event->tm_year);
time_t NineteenEvent = mktime(event);
printf("%ld", NineteenEvent);
fprintf(fptr, "%ld\n", NineteenEvent);
fclose(fptr);
}
You have declared a pointer to a struct tm but you haven't allocated memory for a struct tm. I suggest that you make event an automatic variable instead.
Example:
void EventCreator() { // make the function `void` since you do not return anything
FILE *fptr = fopen("Events_Calendar.txt", "a+");
if (fptr) { // check that opening the file succeeds
// `event` is now not a pointer and set `tm_isdst` to a negative value
// to make `mktime` guess if DST is in effect:
struct tm event = {.tm_isdst = -1};
printf(
"Enter the values of the hour minute day month year in digits and "
"this specific format meaning using spaces to seperate them\n"
"hr min day mon yr\n>");
// %d requires an `int*` but you've supplied it with `int`.
// (also, check that scanf succeeds)
if (scanf("%d %d %d %d %d", &event.tm_hour, &event.tm_min,
&event.tm_mday, &event.tm_mon, &event.tm_year) == 5) {
event.tm_year -= 1900; // years since 1900
--event.tm_mon; // 0-11
time_t NineteenEvent = mktime(&event); // take its address here
printf("%ld", NineteenEvent);
fprintf(fptr, "%ld\n", NineteenEvent);
fclose(fptr);
}
}
}

Sometimes getting wrong time using localtime() function

I want to display time of the current day after every 300 seconds. I am using localtime function. Most of the time ,it show right value but sometime it gives totally different value than expected one.
eg. At time HH:MM:SS i.e. 17:05:46 i get value as 81346 which is right one but at time 17:07:41 expected value is 81461 but I get value as 66052. Why is it so ?
#include<stdio.h>
#include<time.h>
int main(void)
{
time_t curtime ;
struct tm *loctime ;
int cur_time = 0;
unsigned int counter=0;
while(1)
{
if(!(counter %300))
{
curtime = time(NULL)+ 19800;
loctime = localtime ( &curtime ) ;
cur_time = (loctime->tm_hour*3600)+(loctime->tm_min*60)+loctime->tm_sec;
printf("Cur_time= %d \r\n",cur_time);
}
sleep(1);
counter++;
printf("counter= %d \r\n",counter);
}
return 1;
}

C MongoDB driver DateTime update

Ok this is the scenario. I am reading txt file and inserting/updating (with bulk operations ) data to mongodb.
Inserting part works fine since I am creating bson document in a loop and doing mongoc_bulk_operation_insert.
My date comes in a format of 11:37:17.253 SAST JUN 05 2015
so I run it trough the function that removes .253 and SAST
and feed it to the convert_to_date function that you see down in the text.
This is how I create timestamp and append the date to the document:
int convert_to_date(char *dateString)
{
struct tm *parsedTime;
time_t rawTime;
time_t newTime;
int hour, minutes, seconds, millis, year, month, day;
if (sscanf(dateString, "%d:%d:%d %d %d %d",
&hour, &minutes, &seconds, &month, &day, &year) != EOF) {
time(&rawTime);
parsedTime = localtime(&rawTime);
parsedTime->tm_year = year - 1900;
parsedTime->tm_mon = month - 1;
parsedTime->tm_mday = day;
parsedTime->tm_hour = hour;
parsedTime->tm_min = minutes;
parsedTime->tm_sec = seconds;
newTime = mktime(parsedTime);
return newTime;
}
return 0;
}
/* this is somewhere in a loop */
time_t t = convert_to_date(s1);
bson_append_time_t(doc, field_map[y], strlen(field_map[y]), t);
// and after all if insert is supposed to happen i call
// mongoc_bulk_operation_insert(bulk, doc);
// and mongoc_bulk_operation_execute(bulk, &reply, &error);
That part is just fine when I look at the database I get
ISODate("2015-06-05T09:37:17.000Z")
So ther problem is when I try to update existing record.
When i do update i need to do a $set on all of the fields I want to update(upsert actually and I didnt find a way to update complete document at once)
it looks like this:
//update document with sessionid
query = BCON_NEW("SessionId", BCON_UTF8(sid));
update = BCON_NEW("$set", "{", "SomeDateTimeField",
BCON_DATE_TIME(ystack[y]), "}");
mongoc_bulk_operation_update(bulk, query, update, true);
The value of ystack[y] is 1433497048 which is valid timestamp but
i get in the db ISODate("1970-01-17T14:11:37.037Z")
I also found an example online for inserting the whole bson object as date like this:
int64_t currentTime = time(NULL) * 1000;
bson_t *date = BCON_NEW("$date", BCON_DATE_TIME(currentTime));
bson_t *update = BCON_NEW("$set", "{", "last_sync_time", date, "}");
But this produces \u0001 in the DB instead of valid date. I cant tell what is causing this, so much frustration for something that should be straight forward. This is the libbson api for the reference
https://api.mongodb.org/libbson/current/index.html
and c driver api
http://api.mongodb.org/c/1.1.8/
Ok I found the solution myself, should have read the api more carefully :)
The BCON_DATE_TIME accepts milliseconds instead of seconds which I passed to it.
I actually tried to multiply by 1000 before but since my var was int it could not store the correct value.
I had to declare it as
unsigned long long
Here is the changed code just for reference
long convert_to_long(char *dateString)
{
struct tm *parsedTime;
time_t rawTime;
time_t newTime;
int hour, minutes, seconds, millis, year, month, day;
if (sscanf(dateString, "%d:%d:%d %d %d %d",
&hour, &minutes, &seconds, &month, &day, &year) != EOF) {
time(&rawTime);
parsedTime = localtime(&rawTime);
parsedTime->tm_year = year - 1900;
parsedTime->tm_mon = month - 1;
parsedTime->tm_mday = day;
parsedTime->tm_hour = hour;
parsedTime->tm_min = minutes;
parsedTime->tm_sec = seconds;
newTime = mktime(parsedTime);
unsigned long long millis = newTime * 1000;
return millis;
}
return 0;
}

Printing out current date

Implement a function that determines and prints out the current year, month and day. For example:
Today is 03/04/2014.
Code so far that I have:
#include <stdio.h>
#include <time.h>
int main ()
{
int days, weeks, months, years, option, rmd, currentyear, currentmonth;
int daysinjan, daysinfeb, daysinmarch;
time_t seconds;
seconds = time(NULL);
days = seconds/(60*60*24);
weeks = seconds/((60*60*24)*7);
rmd=seconds%31557600;
months = ((seconds/31557600) * 12)+(((float)rmd/31557600)*12);
years = days/(365.25);
currentyear = 1970 + years;
currentmonth = (((float)rmd/31557600)*12)+1;
printf("%ld/%ld", currentmonth,currentyear);
return 0;
}
Please do not mind all the useless stuff in the code, this question is part of a project and i simply used the code from my previous question to try and work with that code in order to solve this question. The problem i have is that i cannot print the current day of the month that it is, because of this i feel that i have gone about this question wrongly.
This uses standard library calls to do all the math for you.
From Here:
#include <time.h>
#include <stdio.h>
#define SIZE 256
int main (void)
{
char buffer[SIZE];
time_t curtime;
struct tm *loctime;
/* Get the current time. */
curtime = time (NULL);
/* Convert it to local time representation. */
loctime = localtime (&curtime);
/* Print out the date and time in the standard format. */
fputs (asctime (loctime), stdout);
/* Print it out in a nice format. */
strftime (buffer, SIZE, "Today is %A, %B %d.\n", loctime);
fputs (buffer, stdout);
strftime (buffer, SIZE, "The time is %I:%M %p.\n", loctime);
fputs (buffer, stdout);
return 0;
}
If you wanted to create this as a function to return a string, you could do it like this:
char * getTimeString (char *str)
{
//replace this comment with relevant code from above with (at least) two additional lines:
strcpy(str, buffer);
return str;
}
Call it like this:
int main(void)
{
char *timeStr;
timeStr = malloc(30);//sufficient length to accept values assigned in getTimeString()
printf("%s\n", getTimeString(timeStr);
free(timeStr);
return 0;
}
#include <time.h> // for time_t
#include <stdio.h> // for printf
int main () {
int days, weeks, months, years, option, rmd, currentyear, currentmonth;
int daysinjan, daysinfeb, daysinmarch;
time_t seconds;
seconds = time(NULL);
days = seconds/(60*60*24);
weeks = seconds/((60*60*24)*7);
rmd=seconds%31557600;
months = ((seconds/31557600) * 12)+(((float)rmd/31557600)*12);
years = days/(365.25);
currentyear = 1970 + years;
currentmonth = (((float)rmd/31557600)*12)+1;
printf("%ld/%ld", currentmonth,currentyear);
return 0;
}

Convert struct array to struct?

I have a struct array (Training data[10]) that contains some data that I want to pass to functions.
int convertTime(Time time)
{
minutes = time.seconds * 60;
// Takes data from data[0].time.seconds and converts them to minutes.
// My only problem is that I am being asked send a struct to this function, but I have to send the array because that's where all my data is stored
return minutes;
}
typedef struct
{
int seconds;
} Time;
typedef struct
{
Time time;
double distance;
} Training;
Training data[10];
Training input;
scanf("%d %lf", input.time.seconds, input.distance);
data[0].time.seconds = input.time.seconds;
data[0].distance = input.distance;
So now data[0].time.seconds and data[0].distance contains all data I need. I just have to pass data[0].time.seconds to the function, but in my assignment I am prompted to send the struct Time to the function, and I don't understand that since Time is only storing temporary data? It's the stored data that I want to send to the function.
How do I convert seconds to hours, minutes and seconds?
time.hours = seconds / 3600;
time.minutes = (seconds - time.hours * 3600) / 60;
time.seconds = seconds - 3600 * time.hours - 60 * time.minutes;
This seems to be right in my eyes but it fails. hours is correctly calculated but not minutes and sconds :(
To pass the structure, name it in the call:
some_function(data[0].time); // By value
other_function(&data[0].time); // By address
Both functions get passed the Time value contained in the data[0] element of your array of Training structures.
Suppose you have a value which is the number of seconds since midnight. And suppose you define another structure with hours/minutes/seconds, you can set this clock structure as follows,
typedef struct
{
int hours;
int minutes;
int seconds;
} Clock;
You can print this structure, either to a char buffer, or to stdout,
char*
clockPrint(Clock* timep,char *stringbuf)
{
if(stringbuf)
sprintf(stringbuf,"%02d:%02d:%02d",(timep)->seconds,(timep)->minutes,(timep)->seconds);
else
printf("%02d:%02d:%02d",(timep)->seconds,(timep)->minutes,(timep)->seconds);
return stringbuf;
}
Extracting hours, minutes, and seconds from an epoch time or a number of seconds since midnight can be done,
int //return days...
TimeSet(Clock* timep, int epoch)
{
(timep)->seconds = (epoch) % 60;
(timep)->minutes = (epoch/60) % 60;
(timep)->hours = (epoch/60/60) % 24;
int days;
return days = (epoch/60/60/24);
}
Should you want to obtain hours, minutes, or seconds from this clock value,
void
TimeGet(Clock* timep, int* hoursp, int* minutesp, int* secondsp)
{
if(hoursp) *hoursp = (timep)->hours;
if(minutesp) *minutesp = (timep)->minutes;
if(secondsp) *secondsp = (timep)->seconds;
return;
}
Since you have stored a Time in your Date struct, which contains a number of seconds (presumably since midnight), and you have an array of some number of these Date's defined,
Training data[10];
Training input;
You can use scanf to read your seconds and distance values. And as stated, you can then place your input into your data[0] element,
//scanf wants pointers to int and float data
float distance;
printf("enter: seconds distance "); fflush(stdout);
scanf("%d %lf", &(input.time.seconds), &distance);
//you can then store the distance into your input struct double
input.distance = distance;
data[0].time.seconds = input.time.seconds;
data[0].distance = input.distance;
You could also use gettimeofday(3) or clock_gettime(2) to grab the current time (seconds since epoch),
struct timeval tv;
gettimeofday(&tv,NULL); //posix.1-2001 function, seconds
input.time.seconds = tv.tv_sec;
//or
struct timespec ts;
clock_gettime(CLOCK_REALTIME,&ts); //posix.1-2008 function, seconds
input.time.seconds = ts.tv_sec;
Then you can separate your seconds into hours, minutes, and seconds,
Clock clk;
int hours, minutes, seconds;
TimeSet(&clk, data[0].time.seconds);
TimeGet(&clk, &hours, &minutes, &seconds);
Or you can format a string for printing, or print to stdout,
char clockbuffer[30];
clockPrint(&clk,NULL);
printf("time (HH:MM:SS): %s\n", clockPrint(&clk,clockbuffer));

Resources