File: timetrans

package info (click to toggle)
dnssec-tools 1.13-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 16,064 kB
  • sloc: perl: 44,399; ansic: 31,547; cpp: 21,306; sh: 15,813; xml: 2,113; makefile: 1,390; pascal: 836; python: 290; csh: 11
file content (399 lines) | stat: -rwxr-xr-x 8,395 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
#!/usr/bin/perl
#
# Copyright 2004-2012 SPARTA, Inc.  All rights reserved.  See the COPYING
# file distributed with this software for details.
#
# timetrans
#
#	This script converts time into time.  If given any of the set of
#	units options, then it will print the number of seconds to which
#	those units add up.  If given the count option, that number of
#	seconds will be converted into the appropriate number of weeks,
#	days, hours, minutes, and seconds.
#
#	timetrans is intended for use with the DNSSEC tools, for calculating
#	a zone's expiration time.
#
#	Usage:
#
#		timetrans [units options] [count option]
#
#		units options:
#			-seconds	Count of seconds.
#			-minutes	Count of minutes.
#			-hours		Count of hours.
#			-days		Count of days.
#			-weeks		Count of weeks.
#
#		count option:
#			-count		Seconds count.
#

use strict;

use Getopt::Long qw(:config no_ignore_case_always);

use Net::DNS::SEC::Tools::timetrans;

#
# Version information.
#
my $NAME   = "timetrans";
my $VERS   = "$NAME version: 1.13.0";
my $DTVERS = "DNSSEC-Tools Version: 1.13";

#######################################################################
#
# Time-related constants.

our $MINUTE = 60;
our $HOUR   = (60 * $MINUTE);
our $DAY    = (24 * $HOUR);
our $WEEK   = (7  * $DAY);

#
# Options fields.
#
my $seconds = 0;				# Number of seconds.
my $minutes = 0;				# Number of minutes.
my $hours   = 0;				# Number of hours.
my $days    = 0;				# Number of days.
my $weeks   = 0;				# Number of weeks.
my $count   = 0;				# Count of seconds.


#
# Command line arguments.
#
my %options = ();				# Filled option array.
my @opts =
(
        "seconds=i",				# Number of seconds.
        "minutes=i",				# Number of minutes.
        "hours=i",				# Number of hours.
        "days=i",				# Number of days.
        "weeks=i",				# Number of weeks.
        "count=i",				# Seconds count.
	"Version",				# Display the version number.
);

#
# Behavior flags.
#
my $countflag = 0;			# Translate seconds count to units.
my $unitsflag = 0;			# Translate units to seconds count.

#
# Do our work.
#
main();
exit(0);

#######################################################################
#
# Routine:	main()
#
# Purpose:	Yeah, yeah, a main() isn't necessary.  However, it offends my
#		sense of aesthetics to have great gobs of code on the same
#		level as a pile of globals.
#
#		But what about all those globals, you ask...
#
sub main
{
	#
	# Munch on the options and arguments.
	#
	optsandargs();

	#
	# If the -count option was given, print the translated string.
	#
	if($countflag)
	{
		print timetrans($count) . "\n";
	}
	else
	{
		my $total = 0;			# Accumulated count of seconds.

		$total  = $seconds;
		$total += $minutes * $MINUTE;
		$total += $hours * $HOUR;
		$total += $days * $DAY;
		$total += $weeks * $WEEK;
		print "$total\n";
	}
}

#######################################################################
#
# Routine:	optsandargs()
#
# Purpose:	Parse the command line for options.
#
sub optsandargs()
{
	my $argc = @ARGV;			# Number of arguments.

	#
	# Make sure we have arguments.
	#
	usage(0) if($argc == 0);

	#
	# Check our options and ensure there weren't any problems.
	#
	Getopt::Long::Configure("pass_through");
	GetOptions(\%options,@opts) || usage();

	#
	# Show the version number if requested
	#
	version() if(defined($options{'Version'}));

	#
	# Check for command line errors.
	#
	if(keys(%options) == 0)
	{
		print STDERR "no options specified\n";
		usage(1);
	}
	if(@ARGV > 0)
	{
		print STDERR "invalid options or arguments\n";
		usage(2);
	}

	#
	# Grab the options values.
	#
	$seconds = $options{'seconds'};
	$minutes = $options{'minutes'};
	$hours	 = $options{'hours'};
	$days	 = $options{'days'};
	$weeks	 = $options{'weeks'};
	$count	 = $options{'count'};

	#
	# Check for negative numbers.
	#
	if(($seconds < 0) || ($minutes < 0) || ($hours < 0) ||
	   ($days < 0)	  || ($weeks < 0)   || ($count < 0))
	{
		print STDERR "invalid option value:  option values must be positive\n";
		usage(3);
	}

	#
	# Check if -count was given.
	#
	$countflag = 1 if(defined($options{'count'}));

	#
	# Check if at least one of the units flags were given.
	#
	if(defined($options{'seconds'}) || defined($options{'minutes'}) ||
	   defined($options{'hours'})   || defined($options{'days'})    ||
	   defined($options{'minutes'}))
	{
		$unitsflag = 1;
	}

	#
	# Ensure that options weren't mixed.
	#
	if($countflag && $unitsflag)
	{
		print STDERR "-count may not be given with the units flags\n";
		exit(1);
	}
}

