HiveBrain v1.2.0
Get Started
← Back to all entries
patterncMinor

Printing out relative date from current time

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
datetimeprintingcurrentfromoutrelative

Problem

Here is some code of mine that prints out the relative date according to the current time:

#include 
#include 
#include 
int main (int argc, char *argv[])
{
  int i = 1;
  struct tm date = {0};
  char relativeDays[80];

  char* temp = strtok(argv[1], "/");
  while(temp != 0)
  {
    switch(i++)
    {
                case 1:
                        date.tm_mon  = atoi(temp) - 1;
                        break;
                case 2:
                        date.tm_mday = atoi(temp);
                        break;
                case 3:
                        date.tm_year = atoi(temp) - 1900;
    }
    temp=strtok(NULL, "/");
  }
  i = (int) difftime(time(NULL), mktime(&date))/86400;
  sprintf(relativeDays, "%d", abs(i));
  if (i > 0) printf("%s\n", strcat(relativeDays, " days ago."));
  else if (i < 0) printf("%s\n", strcat(relativeDays, " days from now."));
  else printf("Today\n");
  return 0;
}


Sample input and output:

$ ./date 12/8/2013
2 days ago.
$ ./date 12/10/2013
Today
$ ./date 12/24/2013
13 days from now.


Any thoughts on how to improve the code, specifically how to make it shorter?

Solution

I've commented your code:

#include 
#include 
#include 

int main(int argc, char *argv[])
{
    int i = 1;
    struct tm date = {0};
    char relativeDays[80];


Check For Proper Input

if(argc != 2)
    {
        printf("Two arguments required!\n");
        return -1;
    }

    char *temp = strtok(argv[1], "/");


temp will be NULL if empty. Also, add a break after case 3: for consistency. If you think you'll need to handle unexpected cases, use default at the end.

while(temp != NULL)
    {
        switch(i++)
        {
            case 1:
                date.tm_mon  = atoi(temp) - 1;
                break;
            case 2:
                date.tm_mday = atoi(temp);
                break;
            case 3:
                date.tm_year = atoi(temp) - 1900;
                break;
            default:
        }


Be consistent with spacing.

temp = strtok(NULL, "/");
    }


Add comments so people know what your intentions are. Why are you dividing by 86400?

i = (int)difftime(time(NULL), mktime(&date)) / 86400;
    sprintf(relativeDays, "%d", abs(i));


Use spaces to make everything more readable.

if(i > 0)
        printf("%s\n", strcat(relativeDays, " days ago."));
    else if(i < 0)
        printf("%s\n", strcat(relativeDays, " days from now."));
    else
        printf("Today\n");

    return 0;
}


Edit:

This while block can be changed to the following. Though it doesn't change the operation, it may improve the aesthetics.

while(temp != NULL)
{
    int num = atoi(temp);    
    switch(i++)
    {
        case 1:
            date.tm_mon = num - 1;
            break;
        case 2:
            date.tm_mday = num;
            break;
        case 3:
            date.tm_year = num - 1900;
            break;
        default:
            printf("Unexpected input. Blowing up now!\n");
            break;
    }

    temp = strtok(NULL, "/");
}

Code Snippets

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main(int argc, char *argv[])
{
    int i = 1;
    struct tm date = {0};
    char relativeDays[80];
if(argc != 2)
    {
        printf("Two arguments required!\n");
        return -1;
    }

    char *temp = strtok(argv[1], "/");
while(temp != NULL)
    {
        switch(i++)
        {
            case 1:
                date.tm_mon  = atoi(temp) - 1;
                break;
            case 2:
                date.tm_mday = atoi(temp);
                break;
            case 3:
                date.tm_year = atoi(temp) - 1900;
                break;
            default:
        }
temp = strtok(NULL, "/");
    }
i = (int)difftime(time(NULL), mktime(&date)) / 86400;
    sprintf(relativeDays, "%d", abs(i));

Context

StackExchange Code Review Q#37096, answer score: 5

Revisions (0)

No revisions yet.