zipios  2.2.0
Zipios – a small C++ library that provides easy access to .zip files.
dosdatetime.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-2019 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 "zipios/dosdatetime.hpp"
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  zipios::DOSDateTime conv;
136  switch(mode)
137  {
138  case time_mode_t::DOS:
139  conv.setUnixTimestamp(t);
140  r = conv.getDOSDateTime();
141  break;
142 
143  case time_mode_t::UNIX:
144  conv.setDOSDateTime(t);
145  r = conv.getUnixTimestamp();
146  dt = r;
147  break;
148 
149  }
150  struct tm *dtm(gmtime(&dt));
151  char buf[256];
152  if(dt == -1)
153  {
154  strcpy(buf, "- -");
155  }
156  else
157  {
158  strftime(buf, sizeof(buf), "%Y/%m/%d %T", dtm);
159  }
160  buf[255] = '\0';
161  std::cout << t << " " << r << " " << buf << std::endl;
162  }
163  }
164 
165  return 0;
166 }
167 
168 
169 // Local Variables:
170 // mode: cpp
171 // indent-tabs-mode: nil
172 // c-basic-offset: 4
173 // tab-width: 4
174 // End:
175 
176 // vim: ts=4 sw=4 et
Define a type to manage date and time in MS-DOS format.
std::time_t getUnixTimestamp() const
Retrieve the DOSDateTime as a Unix timestamp.
dosdatetime_t getDOSDateTime() const
Retrieve the DOSDateTime value as is.
void setUnixTimestamp(std::time_t unix_timestamp)
Set the DOSDateTime value from a Unix timestamp.
int main(int argc, char *argv[])
Definition: dosdatetime.cpp:79
zipios configuration header.
#define ZIPIOS_VERSION_STRING
void setDOSDateTime(dosdatetime_t datetime)
Set the DOSDateTime value as is.