######################################################################
#
# Routine:	version()
#
# Purpose:	Print the version number(s) and exit.
#
sub version
{
	print STDERR "$VERS\n";
	print STDERR "$DTVERS\n";

	exit(0);
}


#######################################################################
#
# Routine:	usage()
#
# Purpose:	Give usage message and exit.
#
sub usage
{
	my $whence = shift;			# Location of call.

	print STDERR "usage:  timetrans [units-options] [count-options] [-help] [-Version]\n";
	print STDERR "\n";

	print STDERR "\t\tunits-options:\n";
	print STDERR "\t\t\t-seconds seconds-count\n";
	print STDERR "\t\t\t-minutes minutes-count\n";
	print STDERR "\t\t\t-hours hours-count\n";
	print STDERR "\t\t\t-days days-count\n";
	print STDERR "\t\t\t-weeks weeks-count\n";
	print STDERR "\n";

	print STDERR "\t\tcount option:\n";
	print STDERR "\t\t\t-count seconds-count\n";
	print STDERR "\n";

#	print "\ncalled from $whence\n" if($verbose && ($whence > 0));

	exit(1);
}

1;

##############################################################################
#

=pod

=head1 NAME

timetrans - Converts time into time

=head1 SYNOPSIS

  timetrans [units-options] [-count]

=head1 DESCRIPTION

B<timetrans> converts time from one type of unit to another.  If any of the
units options are specified, then B<timetrans> will convert those time units
into the number of seconds to which they add up.  If given the count option,
B<timetrans> will convert that number of seconds into the appropriate number
of weeks, days, hours, minutes, and seconds.  The converted result is printed
out.  Units options cannot be specified in the same execution as the count
option, and vice versa.

B<timetrans> is intended for use with DNSSEC-Tools, for calculating
a zone's expiration time.

=head1 OPTIONS

=head2 Units Options

The converted value of each unit is totaled and a single result printed.

=over 4

=item B<-seconds seconds>

Count of seconds to convert to seconds.

=item B<-minutes minutes>

Count of minutes to convert to seconds.

=item B<-hours hours>

Count of hours to convert to seconds.

=item B<-days days>

Count of days to convert to seconds.

=item B<-weeks weeks>

Count of weeks to convert to seconds.

=back

=head2 Count Option

The specified seconds count is converted to the appropriate number of weeks,
days, hours, minutes, and seconds.

=over 4

=item B<-count seconds>

Count of seconds to convert to the appropriate set of units.

=back

=head2 Other Options

B<timetrans> has the following miscellaneous options.

=over 4

=item B<-Version>

Displays the version information for B<timetrans> and the DNSSEC-Tools package.

=back

=head1 EXAMPLES

Example 1:  Converting 5 days into seconds

    $(42)> timetrans -days 5
    432000

Example 2:  Converting 2 weeks into seconds

    $(43)> timetrans -w 2
    1209600

Example 3:  Converting 8 days and 8 hours into seconds

    $(44)> timetrans -d 8 -hours 8
    720000

Example 4:  Converting 1 week, 1 day, and 8 hours into seconds

    $(46)> timetrans -w 1 -days 1 -h 8
    720000

Example 5:  Converting 14 weeks, 4 days, 21 hours, 8 minutes, and 8 seconds into seconds

    $(47)> timetrans -w 14 -d 4 -h 21 -m 8 -s 8
    8888888

Example 6:  Converting 720000 seconds into time units

    $(48)> timetrans -c 720000
    1 week, 1 day, 8 hours

Example 7:  Converting 1814421 seconds into time units

    $(49)> timetrans -c 1814421
    3 weeks, 21 seconds

Example 8:  Converting 8888888 seconds into time units

    $(50)> timetrans -c 8888888
    14 weeks, 4 days, 21 hours, 8 minutes, 8 seconds

=head1 COPYRIGHT

Copyright 2004-2012 SPARTA, Inc.  All rights reserved.
See the COPYING file included with the DNSSEC-Tools package for details.

=head1 AUTHOR

Wayne Morrison, tewok@tislabs.com

=head1 SEE ALSO

B<zonesigner(8)>

B<Net::DNS::SEC::Tools::timetrans.pm(3)>

=cut