Thread: Date manipulation

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    41

    Date manipulation

    Hello All,

    Once more I'm asking for some help from all gurus. I've searched for some help in the history but with now sucess.

    I'm trying to open a file named file_YYYYMMDD.txt.
    Ex: file_20050225.txt

    To open the file, I get the date (YMD). There is no problem with getting the date. The problem is that I need to get the date not from now but from now-5minutes. Here is my code:

    Code:
    #include <time.h>
    
    main()
    {
    
      char s[50];
      size_t i;
      struct tm tim;
      time_t now;
    
      now = time(NULL);
      tim = *(localtime(&now));
      i = strftime(s,50,"%b %d, %Y; %H:%M:%S",&tim);
      printf("time: [%s]\n",s);
      printf("now: [%d]\n",now);
    
    }

    My output is:

    time: [Feb 25, 2005; 14:16:27]
    now: [1109351787]

    Can anyone help me finding a solution on how to get the date - 5 minutes ? Like this:

    time: [Feb 25, 2005; 14:16:27]
    now: [1109351787]

    time2: [Feb 25, 2005; 14:11:27]
    now2: [1109351487]


    Thanks in advance,

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    The time function returns the number of seconds that have passed from Jan 1st, 1970. So, you have a function that returns a value equal to some set number of seconds and you want to find the time 5 minutes ago... you should simply need to subtract 5 minutes worth of seconds from the value returned by the call to time.

    If now = time(NULL) then now2 = now - 5 * 60 and time2 = localtime(&now2).

    For the purposes of opening a file with YYYYMMDD in the name, this is only going to matter if the program is going to be run in the 5 minute window of midnight having passed. Otherwise the YYYYMMDD part isn't going to have changed.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Dec 2004
    Posts
    41
    Realy a dont know how I was missing this !

    Anyway, thanks alot for the help ! My code now is:

    Code:
    #include <time.h>
    
    main()
    {
    
      char s[50],s2[50];
      size_t i;
      size_t i2;
      struct tm tim;
      struct tm tim2;
      time_t now;
      time_t now2;
    
      now = time(NULL);
      now2 = now - (5 * 60);
      tim = *(localtime(&now));
      tim2 = *(localtime(&now2));
      i = strftime(s,50,"%b %d, %Y; %H:%M:%S",&tim);
      i2 = strftime(s2,50,"%b %d, %Y; %H:%M:%S",&tim2);
      printf("time: [%s]\n",s);
      printf("now: [%d]\n\n",now);
      printf("time2: [%s]\n",s2);
      printf("now2: [%d]\n",now2);
    
    }
    and works well !

    About running every 5 minutes, this is a daemon that sleeps for 5 seconds and reads the file again !

    Once more, thanks !

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Advancing day by day until it matches a second date
    By nhubred in forum C++ Programming
    Replies: 1
    Last Post: 05-30-2009, 08:55 AM
  2. Checking array for string
    By Ayreon in forum C Programming
    Replies: 87
    Last Post: 03-09-2009, 03:25 PM
  3. CDate Class - handle date manipulation simply
    By LuckY in forum C++ Programming
    Replies: 5
    Last Post: 07-16-2003, 08:35 AM
  4. C/C++ Date manipulation code
    By lordofthering in forum C Programming
    Replies: 3
    Last Post: 04-10-2002, 03:49 PM