[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

DateAdd Equivalent in C

Posted on 2007-08-09
4
Medium Priority
?
1,100 Views
Last Modified: 2013-11-05
What is the best way to subtract 1 day from today's date in C?
0
Comment
Question by:DanPerlman
  • 2
4 Comments
 
LVL 3

Expert Comment

by:Dimkov
ID: 19666741
can you tell me what structure you are using for representation of the date?
0
 

Author Comment

by:DanPerlman
ID: 19666795
Maybe this helps some,

I am reading a file and need to find yesterdays date with a line. So I am trying to use the time function in c to give me yesterdays date to  compare in the file. I have something like this to get the current date into the format the date is in the file.

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

struct tm *ptr;
time_t lt;

int main(int argc, char** argv) {

    t = time(NULL);

    ptr = localtime(&lt);

    strftime(dt, 75, "%Y/%m/%d", ptr);

   printf(Date: %s \n, dt);

   return (EXIT_SUCCESS);
}

This will give we the date obviously, just not sure how to subtract a day from the calendar to accommodate for month and year.

0
 
LVL 3

Expert Comment

by:Dimkov
ID: 19666894
working with tm is a bit hard, since it is a struct.
I would recomend you to work with time_t which is long. Here the date is represented in seconds passed from (00:00:00), January 1, 1970.
So if you subtrctract 60sec*60min*24h==1day=86400sec from the lt, you will get the previous day

t=t-86400;
0
 
LVL 46

Accepted Solution

by:
Kent Olsen earned 2000 total points
ID: 19669004

Converting dates is one of the most irritating things in C.  There are just too many little details that one works with so seldomly.

#include <time.h>
main ()
{
  time_t rawtime;
  struct tm * timeinfo;

  time ( &rawtime );                            // time resolution in seconds
  rawtime += (7 * 86400);                 // add a week to the current date
  timeinfo = localtime ( &rawtime );
  mktime ( timeinfo );

/* All of the fields of  struct tm *timeinfo are now populated.  They can be formatted for display or used in other calculations.

The fields are:

int tm_sec;
int tm_min;
int tm_hour;
int tm_mday;
int tm_mon;
int tm_year;
int tm_wday;
int tm_yday;
int tm_isdst;
*/
  return 0;
}


Good Luck,
Kent
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
Suggested Courses

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question