I need to parse a string like this:
Apr 3, 2014 10:03:51 AM
to something like this:
YYYY-MM-DD HH:MM:SS
And also, this long long:
1396682344000
To the same kind of string:
YYYY-MM-DD HH:MM:SS
Is there any library or function to do that? I am not very confortable writing C and I am not used to parse this kind of strings.
I tried with strptime with this code:
observationDate_message is the like first string (Apr 3, 2014 10:45:01 AM)
strptime(observationDate_message, "%G-%m-%d %r", &result);
debugLog(DEB_INFO, "observationDateConverted: %d-%d-%d %d:%d:%d\n", result.tm_year, result.tm_mon, result.tm_mday, result.tm_hour, result.tm_min, result.tm_sec);
And what I get is:
0-52-0 36905376:32630:1497284224
Tutorial in; http://pic.dhe.ibm.com/infocenter/iseries/v7r1m0/index.jsp?topic=%2Frtref%2Fstrpti.htm
Check if your system has the function strptime. It's part of POSIX and will do the parsing of the string for you. To convert in the opposite direction there's the C standard function strftime.
you can parse
Apr 3, 2014 10:03:51 AM
this string using sscanf() and get the year month date and time information.
if str contains the string,
sscanf(str,"%s %d, %d %d:%d:%d AM",month,&dd,&yy,&hh,&mm,&ss);
you can get the data from string. This is just a example you can extract formatted data from string as you want using sscanf()
Related
Is there a way I can change a string value "01/01/20" to a date format of dd-mm-yyyy which is 01-01-2020 in SQLite? Or a way I can use strftime(%d%m%y,date) on a string value?
You can't use strftime() because this function operates only on dates of the format YYYY-MM-DD.
In your case you must use string functions and concatenation:
substr(date, 1, 2) || '-' || substr(date, 4, 2) || '-20' || substr(date, -2)
Note that it would be better to save the dates in the table with the format YYYY-MM-DD.
If you do then it is easy to format them as you want:
strftime('%d-%m-%Y', date)
I am trying to split a long format string to fprintf() into multiple lines using the \ character as shown below:
fprintf(stdout, "This program take a date supplied by the user in dd/mm/yyyy format...\n\
And returns the day of the week for that date using Zeller's rule.\n\n\
Enter date in (dd/mm/yyyy) format: ");
this causes whitespaces to be added to the output as shown below:
This program take a date supplied by the user in dd/mm/yyyy format...
And returns the day of the week for that date using Zeller's rule.
Enter date in (dd/mm/yyyy) format:
this answer suggests this should work. I also checked this answer before posting here. A comment on it mentions that this approach...
...suffers from the fact that it breaks if there is any whitespace after
the '\'; a bug that can be baffling when it occurs.
cat -A's output on the program file...
^Ifprintf(stdout, "This program take a date supplied by the user in dd/mm/yyyy format...\n\$
^I^I^IAnd returns the day of the week for that date using Zeller's rule.\n\n\$
^I^I^IEnter date in (dd/mm/yyyy) format: ");$
...shows no whitespace after \; although it does introduce <TAB>s into the lines that follow. I am using vim to edit my source files.
I use line continuation with \ all the time in Bash, and was under the impression that it works similarly with fprintf() format strings in C.
I'd like to keep my code readable and line widths reasonable. Other than splitting the long string into multiple fprintf().
Is this standard behavior for printf()/fprintf()
Is it normal for a vim to my code when I do line continuation with \?
How do I fix this issue?
You do not need \ as it is not a macro definition.
Simply have as many as you want as string literals separated by as many as you want whitespace (new line is also a whitespace). The C compiler ignores the whitespace.
int main(void)
{
fprintf(stdout, "This program take a date supplied by the user in dd/mm/yyyy format...\n"
"And returns the day of the week for that date using Zeller's rule.\n\n"
"Enter date in (dd/mm/yyyy) format: ");
}
int main(void)
{
fprintf(stdout, "This program take a date"
" supplied by the user in dd/"
"mm/yyyy format...\n"
"And returns "
"the "
"day of "
"the "
"week for that "
"date using Zeller's rule.\n\n"
"Enter date in (dd/mm/yyyy) format: ");
}
https://godbolt.org/z/6ovj3G
There are fairly low limits in actual portability, but practically you can almost certainly do:
fprintf(stdout, "This program take a date supplied by the user in dd/mm/yyyy format...\n"
"And returns the day of the week for that date using Zeller's rule.\n\n"
"Enter date in (dd/mm/yyyy) format: "
);
If you do start hitting limits on concatenating strings like that, you can also do:
fprintf(stdout, "%s\n%s\n\n%s",
"This program take a date supplied by the user in dd/mm/yyyy format...",
"And returns the day of the week for that date using Zeller's rule.",
"Enter date in (dd/mm/yyyy) format: "
);
Variable: report_date_time
11/8/2005 12:00:00 AM
How to destring this variable into a date variable with Stata?
I do not need the time (12:00:00 AM) and need to keep the dates only (11/8/2005).
The starting point is reading help datetime and the linked-to section help datetime translation. Assuming your sample date is month-day-year order (and not day-month-year), the following example demonstrates the key concepts. The use of "#" in the mask argument to daily() (which is a more expressive name for the date() function) tells it to ignore the stuff after the year; without that a missing value is the result.
clear
input str30 date_s
"11/8/2005 12:00:00 AM"
end
generate newdate = daily(date_s,"MDY#")
format newdate %td
list, clean
And the output is
date_s newdate
1. 11/8/2005 12:00:00 AM 08nov2005
I want to read a date from a text file in a single variable/array in this format :
Thu Feb 05 16:48:30 2015
to print i used :
fprintf (savegame,"Played Date : %s ",ctime(&time1));
but what to do scan ?
Edit :
Ok . so i probably said something very wrong so i will just reformulate like a task what i get from school... :
I have a text file which contains:
Played Date : Thu Feb 05 16:48:30 2015
Played Date : Thu Jan 01 02:00:00 1970
I want to put them each in a single variable then delete
Played Date :
print them and update to the current date.
Is there any way to do this?
Edit : I tried with:
fscanf (savegame,"Played Date : %s ",ctime(&time1));
but doesn't work....
In POSIX, you can parse a date using strptime(), into a value of type struct tm.
You'd first read the entire line of text from the file (fgets() is the best choice for that), then pass that to strptime().
If you have static non-date text on the line, you need to skip that first. If it's static, you can just add its length to a pointer to the line, and pass that to strptime(). You can also include the text in the format string to strptime(), and it will be skipped.
You can print any date-time format with strftime(), so may be you will use format more convenient for scanning. I suppose that ctime() is locale-dependent, so on different PC with different settings WeekDay and Month can have different spelling (because of language).
But for your format you can use (my variable names should be changed):
res = fscanf(FP,"%3s %3s %d %d:%d:%d %d",dayStr, monStr, &day, &hour, &min, &sec, &year);
and then check format with
if (res == 7) { .... }
I have struct as:
struct stored
{
char *dates; // 12/May/2010, 10/Jun/2010 etc..
};
// const
struct stored structs[] = {{"12/May/2010"}, {"12/May/2011"},
{"21/May/2009"}, {"13/May/2011"},
{"10/May/2011"}, {"19/May/2011"}};
What I want to do is to sort struct 'stored' by stored.dates.
qsort(structs, 9, sizeof(struct stored*), sortdates); // sortdates function
I'm not quite sure what would be a good way to sort those days? Compare them as c-strings?
I would convert the dates to numbers using something like:
year * 10000 + month * 100 + day;
and then do a simple numeric comparison (and for month, you'll need to map from Jan to 1, Feb to 2, etc.).
If you're doing a lot of comparisons, you may want to cache the numeric equivalent in the structure.
If you convert the dates to the format YYYYMMDD (as in 20100314), you can compare them as a string or as an integer (after conversion).
ISO 8601 formatted dates ("YYYYMMDD" or "YYYY-MM-DD" etc.) are trivially comparable as C strings. Your format is not - would changing the format of the date strings be an option?
PS: If you get rid of the "-", you could even store the date as plain 32bit integer. Depending on what your application does with those dates, that might be an additional bonus.
You can't compare these as strings, but you can compare substrings. Compare the years, and if they aren't equal you have your answer. Next compare the months, you'll need some kind of table to order the months by name. Finally if the months are the same, compare the days.