1515 * along with this program. If not, see <https://www.gnu.org/licenses/>.
1616 */
1717#include " bikram.h"
18+ #include < iostream>
19+ #include < ctime>
1820
19-
20- int main (int argc, char * argv[]) {
21- if (argc != 6 || std::string (argv[1 ]) != " --conv" ||
22- (std::string (argv[2 ]) != " --tobs" && std::string (argv[2 ]) != " --toad" )) {
23- std::cerr << " Usage: " << argv[0 ] << " \n \e[31m Convert to Nepali Date: --conv --tobs year month day\n Convert to Gregorian Date: --conv --toad year month day\n \e[0m" ;
21+ // Helper function to get the current date
22+ void getCurrentDate (int &year, int &month, int &day) {
2423 std::time_t now = std::time (nullptr );
25- std::tm timeinfo = * std::localtime ( & now);
26-
27- int year = timeinfo.tm_year + 1900 ;
28- int month = timeinfo.tm_mon + 1 ; // Adjust month from 0-based to 1-based
29- int day = timeinfo. tm_mday ;
24+ std::tm timeinfo = *std::localtime (& now);
25+ year = timeinfo. tm_year + 1900 ;
26+ month = timeinfo.tm_mon + 1 ; // Adjust month from 0-based to 1-based
27+ day = timeinfo.tm_mday ;
28+ }
3029
30+ void NoArgs () {
31+ int year, month, day;
32+ getCurrentDate (year, month, day);
3133 bikram bsdate;
3234 bsdate.fromGregorian (year, month, day);
33-
3435 std::string bs_weekday_name = bsdate.getWeekdayName (year, month, day);
35-
3636 std::cout << " \e[36m Today's Date:\e[0m" << std::endl;
3737 std::cout << " \e[33m Gregorian: \e[0m"
38- " \e[35m" << year << " " << month << " " << day << " " << bsdate.getWeekdayName (year, month, day) << " " << " \e[0m" << std::endl;
38+ " \e[35m" << year << " " << month << " " << day << " " << bsdate.getWeekdayName (year, month, day) << " " << " \e[0m" << std::endl;
3939 std::cout << " \e[33m Bikram Sambat: \e[0m"
40- " \e[35m" << bsdate.getYear () << " " << bsdate.getMonth () << " " << bsdate.getDay () << " " << bs_weekday_name << " " << " \e[33m days in bikram month: \e[0m" << bsdate.daysInMonth (bsdate.getYear (), bsdate.getMonth ()) << " \e[0m" << std::endl;
41- return 1 ;
42- }
43-
44- std::string convType = argv[2 ];
45- int year = std::stoi (argv[3 ]);
46- int month = std::stoi (argv[4 ]);
47- int day = std::stoi (argv[5 ]);
40+ " \e[35m" << bsdate.getYear () << " " << bsdate.getMonth () << " " << bsdate.getDay () << " "
41+ << bs_weekday_name << " " << " \e[33m days in bikram month: \e[0m" << bsdate.daysInMonth (bsdate.getYear (), bsdate.getMonth ()) << " \e[0m" << std::endl;
42+ std::cerr << " Usage: \n \e[31m"
43+ << " Convert to Nepali Date: nepdate-cli --conv --tobs year month day\n "
44+ << " Convert to Gregorian Date: nepdate-cli --conv --toad year month day\n "
45+ << " Show today's date: nepdate-cli --today\n \e[0m" ;
4846
49- bikram bsdate;
47+ }
5048
51- if (convType == " --tobs" ) {
49+ void showTodaysDate () {
50+ int year, month, day;
51+ getCurrentDate (year, month, day);
52+ bikram bsdate;
5253 bsdate.fromGregorian (year, month, day);
53- int convertedYear;
54- int convertedMonth;
55- int convertedDay;
56- int gregorian_weekday = bsdate.getDayOfWeek ();
57-
58- } else if (convType == " --toad" ) {
59- bsdate.fromNepali (year, month, day);
60- }
61-
62- int convertedYear = bsdate.getYear ();
63- int convertedMonth = bsdate.getMonth ();
64- int convertedDay = bsdate.getDay ();
65- std::string bs_weekday_name = bsdate.getWeekdayName (year, month, day);
66- std::string gregorian_weekday_name = " " ;
67- if (convType == " --tobs" ) {
68- // Get weekday name for Gregorian date after conversion
69- gregorian_weekday_name = bsdate.getWeekdayName (convertedYear, convertedMonth, convertedDay);
70-
71- std::cout << " \e[33m Bikram Sambat Date: \e[0m"
72- " \e[35m" << convertedYear << " " << convertedMonth << " " << convertedDay << " " << bs_weekday_name << " " << " \e[0m" << " \e[33m days in bikram month: \e[0m" << bsdate.daysInMonth (bsdate.getYear (), bsdate.getMonth ()) << " \e[0m" << std::endl;
73- } else if (convType == " --toad" ) {
74- bsdate.fromNepali (year, month, day);
75-
76- // Convert to Gregorian date for weekday calculation
77- bsdate.toGregorian (year, month, day, convertedYear, convertedMonth, convertedDay);
78-
79- int gregorian_weekday = bsdate.getDayOfWeek ();
80- std::string gregorian_weekday_name = bsdate.getWeekdayName (convertedYear, convertedMonth, convertedDay);
81-
82- std::cout << " \e[33m Gregorian Date: \e[0m"
83- " \e[35m" << convertedYear << " " << convertedMonth << " " << convertedDay << " " << gregorian_weekday_name << " " << " \e[0m" << std::endl;
84- }
85-
86- return 0 ;
54+ std::string bs_weekday_name = bsdate.getWeekdayName (year, month, day);
55+
56+ std::cout << " \e[35m" << bsdate.getYear () << " " << bsdate.getMonth () << " " << bsdate.getDay () << " " << bs_weekday_name << " " << std::endl;
8757}
58+
59+ int main (int argc, char *argv[]) {
60+ // Check for no arguments
61+ if (argc == 1 ) {
62+ NoArgs (); // Show usage information below today's date
63+ return 0 ;
64+ }
65+
66+ std::string command = argv[1 ];
67+
68+ // Handle the --today command
69+ if (command == " --today" ) {
70+ showTodaysDate ();
71+ return 0 ;
72+ }
73+
74+ // Handle the conversion commands
75+ if (argc != 6 || std::string (argv[1 ]) != " --conv" ||
76+ (std::string (argv[2 ]) != " --tobs" && std::string (argv[2 ]) != " --toad" )) {
77+ NoArgs (); // Show usage information for invalid commands
78+ return 1 ;
79+ }
80+
81+ std::string convType = argv[2 ];
82+ int year = std::stoi (argv[3 ]);
83+ int month = std::stoi (argv[4 ]);
84+ int day = std::stoi (argv[5 ]);
85+
86+ bikram bsdate;
87+
88+ if (convType == " --tobs" ) {
89+ bsdate.fromGregorian (year, month, day);
90+ } else if (convType == " --toad" ) {
91+ bsdate.fromNepali (year, month, day);
92+ }
93+
94+ int convertedYear = bsdate.getYear ();
95+ int convertedMonth = bsdate.getMonth ();
96+ int convertedDay = bsdate.getDay ();
97+ std::string bs_weekday_name = bsdate.getWeekdayName (year, month, day);
98+ std::string gregorian_weekday_name = " " ;
99+
100+ if (convType == " --tobs" ) {
101+ // Get weekday name for Gregorian date after conversion
102+ gregorian_weekday_name = bsdate.getWeekdayName (convertedYear, convertedMonth, convertedDay);
103+
104+ std::cout << " \e[33m Bikram Sambat Date: \e[0m"
105+ " \e[35m" << convertedYear << " " << convertedMonth << " " << convertedDay << " " << bs_weekday_name << " " << " \e[0m"
106+ << " \e[33m days in bikram month: \e[0m" << bsdate.daysInMonth (bsdate.getYear (), bsdate.getMonth ()) << " \e[0m" << std::endl;
107+ } else if (convType == " --toad" ) {
108+ // Convert to Gregorian date for weekday calculation
109+ bsdate.toGregorian (year, month, day, convertedYear, convertedMonth, convertedDay);
110+
111+ int gregorian_weekday = bsdate.getDayOfWeek ();
112+ std::string gregorian_weekday_name = bsdate.getWeekdayName (convertedYear, convertedMonth, convertedDay);
113+
114+ std::cout << " \e[33m Gregorian Date: \e[0m"
115+ " \e[35m" << convertedYear << " " << convertedMonth << " " << convertedDay << " " << gregorian_weekday_name << " " << " \e[0m" << std::endl;
116+ }
117+
118+ return 0 ;
119+ }
0 commit comments