zipios  2.1.1
Zipios++ – a small C++ library that provides easy access to .zip files.
dostime.cpp
Go to the documentation of this file.
1 /*
2  Zipios++ - a small C++ library that provides easy access to .zip files.
3 
4  Copyright (C) 2000-2007 Thomas Sondergaard
5  Copyright (C) 2015 Made to Order Software Corporation
6 
7  This library is free software; you can redistribute it and/or
8  modify it under the terms of the GNU Lesser General Public
9  License as published by the Free Software Foundation; either
10  version 2.1 of the License, or (at your option) any later version.
11 
12  This library is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  Lesser General Public License for more details.
16 
17  You should have received a copy of the GNU Lesser General Public
18  License along with this library; if not, write to the Free Software
19  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21 
29 #include "src/dostime.h"
30 #include "zipios/zipios-config.hpp"
31 
32 #include <cstring>
33 #include <iostream>
34 #include <stdlib.h>
35 
36 
37 // static variables
38 namespace
39 {
40 
41 char *g_progname;
42 
43 
44 
45 void usage()
46 {
47  std::cout << "Usage: " << g_progname << " [-opt] <time>" << std::endl;
48  std::cout << "Where -opt is one or more of:" << std::endl;
49  std::cout << " --dec display the result in decimal" << std::endl;
50  std::cout << " --dos convert a Unix time to a DOS time (default)" << std::endl;
51  std::cout << " --help display this help screen" << std::endl;
52  std::cout << " --hex display the result in hexadecimal" << std::endl;
53  std::cout << " --unix convert a DOS time to a Unix time" << std::endl;
54  std::cout << " --version print the library version and exit" << std::endl;
55  std::cout << std::endl;
56  std::cout << "Examples:" << std::endl;
57  std::cout << " to convert a Unix time to a DOS time:" << std::endl;
58  std::cout << " " << g_progname << " 956794294" << std::endl;
59  std::cout << " to convert a DOS time to a Unix time:" << std::endl;
60  std::cout << " " << g_progname << " --unix 681216369" << std::endl;
61  std::cout << std::endl;
62  std::cout << "Output:" << std::endl;
63  std::cout << "<time entered on command line> <converted time> <date (YYYY/MM/DD)> <time (HH:MM:SS)>" << std::endl;
64  std::cout << " Note: <converted time> shows -1 if an error occurs" << std::endl;
65  exit(1);
66 }
67 
68 
69 enum class time_mode_t
70 {
71  DOS,
72  UNIX
73 };
74 
75 
76 } // no name namespace
77 
78 
79 int main(int argc, char *argv[])
80 {
81  // define program name
82  g_progname = argv[0];
83  char *e(strrchr(g_progname, '/'));
84  if(e)
85  {
86  g_progname = e + 1;
87  }
88  e = strrchr(g_progname, '\\');
89  if(e)
90  {
91  g_progname = e + 1;
92  }
93 
94  // check the various command line options
95  time_mode_t mode(time_mode_t::DOS);
96  for(int i(1); i < argc; ++i)
97  {
98  if(argv[i][0] == '-')
99  {
100  if(strcmp(argv[i], "--help") == 0
101  || strcmp(argv[i], "-h") == 0)
102  {
103  usage();
104  }
105  if(strcmp(argv[i], "--version") == 0)
106  {
107  // version of this tool (compiled with this version)
108  // it should be the same as the --version
109  std::cout << ZIPIOS_VERSION_STRING << std::endl;
110  exit(0);
111  }
112  if(strcmp(argv[i], "--dec") == 0)
113  {
114  std::cout << std::dec;
115  }
116  else if(strcmp(argv[i], "--dos") == 0)
117  {
118  mode = time_mode_t::DOS;
119  }
120  else if(strcmp(argv[i], "--hex") == 0)
121  {
122  std::cout << std::hex;
123  }
124  else if(strcmp(argv[i], "--unix") == 0)
125  {
126  mode = time_mode_t::UNIX;
127  }
128  }
129  else
130  {
131  // TODO: test that the argument is a valid number
132  int64_t const t(atoll(argv[i]));
133  int64_t r(0);
134  time_t dt(t);
135  switch(mode)
136  {
137  case time_mode_t::DOS:
138  r = unix2dostime(t);
139  break;
140 
141  case time_mode_t::UNIX:
142  r = dos2unixtime(t);
143  dt = r;
144  break;
145 
146  }
147  struct tm *dtm(localtime(&dt));
148  char buf[256];
149  if(dt == -1)
150  {
151  strcpy(buf, "- -");
152  }
153  else
154  {
155  strftime(buf, sizeof(buf), "%Y/%m/%d %T", dtm);
156  }
157  buf[255] = '\0';
158  std::cout << t << " " << r << " " << buf << std::endl;
159  }
160  }
161 
162  return 0;
163 }
164 
165 
166 // Local Variables:
167 // mode: cpp
168 // indent-tabs-mode: nil
169 // c-basic-offset: 4
170 // tab-width: 4
171 // End:
172 
173 // vim: ts=4 sw=4 et
dostime_t unix2dostime(time_t unix_time)
Convert a Unix date to a DOS date.
Definition: dostime.c:219
time_t dos2unixtime(dostime_t dostime)
Convert a DOS time to a Unix time.
Definition: dostime.c:131
zipios configuration header.
Definitions for the MS-DOS to Unix time conversions.
#define ZIPIOS_VERSION_STRING
int main(int argc, char *argv[])
Definition: dostime.cpp:79