Advertisement
tamouse

countdown formatter

Jun 22nd, 2012
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.58 KB | None | 0 0
  1. /*
  2.   convdate.c -
  3.  
  4.   Tamara Temple <tamara@tamaratemple.com>
  5.   Time-stamp: <2012-06-21 02:22:57 tamara>
  6.   Created: 2012-06-21
  7.   Copyright (c) 2011 Tamara Temple Web Development
  8.   License: GPLv3
  9.  
  10.   This program is free software: you can redistribute it and/or modify
  11.   it under the terms of the GNU General Public License as published by
  12.   the Free Software Foundation, either version 3 of the License, or
  13.   (at your option) any later version.
  14.  
  15.   This program is distributed in the hope that it will be useful,
  16.   but WITHOUT ANY WARRANTY; without even the implied warranty of
  17.   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  18.   GNU General Public License for more details.
  19.  
  20.   You should have received a copy of the GNU General Public License
  21.   along with this program.  If not, see <http://www.gnu.org/licenses/>.
  22.  
  23.  
  24. */
  25.  
  26. #include <stdio.h>
  27. #include <time.h>
  28.  
  29. int main(int argc, char *argv[])
  30. {
  31.   struct tm tm;
  32.   time_t t;
  33.   time_t now;
  34.   long left, weeks, days, hours, minutes, seconds;
  35.  
  36.   if (argc < 3) {
  37.     printf("Usage: %s <format> <time_string>\n",argv[0]);
  38.     return 1;
  39.   }
  40.  
  41.  
  42.   strptime(argv[2],argv[1],&tm);
  43.   t=mktime(&tm);
  44.   time(&now);
  45.  
  46.   left=(long) t - (long) now;
  47.   if (left < 0) {
  48.     printf("Deadline past.\n");
  49.     return 2;
  50.   }
  51.  
  52.   weeks = left/(60*60*24*7);
  53.   days = (left%(60*60*24*7))/(60*60*24);
  54.   hours = (left%(60*60*24))/(60*60);
  55.   minutes = (left%(60*60))/(60);
  56.   seconds = left%(60);
  57.  
  58.  
  59.   printf("%d week(s), %d day(s), %d hour(s), and %d minute(s) left\n",weeks, days, hours, minutes, seconds);
  60.  
  61.   return 0;
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement