{"id":4568,"date":"2021-01-30T00:01:28","date_gmt":"2021-01-30T08:01:28","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=4568"},"modified":"2021-02-06T08:46:57","modified_gmt":"2021-02-06T16:46:57","slug":"its-parsing-time","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=4568","title":{"rendered":"It&#8217;s Parsing Time"},"content":{"rendered":"<p>A recent puzzle presented itself, one where I must extract a date based on a file&#8217;s name. The date is part of the name, but my code required I translate the date into a <em>time_t<\/em> value. It&#8217;s an awesome programming puzzle that involves many different tricks.<br \/>\n<!--more--><br \/>\nIn short, given the filename <code>20210130a.txt<\/code>, the code must generate the <em>time_t<\/em> value 1611993600. This value represents the number of seconds ticked since the Unix epoch, January 1 1970. To accomplish this feat, the date values (2021, 01, 30) must be extracted from the filename string, converted to integers, then stuffed into a <em>tm<\/em> structure for final conversion into a <em>time_t<\/em> value &mdash; at least this approach is what I first considered.<\/p>\n<p>The process of pulling out specific tidbits from a string is called <em>parsing<\/em>. Especially when the information is well-formatted, parsing helps you cull through the data to find the tidbit you want.<\/p>\n<p>The C library feature the <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=1758\"><em>strtok()<\/em> function<\/a>, which can parse strings into separate chunks. For this Lesson, however, the data bits aren&#8217;t delineated by characters but rather by their positions. This consistency is what allows the dates to be cleanly and consistently extracted:<\/p>\n<p><code><em>yyyymmdd<\/em>[<em>a<\/em>].txt<\/code><\/p>\n<p>The first four characters <em>yyyy<\/em> represent a year, a positive value.<\/p>\n<p>The next two characters <em>mm<\/em> represent a month, 01 through 12.<\/p>\n<p>Following the month are two characters <em>dd<\/em> that represent a day of the month, 01 up to 31.<\/p>\n<p>These four digits &mdash; always at the start of the filename, always digits, and always in the same order &mdash; can be followed by an optional letter in the range of a through z.<\/p>\n<p>As usual, I build such programs in steps. The first step is to write code that confirms a filename argument is present:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2021_01_30-Lesson.c\" rel=\"noopener\" target=\"_blank\">2021_01_30-Lesson-a.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;stdlib.h&gt;\r\n\r\nint main(int argc, char *argv[])\r\n{\r\n    char *filename;\r\n    \r\n    <span class=\"comments\">\/* check for filename argument *\/<\/span>\r\n    if( argc&lt;2 )\r\n    {\r\n        <span class=\"comments\">\/* output error message to standard error *\/<\/span>\r\n        fprintf(stderr,\"Filename option required\\n\\n\");\r\n        <span class=\"comments\">\/* leave with exit code 1*\/<\/span>\r\n        exit(1);\r\n    }\r\n    <span class=\"comments\">\/* assign to pointer for convenience *\/<\/span>\r\n    filename = argv[1];\r\n\r\n    <span class=\"comments\">\/* code to confirm that the file exists goes here *\/<\/span>\r\n\r\n    printf(\"Filename '%s' specified\\n\",filename);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>If the argument count is less than 2 at Line 9, an error message is output to <em>stdout<\/em> and the program quits. If the argument count is greater, a command line option is available. The value&#8217;s address is stored in the <code>argv[1]<\/code> array element. This value is output at Line 21.<\/p>\n<p>The next step is to extract the digits from the string and convert them into numbers. For this task I use two functions, <em>convert()<\/em> and <em>strtol()<\/em>.<\/p>\n<p>The <em>convert()<\/em> function extracts a string of a given length (a substring) from another string. This process may seem similar to what the <em>strstr()<\/em> function does, but <em>convert()<\/em> extracts a string without searching, Plus it validates characters to some extent.<\/p>\n<p>The <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=889\"><em>strtol()<\/em> function<\/a> is a C library function that converts a string value into a <em>long int<\/em>.<\/p>\n<p>In <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=4595\">next week&#8217;s Lesson<\/a>, I show the code update that adds the <em>convert()<\/em> function as well as three statements to extract integer values from the filename argument presented.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>From string to <em>time_t<\/em> value, it&#8217;s a journey. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=4568\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-4568","post","type-post","status-publish","format-standard","hentry","category-main"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4568","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4568"}],"version-history":[{"count":8,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4568\/revisions"}],"predecessor-version":[{"id":4612,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4568\/revisions\/4612"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4568"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4568"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4568"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}