Repository navigation
Expand file tree
/
Copy pathwhen.cpp
More file actions
91 lines (76 loc) · 2.5 KB
/
Copy pathwhen.cpp
File metadata and controls
91 lines (76 loc) · 2.5 KB
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
#include <iostream>
#include <iomanip>
using namespace std;
#include <time.h>
#include <stdlib.h>
#include <stdio.h>
#include <strings.h>
#include <tz.h>
void die( const char* const msg )
{
cerr << msg << endl << endl;
cerr << "useage: when tz day(sun, mon, etc.) time" << endl;
cerr << "example: when Asia/Muscat fri 15:47" << endl;
cerr << "example: when America/Los_Angeles fri 15:47" << endl;
cerr << "example: when America/Kentucky/Louisville fri 15:47" << endl;
cerr << "example: when America/New_York fri 15:47" << endl;
exit(-1);
}
int day_of_the_week( const char *const arg )
{
if ( !strncasecmp( "sun", arg, 3 ) )
return 0;
if ( !strncasecmp( "mon", arg, 3 ) )
return 1;
if ( !strncasecmp( "tue", arg, 3 ) )
return 2;
if ( !strncasecmp( "wed", arg, 3 ) )
return 3;
if ( !strncasecmp( "thu", arg, 3 ) )
return 4;
if ( !strncasecmp( "fri", arg, 3 ) )
return 5;
if ( !strncasecmp( "sat", arg, 3 ) )
return 6;
char msg[80];
snprintf( msg, 80, "'%s' doesn't look like a day of the week.", arg );
die( msg );
}
int main( int argc, char *argv[] )
{
if ( argc < 4 )
die( "tell the program the day and time (future only) you are interested in" );
tz there( argv[1] );
struct tm there_time = there.local_time();
cout << "Now, there, it's " << there << endl;
///////////////////////////////////////////////////////////////////
// process & convert arguments.
int day = day_of_the_week( argv[2] );
char *endptr;
int hour = strtol(argv[3], &endptr, 10);
int min = strtol(endptr+1, NULL, 10);
///////////////////////////////////////////////////////////////////
// adjust the day of the month.
int old_day = there_time.tm_wday;
int day_diff = day - old_day;
cout << day_diff << " days; ";
if ( day_diff <= 0 )
day_diff = day_diff + 7;
cout << day_diff << " days;" << endl;
there_time.tm_mday += day_diff;
there_time.tm_isdst = -1;
///////////////////////////////////////////////////////////////////
// adjust the time of day.
there_time.tm_hour = hour;
there_time.tm_min = min;
there_time.tm_sec = 0;
///////////////////////////////////////////////////////////////////
// normalize the time.
there.set_local_time( there_time );
cout << "Then, and there, it will be " << there << endl;
///////////////////////////////////////////////////////////////////
// convert it to local time.
struct tm local = there.convert_from_there_to_here();
cout << "Then, and here, it will be " << asctime(&local) << endl;
return 0;
}