Instruction
stringlengths
45
106
input_code
stringlengths
1
13.7k
output_code
stringlengths
1
13.7k
Generate a C++ translation of this Arturo snippet without changing its computational steps.
daysBetweenDates: function [startDate, endDate][ a: to :date.format: "dd/MM/yyyy" startDate b: to :date.format: "dd/MM/yyyy" endDate return abs b\days - a\days ] print [ "days between the two dates:" daysBetweenDates "01/01/2019" "19/10/2019" ]
#include <iomanip> #include <iostream> class Date { private: int year, month, day; public: Date(std::string str) { if (isValidDate(str)) { year = atoi(&str[0]); month = atoi(&str[5]); day = atoi(&str[8]); } else { throw std::exception("Invalid date"); } } int getYear() { return year; } int getMonth() { return month; } int getDay() { return day; } static bool isValidDate(std::string str) { if (str.length() != 10 || str[4] != '-' || str[7] != '-') { return false; } if (!isdigit(str[0]) || !isdigit(str[1]) || !isdigit(str[2]) || !isdigit(str[3])) { return false; } if (!isdigit(str[5]) || !isdigit(str[6])) { return false; } if (!isdigit(str[8]) || !isdigit(str[9])) { return false; } int year = atoi(&str[0]); int month = atoi(&str[5]); int day = atoi(&str[8]); if (year <= 0 || month <= 0 || day <= 0) { return false; } if (month > 12) { return false; } switch (month) { case 2: if (day > 29) { return false; } if (!isLeapYear(year) && day == 29) { return false; } break; case 1: case 3: case 5: case 7: case 8: case 10: case 12: if (day > 31) { return false; } break; default: if (day > 30) { return false; } break; } return true; } static bool isLeapYear(int year) { if (year > 1582) { return ((year % 4 == 0) && (year % 100 > 0)) || (year % 400 == 0); } if (year > 10) { return year % 4 == 0; } return false; } friend std::ostream &operator<<(std::ostream &, Date &); }; std::ostream &operator<<(std::ostream &os, Date &d) { os << std::setfill('0') << std::setw(4) << d.year << '-'; os << std::setfill('0') << std::setw(2) << d.month << '-'; os << std::setfill('0') << std::setw(2) << d.day; return os; } int diffDays(Date date1, Date date2) { int d1m = (date1.getMonth() + 9) % 12; int d1y = date1.getYear() - d1m / 10; int d2m = (date2.getMonth() + 9) % 12; int d2y = date2.getYear() - d2m / 10; int days1 = 365 * d1y + d1y / 4 - d1y / 100 + d1y / 400 + (d1m * 306 + 5) / 10 + (date1.getDay() - 1); int days2 = 365 * d2y + d2y / 4 - d2y / 100 + d2y / 400 + (d2m * 306 + 5) / 10 + (date2.getDay() - 1); return days2 - days1; } int main() { std::string ds1 = "2019-01-01"; std::string ds2 = "2019-12-02"; if (Date::isValidDate(ds1) && Date::isValidDate(ds2)) { Date d1(ds1); Date d2(ds2); std::cout << "Days difference : " << diffDays(d1, d2); } else { std::cout << "Dates are invalid.\n"; } return 0; }
Port the following code from Arturo to Java with equivalent syntax and logic.
daysBetweenDates: function [startDate, endDate][ a: to :date.format: "dd/MM/yyyy" startDate b: to :date.format: "dd/MM/yyyy" endDate return abs b\days - a\days ] print [ "days between the two dates:" daysBetweenDates "01/01/2019" "19/10/2019" ]
import java.time.LocalDate; import java.time.temporal.ChronoUnit; public class DaysBetweenDates { public static void main(String[] args) { LocalDate fromDate = LocalDate.parse("2019-01-01"); LocalDate toDate = LocalDate.parse("2019-10-19"); long diff = ChronoUnit.DAYS.between(fromDate, toDate); System.out.printf("Number of days between %s and %s: %d\n", fromDate, toDate, diff); } }
Port the provided Arturo code into Java while preserving the original functionality.
daysBetweenDates: function [startDate, endDate][ a: to :date.format: "dd/MM/yyyy" startDate b: to :date.format: "dd/MM/yyyy" endDate return abs b\days - a\days ] print [ "days between the two dates:" daysBetweenDates "01/01/2019" "19/10/2019" ]
import java.time.LocalDate; import java.time.temporal.ChronoUnit; public class DaysBetweenDates { public static void main(String[] args) { LocalDate fromDate = LocalDate.parse("2019-01-01"); LocalDate toDate = LocalDate.parse("2019-10-19"); long diff = ChronoUnit.DAYS.between(fromDate, toDate); System.out.printf("Number of days between %s and %s: %d\n", fromDate, toDate, diff); } }
Convert the following code from Arturo to Python, ensuring the logic remains intact.
daysBetweenDates: function [startDate, endDate][ a: to :date.format: "dd/MM/yyyy" startDate b: to :date.format: "dd/MM/yyyy" endDate return abs b\days - a\days ] print [ "days between the two dates:" daysBetweenDates "01/01/2019" "19/10/2019" ]
import sys def days( y,m,d ): m = (m + 9) % 12 y = y - m/10 result = 365*y + y/4 - y/100 + y/400 + (m*306 + 5)/10 + ( d - 1 ) return result def diff(one,two): [y1,m1,d1] = one.split('-') [y2,m2,d2] = two.split('-') year2 = days( int(y2),int(m2),int(d2)) year1 = days( int(y1), int(m1), int(d1) ) return year2 - year1 if __name__ == "__main__": one = sys.argv[1] two = sys.argv[2] print diff(one,two)
Ensure the translated VB code behaves exactly like the original Arturo snippet.
daysBetweenDates: function [startDate, endDate][ a: to :date.format: "dd/MM/yyyy" startDate b: to :date.format: "dd/MM/yyyy" endDate return abs b\days - a\days ] print [ "days between the two dates:" daysBetweenDates "01/01/2019" "19/10/2019" ]
Imports System.Globalization Module Module1 Function DateDiff(d1 As String, d2 As String) As Integer Dim a = DateTime.ParseExact(d1, "yyyy-MM-dd", CultureInfo.InvariantCulture) Dim b = DateTime.ParseExact(d2, "yyyy-MM-dd", CultureInfo.InvariantCulture) Return (b - a).TotalDays End Function Sub Main() Console.WriteLine(DateDiff("1970-01-01", "2019-10-18")) End Sub End Module
Port the following code from Arturo to VB with equivalent syntax and logic.
daysBetweenDates: function [startDate, endDate][ a: to :date.format: "dd/MM/yyyy" startDate b: to :date.format: "dd/MM/yyyy" endDate return abs b\days - a\days ] print [ "days between the two dates:" daysBetweenDates "01/01/2019" "19/10/2019" ]
Imports System.Globalization Module Module1 Function DateDiff(d1 As String, d2 As String) As Integer Dim a = DateTime.ParseExact(d1, "yyyy-MM-dd", CultureInfo.InvariantCulture) Dim b = DateTime.ParseExact(d2, "yyyy-MM-dd", CultureInfo.InvariantCulture) Return (b - a).TotalDays End Function Sub Main() Console.WriteLine(DateDiff("1970-01-01", "2019-10-18")) End Sub End Module
Preserve the algorithm and functionality while converting the code from Arturo to Go.
daysBetweenDates: function [startDate, endDate][ a: to :date.format: "dd/MM/yyyy" startDate b: to :date.format: "dd/MM/yyyy" endDate return abs b\days - a\days ] print [ "days between the two dates:" daysBetweenDates "01/01/2019" "19/10/2019" ]
package main import ( "fmt" "log" "time" ) const layout = "2006-01-02" func daysBetween(date1, date2 string) int { t1, err := time.Parse(layout, date1) check(err) t2, err := time.Parse(layout, date2) check(err) days := int(t1.Sub(t2).Hours() / 24) if days < 0 { days = -days } return days } func check(err error) { if err != nil { log.Fatal(err) } } func main() { date1, date2 := "2019-01-01", "2019-09-30" days := daysBetween(date1, date2) fmt.Printf("There are %d days between %s and %s\n", days, date1, date2) date1, date2 = "2015-12-31", "2016-09-30" days = daysBetween(date1, date2) fmt.Printf("There are %d days between %s and %s\n", days, date1, date2) }
Change the following Arturo code into Go without altering its purpose.
daysBetweenDates: function [startDate, endDate][ a: to :date.format: "dd/MM/yyyy" startDate b: to :date.format: "dd/MM/yyyy" endDate return abs b\days - a\days ] print [ "days between the two dates:" daysBetweenDates "01/01/2019" "19/10/2019" ]
package main import ( "fmt" "log" "time" ) const layout = "2006-01-02" func daysBetween(date1, date2 string) int { t1, err := time.Parse(layout, date1) check(err) t2, err := time.Parse(layout, date2) check(err) days := int(t1.Sub(t2).Hours() / 24) if days < 0 { days = -days } return days } func check(err error) { if err != nil { log.Fatal(err) } } func main() { date1, date2 := "2019-01-01", "2019-09-30" days := daysBetween(date1, date2) fmt.Printf("There are %d days between %s and %s\n", days, date1, date2) date1, date2 = "2015-12-31", "2016-09-30" days = daysBetween(date1, date2) fmt.Printf("There are %d days between %s and %s\n", days, date1, date2) }
Convert this AutoHotKey snippet to C and keep its semantics consistent.
db = ( 1995-11-21|1995-11-21 2019-01-01|2019-01-02 2019-01-02|2019-01-01 2019-01-01|2019-03-01 2020-01-01|2020-03-01 1902-01-01|1968-12-25 2090-01-01|2098-12-25 1902-01-01|2098-12-25 ) for i, line in StrSplit(db, "`n", "`r"){ D := StrSplit(line, "|") result .= "Days between " D.1 " and " D.2 "  : " Days_between(D.1, D.2) " Day(s)`n" } MsgBox, 262144, , % result return Days_between(D1, D2){ D1 := RegExReplace(D1, "\D") D2 := RegExReplace(D2, "\D") EnvSub, D2, % D1, days return D2 }
#include<stdbool.h> #include<string.h> #include<stdio.h> typedef struct{ int year, month, day; }date; date extractDate(char* str){ return (date){.year = 1000 * (str[0]-'0') + 100 * (str[1]-'0') + 10 * (str[2]-'0') + (str[3]-'0'), .month = 10*(str[5]-'0') + (str[6]-'0'), .day = 10*(str[8]-'0') + (str[9]-'0')}; } bool isValidDate(char* str){ date newDate; if(strlen(str)!=10 && str[4]!='-' && str[7]!='-'){ return false; } newDate = extractDate(str); if(newDate.year<=0 || newDate.month<=0 || newDate.day<=0 || newDate.month>12 || (newDate.month==2 && newDate.day>29) || ((newDate.month==1 || newDate.month==3 || newDate.month==5 || newDate.month==7 || newDate.month==8 || newDate.month==10 || newDate.month==12) && newDate.day>31) || newDate.day>30 || (newDate.year%4==0 && newDate.month==2 && newDate.month>28)){ return false; } return true; } int diffDays(date date1,date date2){ int days1, days2; date1.month = (date1.month + 9)%12; date1.year = date1.year - date1.month/10; date2.month = (date2.month + 9)%12; date2.year = date2.year - date2.month/10; days1 = 365*date1.year + date1.year/4 - date1.year/100 + date1.year/400 + (date1.month*306 + 5)/10 + ( date1.day - 1 ); days2 = 365*date2.year + date2.year/4 - date2.year/100 + date2.year/400 + (date2.month*306 + 5)/10 + ( date2.day - 1 ); return days2 - days1; } int main(int argc,char** argv) { if(argc!=3){ return printf("Usage : %s <yyyy-mm-dd> <yyyy-mm-dd>",argv[0]); } if(isValidDate(argv[1])&&isValidDate(argv[2]) == false){ return printf("Dates are invalid.\n"); } printf("Days Difference : %d\n", diffDays(extractDate(argv[1]),extractDate(argv[2]))); return 0; }
Translate the given AutoHotKey code snippet into C without altering its behavior.
db = ( 1995-11-21|1995-11-21 2019-01-01|2019-01-02 2019-01-02|2019-01-01 2019-01-01|2019-03-01 2020-01-01|2020-03-01 1902-01-01|1968-12-25 2090-01-01|2098-12-25 1902-01-01|2098-12-25 ) for i, line in StrSplit(db, "`n", "`r"){ D := StrSplit(line, "|") result .= "Days between " D.1 " and " D.2 "  : " Days_between(D.1, D.2) " Day(s)`n" } MsgBox, 262144, , % result return Days_between(D1, D2){ D1 := RegExReplace(D1, "\D") D2 := RegExReplace(D2, "\D") EnvSub, D2, % D1, days return D2 }
#include<stdbool.h> #include<string.h> #include<stdio.h> typedef struct{ int year, month, day; }date; date extractDate(char* str){ return (date){.year = 1000 * (str[0]-'0') + 100 * (str[1]-'0') + 10 * (str[2]-'0') + (str[3]-'0'), .month = 10*(str[5]-'0') + (str[6]-'0'), .day = 10*(str[8]-'0') + (str[9]-'0')}; } bool isValidDate(char* str){ date newDate; if(strlen(str)!=10 && str[4]!='-' && str[7]!='-'){ return false; } newDate = extractDate(str); if(newDate.year<=0 || newDate.month<=0 || newDate.day<=0 || newDate.month>12 || (newDate.month==2 && newDate.day>29) || ((newDate.month==1 || newDate.month==3 || newDate.month==5 || newDate.month==7 || newDate.month==8 || newDate.month==10 || newDate.month==12) && newDate.day>31) || newDate.day>30 || (newDate.year%4==0 && newDate.month==2 && newDate.month>28)){ return false; } return true; } int diffDays(date date1,date date2){ int days1, days2; date1.month = (date1.month + 9)%12; date1.year = date1.year - date1.month/10; date2.month = (date2.month + 9)%12; date2.year = date2.year - date2.month/10; days1 = 365*date1.year + date1.year/4 - date1.year/100 + date1.year/400 + (date1.month*306 + 5)/10 + ( date1.day - 1 ); days2 = 365*date2.year + date2.year/4 - date2.year/100 + date2.year/400 + (date2.month*306 + 5)/10 + ( date2.day - 1 ); return days2 - days1; } int main(int argc,char** argv) { if(argc!=3){ return printf("Usage : %s <yyyy-mm-dd> <yyyy-mm-dd>",argv[0]); } if(isValidDate(argv[1])&&isValidDate(argv[2]) == false){ return printf("Dates are invalid.\n"); } printf("Days Difference : %d\n", diffDays(extractDate(argv[1]),extractDate(argv[2]))); return 0; }
Generate an equivalent C# version of this AutoHotKey code.
db = ( 1995-11-21|1995-11-21 2019-01-01|2019-01-02 2019-01-02|2019-01-01 2019-01-01|2019-03-01 2020-01-01|2020-03-01 1902-01-01|1968-12-25 2090-01-01|2098-12-25 1902-01-01|2098-12-25 ) for i, line in StrSplit(db, "`n", "`r"){ D := StrSplit(line, "|") result .= "Days between " D.1 " and " D.2 "  : " Days_between(D.1, D.2) " Day(s)`n" } MsgBox, 262144, , % result return Days_between(D1, D2){ D1 := RegExReplace(D1, "\D") D2 := RegExReplace(D2, "\D") EnvSub, D2, % D1, days return D2 }
using System; using System.Globalization; public class Program { public static void Main() => WriteLine(DateDiff("1970-01-01", "2019-10-18")); public static int DateDiff(string d1, string d2) { var a = DateTime.ParseExact(d1, "yyyy-MM-dd", CultureInfo.InvariantCulture); var b = DateTime.ParseExact(d2, "yyyy-MM-dd", CultureInfo.InvariantCulture); return (int)(b - a).TotalDays; } }
Convert this AutoHotKey block to C#, preserving its control flow and logic.
db = ( 1995-11-21|1995-11-21 2019-01-01|2019-01-02 2019-01-02|2019-01-01 2019-01-01|2019-03-01 2020-01-01|2020-03-01 1902-01-01|1968-12-25 2090-01-01|2098-12-25 1902-01-01|2098-12-25 ) for i, line in StrSplit(db, "`n", "`r"){ D := StrSplit(line, "|") result .= "Days between " D.1 " and " D.2 "  : " Days_between(D.1, D.2) " Day(s)`n" } MsgBox, 262144, , % result return Days_between(D1, D2){ D1 := RegExReplace(D1, "\D") D2 := RegExReplace(D2, "\D") EnvSub, D2, % D1, days return D2 }
using System; using System.Globalization; public class Program { public static void Main() => WriteLine(DateDiff("1970-01-01", "2019-10-18")); public static int DateDiff(string d1, string d2) { var a = DateTime.ParseExact(d1, "yyyy-MM-dd", CultureInfo.InvariantCulture); var b = DateTime.ParseExact(d2, "yyyy-MM-dd", CultureInfo.InvariantCulture); return (int)(b - a).TotalDays; } }
Translate this program into C++ but keep the logic exactly as in AutoHotKey.
db = ( 1995-11-21|1995-11-21 2019-01-01|2019-01-02 2019-01-02|2019-01-01 2019-01-01|2019-03-01 2020-01-01|2020-03-01 1902-01-01|1968-12-25 2090-01-01|2098-12-25 1902-01-01|2098-12-25 ) for i, line in StrSplit(db, "`n", "`r"){ D := StrSplit(line, "|") result .= "Days between " D.1 " and " D.2 "  : " Days_between(D.1, D.2) " Day(s)`n" } MsgBox, 262144, , % result return Days_between(D1, D2){ D1 := RegExReplace(D1, "\D") D2 := RegExReplace(D2, "\D") EnvSub, D2, % D1, days return D2 }
#include <iomanip> #include <iostream> class Date { private: int year, month, day; public: Date(std::string str) { if (isValidDate(str)) { year = atoi(&str[0]); month = atoi(&str[5]); day = atoi(&str[8]); } else { throw std::exception("Invalid date"); } } int getYear() { return year; } int getMonth() { return month; } int getDay() { return day; } static bool isValidDate(std::string str) { if (str.length() != 10 || str[4] != '-' || str[7] != '-') { return false; } if (!isdigit(str[0]) || !isdigit(str[1]) || !isdigit(str[2]) || !isdigit(str[3])) { return false; } if (!isdigit(str[5]) || !isdigit(str[6])) { return false; } if (!isdigit(str[8]) || !isdigit(str[9])) { return false; } int year = atoi(&str[0]); int month = atoi(&str[5]); int day = atoi(&str[8]); if (year <= 0 || month <= 0 || day <= 0) { return false; } if (month > 12) { return false; } switch (month) { case 2: if (day > 29) { return false; } if (!isLeapYear(year) && day == 29) { return false; } break; case 1: case 3: case 5: case 7: case 8: case 10: case 12: if (day > 31) { return false; } break; default: if (day > 30) { return false; } break; } return true; } static bool isLeapYear(int year) { if (year > 1582) { return ((year % 4 == 0) && (year % 100 > 0)) || (year % 400 == 0); } if (year > 10) { return year % 4 == 0; } return false; } friend std::ostream &operator<<(std::ostream &, Date &); }; std::ostream &operator<<(std::ostream &os, Date &d) { os << std::setfill('0') << std::setw(4) << d.year << '-'; os << std::setfill('0') << std::setw(2) << d.month << '-'; os << std::setfill('0') << std::setw(2) << d.day; return os; } int diffDays(Date date1, Date date2) { int d1m = (date1.getMonth() + 9) % 12; int d1y = date1.getYear() - d1m / 10; int d2m = (date2.getMonth() + 9) % 12; int d2y = date2.getYear() - d2m / 10; int days1 = 365 * d1y + d1y / 4 - d1y / 100 + d1y / 400 + (d1m * 306 + 5) / 10 + (date1.getDay() - 1); int days2 = 365 * d2y + d2y / 4 - d2y / 100 + d2y / 400 + (d2m * 306 + 5) / 10 + (date2.getDay() - 1); return days2 - days1; } int main() { std::string ds1 = "2019-01-01"; std::string ds2 = "2019-12-02"; if (Date::isValidDate(ds1) && Date::isValidDate(ds2)) { Date d1(ds1); Date d2(ds2); std::cout << "Days difference : " << diffDays(d1, d2); } else { std::cout << "Dates are invalid.\n"; } return 0; }
Rewrite this program in C++ while keeping its functionality equivalent to the AutoHotKey version.
db = ( 1995-11-21|1995-11-21 2019-01-01|2019-01-02 2019-01-02|2019-01-01 2019-01-01|2019-03-01 2020-01-01|2020-03-01 1902-01-01|1968-12-25 2090-01-01|2098-12-25 1902-01-01|2098-12-25 ) for i, line in StrSplit(db, "`n", "`r"){ D := StrSplit(line, "|") result .= "Days between " D.1 " and " D.2 "  : " Days_between(D.1, D.2) " Day(s)`n" } MsgBox, 262144, , % result return Days_between(D1, D2){ D1 := RegExReplace(D1, "\D") D2 := RegExReplace(D2, "\D") EnvSub, D2, % D1, days return D2 }
#include <iomanip> #include <iostream> class Date { private: int year, month, day; public: Date(std::string str) { if (isValidDate(str)) { year = atoi(&str[0]); month = atoi(&str[5]); day = atoi(&str[8]); } else { throw std::exception("Invalid date"); } } int getYear() { return year; } int getMonth() { return month; } int getDay() { return day; } static bool isValidDate(std::string str) { if (str.length() != 10 || str[4] != '-' || str[7] != '-') { return false; } if (!isdigit(str[0]) || !isdigit(str[1]) || !isdigit(str[2]) || !isdigit(str[3])) { return false; } if (!isdigit(str[5]) || !isdigit(str[6])) { return false; } if (!isdigit(str[8]) || !isdigit(str[9])) { return false; } int year = atoi(&str[0]); int month = atoi(&str[5]); int day = atoi(&str[8]); if (year <= 0 || month <= 0 || day <= 0) { return false; } if (month > 12) { return false; } switch (month) { case 2: if (day > 29) { return false; } if (!isLeapYear(year) && day == 29) { return false; } break; case 1: case 3: case 5: case 7: case 8: case 10: case 12: if (day > 31) { return false; } break; default: if (day > 30) { return false; } break; } return true; } static bool isLeapYear(int year) { if (year > 1582) { return ((year % 4 == 0) && (year % 100 > 0)) || (year % 400 == 0); } if (year > 10) { return year % 4 == 0; } return false; } friend std::ostream &operator<<(std::ostream &, Date &); }; std::ostream &operator<<(std::ostream &os, Date &d) { os << std::setfill('0') << std::setw(4) << d.year << '-'; os << std::setfill('0') << std::setw(2) << d.month << '-'; os << std::setfill('0') << std::setw(2) << d.day; return os; } int diffDays(Date date1, Date date2) { int d1m = (date1.getMonth() + 9) % 12; int d1y = date1.getYear() - d1m / 10; int d2m = (date2.getMonth() + 9) % 12; int d2y = date2.getYear() - d2m / 10; int days1 = 365 * d1y + d1y / 4 - d1y / 100 + d1y / 400 + (d1m * 306 + 5) / 10 + (date1.getDay() - 1); int days2 = 365 * d2y + d2y / 4 - d2y / 100 + d2y / 400 + (d2m * 306 + 5) / 10 + (date2.getDay() - 1); return days2 - days1; } int main() { std::string ds1 = "2019-01-01"; std::string ds2 = "2019-12-02"; if (Date::isValidDate(ds1) && Date::isValidDate(ds2)) { Date d1(ds1); Date d2(ds2); std::cout << "Days difference : " << diffDays(d1, d2); } else { std::cout << "Dates are invalid.\n"; } return 0; }
Write the same algorithm in Java as shown in this AutoHotKey implementation.
db = ( 1995-11-21|1995-11-21 2019-01-01|2019-01-02 2019-01-02|2019-01-01 2019-01-01|2019-03-01 2020-01-01|2020-03-01 1902-01-01|1968-12-25 2090-01-01|2098-12-25 1902-01-01|2098-12-25 ) for i, line in StrSplit(db, "`n", "`r"){ D := StrSplit(line, "|") result .= "Days between " D.1 " and " D.2 "  : " Days_between(D.1, D.2) " Day(s)`n" } MsgBox, 262144, , % result return Days_between(D1, D2){ D1 := RegExReplace(D1, "\D") D2 := RegExReplace(D2, "\D") EnvSub, D2, % D1, days return D2 }
import java.time.LocalDate; import java.time.temporal.ChronoUnit; public class DaysBetweenDates { public static void main(String[] args) { LocalDate fromDate = LocalDate.parse("2019-01-01"); LocalDate toDate = LocalDate.parse("2019-10-19"); long diff = ChronoUnit.DAYS.between(fromDate, toDate); System.out.printf("Number of days between %s and %s: %d\n", fromDate, toDate, diff); } }
Convert this AutoHotKey block to Java, preserving its control flow and logic.
db = ( 1995-11-21|1995-11-21 2019-01-01|2019-01-02 2019-01-02|2019-01-01 2019-01-01|2019-03-01 2020-01-01|2020-03-01 1902-01-01|1968-12-25 2090-01-01|2098-12-25 1902-01-01|2098-12-25 ) for i, line in StrSplit(db, "`n", "`r"){ D := StrSplit(line, "|") result .= "Days between " D.1 " and " D.2 "  : " Days_between(D.1, D.2) " Day(s)`n" } MsgBox, 262144, , % result return Days_between(D1, D2){ D1 := RegExReplace(D1, "\D") D2 := RegExReplace(D2, "\D") EnvSub, D2, % D1, days return D2 }
import java.time.LocalDate; import java.time.temporal.ChronoUnit; public class DaysBetweenDates { public static void main(String[] args) { LocalDate fromDate = LocalDate.parse("2019-01-01"); LocalDate toDate = LocalDate.parse("2019-10-19"); long diff = ChronoUnit.DAYS.between(fromDate, toDate); System.out.printf("Number of days between %s and %s: %d\n", fromDate, toDate, diff); } }
Keep all operations the same but rewrite the snippet in Python.
db = ( 1995-11-21|1995-11-21 2019-01-01|2019-01-02 2019-01-02|2019-01-01 2019-01-01|2019-03-01 2020-01-01|2020-03-01 1902-01-01|1968-12-25 2090-01-01|2098-12-25 1902-01-01|2098-12-25 ) for i, line in StrSplit(db, "`n", "`r"){ D := StrSplit(line, "|") result .= "Days between " D.1 " and " D.2 "  : " Days_between(D.1, D.2) " Day(s)`n" } MsgBox, 262144, , % result return Days_between(D1, D2){ D1 := RegExReplace(D1, "\D") D2 := RegExReplace(D2, "\D") EnvSub, D2, % D1, days return D2 }
import sys def days( y,m,d ): m = (m + 9) % 12 y = y - m/10 result = 365*y + y/4 - y/100 + y/400 + (m*306 + 5)/10 + ( d - 1 ) return result def diff(one,two): [y1,m1,d1] = one.split('-') [y2,m2,d2] = two.split('-') year2 = days( int(y2),int(m2),int(d2)) year1 = days( int(y1), int(m1), int(d1) ) return year2 - year1 if __name__ == "__main__": one = sys.argv[1] two = sys.argv[2] print diff(one,two)
Please provide an equivalent version of this AutoHotKey code in Python.
db = ( 1995-11-21|1995-11-21 2019-01-01|2019-01-02 2019-01-02|2019-01-01 2019-01-01|2019-03-01 2020-01-01|2020-03-01 1902-01-01|1968-12-25 2090-01-01|2098-12-25 1902-01-01|2098-12-25 ) for i, line in StrSplit(db, "`n", "`r"){ D := StrSplit(line, "|") result .= "Days between " D.1 " and " D.2 "  : " Days_between(D.1, D.2) " Day(s)`n" } MsgBox, 262144, , % result return Days_between(D1, D2){ D1 := RegExReplace(D1, "\D") D2 := RegExReplace(D2, "\D") EnvSub, D2, % D1, days return D2 }
import sys def days( y,m,d ): m = (m + 9) % 12 y = y - m/10 result = 365*y + y/4 - y/100 + y/400 + (m*306 + 5)/10 + ( d - 1 ) return result def diff(one,two): [y1,m1,d1] = one.split('-') [y2,m2,d2] = two.split('-') year2 = days( int(y2),int(m2),int(d2)) year1 = days( int(y1), int(m1), int(d1) ) return year2 - year1 if __name__ == "__main__": one = sys.argv[1] two = sys.argv[2] print diff(one,two)
Convert this AutoHotKey snippet to VB and keep its semantics consistent.
db = ( 1995-11-21|1995-11-21 2019-01-01|2019-01-02 2019-01-02|2019-01-01 2019-01-01|2019-03-01 2020-01-01|2020-03-01 1902-01-01|1968-12-25 2090-01-01|2098-12-25 1902-01-01|2098-12-25 ) for i, line in StrSplit(db, "`n", "`r"){ D := StrSplit(line, "|") result .= "Days between " D.1 " and " D.2 "  : " Days_between(D.1, D.2) " Day(s)`n" } MsgBox, 262144, , % result return Days_between(D1, D2){ D1 := RegExReplace(D1, "\D") D2 := RegExReplace(D2, "\D") EnvSub, D2, % D1, days return D2 }
Imports System.Globalization Module Module1 Function DateDiff(d1 As String, d2 As String) As Integer Dim a = DateTime.ParseExact(d1, "yyyy-MM-dd", CultureInfo.InvariantCulture) Dim b = DateTime.ParseExact(d2, "yyyy-MM-dd", CultureInfo.InvariantCulture) Return (b - a).TotalDays End Function Sub Main() Console.WriteLine(DateDiff("1970-01-01", "2019-10-18")) End Sub End Module
Port the following code from AutoHotKey to VB with equivalent syntax and logic.
db = ( 1995-11-21|1995-11-21 2019-01-01|2019-01-02 2019-01-02|2019-01-01 2019-01-01|2019-03-01 2020-01-01|2020-03-01 1902-01-01|1968-12-25 2090-01-01|2098-12-25 1902-01-01|2098-12-25 ) for i, line in StrSplit(db, "`n", "`r"){ D := StrSplit(line, "|") result .= "Days between " D.1 " and " D.2 "  : " Days_between(D.1, D.2) " Day(s)`n" } MsgBox, 262144, , % result return Days_between(D1, D2){ D1 := RegExReplace(D1, "\D") D2 := RegExReplace(D2, "\D") EnvSub, D2, % D1, days return D2 }
Imports System.Globalization Module Module1 Function DateDiff(d1 As String, d2 As String) As Integer Dim a = DateTime.ParseExact(d1, "yyyy-MM-dd", CultureInfo.InvariantCulture) Dim b = DateTime.ParseExact(d2, "yyyy-MM-dd", CultureInfo.InvariantCulture) Return (b - a).TotalDays End Function Sub Main() Console.WriteLine(DateDiff("1970-01-01", "2019-10-18")) End Sub End Module
Ensure the translated Go code behaves exactly like the original AutoHotKey snippet.
db = ( 1995-11-21|1995-11-21 2019-01-01|2019-01-02 2019-01-02|2019-01-01 2019-01-01|2019-03-01 2020-01-01|2020-03-01 1902-01-01|1968-12-25 2090-01-01|2098-12-25 1902-01-01|2098-12-25 ) for i, line in StrSplit(db, "`n", "`r"){ D := StrSplit(line, "|") result .= "Days between " D.1 " and " D.2 "  : " Days_between(D.1, D.2) " Day(s)`n" } MsgBox, 262144, , % result return Days_between(D1, D2){ D1 := RegExReplace(D1, "\D") D2 := RegExReplace(D2, "\D") EnvSub, D2, % D1, days return D2 }
package main import ( "fmt" "log" "time" ) const layout = "2006-01-02" func daysBetween(date1, date2 string) int { t1, err := time.Parse(layout, date1) check(err) t2, err := time.Parse(layout, date2) check(err) days := int(t1.Sub(t2).Hours() / 24) if days < 0 { days = -days } return days } func check(err error) { if err != nil { log.Fatal(err) } } func main() { date1, date2 := "2019-01-01", "2019-09-30" days := daysBetween(date1, date2) fmt.Printf("There are %d days between %s and %s\n", days, date1, date2) date1, date2 = "2015-12-31", "2016-09-30" days = daysBetween(date1, date2) fmt.Printf("There are %d days between %s and %s\n", days, date1, date2) }
Translate the given AutoHotKey code snippet into Go without altering its behavior.
db = ( 1995-11-21|1995-11-21 2019-01-01|2019-01-02 2019-01-02|2019-01-01 2019-01-01|2019-03-01 2020-01-01|2020-03-01 1902-01-01|1968-12-25 2090-01-01|2098-12-25 1902-01-01|2098-12-25 ) for i, line in StrSplit(db, "`n", "`r"){ D := StrSplit(line, "|") result .= "Days between " D.1 " and " D.2 "  : " Days_between(D.1, D.2) " Day(s)`n" } MsgBox, 262144, , % result return Days_between(D1, D2){ D1 := RegExReplace(D1, "\D") D2 := RegExReplace(D2, "\D") EnvSub, D2, % D1, days return D2 }
package main import ( "fmt" "log" "time" ) const layout = "2006-01-02" func daysBetween(date1, date2 string) int { t1, err := time.Parse(layout, date1) check(err) t2, err := time.Parse(layout, date2) check(err) days := int(t1.Sub(t2).Hours() / 24) if days < 0 { days = -days } return days } func check(err error) { if err != nil { log.Fatal(err) } } func main() { date1, date2 := "2019-01-01", "2019-09-30" days := daysBetween(date1, date2) fmt.Printf("There are %d days between %s and %s\n", days, date1, date2) date1, date2 = "2015-12-31", "2016-09-30" days = daysBetween(date1, date2) fmt.Printf("There are %d days between %s and %s\n", days, date1, date2) }
Convert this AWK block to C, preserving its control flow and logic.
BEGIN { regexp = "^....-..-..$" main("1969-12-31","1970-01-01","builtin has bad POSIX start date") main("1970-01-01","2038-01-19","builtin has bad POSIX stop date") main("1970-01-01","2019-10-02","format OK") main("1970-01-01","2019/10/02","format NG") main("1995-11-21","1995-11-21","identical dates") main("2019-01-01","2019-01-02","positive date") main("2019-01-02","2019-01-01","negative date") main("2019-01-01","2019-03-01","non-leap year") main("2020-01-01","2020-03-01","leap year") exit(0) } function main(date1,date2,comment, d1,d2,diff) { printf("\t%s\n",comment) d1 = days_builtin(date1) d2 = days_builtin(date2) diff = (d1 == "" || d2 == "") ? "error" : d2-d1 printf("builtin %10s to %10s = %s\n",date1,date2,diff) d1 = days_generic(date1) d2 = days_generic(date2) diff = (d1 == "" || d2 == "") ? "error" : d2-d1 printf("generic %10s to %10s = %s\n",date1,date2,diff) } function days_builtin(ymd) { if (ymd !~ regexp) { return("") } if (ymd < "1970-01-01" || ymd > "2038-01-18") { return("") } gsub(/-/," ",ymd) return(int(mktime(sprintf("%s 0 0 0",ymd)) / (60*60*24))) } function days_generic(ymd, d,m,y,result) { if (ymd !~ regexp) { return("") } y = substr(ymd,1,4) m = substr(ymd,6,2) d = substr(ymd,9,2) m = (m + 9) % 12 y = int(y - int(m/10)) result = 365*y + int(y/4) - int(y/100) + int(y/400) + int((m*306+5)/10) + (d-1) return(result) }
#include<stdbool.h> #include<string.h> #include<stdio.h> typedef struct{ int year, month, day; }date; date extractDate(char* str){ return (date){.year = 1000 * (str[0]-'0') + 100 * (str[1]-'0') + 10 * (str[2]-'0') + (str[3]-'0'), .month = 10*(str[5]-'0') + (str[6]-'0'), .day = 10*(str[8]-'0') + (str[9]-'0')}; } bool isValidDate(char* str){ date newDate; if(strlen(str)!=10 && str[4]!='-' && str[7]!='-'){ return false; } newDate = extractDate(str); if(newDate.year<=0 || newDate.month<=0 || newDate.day<=0 || newDate.month>12 || (newDate.month==2 && newDate.day>29) || ((newDate.month==1 || newDate.month==3 || newDate.month==5 || newDate.month==7 || newDate.month==8 || newDate.month==10 || newDate.month==12) && newDate.day>31) || newDate.day>30 || (newDate.year%4==0 && newDate.month==2 && newDate.month>28)){ return false; } return true; } int diffDays(date date1,date date2){ int days1, days2; date1.month = (date1.month + 9)%12; date1.year = date1.year - date1.month/10; date2.month = (date2.month + 9)%12; date2.year = date2.year - date2.month/10; days1 = 365*date1.year + date1.year/4 - date1.year/100 + date1.year/400 + (date1.month*306 + 5)/10 + ( date1.day - 1 ); days2 = 365*date2.year + date2.year/4 - date2.year/100 + date2.year/400 + (date2.month*306 + 5)/10 + ( date2.day - 1 ); return days2 - days1; } int main(int argc,char** argv) { if(argc!=3){ return printf("Usage : %s <yyyy-mm-dd> <yyyy-mm-dd>",argv[0]); } if(isValidDate(argv[1])&&isValidDate(argv[2]) == false){ return printf("Dates are invalid.\n"); } printf("Days Difference : %d\n", diffDays(extractDate(argv[1]),extractDate(argv[2]))); return 0; }
Please provide an equivalent version of this AWK code in C.
BEGIN { regexp = "^....-..-..$" main("1969-12-31","1970-01-01","builtin has bad POSIX start date") main("1970-01-01","2038-01-19","builtin has bad POSIX stop date") main("1970-01-01","2019-10-02","format OK") main("1970-01-01","2019/10/02","format NG") main("1995-11-21","1995-11-21","identical dates") main("2019-01-01","2019-01-02","positive date") main("2019-01-02","2019-01-01","negative date") main("2019-01-01","2019-03-01","non-leap year") main("2020-01-01","2020-03-01","leap year") exit(0) } function main(date1,date2,comment, d1,d2,diff) { printf("\t%s\n",comment) d1 = days_builtin(date1) d2 = days_builtin(date2) diff = (d1 == "" || d2 == "") ? "error" : d2-d1 printf("builtin %10s to %10s = %s\n",date1,date2,diff) d1 = days_generic(date1) d2 = days_generic(date2) diff = (d1 == "" || d2 == "") ? "error" : d2-d1 printf("generic %10s to %10s = %s\n",date1,date2,diff) } function days_builtin(ymd) { if (ymd !~ regexp) { return("") } if (ymd < "1970-01-01" || ymd > "2038-01-18") { return("") } gsub(/-/," ",ymd) return(int(mktime(sprintf("%s 0 0 0",ymd)) / (60*60*24))) } function days_generic(ymd, d,m,y,result) { if (ymd !~ regexp) { return("") } y = substr(ymd,1,4) m = substr(ymd,6,2) d = substr(ymd,9,2) m = (m + 9) % 12 y = int(y - int(m/10)) result = 365*y + int(y/4) - int(y/100) + int(y/400) + int((m*306+5)/10) + (d-1) return(result) }
#include<stdbool.h> #include<string.h> #include<stdio.h> typedef struct{ int year, month, day; }date; date extractDate(char* str){ return (date){.year = 1000 * (str[0]-'0') + 100 * (str[1]-'0') + 10 * (str[2]-'0') + (str[3]-'0'), .month = 10*(str[5]-'0') + (str[6]-'0'), .day = 10*(str[8]-'0') + (str[9]-'0')}; } bool isValidDate(char* str){ date newDate; if(strlen(str)!=10 && str[4]!='-' && str[7]!='-'){ return false; } newDate = extractDate(str); if(newDate.year<=0 || newDate.month<=0 || newDate.day<=0 || newDate.month>12 || (newDate.month==2 && newDate.day>29) || ((newDate.month==1 || newDate.month==3 || newDate.month==5 || newDate.month==7 || newDate.month==8 || newDate.month==10 || newDate.month==12) && newDate.day>31) || newDate.day>30 || (newDate.year%4==0 && newDate.month==2 && newDate.month>28)){ return false; } return true; } int diffDays(date date1,date date2){ int days1, days2; date1.month = (date1.month + 9)%12; date1.year = date1.year - date1.month/10; date2.month = (date2.month + 9)%12; date2.year = date2.year - date2.month/10; days1 = 365*date1.year + date1.year/4 - date1.year/100 + date1.year/400 + (date1.month*306 + 5)/10 + ( date1.day - 1 ); days2 = 365*date2.year + date2.year/4 - date2.year/100 + date2.year/400 + (date2.month*306 + 5)/10 + ( date2.day - 1 ); return days2 - days1; } int main(int argc,char** argv) { if(argc!=3){ return printf("Usage : %s <yyyy-mm-dd> <yyyy-mm-dd>",argv[0]); } if(isValidDate(argv[1])&&isValidDate(argv[2]) == false){ return printf("Dates are invalid.\n"); } printf("Days Difference : %d\n", diffDays(extractDate(argv[1]),extractDate(argv[2]))); return 0; }
Write the same code in C# as shown below in AWK.
BEGIN { regexp = "^....-..-..$" main("1969-12-31","1970-01-01","builtin has bad POSIX start date") main("1970-01-01","2038-01-19","builtin has bad POSIX stop date") main("1970-01-01","2019-10-02","format OK") main("1970-01-01","2019/10/02","format NG") main("1995-11-21","1995-11-21","identical dates") main("2019-01-01","2019-01-02","positive date") main("2019-01-02","2019-01-01","negative date") main("2019-01-01","2019-03-01","non-leap year") main("2020-01-01","2020-03-01","leap year") exit(0) } function main(date1,date2,comment, d1,d2,diff) { printf("\t%s\n",comment) d1 = days_builtin(date1) d2 = days_builtin(date2) diff = (d1 == "" || d2 == "") ? "error" : d2-d1 printf("builtin %10s to %10s = %s\n",date1,date2,diff) d1 = days_generic(date1) d2 = days_generic(date2) diff = (d1 == "" || d2 == "") ? "error" : d2-d1 printf("generic %10s to %10s = %s\n",date1,date2,diff) } function days_builtin(ymd) { if (ymd !~ regexp) { return("") } if (ymd < "1970-01-01" || ymd > "2038-01-18") { return("") } gsub(/-/," ",ymd) return(int(mktime(sprintf("%s 0 0 0",ymd)) / (60*60*24))) } function days_generic(ymd, d,m,y,result) { if (ymd !~ regexp) { return("") } y = substr(ymd,1,4) m = substr(ymd,6,2) d = substr(ymd,9,2) m = (m + 9) % 12 y = int(y - int(m/10)) result = 365*y + int(y/4) - int(y/100) + int(y/400) + int((m*306+5)/10) + (d-1) return(result) }
using System; using System.Globalization; public class Program { public static void Main() => WriteLine(DateDiff("1970-01-01", "2019-10-18")); public static int DateDiff(string d1, string d2) { var a = DateTime.ParseExact(d1, "yyyy-MM-dd", CultureInfo.InvariantCulture); var b = DateTime.ParseExact(d2, "yyyy-MM-dd", CultureInfo.InvariantCulture); return (int)(b - a).TotalDays; } }
Write the same algorithm in C# as shown in this AWK implementation.
BEGIN { regexp = "^....-..-..$" main("1969-12-31","1970-01-01","builtin has bad POSIX start date") main("1970-01-01","2038-01-19","builtin has bad POSIX stop date") main("1970-01-01","2019-10-02","format OK") main("1970-01-01","2019/10/02","format NG") main("1995-11-21","1995-11-21","identical dates") main("2019-01-01","2019-01-02","positive date") main("2019-01-02","2019-01-01","negative date") main("2019-01-01","2019-03-01","non-leap year") main("2020-01-01","2020-03-01","leap year") exit(0) } function main(date1,date2,comment, d1,d2,diff) { printf("\t%s\n",comment) d1 = days_builtin(date1) d2 = days_builtin(date2) diff = (d1 == "" || d2 == "") ? "error" : d2-d1 printf("builtin %10s to %10s = %s\n",date1,date2,diff) d1 = days_generic(date1) d2 = days_generic(date2) diff = (d1 == "" || d2 == "") ? "error" : d2-d1 printf("generic %10s to %10s = %s\n",date1,date2,diff) } function days_builtin(ymd) { if (ymd !~ regexp) { return("") } if (ymd < "1970-01-01" || ymd > "2038-01-18") { return("") } gsub(/-/," ",ymd) return(int(mktime(sprintf("%s 0 0 0",ymd)) / (60*60*24))) } function days_generic(ymd, d,m,y,result) { if (ymd !~ regexp) { return("") } y = substr(ymd,1,4) m = substr(ymd,6,2) d = substr(ymd,9,2) m = (m + 9) % 12 y = int(y - int(m/10)) result = 365*y + int(y/4) - int(y/100) + int(y/400) + int((m*306+5)/10) + (d-1) return(result) }
using System; using System.Globalization; public class Program { public static void Main() => WriteLine(DateDiff("1970-01-01", "2019-10-18")); public static int DateDiff(string d1, string d2) { var a = DateTime.ParseExact(d1, "yyyy-MM-dd", CultureInfo.InvariantCulture); var b = DateTime.ParseExact(d2, "yyyy-MM-dd", CultureInfo.InvariantCulture); return (int)(b - a).TotalDays; } }
Ensure the translated C++ code behaves exactly like the original AWK snippet.
BEGIN { regexp = "^....-..-..$" main("1969-12-31","1970-01-01","builtin has bad POSIX start date") main("1970-01-01","2038-01-19","builtin has bad POSIX stop date") main("1970-01-01","2019-10-02","format OK") main("1970-01-01","2019/10/02","format NG") main("1995-11-21","1995-11-21","identical dates") main("2019-01-01","2019-01-02","positive date") main("2019-01-02","2019-01-01","negative date") main("2019-01-01","2019-03-01","non-leap year") main("2020-01-01","2020-03-01","leap year") exit(0) } function main(date1,date2,comment, d1,d2,diff) { printf("\t%s\n",comment) d1 = days_builtin(date1) d2 = days_builtin(date2) diff = (d1 == "" || d2 == "") ? "error" : d2-d1 printf("builtin %10s to %10s = %s\n",date1,date2,diff) d1 = days_generic(date1) d2 = days_generic(date2) diff = (d1 == "" || d2 == "") ? "error" : d2-d1 printf("generic %10s to %10s = %s\n",date1,date2,diff) } function days_builtin(ymd) { if (ymd !~ regexp) { return("") } if (ymd < "1970-01-01" || ymd > "2038-01-18") { return("") } gsub(/-/," ",ymd) return(int(mktime(sprintf("%s 0 0 0",ymd)) / (60*60*24))) } function days_generic(ymd, d,m,y,result) { if (ymd !~ regexp) { return("") } y = substr(ymd,1,4) m = substr(ymd,6,2) d = substr(ymd,9,2) m = (m + 9) % 12 y = int(y - int(m/10)) result = 365*y + int(y/4) - int(y/100) + int(y/400) + int((m*306+5)/10) + (d-1) return(result) }
#include <iomanip> #include <iostream> class Date { private: int year, month, day; public: Date(std::string str) { if (isValidDate(str)) { year = atoi(&str[0]); month = atoi(&str[5]); day = atoi(&str[8]); } else { throw std::exception("Invalid date"); } } int getYear() { return year; } int getMonth() { return month; } int getDay() { return day; } static bool isValidDate(std::string str) { if (str.length() != 10 || str[4] != '-' || str[7] != '-') { return false; } if (!isdigit(str[0]) || !isdigit(str[1]) || !isdigit(str[2]) || !isdigit(str[3])) { return false; } if (!isdigit(str[5]) || !isdigit(str[6])) { return false; } if (!isdigit(str[8]) || !isdigit(str[9])) { return false; } int year = atoi(&str[0]); int month = atoi(&str[5]); int day = atoi(&str[8]); if (year <= 0 || month <= 0 || day <= 0) { return false; } if (month > 12) { return false; } switch (month) { case 2: if (day > 29) { return false; } if (!isLeapYear(year) && day == 29) { return false; } break; case 1: case 3: case 5: case 7: case 8: case 10: case 12: if (day > 31) { return false; } break; default: if (day > 30) { return false; } break; } return true; } static bool isLeapYear(int year) { if (year > 1582) { return ((year % 4 == 0) && (year % 100 > 0)) || (year % 400 == 0); } if (year > 10) { return year % 4 == 0; } return false; } friend std::ostream &operator<<(std::ostream &, Date &); }; std::ostream &operator<<(std::ostream &os, Date &d) { os << std::setfill('0') << std::setw(4) << d.year << '-'; os << std::setfill('0') << std::setw(2) << d.month << '-'; os << std::setfill('0') << std::setw(2) << d.day; return os; } int diffDays(Date date1, Date date2) { int d1m = (date1.getMonth() + 9) % 12; int d1y = date1.getYear() - d1m / 10; int d2m = (date2.getMonth() + 9) % 12; int d2y = date2.getYear() - d2m / 10; int days1 = 365 * d1y + d1y / 4 - d1y / 100 + d1y / 400 + (d1m * 306 + 5) / 10 + (date1.getDay() - 1); int days2 = 365 * d2y + d2y / 4 - d2y / 100 + d2y / 400 + (d2m * 306 + 5) / 10 + (date2.getDay() - 1); return days2 - days1; } int main() { std::string ds1 = "2019-01-01"; std::string ds2 = "2019-12-02"; if (Date::isValidDate(ds1) && Date::isValidDate(ds2)) { Date d1(ds1); Date d2(ds2); std::cout << "Days difference : " << diffDays(d1, d2); } else { std::cout << "Dates are invalid.\n"; } return 0; }
Convert this AWK block to C++, preserving its control flow and logic.
BEGIN { regexp = "^....-..-..$" main("1969-12-31","1970-01-01","builtin has bad POSIX start date") main("1970-01-01","2038-01-19","builtin has bad POSIX stop date") main("1970-01-01","2019-10-02","format OK") main("1970-01-01","2019/10/02","format NG") main("1995-11-21","1995-11-21","identical dates") main("2019-01-01","2019-01-02","positive date") main("2019-01-02","2019-01-01","negative date") main("2019-01-01","2019-03-01","non-leap year") main("2020-01-01","2020-03-01","leap year") exit(0) } function main(date1,date2,comment, d1,d2,diff) { printf("\t%s\n",comment) d1 = days_builtin(date1) d2 = days_builtin(date2) diff = (d1 == "" || d2 == "") ? "error" : d2-d1 printf("builtin %10s to %10s = %s\n",date1,date2,diff) d1 = days_generic(date1) d2 = days_generic(date2) diff = (d1 == "" || d2 == "") ? "error" : d2-d1 printf("generic %10s to %10s = %s\n",date1,date2,diff) } function days_builtin(ymd) { if (ymd !~ regexp) { return("") } if (ymd < "1970-01-01" || ymd > "2038-01-18") { return("") } gsub(/-/," ",ymd) return(int(mktime(sprintf("%s 0 0 0",ymd)) / (60*60*24))) } function days_generic(ymd, d,m,y,result) { if (ymd !~ regexp) { return("") } y = substr(ymd,1,4) m = substr(ymd,6,2) d = substr(ymd,9,2) m = (m + 9) % 12 y = int(y - int(m/10)) result = 365*y + int(y/4) - int(y/100) + int(y/400) + int((m*306+5)/10) + (d-1) return(result) }
#include <iomanip> #include <iostream> class Date { private: int year, month, day; public: Date(std::string str) { if (isValidDate(str)) { year = atoi(&str[0]); month = atoi(&str[5]); day = atoi(&str[8]); } else { throw std::exception("Invalid date"); } } int getYear() { return year; } int getMonth() { return month; } int getDay() { return day; } static bool isValidDate(std::string str) { if (str.length() != 10 || str[4] != '-' || str[7] != '-') { return false; } if (!isdigit(str[0]) || !isdigit(str[1]) || !isdigit(str[2]) || !isdigit(str[3])) { return false; } if (!isdigit(str[5]) || !isdigit(str[6])) { return false; } if (!isdigit(str[8]) || !isdigit(str[9])) { return false; } int year = atoi(&str[0]); int month = atoi(&str[5]); int day = atoi(&str[8]); if (year <= 0 || month <= 0 || day <= 0) { return false; } if (month > 12) { return false; } switch (month) { case 2: if (day > 29) { return false; } if (!isLeapYear(year) && day == 29) { return false; } break; case 1: case 3: case 5: case 7: case 8: case 10: case 12: if (day > 31) { return false; } break; default: if (day > 30) { return false; } break; } return true; } static bool isLeapYear(int year) { if (year > 1582) { return ((year % 4 == 0) && (year % 100 > 0)) || (year % 400 == 0); } if (year > 10) { return year % 4 == 0; } return false; } friend std::ostream &operator<<(std::ostream &, Date &); }; std::ostream &operator<<(std::ostream &os, Date &d) { os << std::setfill('0') << std::setw(4) << d.year << '-'; os << std::setfill('0') << std::setw(2) << d.month << '-'; os << std::setfill('0') << std::setw(2) << d.day; return os; } int diffDays(Date date1, Date date2) { int d1m = (date1.getMonth() + 9) % 12; int d1y = date1.getYear() - d1m / 10; int d2m = (date2.getMonth() + 9) % 12; int d2y = date2.getYear() - d2m / 10; int days1 = 365 * d1y + d1y / 4 - d1y / 100 + d1y / 400 + (d1m * 306 + 5) / 10 + (date1.getDay() - 1); int days2 = 365 * d2y + d2y / 4 - d2y / 100 + d2y / 400 + (d2m * 306 + 5) / 10 + (date2.getDay() - 1); return days2 - days1; } int main() { std::string ds1 = "2019-01-01"; std::string ds2 = "2019-12-02"; if (Date::isValidDate(ds1) && Date::isValidDate(ds2)) { Date d1(ds1); Date d2(ds2); std::cout << "Days difference : " << diffDays(d1, d2); } else { std::cout << "Dates are invalid.\n"; } return 0; }
Can you help me rewrite this code in Java instead of AWK, keeping it the same logically?
BEGIN { regexp = "^....-..-..$" main("1969-12-31","1970-01-01","builtin has bad POSIX start date") main("1970-01-01","2038-01-19","builtin has bad POSIX stop date") main("1970-01-01","2019-10-02","format OK") main("1970-01-01","2019/10/02","format NG") main("1995-11-21","1995-11-21","identical dates") main("2019-01-01","2019-01-02","positive date") main("2019-01-02","2019-01-01","negative date") main("2019-01-01","2019-03-01","non-leap year") main("2020-01-01","2020-03-01","leap year") exit(0) } function main(date1,date2,comment, d1,d2,diff) { printf("\t%s\n",comment) d1 = days_builtin(date1) d2 = days_builtin(date2) diff = (d1 == "" || d2 == "") ? "error" : d2-d1 printf("builtin %10s to %10s = %s\n",date1,date2,diff) d1 = days_generic(date1) d2 = days_generic(date2) diff = (d1 == "" || d2 == "") ? "error" : d2-d1 printf("generic %10s to %10s = %s\n",date1,date2,diff) } function days_builtin(ymd) { if (ymd !~ regexp) { return("") } if (ymd < "1970-01-01" || ymd > "2038-01-18") { return("") } gsub(/-/," ",ymd) return(int(mktime(sprintf("%s 0 0 0",ymd)) / (60*60*24))) } function days_generic(ymd, d,m,y,result) { if (ymd !~ regexp) { return("") } y = substr(ymd,1,4) m = substr(ymd,6,2) d = substr(ymd,9,2) m = (m + 9) % 12 y = int(y - int(m/10)) result = 365*y + int(y/4) - int(y/100) + int(y/400) + int((m*306+5)/10) + (d-1) return(result) }
import java.time.LocalDate; import java.time.temporal.ChronoUnit; public class DaysBetweenDates { public static void main(String[] args) { LocalDate fromDate = LocalDate.parse("2019-01-01"); LocalDate toDate = LocalDate.parse("2019-10-19"); long diff = ChronoUnit.DAYS.between(fromDate, toDate); System.out.printf("Number of days between %s and %s: %d\n", fromDate, toDate, diff); } }
Change the programming language of this snippet from AWK to Python without modifying what it does.
BEGIN { regexp = "^....-..-..$" main("1969-12-31","1970-01-01","builtin has bad POSIX start date") main("1970-01-01","2038-01-19","builtin has bad POSIX stop date") main("1970-01-01","2019-10-02","format OK") main("1970-01-01","2019/10/02","format NG") main("1995-11-21","1995-11-21","identical dates") main("2019-01-01","2019-01-02","positive date") main("2019-01-02","2019-01-01","negative date") main("2019-01-01","2019-03-01","non-leap year") main("2020-01-01","2020-03-01","leap year") exit(0) } function main(date1,date2,comment, d1,d2,diff) { printf("\t%s\n",comment) d1 = days_builtin(date1) d2 = days_builtin(date2) diff = (d1 == "" || d2 == "") ? "error" : d2-d1 printf("builtin %10s to %10s = %s\n",date1,date2,diff) d1 = days_generic(date1) d2 = days_generic(date2) diff = (d1 == "" || d2 == "") ? "error" : d2-d1 printf("generic %10s to %10s = %s\n",date1,date2,diff) } function days_builtin(ymd) { if (ymd !~ regexp) { return("") } if (ymd < "1970-01-01" || ymd > "2038-01-18") { return("") } gsub(/-/," ",ymd) return(int(mktime(sprintf("%s 0 0 0",ymd)) / (60*60*24))) } function days_generic(ymd, d,m,y,result) { if (ymd !~ regexp) { return("") } y = substr(ymd,1,4) m = substr(ymd,6,2) d = substr(ymd,9,2) m = (m + 9) % 12 y = int(y - int(m/10)) result = 365*y + int(y/4) - int(y/100) + int(y/400) + int((m*306+5)/10) + (d-1) return(result) }
import sys def days( y,m,d ): m = (m + 9) % 12 y = y - m/10 result = 365*y + y/4 - y/100 + y/400 + (m*306 + 5)/10 + ( d - 1 ) return result def diff(one,two): [y1,m1,d1] = one.split('-') [y2,m2,d2] = two.split('-') year2 = days( int(y2),int(m2),int(d2)) year1 = days( int(y1), int(m1), int(d1) ) return year2 - year1 if __name__ == "__main__": one = sys.argv[1] two = sys.argv[2] print diff(one,two)
Write a version of this AWK function in VB with identical behavior.
BEGIN { regexp = "^....-..-..$" main("1969-12-31","1970-01-01","builtin has bad POSIX start date") main("1970-01-01","2038-01-19","builtin has bad POSIX stop date") main("1970-01-01","2019-10-02","format OK") main("1970-01-01","2019/10/02","format NG") main("1995-11-21","1995-11-21","identical dates") main("2019-01-01","2019-01-02","positive date") main("2019-01-02","2019-01-01","negative date") main("2019-01-01","2019-03-01","non-leap year") main("2020-01-01","2020-03-01","leap year") exit(0) } function main(date1,date2,comment, d1,d2,diff) { printf("\t%s\n",comment) d1 = days_builtin(date1) d2 = days_builtin(date2) diff = (d1 == "" || d2 == "") ? "error" : d2-d1 printf("builtin %10s to %10s = %s\n",date1,date2,diff) d1 = days_generic(date1) d2 = days_generic(date2) diff = (d1 == "" || d2 == "") ? "error" : d2-d1 printf("generic %10s to %10s = %s\n",date1,date2,diff) } function days_builtin(ymd) { if (ymd !~ regexp) { return("") } if (ymd < "1970-01-01" || ymd > "2038-01-18") { return("") } gsub(/-/," ",ymd) return(int(mktime(sprintf("%s 0 0 0",ymd)) / (60*60*24))) } function days_generic(ymd, d,m,y,result) { if (ymd !~ regexp) { return("") } y = substr(ymd,1,4) m = substr(ymd,6,2) d = substr(ymd,9,2) m = (m + 9) % 12 y = int(y - int(m/10)) result = 365*y + int(y/4) - int(y/100) + int(y/400) + int((m*306+5)/10) + (d-1) return(result) }
Imports System.Globalization Module Module1 Function DateDiff(d1 As String, d2 As String) As Integer Dim a = DateTime.ParseExact(d1, "yyyy-MM-dd", CultureInfo.InvariantCulture) Dim b = DateTime.ParseExact(d2, "yyyy-MM-dd", CultureInfo.InvariantCulture) Return (b - a).TotalDays End Function Sub Main() Console.WriteLine(DateDiff("1970-01-01", "2019-10-18")) End Sub End Module
Write the same algorithm in VB as shown in this AWK implementation.
BEGIN { regexp = "^....-..-..$" main("1969-12-31","1970-01-01","builtin has bad POSIX start date") main("1970-01-01","2038-01-19","builtin has bad POSIX stop date") main("1970-01-01","2019-10-02","format OK") main("1970-01-01","2019/10/02","format NG") main("1995-11-21","1995-11-21","identical dates") main("2019-01-01","2019-01-02","positive date") main("2019-01-02","2019-01-01","negative date") main("2019-01-01","2019-03-01","non-leap year") main("2020-01-01","2020-03-01","leap year") exit(0) } function main(date1,date2,comment, d1,d2,diff) { printf("\t%s\n",comment) d1 = days_builtin(date1) d2 = days_builtin(date2) diff = (d1 == "" || d2 == "") ? "error" : d2-d1 printf("builtin %10s to %10s = %s\n",date1,date2,diff) d1 = days_generic(date1) d2 = days_generic(date2) diff = (d1 == "" || d2 == "") ? "error" : d2-d1 printf("generic %10s to %10s = %s\n",date1,date2,diff) } function days_builtin(ymd) { if (ymd !~ regexp) { return("") } if (ymd < "1970-01-01" || ymd > "2038-01-18") { return("") } gsub(/-/," ",ymd) return(int(mktime(sprintf("%s 0 0 0",ymd)) / (60*60*24))) } function days_generic(ymd, d,m,y,result) { if (ymd !~ regexp) { return("") } y = substr(ymd,1,4) m = substr(ymd,6,2) d = substr(ymd,9,2) m = (m + 9) % 12 y = int(y - int(m/10)) result = 365*y + int(y/4) - int(y/100) + int(y/400) + int((m*306+5)/10) + (d-1) return(result) }
Imports System.Globalization Module Module1 Function DateDiff(d1 As String, d2 As String) As Integer Dim a = DateTime.ParseExact(d1, "yyyy-MM-dd", CultureInfo.InvariantCulture) Dim b = DateTime.ParseExact(d2, "yyyy-MM-dd", CultureInfo.InvariantCulture) Return (b - a).TotalDays End Function Sub Main() Console.WriteLine(DateDiff("1970-01-01", "2019-10-18")) End Sub End Module
Write the same code in Go as shown below in AWK.
BEGIN { regexp = "^....-..-..$" main("1969-12-31","1970-01-01","builtin has bad POSIX start date") main("1970-01-01","2038-01-19","builtin has bad POSIX stop date") main("1970-01-01","2019-10-02","format OK") main("1970-01-01","2019/10/02","format NG") main("1995-11-21","1995-11-21","identical dates") main("2019-01-01","2019-01-02","positive date") main("2019-01-02","2019-01-01","negative date") main("2019-01-01","2019-03-01","non-leap year") main("2020-01-01","2020-03-01","leap year") exit(0) } function main(date1,date2,comment, d1,d2,diff) { printf("\t%s\n",comment) d1 = days_builtin(date1) d2 = days_builtin(date2) diff = (d1 == "" || d2 == "") ? "error" : d2-d1 printf("builtin %10s to %10s = %s\n",date1,date2,diff) d1 = days_generic(date1) d2 = days_generic(date2) diff = (d1 == "" || d2 == "") ? "error" : d2-d1 printf("generic %10s to %10s = %s\n",date1,date2,diff) } function days_builtin(ymd) { if (ymd !~ regexp) { return("") } if (ymd < "1970-01-01" || ymd > "2038-01-18") { return("") } gsub(/-/," ",ymd) return(int(mktime(sprintf("%s 0 0 0",ymd)) / (60*60*24))) } function days_generic(ymd, d,m,y,result) { if (ymd !~ regexp) { return("") } y = substr(ymd,1,4) m = substr(ymd,6,2) d = substr(ymd,9,2) m = (m + 9) % 12 y = int(y - int(m/10)) result = 365*y + int(y/4) - int(y/100) + int(y/400) + int((m*306+5)/10) + (d-1) return(result) }
package main import ( "fmt" "log" "time" ) const layout = "2006-01-02" func daysBetween(date1, date2 string) int { t1, err := time.Parse(layout, date1) check(err) t2, err := time.Parse(layout, date2) check(err) days := int(t1.Sub(t2).Hours() / 24) if days < 0 { days = -days } return days } func check(err error) { if err != nil { log.Fatal(err) } } func main() { date1, date2 := "2019-01-01", "2019-09-30" days := daysBetween(date1, date2) fmt.Printf("There are %d days between %s and %s\n", days, date1, date2) date1, date2 = "2015-12-31", "2016-09-30" days = daysBetween(date1, date2) fmt.Printf("There are %d days between %s and %s\n", days, date1, date2) }
Translate the given AWK code snippet into Go without altering its behavior.
BEGIN { regexp = "^....-..-..$" main("1969-12-31","1970-01-01","builtin has bad POSIX start date") main("1970-01-01","2038-01-19","builtin has bad POSIX stop date") main("1970-01-01","2019-10-02","format OK") main("1970-01-01","2019/10/02","format NG") main("1995-11-21","1995-11-21","identical dates") main("2019-01-01","2019-01-02","positive date") main("2019-01-02","2019-01-01","negative date") main("2019-01-01","2019-03-01","non-leap year") main("2020-01-01","2020-03-01","leap year") exit(0) } function main(date1,date2,comment, d1,d2,diff) { printf("\t%s\n",comment) d1 = days_builtin(date1) d2 = days_builtin(date2) diff = (d1 == "" || d2 == "") ? "error" : d2-d1 printf("builtin %10s to %10s = %s\n",date1,date2,diff) d1 = days_generic(date1) d2 = days_generic(date2) diff = (d1 == "" || d2 == "") ? "error" : d2-d1 printf("generic %10s to %10s = %s\n",date1,date2,diff) } function days_builtin(ymd) { if (ymd !~ regexp) { return("") } if (ymd < "1970-01-01" || ymd > "2038-01-18") { return("") } gsub(/-/," ",ymd) return(int(mktime(sprintf("%s 0 0 0",ymd)) / (60*60*24))) } function days_generic(ymd, d,m,y,result) { if (ymd !~ regexp) { return("") } y = substr(ymd,1,4) m = substr(ymd,6,2) d = substr(ymd,9,2) m = (m + 9) % 12 y = int(y - int(m/10)) result = 365*y + int(y/4) - int(y/100) + int(y/400) + int((m*306+5)/10) + (d-1) return(result) }
package main import ( "fmt" "log" "time" ) const layout = "2006-01-02" func daysBetween(date1, date2 string) int { t1, err := time.Parse(layout, date1) check(err) t2, err := time.Parse(layout, date2) check(err) days := int(t1.Sub(t2).Hours() / 24) if days < 0 { days = -days } return days } func check(err error) { if err != nil { log.Fatal(err) } } func main() { date1, date2 := "2019-01-01", "2019-09-30" days := daysBetween(date1, date2) fmt.Printf("There are %d days between %s and %s\n", days, date1, date2) date1, date2 = "2015-12-31", "2016-09-30" days = daysBetween(date1, date2) fmt.Printf("There are %d days between %s and %s\n", days, date1, date2) }
Produce a language-to-language conversion: from D to C, same semantics.
import std.datetime.date; import std.stdio; void main() { auto fromDate = Date.fromISOExtString("2019-01-01"); auto toDate = Date.fromISOExtString("2019-10-07"); auto diff = toDate - fromDate; writeln("Number of days between ", fromDate, " and ", toDate, ": ", diff.total!"days"); }
#include<stdbool.h> #include<string.h> #include<stdio.h> typedef struct{ int year, month, day; }date; date extractDate(char* str){ return (date){.year = 1000 * (str[0]-'0') + 100 * (str[1]-'0') + 10 * (str[2]-'0') + (str[3]-'0'), .month = 10*(str[5]-'0') + (str[6]-'0'), .day = 10*(str[8]-'0') + (str[9]-'0')}; } bool isValidDate(char* str){ date newDate; if(strlen(str)!=10 && str[4]!='-' && str[7]!='-'){ return false; } newDate = extractDate(str); if(newDate.year<=0 || newDate.month<=0 || newDate.day<=0 || newDate.month>12 || (newDate.month==2 && newDate.day>29) || ((newDate.month==1 || newDate.month==3 || newDate.month==5 || newDate.month==7 || newDate.month==8 || newDate.month==10 || newDate.month==12) && newDate.day>31) || newDate.day>30 || (newDate.year%4==0 && newDate.month==2 && newDate.month>28)){ return false; } return true; } int diffDays(date date1,date date2){ int days1, days2; date1.month = (date1.month + 9)%12; date1.year = date1.year - date1.month/10; date2.month = (date2.month + 9)%12; date2.year = date2.year - date2.month/10; days1 = 365*date1.year + date1.year/4 - date1.year/100 + date1.year/400 + (date1.month*306 + 5)/10 + ( date1.day - 1 ); days2 = 365*date2.year + date2.year/4 - date2.year/100 + date2.year/400 + (date2.month*306 + 5)/10 + ( date2.day - 1 ); return days2 - days1; } int main(int argc,char** argv) { if(argc!=3){ return printf("Usage : %s <yyyy-mm-dd> <yyyy-mm-dd>",argv[0]); } if(isValidDate(argv[1])&&isValidDate(argv[2]) == false){ return printf("Dates are invalid.\n"); } printf("Days Difference : %d\n", diffDays(extractDate(argv[1]),extractDate(argv[2]))); return 0; }
Generate an equivalent C version of this D code.
import std.datetime.date; import std.stdio; void main() { auto fromDate = Date.fromISOExtString("2019-01-01"); auto toDate = Date.fromISOExtString("2019-10-07"); auto diff = toDate - fromDate; writeln("Number of days between ", fromDate, " and ", toDate, ": ", diff.total!"days"); }
#include<stdbool.h> #include<string.h> #include<stdio.h> typedef struct{ int year, month, day; }date; date extractDate(char* str){ return (date){.year = 1000 * (str[0]-'0') + 100 * (str[1]-'0') + 10 * (str[2]-'0') + (str[3]-'0'), .month = 10*(str[5]-'0') + (str[6]-'0'), .day = 10*(str[8]-'0') + (str[9]-'0')}; } bool isValidDate(char* str){ date newDate; if(strlen(str)!=10 && str[4]!='-' && str[7]!='-'){ return false; } newDate = extractDate(str); if(newDate.year<=0 || newDate.month<=0 || newDate.day<=0 || newDate.month>12 || (newDate.month==2 && newDate.day>29) || ((newDate.month==1 || newDate.month==3 || newDate.month==5 || newDate.month==7 || newDate.month==8 || newDate.month==10 || newDate.month==12) && newDate.day>31) || newDate.day>30 || (newDate.year%4==0 && newDate.month==2 && newDate.month>28)){ return false; } return true; } int diffDays(date date1,date date2){ int days1, days2; date1.month = (date1.month + 9)%12; date1.year = date1.year - date1.month/10; date2.month = (date2.month + 9)%12; date2.year = date2.year - date2.month/10; days1 = 365*date1.year + date1.year/4 - date1.year/100 + date1.year/400 + (date1.month*306 + 5)/10 + ( date1.day - 1 ); days2 = 365*date2.year + date2.year/4 - date2.year/100 + date2.year/400 + (date2.month*306 + 5)/10 + ( date2.day - 1 ); return days2 - days1; } int main(int argc,char** argv) { if(argc!=3){ return printf("Usage : %s <yyyy-mm-dd> <yyyy-mm-dd>",argv[0]); } if(isValidDate(argv[1])&&isValidDate(argv[2]) == false){ return printf("Dates are invalid.\n"); } printf("Days Difference : %d\n", diffDays(extractDate(argv[1]),extractDate(argv[2]))); return 0; }
Maintain the same structure and functionality when rewriting this code in C#.
import std.datetime.date; import std.stdio; void main() { auto fromDate = Date.fromISOExtString("2019-01-01"); auto toDate = Date.fromISOExtString("2019-10-07"); auto diff = toDate - fromDate; writeln("Number of days between ", fromDate, " and ", toDate, ": ", diff.total!"days"); }
using System; using System.Globalization; public class Program { public static void Main() => WriteLine(DateDiff("1970-01-01", "2019-10-18")); public static int DateDiff(string d1, string d2) { var a = DateTime.ParseExact(d1, "yyyy-MM-dd", CultureInfo.InvariantCulture); var b = DateTime.ParseExact(d2, "yyyy-MM-dd", CultureInfo.InvariantCulture); return (int)(b - a).TotalDays; } }
Transform the following D implementation into C++, maintaining the same output and logic.
import std.datetime.date; import std.stdio; void main() { auto fromDate = Date.fromISOExtString("2019-01-01"); auto toDate = Date.fromISOExtString("2019-10-07"); auto diff = toDate - fromDate; writeln("Number of days between ", fromDate, " and ", toDate, ": ", diff.total!"days"); }
#include <iomanip> #include <iostream> class Date { private: int year, month, day; public: Date(std::string str) { if (isValidDate(str)) { year = atoi(&str[0]); month = atoi(&str[5]); day = atoi(&str[8]); } else { throw std::exception("Invalid date"); } } int getYear() { return year; } int getMonth() { return month; } int getDay() { return day; } static bool isValidDate(std::string str) { if (str.length() != 10 || str[4] != '-' || str[7] != '-') { return false; } if (!isdigit(str[0]) || !isdigit(str[1]) || !isdigit(str[2]) || !isdigit(str[3])) { return false; } if (!isdigit(str[5]) || !isdigit(str[6])) { return false; } if (!isdigit(str[8]) || !isdigit(str[9])) { return false; } int year = atoi(&str[0]); int month = atoi(&str[5]); int day = atoi(&str[8]); if (year <= 0 || month <= 0 || day <= 0) { return false; } if (month > 12) { return false; } switch (month) { case 2: if (day > 29) { return false; } if (!isLeapYear(year) && day == 29) { return false; } break; case 1: case 3: case 5: case 7: case 8: case 10: case 12: if (day > 31) { return false; } break; default: if (day > 30) { return false; } break; } return true; } static bool isLeapYear(int year) { if (year > 1582) { return ((year % 4 == 0) && (year % 100 > 0)) || (year % 400 == 0); } if (year > 10) { return year % 4 == 0; } return false; } friend std::ostream &operator<<(std::ostream &, Date &); }; std::ostream &operator<<(std::ostream &os, Date &d) { os << std::setfill('0') << std::setw(4) << d.year << '-'; os << std::setfill('0') << std::setw(2) << d.month << '-'; os << std::setfill('0') << std::setw(2) << d.day; return os; } int diffDays(Date date1, Date date2) { int d1m = (date1.getMonth() + 9) % 12; int d1y = date1.getYear() - d1m / 10; int d2m = (date2.getMonth() + 9) % 12; int d2y = date2.getYear() - d2m / 10; int days1 = 365 * d1y + d1y / 4 - d1y / 100 + d1y / 400 + (d1m * 306 + 5) / 10 + (date1.getDay() - 1); int days2 = 365 * d2y + d2y / 4 - d2y / 100 + d2y / 400 + (d2m * 306 + 5) / 10 + (date2.getDay() - 1); return days2 - days1; } int main() { std::string ds1 = "2019-01-01"; std::string ds2 = "2019-12-02"; if (Date::isValidDate(ds1) && Date::isValidDate(ds2)) { Date d1(ds1); Date d2(ds2); std::cout << "Days difference : " << diffDays(d1, d2); } else { std::cout << "Dates are invalid.\n"; } return 0; }
Write the same code in C++ as shown below in D.
import std.datetime.date; import std.stdio; void main() { auto fromDate = Date.fromISOExtString("2019-01-01"); auto toDate = Date.fromISOExtString("2019-10-07"); auto diff = toDate - fromDate; writeln("Number of days between ", fromDate, " and ", toDate, ": ", diff.total!"days"); }
#include <iomanip> #include <iostream> class Date { private: int year, month, day; public: Date(std::string str) { if (isValidDate(str)) { year = atoi(&str[0]); month = atoi(&str[5]); day = atoi(&str[8]); } else { throw std::exception("Invalid date"); } } int getYear() { return year; } int getMonth() { return month; } int getDay() { return day; } static bool isValidDate(std::string str) { if (str.length() != 10 || str[4] != '-' || str[7] != '-') { return false; } if (!isdigit(str[0]) || !isdigit(str[1]) || !isdigit(str[2]) || !isdigit(str[3])) { return false; } if (!isdigit(str[5]) || !isdigit(str[6])) { return false; } if (!isdigit(str[8]) || !isdigit(str[9])) { return false; } int year = atoi(&str[0]); int month = atoi(&str[5]); int day = atoi(&str[8]); if (year <= 0 || month <= 0 || day <= 0) { return false; } if (month > 12) { return false; } switch (month) { case 2: if (day > 29) { return false; } if (!isLeapYear(year) && day == 29) { return false; } break; case 1: case 3: case 5: case 7: case 8: case 10: case 12: if (day > 31) { return false; } break; default: if (day > 30) { return false; } break; } return true; } static bool isLeapYear(int year) { if (year > 1582) { return ((year % 4 == 0) && (year % 100 > 0)) || (year % 400 == 0); } if (year > 10) { return year % 4 == 0; } return false; } friend std::ostream &operator<<(std::ostream &, Date &); }; std::ostream &operator<<(std::ostream &os, Date &d) { os << std::setfill('0') << std::setw(4) << d.year << '-'; os << std::setfill('0') << std::setw(2) << d.month << '-'; os << std::setfill('0') << std::setw(2) << d.day; return os; } int diffDays(Date date1, Date date2) { int d1m = (date1.getMonth() + 9) % 12; int d1y = date1.getYear() - d1m / 10; int d2m = (date2.getMonth() + 9) % 12; int d2y = date2.getYear() - d2m / 10; int days1 = 365 * d1y + d1y / 4 - d1y / 100 + d1y / 400 + (d1m * 306 + 5) / 10 + (date1.getDay() - 1); int days2 = 365 * d2y + d2y / 4 - d2y / 100 + d2y / 400 + (d2m * 306 + 5) / 10 + (date2.getDay() - 1); return days2 - days1; } int main() { std::string ds1 = "2019-01-01"; std::string ds2 = "2019-12-02"; if (Date::isValidDate(ds1) && Date::isValidDate(ds2)) { Date d1(ds1); Date d2(ds2); std::cout << "Days difference : " << diffDays(d1, d2); } else { std::cout << "Dates are invalid.\n"; } return 0; }
Convert the following code from D to Java, ensuring the logic remains intact.
import std.datetime.date; import std.stdio; void main() { auto fromDate = Date.fromISOExtString("2019-01-01"); auto toDate = Date.fromISOExtString("2019-10-07"); auto diff = toDate - fromDate; writeln("Number of days between ", fromDate, " and ", toDate, ": ", diff.total!"days"); }
import java.time.LocalDate; import java.time.temporal.ChronoUnit; public class DaysBetweenDates { public static void main(String[] args) { LocalDate fromDate = LocalDate.parse("2019-01-01"); LocalDate toDate = LocalDate.parse("2019-10-19"); long diff = ChronoUnit.DAYS.between(fromDate, toDate); System.out.printf("Number of days between %s and %s: %d\n", fromDate, toDate, diff); } }
Port the provided D code into Java while preserving the original functionality.
import std.datetime.date; import std.stdio; void main() { auto fromDate = Date.fromISOExtString("2019-01-01"); auto toDate = Date.fromISOExtString("2019-10-07"); auto diff = toDate - fromDate; writeln("Number of days between ", fromDate, " and ", toDate, ": ", diff.total!"days"); }
import java.time.LocalDate; import java.time.temporal.ChronoUnit; public class DaysBetweenDates { public static void main(String[] args) { LocalDate fromDate = LocalDate.parse("2019-01-01"); LocalDate toDate = LocalDate.parse("2019-10-19"); long diff = ChronoUnit.DAYS.between(fromDate, toDate); System.out.printf("Number of days between %s and %s: %d\n", fromDate, toDate, diff); } }
Convert the following code from D to Python, ensuring the logic remains intact.
import std.datetime.date; import std.stdio; void main() { auto fromDate = Date.fromISOExtString("2019-01-01"); auto toDate = Date.fromISOExtString("2019-10-07"); auto diff = toDate - fromDate; writeln("Number of days between ", fromDate, " and ", toDate, ": ", diff.total!"days"); }
import sys def days( y,m,d ): m = (m + 9) % 12 y = y - m/10 result = 365*y + y/4 - y/100 + y/400 + (m*306 + 5)/10 + ( d - 1 ) return result def diff(one,two): [y1,m1,d1] = one.split('-') [y2,m2,d2] = two.split('-') year2 = days( int(y2),int(m2),int(d2)) year1 = days( int(y1), int(m1), int(d1) ) return year2 - year1 if __name__ == "__main__": one = sys.argv[1] two = sys.argv[2] print diff(one,two)
Preserve the algorithm and functionality while converting the code from D to Python.
import std.datetime.date; import std.stdio; void main() { auto fromDate = Date.fromISOExtString("2019-01-01"); auto toDate = Date.fromISOExtString("2019-10-07"); auto diff = toDate - fromDate; writeln("Number of days between ", fromDate, " and ", toDate, ": ", diff.total!"days"); }
import sys def days( y,m,d ): m = (m + 9) % 12 y = y - m/10 result = 365*y + y/4 - y/100 + y/400 + (m*306 + 5)/10 + ( d - 1 ) return result def diff(one,two): [y1,m1,d1] = one.split('-') [y2,m2,d2] = two.split('-') year2 = days( int(y2),int(m2),int(d2)) year1 = days( int(y1), int(m1), int(d1) ) return year2 - year1 if __name__ == "__main__": one = sys.argv[1] two = sys.argv[2] print diff(one,two)
Ensure the translated VB code behaves exactly like the original D snippet.
import std.datetime.date; import std.stdio; void main() { auto fromDate = Date.fromISOExtString("2019-01-01"); auto toDate = Date.fromISOExtString("2019-10-07"); auto diff = toDate - fromDate; writeln("Number of days between ", fromDate, " and ", toDate, ": ", diff.total!"days"); }
Imports System.Globalization Module Module1 Function DateDiff(d1 As String, d2 As String) As Integer Dim a = DateTime.ParseExact(d1, "yyyy-MM-dd", CultureInfo.InvariantCulture) Dim b = DateTime.ParseExact(d2, "yyyy-MM-dd", CultureInfo.InvariantCulture) Return (b - a).TotalDays End Function Sub Main() Console.WriteLine(DateDiff("1970-01-01", "2019-10-18")) End Sub End Module
Keep all operations the same but rewrite the snippet in VB.
import std.datetime.date; import std.stdio; void main() { auto fromDate = Date.fromISOExtString("2019-01-01"); auto toDate = Date.fromISOExtString("2019-10-07"); auto diff = toDate - fromDate; writeln("Number of days between ", fromDate, " and ", toDate, ": ", diff.total!"days"); }
Imports System.Globalization Module Module1 Function DateDiff(d1 As String, d2 As String) As Integer Dim a = DateTime.ParseExact(d1, "yyyy-MM-dd", CultureInfo.InvariantCulture) Dim b = DateTime.ParseExact(d2, "yyyy-MM-dd", CultureInfo.InvariantCulture) Return (b - a).TotalDays End Function Sub Main() Console.WriteLine(DateDiff("1970-01-01", "2019-10-18")) End Sub End Module
Rewrite the snippet below in Go so it works the same as the original D code.
import std.datetime.date; import std.stdio; void main() { auto fromDate = Date.fromISOExtString("2019-01-01"); auto toDate = Date.fromISOExtString("2019-10-07"); auto diff = toDate - fromDate; writeln("Number of days between ", fromDate, " and ", toDate, ": ", diff.total!"days"); }
package main import ( "fmt" "log" "time" ) const layout = "2006-01-02" func daysBetween(date1, date2 string) int { t1, err := time.Parse(layout, date1) check(err) t2, err := time.Parse(layout, date2) check(err) days := int(t1.Sub(t2).Hours() / 24) if days < 0 { days = -days } return days } func check(err error) { if err != nil { log.Fatal(err) } } func main() { date1, date2 := "2019-01-01", "2019-09-30" days := daysBetween(date1, date2) fmt.Printf("There are %d days between %s and %s\n", days, date1, date2) date1, date2 = "2015-12-31", "2016-09-30" days = daysBetween(date1, date2) fmt.Printf("There are %d days between %s and %s\n", days, date1, date2) }
Rewrite the snippet below in C so it works the same as the original Delphi code.
program Days_between_dates; uses System.SysUtils; function CreateFormat(fmt: string; Delimiter: Char): TFormatSettings; begin Result := TFormatSettings.Create(); with Result do begin DateSeparator := Delimiter; ShortDateFormat := fmt; end; end; function DaysBetween(Date1, Date2: string): Integer; var dt1, dt2: TDateTime; fmt: TFormatSettings; begin fmt := CreateFormat('yyyy-mm-dd', '-'); dt1 := StrToDate(Date1, fmt); dt2 := StrToDate(Date2, fmt); Result := Trunc(dt2 - dt1); end; begin Writeln(DaysBetween('1970-01-01', '2019-10-18')); readln; end.
#include<stdbool.h> #include<string.h> #include<stdio.h> typedef struct{ int year, month, day; }date; date extractDate(char* str){ return (date){.year = 1000 * (str[0]-'0') + 100 * (str[1]-'0') + 10 * (str[2]-'0') + (str[3]-'0'), .month = 10*(str[5]-'0') + (str[6]-'0'), .day = 10*(str[8]-'0') + (str[9]-'0')}; } bool isValidDate(char* str){ date newDate; if(strlen(str)!=10 && str[4]!='-' && str[7]!='-'){ return false; } newDate = extractDate(str); if(newDate.year<=0 || newDate.month<=0 || newDate.day<=0 || newDate.month>12 || (newDate.month==2 && newDate.day>29) || ((newDate.month==1 || newDate.month==3 || newDate.month==5 || newDate.month==7 || newDate.month==8 || newDate.month==10 || newDate.month==12) && newDate.day>31) || newDate.day>30 || (newDate.year%4==0 && newDate.month==2 && newDate.month>28)){ return false; } return true; } int diffDays(date date1,date date2){ int days1, days2; date1.month = (date1.month + 9)%12; date1.year = date1.year - date1.month/10; date2.month = (date2.month + 9)%12; date2.year = date2.year - date2.month/10; days1 = 365*date1.year + date1.year/4 - date1.year/100 + date1.year/400 + (date1.month*306 + 5)/10 + ( date1.day - 1 ); days2 = 365*date2.year + date2.year/4 - date2.year/100 + date2.year/400 + (date2.month*306 + 5)/10 + ( date2.day - 1 ); return days2 - days1; } int main(int argc,char** argv) { if(argc!=3){ return printf("Usage : %s <yyyy-mm-dd> <yyyy-mm-dd>",argv[0]); } if(isValidDate(argv[1])&&isValidDate(argv[2]) == false){ return printf("Dates are invalid.\n"); } printf("Days Difference : %d\n", diffDays(extractDate(argv[1]),extractDate(argv[2]))); return 0; }
Convert this Delphi block to C, preserving its control flow and logic.
program Days_between_dates; uses System.SysUtils; function CreateFormat(fmt: string; Delimiter: Char): TFormatSettings; begin Result := TFormatSettings.Create(); with Result do begin DateSeparator := Delimiter; ShortDateFormat := fmt; end; end; function DaysBetween(Date1, Date2: string): Integer; var dt1, dt2: TDateTime; fmt: TFormatSettings; begin fmt := CreateFormat('yyyy-mm-dd', '-'); dt1 := StrToDate(Date1, fmt); dt2 := StrToDate(Date2, fmt); Result := Trunc(dt2 - dt1); end; begin Writeln(DaysBetween('1970-01-01', '2019-10-18')); readln; end.
#include<stdbool.h> #include<string.h> #include<stdio.h> typedef struct{ int year, month, day; }date; date extractDate(char* str){ return (date){.year = 1000 * (str[0]-'0') + 100 * (str[1]-'0') + 10 * (str[2]-'0') + (str[3]-'0'), .month = 10*(str[5]-'0') + (str[6]-'0'), .day = 10*(str[8]-'0') + (str[9]-'0')}; } bool isValidDate(char* str){ date newDate; if(strlen(str)!=10 && str[4]!='-' && str[7]!='-'){ return false; } newDate = extractDate(str); if(newDate.year<=0 || newDate.month<=0 || newDate.day<=0 || newDate.month>12 || (newDate.month==2 && newDate.day>29) || ((newDate.month==1 || newDate.month==3 || newDate.month==5 || newDate.month==7 || newDate.month==8 || newDate.month==10 || newDate.month==12) && newDate.day>31) || newDate.day>30 || (newDate.year%4==0 && newDate.month==2 && newDate.month>28)){ return false; } return true; } int diffDays(date date1,date date2){ int days1, days2; date1.month = (date1.month + 9)%12; date1.year = date1.year - date1.month/10; date2.month = (date2.month + 9)%12; date2.year = date2.year - date2.month/10; days1 = 365*date1.year + date1.year/4 - date1.year/100 + date1.year/400 + (date1.month*306 + 5)/10 + ( date1.day - 1 ); days2 = 365*date2.year + date2.year/4 - date2.year/100 + date2.year/400 + (date2.month*306 + 5)/10 + ( date2.day - 1 ); return days2 - days1; } int main(int argc,char** argv) { if(argc!=3){ return printf("Usage : %s <yyyy-mm-dd> <yyyy-mm-dd>",argv[0]); } if(isValidDate(argv[1])&&isValidDate(argv[2]) == false){ return printf("Dates are invalid.\n"); } printf("Days Difference : %d\n", diffDays(extractDate(argv[1]),extractDate(argv[2]))); return 0; }
Rewrite this program in C# while keeping its functionality equivalent to the Delphi version.
program Days_between_dates; uses System.SysUtils; function CreateFormat(fmt: string; Delimiter: Char): TFormatSettings; begin Result := TFormatSettings.Create(); with Result do begin DateSeparator := Delimiter; ShortDateFormat := fmt; end; end; function DaysBetween(Date1, Date2: string): Integer; var dt1, dt2: TDateTime; fmt: TFormatSettings; begin fmt := CreateFormat('yyyy-mm-dd', '-'); dt1 := StrToDate(Date1, fmt); dt2 := StrToDate(Date2, fmt); Result := Trunc(dt2 - dt1); end; begin Writeln(DaysBetween('1970-01-01', '2019-10-18')); readln; end.
using System; using System.Globalization; public class Program { public static void Main() => WriteLine(DateDiff("1970-01-01", "2019-10-18")); public static int DateDiff(string d1, string d2) { var a = DateTime.ParseExact(d1, "yyyy-MM-dd", CultureInfo.InvariantCulture); var b = DateTime.ParseExact(d2, "yyyy-MM-dd", CultureInfo.InvariantCulture); return (int)(b - a).TotalDays; } }
Can you help me rewrite this code in C# instead of Delphi, keeping it the same logically?
program Days_between_dates; uses System.SysUtils; function CreateFormat(fmt: string; Delimiter: Char): TFormatSettings; begin Result := TFormatSettings.Create(); with Result do begin DateSeparator := Delimiter; ShortDateFormat := fmt; end; end; function DaysBetween(Date1, Date2: string): Integer; var dt1, dt2: TDateTime; fmt: TFormatSettings; begin fmt := CreateFormat('yyyy-mm-dd', '-'); dt1 := StrToDate(Date1, fmt); dt2 := StrToDate(Date2, fmt); Result := Trunc(dt2 - dt1); end; begin Writeln(DaysBetween('1970-01-01', '2019-10-18')); readln; end.
using System; using System.Globalization; public class Program { public static void Main() => WriteLine(DateDiff("1970-01-01", "2019-10-18")); public static int DateDiff(string d1, string d2) { var a = DateTime.ParseExact(d1, "yyyy-MM-dd", CultureInfo.InvariantCulture); var b = DateTime.ParseExact(d2, "yyyy-MM-dd", CultureInfo.InvariantCulture); return (int)(b - a).TotalDays; } }
Rewrite this program in C++ while keeping its functionality equivalent to the Delphi version.
program Days_between_dates; uses System.SysUtils; function CreateFormat(fmt: string; Delimiter: Char): TFormatSettings; begin Result := TFormatSettings.Create(); with Result do begin DateSeparator := Delimiter; ShortDateFormat := fmt; end; end; function DaysBetween(Date1, Date2: string): Integer; var dt1, dt2: TDateTime; fmt: TFormatSettings; begin fmt := CreateFormat('yyyy-mm-dd', '-'); dt1 := StrToDate(Date1, fmt); dt2 := StrToDate(Date2, fmt); Result := Trunc(dt2 - dt1); end; begin Writeln(DaysBetween('1970-01-01', '2019-10-18')); readln; end.
#include <iomanip> #include <iostream> class Date { private: int year, month, day; public: Date(std::string str) { if (isValidDate(str)) { year = atoi(&str[0]); month = atoi(&str[5]); day = atoi(&str[8]); } else { throw std::exception("Invalid date"); } } int getYear() { return year; } int getMonth() { return month; } int getDay() { return day; } static bool isValidDate(std::string str) { if (str.length() != 10 || str[4] != '-' || str[7] != '-') { return false; } if (!isdigit(str[0]) || !isdigit(str[1]) || !isdigit(str[2]) || !isdigit(str[3])) { return false; } if (!isdigit(str[5]) || !isdigit(str[6])) { return false; } if (!isdigit(str[8]) || !isdigit(str[9])) { return false; } int year = atoi(&str[0]); int month = atoi(&str[5]); int day = atoi(&str[8]); if (year <= 0 || month <= 0 || day <= 0) { return false; } if (month > 12) { return false; } switch (month) { case 2: if (day > 29) { return false; } if (!isLeapYear(year) && day == 29) { return false; } break; case 1: case 3: case 5: case 7: case 8: case 10: case 12: if (day > 31) { return false; } break; default: if (day > 30) { return false; } break; } return true; } static bool isLeapYear(int year) { if (year > 1582) { return ((year % 4 == 0) && (year % 100 > 0)) || (year % 400 == 0); } if (year > 10) { return year % 4 == 0; } return false; } friend std::ostream &operator<<(std::ostream &, Date &); }; std::ostream &operator<<(std::ostream &os, Date &d) { os << std::setfill('0') << std::setw(4) << d.year << '-'; os << std::setfill('0') << std::setw(2) << d.month << '-'; os << std::setfill('0') << std::setw(2) << d.day; return os; } int diffDays(Date date1, Date date2) { int d1m = (date1.getMonth() + 9) % 12; int d1y = date1.getYear() - d1m / 10; int d2m = (date2.getMonth() + 9) % 12; int d2y = date2.getYear() - d2m / 10; int days1 = 365 * d1y + d1y / 4 - d1y / 100 + d1y / 400 + (d1m * 306 + 5) / 10 + (date1.getDay() - 1); int days2 = 365 * d2y + d2y / 4 - d2y / 100 + d2y / 400 + (d2m * 306 + 5) / 10 + (date2.getDay() - 1); return days2 - days1; } int main() { std::string ds1 = "2019-01-01"; std::string ds2 = "2019-12-02"; if (Date::isValidDate(ds1) && Date::isValidDate(ds2)) { Date d1(ds1); Date d2(ds2); std::cout << "Days difference : " << diffDays(d1, d2); } else { std::cout << "Dates are invalid.\n"; } return 0; }
Maintain the same structure and functionality when rewriting this code in C++.
program Days_between_dates; uses System.SysUtils; function CreateFormat(fmt: string; Delimiter: Char): TFormatSettings; begin Result := TFormatSettings.Create(); with Result do begin DateSeparator := Delimiter; ShortDateFormat := fmt; end; end; function DaysBetween(Date1, Date2: string): Integer; var dt1, dt2: TDateTime; fmt: TFormatSettings; begin fmt := CreateFormat('yyyy-mm-dd', '-'); dt1 := StrToDate(Date1, fmt); dt2 := StrToDate(Date2, fmt); Result := Trunc(dt2 - dt1); end; begin Writeln(DaysBetween('1970-01-01', '2019-10-18')); readln; end.
#include <iomanip> #include <iostream> class Date { private: int year, month, day; public: Date(std::string str) { if (isValidDate(str)) { year = atoi(&str[0]); month = atoi(&str[5]); day = atoi(&str[8]); } else { throw std::exception("Invalid date"); } } int getYear() { return year; } int getMonth() { return month; } int getDay() { return day; } static bool isValidDate(std::string str) { if (str.length() != 10 || str[4] != '-' || str[7] != '-') { return false; } if (!isdigit(str[0]) || !isdigit(str[1]) || !isdigit(str[2]) || !isdigit(str[3])) { return false; } if (!isdigit(str[5]) || !isdigit(str[6])) { return false; } if (!isdigit(str[8]) || !isdigit(str[9])) { return false; } int year = atoi(&str[0]); int month = atoi(&str[5]); int day = atoi(&str[8]); if (year <= 0 || month <= 0 || day <= 0) { return false; } if (month > 12) { return false; } switch (month) { case 2: if (day > 29) { return false; } if (!isLeapYear(year) && day == 29) { return false; } break; case 1: case 3: case 5: case 7: case 8: case 10: case 12: if (day > 31) { return false; } break; default: if (day > 30) { return false; } break; } return true; } static bool isLeapYear(int year) { if (year > 1582) { return ((year % 4 == 0) && (year % 100 > 0)) || (year % 400 == 0); } if (year > 10) { return year % 4 == 0; } return false; } friend std::ostream &operator<<(std::ostream &, Date &); }; std::ostream &operator<<(std::ostream &os, Date &d) { os << std::setfill('0') << std::setw(4) << d.year << '-'; os << std::setfill('0') << std::setw(2) << d.month << '-'; os << std::setfill('0') << std::setw(2) << d.day; return os; } int diffDays(Date date1, Date date2) { int d1m = (date1.getMonth() + 9) % 12; int d1y = date1.getYear() - d1m / 10; int d2m = (date2.getMonth() + 9) % 12; int d2y = date2.getYear() - d2m / 10; int days1 = 365 * d1y + d1y / 4 - d1y / 100 + d1y / 400 + (d1m * 306 + 5) / 10 + (date1.getDay() - 1); int days2 = 365 * d2y + d2y / 4 - d2y / 100 + d2y / 400 + (d2m * 306 + 5) / 10 + (date2.getDay() - 1); return days2 - days1; } int main() { std::string ds1 = "2019-01-01"; std::string ds2 = "2019-12-02"; if (Date::isValidDate(ds1) && Date::isValidDate(ds2)) { Date d1(ds1); Date d2(ds2); std::cout << "Days difference : " << diffDays(d1, d2); } else { std::cout << "Dates are invalid.\n"; } return 0; }
Change the following Delphi code into Java without altering its purpose.
program Days_between_dates; uses System.SysUtils; function CreateFormat(fmt: string; Delimiter: Char): TFormatSettings; begin Result := TFormatSettings.Create(); with Result do begin DateSeparator := Delimiter; ShortDateFormat := fmt; end; end; function DaysBetween(Date1, Date2: string): Integer; var dt1, dt2: TDateTime; fmt: TFormatSettings; begin fmt := CreateFormat('yyyy-mm-dd', '-'); dt1 := StrToDate(Date1, fmt); dt2 := StrToDate(Date2, fmt); Result := Trunc(dt2 - dt1); end; begin Writeln(DaysBetween('1970-01-01', '2019-10-18')); readln; end.
import java.time.LocalDate; import java.time.temporal.ChronoUnit; public class DaysBetweenDates { public static void main(String[] args) { LocalDate fromDate = LocalDate.parse("2019-01-01"); LocalDate toDate = LocalDate.parse("2019-10-19"); long diff = ChronoUnit.DAYS.between(fromDate, toDate); System.out.printf("Number of days between %s and %s: %d\n", fromDate, toDate, diff); } }
Convert the following code from Delphi to Java, ensuring the logic remains intact.
program Days_between_dates; uses System.SysUtils; function CreateFormat(fmt: string; Delimiter: Char): TFormatSettings; begin Result := TFormatSettings.Create(); with Result do begin DateSeparator := Delimiter; ShortDateFormat := fmt; end; end; function DaysBetween(Date1, Date2: string): Integer; var dt1, dt2: TDateTime; fmt: TFormatSettings; begin fmt := CreateFormat('yyyy-mm-dd', '-'); dt1 := StrToDate(Date1, fmt); dt2 := StrToDate(Date2, fmt); Result := Trunc(dt2 - dt1); end; begin Writeln(DaysBetween('1970-01-01', '2019-10-18')); readln; end.
import java.time.LocalDate; import java.time.temporal.ChronoUnit; public class DaysBetweenDates { public static void main(String[] args) { LocalDate fromDate = LocalDate.parse("2019-01-01"); LocalDate toDate = LocalDate.parse("2019-10-19"); long diff = ChronoUnit.DAYS.between(fromDate, toDate); System.out.printf("Number of days between %s and %s: %d\n", fromDate, toDate, diff); } }
Convert the following code from Delphi to Python, ensuring the logic remains intact.
program Days_between_dates; uses System.SysUtils; function CreateFormat(fmt: string; Delimiter: Char): TFormatSettings; begin Result := TFormatSettings.Create(); with Result do begin DateSeparator := Delimiter; ShortDateFormat := fmt; end; end; function DaysBetween(Date1, Date2: string): Integer; var dt1, dt2: TDateTime; fmt: TFormatSettings; begin fmt := CreateFormat('yyyy-mm-dd', '-'); dt1 := StrToDate(Date1, fmt); dt2 := StrToDate(Date2, fmt); Result := Trunc(dt2 - dt1); end; begin Writeln(DaysBetween('1970-01-01', '2019-10-18')); readln; end.
import sys def days( y,m,d ): m = (m + 9) % 12 y = y - m/10 result = 365*y + y/4 - y/100 + y/400 + (m*306 + 5)/10 + ( d - 1 ) return result def diff(one,two): [y1,m1,d1] = one.split('-') [y2,m2,d2] = two.split('-') year2 = days( int(y2),int(m2),int(d2)) year1 = days( int(y1), int(m1), int(d1) ) return year2 - year1 if __name__ == "__main__": one = sys.argv[1] two = sys.argv[2] print diff(one,two)
Transform the following Delphi implementation into VB, maintaining the same output and logic.
program Days_between_dates; uses System.SysUtils; function CreateFormat(fmt: string; Delimiter: Char): TFormatSettings; begin Result := TFormatSettings.Create(); with Result do begin DateSeparator := Delimiter; ShortDateFormat := fmt; end; end; function DaysBetween(Date1, Date2: string): Integer; var dt1, dt2: TDateTime; fmt: TFormatSettings; begin fmt := CreateFormat('yyyy-mm-dd', '-'); dt1 := StrToDate(Date1, fmt); dt2 := StrToDate(Date2, fmt); Result := Trunc(dt2 - dt1); end; begin Writeln(DaysBetween('1970-01-01', '2019-10-18')); readln; end.
Imports System.Globalization Module Module1 Function DateDiff(d1 As String, d2 As String) As Integer Dim a = DateTime.ParseExact(d1, "yyyy-MM-dd", CultureInfo.InvariantCulture) Dim b = DateTime.ParseExact(d2, "yyyy-MM-dd", CultureInfo.InvariantCulture) Return (b - a).TotalDays End Function Sub Main() Console.WriteLine(DateDiff("1970-01-01", "2019-10-18")) End Sub End Module
Please provide an equivalent version of this Delphi code in VB.
program Days_between_dates; uses System.SysUtils; function CreateFormat(fmt: string; Delimiter: Char): TFormatSettings; begin Result := TFormatSettings.Create(); with Result do begin DateSeparator := Delimiter; ShortDateFormat := fmt; end; end; function DaysBetween(Date1, Date2: string): Integer; var dt1, dt2: TDateTime; fmt: TFormatSettings; begin fmt := CreateFormat('yyyy-mm-dd', '-'); dt1 := StrToDate(Date1, fmt); dt2 := StrToDate(Date2, fmt); Result := Trunc(dt2 - dt1); end; begin Writeln(DaysBetween('1970-01-01', '2019-10-18')); readln; end.
Imports System.Globalization Module Module1 Function DateDiff(d1 As String, d2 As String) As Integer Dim a = DateTime.ParseExact(d1, "yyyy-MM-dd", CultureInfo.InvariantCulture) Dim b = DateTime.ParseExact(d2, "yyyy-MM-dd", CultureInfo.InvariantCulture) Return (b - a).TotalDays End Function Sub Main() Console.WriteLine(DateDiff("1970-01-01", "2019-10-18")) End Sub End Module
Port the following code from Delphi to Go with equivalent syntax and logic.
program Days_between_dates; uses System.SysUtils; function CreateFormat(fmt: string; Delimiter: Char): TFormatSettings; begin Result := TFormatSettings.Create(); with Result do begin DateSeparator := Delimiter; ShortDateFormat := fmt; end; end; function DaysBetween(Date1, Date2: string): Integer; var dt1, dt2: TDateTime; fmt: TFormatSettings; begin fmt := CreateFormat('yyyy-mm-dd', '-'); dt1 := StrToDate(Date1, fmt); dt2 := StrToDate(Date2, fmt); Result := Trunc(dt2 - dt1); end; begin Writeln(DaysBetween('1970-01-01', '2019-10-18')); readln; end.
package main import ( "fmt" "log" "time" ) const layout = "2006-01-02" func daysBetween(date1, date2 string) int { t1, err := time.Parse(layout, date1) check(err) t2, err := time.Parse(layout, date2) check(err) days := int(t1.Sub(t2).Hours() / 24) if days < 0 { days = -days } return days } func check(err error) { if err != nil { log.Fatal(err) } } func main() { date1, date2 := "2019-01-01", "2019-09-30" days := daysBetween(date1, date2) fmt.Printf("There are %d days between %s and %s\n", days, date1, date2) date1, date2 = "2015-12-31", "2016-09-30" days = daysBetween(date1, date2) fmt.Printf("There are %d days between %s and %s\n", days, date1, date2) }
Produce a language-to-language conversion: from Delphi to Go, same semantics.
program Days_between_dates; uses System.SysUtils; function CreateFormat(fmt: string; Delimiter: Char): TFormatSettings; begin Result := TFormatSettings.Create(); with Result do begin DateSeparator := Delimiter; ShortDateFormat := fmt; end; end; function DaysBetween(Date1, Date2: string): Integer; var dt1, dt2: TDateTime; fmt: TFormatSettings; begin fmt := CreateFormat('yyyy-mm-dd', '-'); dt1 := StrToDate(Date1, fmt); dt2 := StrToDate(Date2, fmt); Result := Trunc(dt2 - dt1); end; begin Writeln(DaysBetween('1970-01-01', '2019-10-18')); readln; end.
package main import ( "fmt" "log" "time" ) const layout = "2006-01-02" func daysBetween(date1, date2 string) int { t1, err := time.Parse(layout, date1) check(err) t2, err := time.Parse(layout, date2) check(err) days := int(t1.Sub(t2).Hours() / 24) if days < 0 { days = -days } return days } func check(err error) { if err != nil { log.Fatal(err) } } func main() { date1, date2 := "2019-01-01", "2019-09-30" days := daysBetween(date1, date2) fmt.Printf("There are %d days between %s and %s\n", days, date1, date2) date1, date2 = "2015-12-31", "2016-09-30" days = daysBetween(date1, date2) fmt.Printf("There are %d days between %s and %s\n", days, date1, date2) }
Translate this program into C but keep the logic exactly as in Erlang.
-module(daysbetween). -export([between/2,dateToInts/2]). dateToInts(String, POS) -> list_to_integer( lists:nth( POS, string:tokens(String, "-") ) ). between(DateOne,DateTwo) -> L = [1,2,3], [Y1,M1,D1] = [ dateToInts(DateOne,X) || X <- L], [Y2,M2,D2] = [ dateToInts(DateTwo,X) || X <- L], GregOne = calendar:date_to_gregorian_days(Y1,M1,D1), GregTwo = calendar:date_to_gregorian_days(Y2,M2,D2), GregTwo - GregOne.
#include<stdbool.h> #include<string.h> #include<stdio.h> typedef struct{ int year, month, day; }date; date extractDate(char* str){ return (date){.year = 1000 * (str[0]-'0') + 100 * (str[1]-'0') + 10 * (str[2]-'0') + (str[3]-'0'), .month = 10*(str[5]-'0') + (str[6]-'0'), .day = 10*(str[8]-'0') + (str[9]-'0')}; } bool isValidDate(char* str){ date newDate; if(strlen(str)!=10 && str[4]!='-' && str[7]!='-'){ return false; } newDate = extractDate(str); if(newDate.year<=0 || newDate.month<=0 || newDate.day<=0 || newDate.month>12 || (newDate.month==2 && newDate.day>29) || ((newDate.month==1 || newDate.month==3 || newDate.month==5 || newDate.month==7 || newDate.month==8 || newDate.month==10 || newDate.month==12) && newDate.day>31) || newDate.day>30 || (newDate.year%4==0 && newDate.month==2 && newDate.month>28)){ return false; } return true; } int diffDays(date date1,date date2){ int days1, days2; date1.month = (date1.month + 9)%12; date1.year = date1.year - date1.month/10; date2.month = (date2.month + 9)%12; date2.year = date2.year - date2.month/10; days1 = 365*date1.year + date1.year/4 - date1.year/100 + date1.year/400 + (date1.month*306 + 5)/10 + ( date1.day - 1 ); days2 = 365*date2.year + date2.year/4 - date2.year/100 + date2.year/400 + (date2.month*306 + 5)/10 + ( date2.day - 1 ); return days2 - days1; } int main(int argc,char** argv) { if(argc!=3){ return printf("Usage : %s <yyyy-mm-dd> <yyyy-mm-dd>",argv[0]); } if(isValidDate(argv[1])&&isValidDate(argv[2]) == false){ return printf("Dates are invalid.\n"); } printf("Days Difference : %d\n", diffDays(extractDate(argv[1]),extractDate(argv[2]))); return 0; }
Generate an equivalent C version of this Erlang code.
-module(daysbetween). -export([between/2,dateToInts/2]). dateToInts(String, POS) -> list_to_integer( lists:nth( POS, string:tokens(String, "-") ) ). between(DateOne,DateTwo) -> L = [1,2,3], [Y1,M1,D1] = [ dateToInts(DateOne,X) || X <- L], [Y2,M2,D2] = [ dateToInts(DateTwo,X) || X <- L], GregOne = calendar:date_to_gregorian_days(Y1,M1,D1), GregTwo = calendar:date_to_gregorian_days(Y2,M2,D2), GregTwo - GregOne.
#include<stdbool.h> #include<string.h> #include<stdio.h> typedef struct{ int year, month, day; }date; date extractDate(char* str){ return (date){.year = 1000 * (str[0]-'0') + 100 * (str[1]-'0') + 10 * (str[2]-'0') + (str[3]-'0'), .month = 10*(str[5]-'0') + (str[6]-'0'), .day = 10*(str[8]-'0') + (str[9]-'0')}; } bool isValidDate(char* str){ date newDate; if(strlen(str)!=10 && str[4]!='-' && str[7]!='-'){ return false; } newDate = extractDate(str); if(newDate.year<=0 || newDate.month<=0 || newDate.day<=0 || newDate.month>12 || (newDate.month==2 && newDate.day>29) || ((newDate.month==1 || newDate.month==3 || newDate.month==5 || newDate.month==7 || newDate.month==8 || newDate.month==10 || newDate.month==12) && newDate.day>31) || newDate.day>30 || (newDate.year%4==0 && newDate.month==2 && newDate.month>28)){ return false; } return true; } int diffDays(date date1,date date2){ int days1, days2; date1.month = (date1.month + 9)%12; date1.year = date1.year - date1.month/10; date2.month = (date2.month + 9)%12; date2.year = date2.year - date2.month/10; days1 = 365*date1.year + date1.year/4 - date1.year/100 + date1.year/400 + (date1.month*306 + 5)/10 + ( date1.day - 1 ); days2 = 365*date2.year + date2.year/4 - date2.year/100 + date2.year/400 + (date2.month*306 + 5)/10 + ( date2.day - 1 ); return days2 - days1; } int main(int argc,char** argv) { if(argc!=3){ return printf("Usage : %s <yyyy-mm-dd> <yyyy-mm-dd>",argv[0]); } if(isValidDate(argv[1])&&isValidDate(argv[2]) == false){ return printf("Dates are invalid.\n"); } printf("Days Difference : %d\n", diffDays(extractDate(argv[1]),extractDate(argv[2]))); return 0; }
Produce a language-to-language conversion: from Erlang to C#, same semantics.
-module(daysbetween). -export([between/2,dateToInts/2]). dateToInts(String, POS) -> list_to_integer( lists:nth( POS, string:tokens(String, "-") ) ). between(DateOne,DateTwo) -> L = [1,2,3], [Y1,M1,D1] = [ dateToInts(DateOne,X) || X <- L], [Y2,M2,D2] = [ dateToInts(DateTwo,X) || X <- L], GregOne = calendar:date_to_gregorian_days(Y1,M1,D1), GregTwo = calendar:date_to_gregorian_days(Y2,M2,D2), GregTwo - GregOne.
using System; using System.Globalization; public class Program { public static void Main() => WriteLine(DateDiff("1970-01-01", "2019-10-18")); public static int DateDiff(string d1, string d2) { var a = DateTime.ParseExact(d1, "yyyy-MM-dd", CultureInfo.InvariantCulture); var b = DateTime.ParseExact(d2, "yyyy-MM-dd", CultureInfo.InvariantCulture); return (int)(b - a).TotalDays; } }
Transform the following Erlang implementation into C++, maintaining the same output and logic.
-module(daysbetween). -export([between/2,dateToInts/2]). dateToInts(String, POS) -> list_to_integer( lists:nth( POS, string:tokens(String, "-") ) ). between(DateOne,DateTwo) -> L = [1,2,3], [Y1,M1,D1] = [ dateToInts(DateOne,X) || X <- L], [Y2,M2,D2] = [ dateToInts(DateTwo,X) || X <- L], GregOne = calendar:date_to_gregorian_days(Y1,M1,D1), GregTwo = calendar:date_to_gregorian_days(Y2,M2,D2), GregTwo - GregOne.
#include <iomanip> #include <iostream> class Date { private: int year, month, day; public: Date(std::string str) { if (isValidDate(str)) { year = atoi(&str[0]); month = atoi(&str[5]); day = atoi(&str[8]); } else { throw std::exception("Invalid date"); } } int getYear() { return year; } int getMonth() { return month; } int getDay() { return day; } static bool isValidDate(std::string str) { if (str.length() != 10 || str[4] != '-' || str[7] != '-') { return false; } if (!isdigit(str[0]) || !isdigit(str[1]) || !isdigit(str[2]) || !isdigit(str[3])) { return false; } if (!isdigit(str[5]) || !isdigit(str[6])) { return false; } if (!isdigit(str[8]) || !isdigit(str[9])) { return false; } int year = atoi(&str[0]); int month = atoi(&str[5]); int day = atoi(&str[8]); if (year <= 0 || month <= 0 || day <= 0) { return false; } if (month > 12) { return false; } switch (month) { case 2: if (day > 29) { return false; } if (!isLeapYear(year) && day == 29) { return false; } break; case 1: case 3: case 5: case 7: case 8: case 10: case 12: if (day > 31) { return false; } break; default: if (day > 30) { return false; } break; } return true; } static bool isLeapYear(int year) { if (year > 1582) { return ((year % 4 == 0) && (year % 100 > 0)) || (year % 400 == 0); } if (year > 10) { return year % 4 == 0; } return false; } friend std::ostream &operator<<(std::ostream &, Date &); }; std::ostream &operator<<(std::ostream &os, Date &d) { os << std::setfill('0') << std::setw(4) << d.year << '-'; os << std::setfill('0') << std::setw(2) << d.month << '-'; os << std::setfill('0') << std::setw(2) << d.day; return os; } int diffDays(Date date1, Date date2) { int d1m = (date1.getMonth() + 9) % 12; int d1y = date1.getYear() - d1m / 10; int d2m = (date2.getMonth() + 9) % 12; int d2y = date2.getYear() - d2m / 10; int days1 = 365 * d1y + d1y / 4 - d1y / 100 + d1y / 400 + (d1m * 306 + 5) / 10 + (date1.getDay() - 1); int days2 = 365 * d2y + d2y / 4 - d2y / 100 + d2y / 400 + (d2m * 306 + 5) / 10 + (date2.getDay() - 1); return days2 - days1; } int main() { std::string ds1 = "2019-01-01"; std::string ds2 = "2019-12-02"; if (Date::isValidDate(ds1) && Date::isValidDate(ds2)) { Date d1(ds1); Date d2(ds2); std::cout << "Days difference : " << diffDays(d1, d2); } else { std::cout << "Dates are invalid.\n"; } return 0; }
Translate the given Erlang code snippet into C++ without altering its behavior.
-module(daysbetween). -export([between/2,dateToInts/2]). dateToInts(String, POS) -> list_to_integer( lists:nth( POS, string:tokens(String, "-") ) ). between(DateOne,DateTwo) -> L = [1,2,3], [Y1,M1,D1] = [ dateToInts(DateOne,X) || X <- L], [Y2,M2,D2] = [ dateToInts(DateTwo,X) || X <- L], GregOne = calendar:date_to_gregorian_days(Y1,M1,D1), GregTwo = calendar:date_to_gregorian_days(Y2,M2,D2), GregTwo - GregOne.
#include <iomanip> #include <iostream> class Date { private: int year, month, day; public: Date(std::string str) { if (isValidDate(str)) { year = atoi(&str[0]); month = atoi(&str[5]); day = atoi(&str[8]); } else { throw std::exception("Invalid date"); } } int getYear() { return year; } int getMonth() { return month; } int getDay() { return day; } static bool isValidDate(std::string str) { if (str.length() != 10 || str[4] != '-' || str[7] != '-') { return false; } if (!isdigit(str[0]) || !isdigit(str[1]) || !isdigit(str[2]) || !isdigit(str[3])) { return false; } if (!isdigit(str[5]) || !isdigit(str[6])) { return false; } if (!isdigit(str[8]) || !isdigit(str[9])) { return false; } int year = atoi(&str[0]); int month = atoi(&str[5]); int day = atoi(&str[8]); if (year <= 0 || month <= 0 || day <= 0) { return false; } if (month > 12) { return false; } switch (month) { case 2: if (day > 29) { return false; } if (!isLeapYear(year) && day == 29) { return false; } break; case 1: case 3: case 5: case 7: case 8: case 10: case 12: if (day > 31) { return false; } break; default: if (day > 30) { return false; } break; } return true; } static bool isLeapYear(int year) { if (year > 1582) { return ((year % 4 == 0) && (year % 100 > 0)) || (year % 400 == 0); } if (year > 10) { return year % 4 == 0; } return false; } friend std::ostream &operator<<(std::ostream &, Date &); }; std::ostream &operator<<(std::ostream &os, Date &d) { os << std::setfill('0') << std::setw(4) << d.year << '-'; os << std::setfill('0') << std::setw(2) << d.month << '-'; os << std::setfill('0') << std::setw(2) << d.day; return os; } int diffDays(Date date1, Date date2) { int d1m = (date1.getMonth() + 9) % 12; int d1y = date1.getYear() - d1m / 10; int d2m = (date2.getMonth() + 9) % 12; int d2y = date2.getYear() - d2m / 10; int days1 = 365 * d1y + d1y / 4 - d1y / 100 + d1y / 400 + (d1m * 306 + 5) / 10 + (date1.getDay() - 1); int days2 = 365 * d2y + d2y / 4 - d2y / 100 + d2y / 400 + (d2m * 306 + 5) / 10 + (date2.getDay() - 1); return days2 - days1; } int main() { std::string ds1 = "2019-01-01"; std::string ds2 = "2019-12-02"; if (Date::isValidDate(ds1) && Date::isValidDate(ds2)) { Date d1(ds1); Date d2(ds2); std::cout << "Days difference : " << diffDays(d1, d2); } else { std::cout << "Dates are invalid.\n"; } return 0; }
Produce a language-to-language conversion: from Erlang to Java, same semantics.
-module(daysbetween). -export([between/2,dateToInts/2]). dateToInts(String, POS) -> list_to_integer( lists:nth( POS, string:tokens(String, "-") ) ). between(DateOne,DateTwo) -> L = [1,2,3], [Y1,M1,D1] = [ dateToInts(DateOne,X) || X <- L], [Y2,M2,D2] = [ dateToInts(DateTwo,X) || X <- L], GregOne = calendar:date_to_gregorian_days(Y1,M1,D1), GregTwo = calendar:date_to_gregorian_days(Y2,M2,D2), GregTwo - GregOne.
import java.time.LocalDate; import java.time.temporal.ChronoUnit; public class DaysBetweenDates { public static void main(String[] args) { LocalDate fromDate = LocalDate.parse("2019-01-01"); LocalDate toDate = LocalDate.parse("2019-10-19"); long diff = ChronoUnit.DAYS.between(fromDate, toDate); System.out.printf("Number of days between %s and %s: %d\n", fromDate, toDate, diff); } }
Generate an equivalent Java version of this Erlang code.
-module(daysbetween). -export([between/2,dateToInts/2]). dateToInts(String, POS) -> list_to_integer( lists:nth( POS, string:tokens(String, "-") ) ). between(DateOne,DateTwo) -> L = [1,2,3], [Y1,M1,D1] = [ dateToInts(DateOne,X) || X <- L], [Y2,M2,D2] = [ dateToInts(DateTwo,X) || X <- L], GregOne = calendar:date_to_gregorian_days(Y1,M1,D1), GregTwo = calendar:date_to_gregorian_days(Y2,M2,D2), GregTwo - GregOne.
import java.time.LocalDate; import java.time.temporal.ChronoUnit; public class DaysBetweenDates { public static void main(String[] args) { LocalDate fromDate = LocalDate.parse("2019-01-01"); LocalDate toDate = LocalDate.parse("2019-10-19"); long diff = ChronoUnit.DAYS.between(fromDate, toDate); System.out.printf("Number of days between %s and %s: %d\n", fromDate, toDate, diff); } }
Translate this program into Python but keep the logic exactly as in Erlang.
-module(daysbetween). -export([between/2,dateToInts/2]). dateToInts(String, POS) -> list_to_integer( lists:nth( POS, string:tokens(String, "-") ) ). between(DateOne,DateTwo) -> L = [1,2,3], [Y1,M1,D1] = [ dateToInts(DateOne,X) || X <- L], [Y2,M2,D2] = [ dateToInts(DateTwo,X) || X <- L], GregOne = calendar:date_to_gregorian_days(Y1,M1,D1), GregTwo = calendar:date_to_gregorian_days(Y2,M2,D2), GregTwo - GregOne.
import sys def days( y,m,d ): m = (m + 9) % 12 y = y - m/10 result = 365*y + y/4 - y/100 + y/400 + (m*306 + 5)/10 + ( d - 1 ) return result def diff(one,two): [y1,m1,d1] = one.split('-') [y2,m2,d2] = two.split('-') year2 = days( int(y2),int(m2),int(d2)) year1 = days( int(y1), int(m1), int(d1) ) return year2 - year1 if __name__ == "__main__": one = sys.argv[1] two = sys.argv[2] print diff(one,two)
Write the same algorithm in Python as shown in this Erlang implementation.
-module(daysbetween). -export([between/2,dateToInts/2]). dateToInts(String, POS) -> list_to_integer( lists:nth( POS, string:tokens(String, "-") ) ). between(DateOne,DateTwo) -> L = [1,2,3], [Y1,M1,D1] = [ dateToInts(DateOne,X) || X <- L], [Y2,M2,D2] = [ dateToInts(DateTwo,X) || X <- L], GregOne = calendar:date_to_gregorian_days(Y1,M1,D1), GregTwo = calendar:date_to_gregorian_days(Y2,M2,D2), GregTwo - GregOne.
import sys def days( y,m,d ): m = (m + 9) % 12 y = y - m/10 result = 365*y + y/4 - y/100 + y/400 + (m*306 + 5)/10 + ( d - 1 ) return result def diff(one,two): [y1,m1,d1] = one.split('-') [y2,m2,d2] = two.split('-') year2 = days( int(y2),int(m2),int(d2)) year1 = days( int(y1), int(m1), int(d1) ) return year2 - year1 if __name__ == "__main__": one = sys.argv[1] two = sys.argv[2] print diff(one,two)
Change the following Erlang code into VB without altering its purpose.
-module(daysbetween). -export([between/2,dateToInts/2]). dateToInts(String, POS) -> list_to_integer( lists:nth( POS, string:tokens(String, "-") ) ). between(DateOne,DateTwo) -> L = [1,2,3], [Y1,M1,D1] = [ dateToInts(DateOne,X) || X <- L], [Y2,M2,D2] = [ dateToInts(DateTwo,X) || X <- L], GregOne = calendar:date_to_gregorian_days(Y1,M1,D1), GregTwo = calendar:date_to_gregorian_days(Y2,M2,D2), GregTwo - GregOne.
Imports System.Globalization Module Module1 Function DateDiff(d1 As String, d2 As String) As Integer Dim a = DateTime.ParseExact(d1, "yyyy-MM-dd", CultureInfo.InvariantCulture) Dim b = DateTime.ParseExact(d2, "yyyy-MM-dd", CultureInfo.InvariantCulture) Return (b - a).TotalDays End Function Sub Main() Console.WriteLine(DateDiff("1970-01-01", "2019-10-18")) End Sub End Module
Preserve the algorithm and functionality while converting the code from Erlang to VB.
-module(daysbetween). -export([between/2,dateToInts/2]). dateToInts(String, POS) -> list_to_integer( lists:nth( POS, string:tokens(String, "-") ) ). between(DateOne,DateTwo) -> L = [1,2,3], [Y1,M1,D1] = [ dateToInts(DateOne,X) || X <- L], [Y2,M2,D2] = [ dateToInts(DateTwo,X) || X <- L], GregOne = calendar:date_to_gregorian_days(Y1,M1,D1), GregTwo = calendar:date_to_gregorian_days(Y2,M2,D2), GregTwo - GregOne.
Imports System.Globalization Module Module1 Function DateDiff(d1 As String, d2 As String) As Integer Dim a = DateTime.ParseExact(d1, "yyyy-MM-dd", CultureInfo.InvariantCulture) Dim b = DateTime.ParseExact(d2, "yyyy-MM-dd", CultureInfo.InvariantCulture) Return (b - a).TotalDays End Function Sub Main() Console.WriteLine(DateDiff("1970-01-01", "2019-10-18")) End Sub End Module
Rewrite this program in Go while keeping its functionality equivalent to the Erlang version.
-module(daysbetween). -export([between/2,dateToInts/2]). dateToInts(String, POS) -> list_to_integer( lists:nth( POS, string:tokens(String, "-") ) ). between(DateOne,DateTwo) -> L = [1,2,3], [Y1,M1,D1] = [ dateToInts(DateOne,X) || X <- L], [Y2,M2,D2] = [ dateToInts(DateTwo,X) || X <- L], GregOne = calendar:date_to_gregorian_days(Y1,M1,D1), GregTwo = calendar:date_to_gregorian_days(Y2,M2,D2), GregTwo - GregOne.
package main import ( "fmt" "log" "time" ) const layout = "2006-01-02" func daysBetween(date1, date2 string) int { t1, err := time.Parse(layout, date1) check(err) t2, err := time.Parse(layout, date2) check(err) days := int(t1.Sub(t2).Hours() / 24) if days < 0 { days = -days } return days } func check(err error) { if err != nil { log.Fatal(err) } } func main() { date1, date2 := "2019-01-01", "2019-09-30" days := daysBetween(date1, date2) fmt.Printf("There are %d days between %s and %s\n", days, date1, date2) date1, date2 = "2015-12-31", "2016-09-30" days = daysBetween(date1, date2) fmt.Printf("There are %d days between %s and %s\n", days, date1, date2) }
Change the programming language of this snippet from Erlang to Go without modifying what it does.
-module(daysbetween). -export([between/2,dateToInts/2]). dateToInts(String, POS) -> list_to_integer( lists:nth( POS, string:tokens(String, "-") ) ). between(DateOne,DateTwo) -> L = [1,2,3], [Y1,M1,D1] = [ dateToInts(DateOne,X) || X <- L], [Y2,M2,D2] = [ dateToInts(DateTwo,X) || X <- L], GregOne = calendar:date_to_gregorian_days(Y1,M1,D1), GregTwo = calendar:date_to_gregorian_days(Y2,M2,D2), GregTwo - GregOne.
package main import ( "fmt" "log" "time" ) const layout = "2006-01-02" func daysBetween(date1, date2 string) int { t1, err := time.Parse(layout, date1) check(err) t2, err := time.Parse(layout, date2) check(err) days := int(t1.Sub(t2).Hours() / 24) if days < 0 { days = -days } return days } func check(err error) { if err != nil { log.Fatal(err) } } func main() { date1, date2 := "2019-01-01", "2019-09-30" days := daysBetween(date1, date2) fmt.Printf("There are %d days between %s and %s\n", days, date1, date2) date1, date2 = "2015-12-31", "2016-09-30" days = daysBetween(date1, date2) fmt.Printf("There are %d days between %s and %s\n", days, date1, date2) }
Write the same code in C as shown below in F#.
let n,g=System.DateTime.Parse("1792-9-22"),System.DateTime.Parse("1805-12-31") printfn "There are %d days between %d-%d-%d and %d-%d-%d" (g-n).Days n.Year n.Month n.Day g.Year g.Month g.Day
#include<stdbool.h> #include<string.h> #include<stdio.h> typedef struct{ int year, month, day; }date; date extractDate(char* str){ return (date){.year = 1000 * (str[0]-'0') + 100 * (str[1]-'0') + 10 * (str[2]-'0') + (str[3]-'0'), .month = 10*(str[5]-'0') + (str[6]-'0'), .day = 10*(str[8]-'0') + (str[9]-'0')}; } bool isValidDate(char* str){ date newDate; if(strlen(str)!=10 && str[4]!='-' && str[7]!='-'){ return false; } newDate = extractDate(str); if(newDate.year<=0 || newDate.month<=0 || newDate.day<=0 || newDate.month>12 || (newDate.month==2 && newDate.day>29) || ((newDate.month==1 || newDate.month==3 || newDate.month==5 || newDate.month==7 || newDate.month==8 || newDate.month==10 || newDate.month==12) && newDate.day>31) || newDate.day>30 || (newDate.year%4==0 && newDate.month==2 && newDate.month>28)){ return false; } return true; } int diffDays(date date1,date date2){ int days1, days2; date1.month = (date1.month + 9)%12; date1.year = date1.year - date1.month/10; date2.month = (date2.month + 9)%12; date2.year = date2.year - date2.month/10; days1 = 365*date1.year + date1.year/4 - date1.year/100 + date1.year/400 + (date1.month*306 + 5)/10 + ( date1.day - 1 ); days2 = 365*date2.year + date2.year/4 - date2.year/100 + date2.year/400 + (date2.month*306 + 5)/10 + ( date2.day - 1 ); return days2 - days1; } int main(int argc,char** argv) { if(argc!=3){ return printf("Usage : %s <yyyy-mm-dd> <yyyy-mm-dd>",argv[0]); } if(isValidDate(argv[1])&&isValidDate(argv[2]) == false){ return printf("Dates are invalid.\n"); } printf("Days Difference : %d\n", diffDays(extractDate(argv[1]),extractDate(argv[2]))); return 0; }
Generate a C translation of this F# snippet without changing its computational steps.
let n,g=System.DateTime.Parse("1792-9-22"),System.DateTime.Parse("1805-12-31") printfn "There are %d days between %d-%d-%d and %d-%d-%d" (g-n).Days n.Year n.Month n.Day g.Year g.Month g.Day
#include<stdbool.h> #include<string.h> #include<stdio.h> typedef struct{ int year, month, day; }date; date extractDate(char* str){ return (date){.year = 1000 * (str[0]-'0') + 100 * (str[1]-'0') + 10 * (str[2]-'0') + (str[3]-'0'), .month = 10*(str[5]-'0') + (str[6]-'0'), .day = 10*(str[8]-'0') + (str[9]-'0')}; } bool isValidDate(char* str){ date newDate; if(strlen(str)!=10 && str[4]!='-' && str[7]!='-'){ return false; } newDate = extractDate(str); if(newDate.year<=0 || newDate.month<=0 || newDate.day<=0 || newDate.month>12 || (newDate.month==2 && newDate.day>29) || ((newDate.month==1 || newDate.month==3 || newDate.month==5 || newDate.month==7 || newDate.month==8 || newDate.month==10 || newDate.month==12) && newDate.day>31) || newDate.day>30 || (newDate.year%4==0 && newDate.month==2 && newDate.month>28)){ return false; } return true; } int diffDays(date date1,date date2){ int days1, days2; date1.month = (date1.month + 9)%12; date1.year = date1.year - date1.month/10; date2.month = (date2.month + 9)%12; date2.year = date2.year - date2.month/10; days1 = 365*date1.year + date1.year/4 - date1.year/100 + date1.year/400 + (date1.month*306 + 5)/10 + ( date1.day - 1 ); days2 = 365*date2.year + date2.year/4 - date2.year/100 + date2.year/400 + (date2.month*306 + 5)/10 + ( date2.day - 1 ); return days2 - days1; } int main(int argc,char** argv) { if(argc!=3){ return printf("Usage : %s <yyyy-mm-dd> <yyyy-mm-dd>",argv[0]); } if(isValidDate(argv[1])&&isValidDate(argv[2]) == false){ return printf("Dates are invalid.\n"); } printf("Days Difference : %d\n", diffDays(extractDate(argv[1]),extractDate(argv[2]))); return 0; }
Generate an equivalent C# version of this F# code.
let n,g=System.DateTime.Parse("1792-9-22"),System.DateTime.Parse("1805-12-31") printfn "There are %d days between %d-%d-%d and %d-%d-%d" (g-n).Days n.Year n.Month n.Day g.Year g.Month g.Day
using System; using System.Globalization; public class Program { public static void Main() => WriteLine(DateDiff("1970-01-01", "2019-10-18")); public static int DateDiff(string d1, string d2) { var a = DateTime.ParseExact(d1, "yyyy-MM-dd", CultureInfo.InvariantCulture); var b = DateTime.ParseExact(d2, "yyyy-MM-dd", CultureInfo.InvariantCulture); return (int)(b - a).TotalDays; } }
Port the following code from F# to C# with equivalent syntax and logic.
let n,g=System.DateTime.Parse("1792-9-22"),System.DateTime.Parse("1805-12-31") printfn "There are %d days between %d-%d-%d and %d-%d-%d" (g-n).Days n.Year n.Month n.Day g.Year g.Month g.Day
using System; using System.Globalization; public class Program { public static void Main() => WriteLine(DateDiff("1970-01-01", "2019-10-18")); public static int DateDiff(string d1, string d2) { var a = DateTime.ParseExact(d1, "yyyy-MM-dd", CultureInfo.InvariantCulture); var b = DateTime.ParseExact(d2, "yyyy-MM-dd", CultureInfo.InvariantCulture); return (int)(b - a).TotalDays; } }
Ensure the translated C++ code behaves exactly like the original F# snippet.
let n,g=System.DateTime.Parse("1792-9-22"),System.DateTime.Parse("1805-12-31") printfn "There are %d days between %d-%d-%d and %d-%d-%d" (g-n).Days n.Year n.Month n.Day g.Year g.Month g.Day
#include <iomanip> #include <iostream> class Date { private: int year, month, day; public: Date(std::string str) { if (isValidDate(str)) { year = atoi(&str[0]); month = atoi(&str[5]); day = atoi(&str[8]); } else { throw std::exception("Invalid date"); } } int getYear() { return year; } int getMonth() { return month; } int getDay() { return day; } static bool isValidDate(std::string str) { if (str.length() != 10 || str[4] != '-' || str[7] != '-') { return false; } if (!isdigit(str[0]) || !isdigit(str[1]) || !isdigit(str[2]) || !isdigit(str[3])) { return false; } if (!isdigit(str[5]) || !isdigit(str[6])) { return false; } if (!isdigit(str[8]) || !isdigit(str[9])) { return false; } int year = atoi(&str[0]); int month = atoi(&str[5]); int day = atoi(&str[8]); if (year <= 0 || month <= 0 || day <= 0) { return false; } if (month > 12) { return false; } switch (month) { case 2: if (day > 29) { return false; } if (!isLeapYear(year) && day == 29) { return false; } break; case 1: case 3: case 5: case 7: case 8: case 10: case 12: if (day > 31) { return false; } break; default: if (day > 30) { return false; } break; } return true; } static bool isLeapYear(int year) { if (year > 1582) { return ((year % 4 == 0) && (year % 100 > 0)) || (year % 400 == 0); } if (year > 10) { return year % 4 == 0; } return false; } friend std::ostream &operator<<(std::ostream &, Date &); }; std::ostream &operator<<(std::ostream &os, Date &d) { os << std::setfill('0') << std::setw(4) << d.year << '-'; os << std::setfill('0') << std::setw(2) << d.month << '-'; os << std::setfill('0') << std::setw(2) << d.day; return os; } int diffDays(Date date1, Date date2) { int d1m = (date1.getMonth() + 9) % 12; int d1y = date1.getYear() - d1m / 10; int d2m = (date2.getMonth() + 9) % 12; int d2y = date2.getYear() - d2m / 10; int days1 = 365 * d1y + d1y / 4 - d1y / 100 + d1y / 400 + (d1m * 306 + 5) / 10 + (date1.getDay() - 1); int days2 = 365 * d2y + d2y / 4 - d2y / 100 + d2y / 400 + (d2m * 306 + 5) / 10 + (date2.getDay() - 1); return days2 - days1; } int main() { std::string ds1 = "2019-01-01"; std::string ds2 = "2019-12-02"; if (Date::isValidDate(ds1) && Date::isValidDate(ds2)) { Date d1(ds1); Date d2(ds2); std::cout << "Days difference : " << diffDays(d1, d2); } else { std::cout << "Dates are invalid.\n"; } return 0; }
Port the following code from F# to C++ with equivalent syntax and logic.
let n,g=System.DateTime.Parse("1792-9-22"),System.DateTime.Parse("1805-12-31") printfn "There are %d days between %d-%d-%d and %d-%d-%d" (g-n).Days n.Year n.Month n.Day g.Year g.Month g.Day
#include <iomanip> #include <iostream> class Date { private: int year, month, day; public: Date(std::string str) { if (isValidDate(str)) { year = atoi(&str[0]); month = atoi(&str[5]); day = atoi(&str[8]); } else { throw std::exception("Invalid date"); } } int getYear() { return year; } int getMonth() { return month; } int getDay() { return day; } static bool isValidDate(std::string str) { if (str.length() != 10 || str[4] != '-' || str[7] != '-') { return false; } if (!isdigit(str[0]) || !isdigit(str[1]) || !isdigit(str[2]) || !isdigit(str[3])) { return false; } if (!isdigit(str[5]) || !isdigit(str[6])) { return false; } if (!isdigit(str[8]) || !isdigit(str[9])) { return false; } int year = atoi(&str[0]); int month = atoi(&str[5]); int day = atoi(&str[8]); if (year <= 0 || month <= 0 || day <= 0) { return false; } if (month > 12) { return false; } switch (month) { case 2: if (day > 29) { return false; } if (!isLeapYear(year) && day == 29) { return false; } break; case 1: case 3: case 5: case 7: case 8: case 10: case 12: if (day > 31) { return false; } break; default: if (day > 30) { return false; } break; } return true; } static bool isLeapYear(int year) { if (year > 1582) { return ((year % 4 == 0) && (year % 100 > 0)) || (year % 400 == 0); } if (year > 10) { return year % 4 == 0; } return false; } friend std::ostream &operator<<(std::ostream &, Date &); }; std::ostream &operator<<(std::ostream &os, Date &d) { os << std::setfill('0') << std::setw(4) << d.year << '-'; os << std::setfill('0') << std::setw(2) << d.month << '-'; os << std::setfill('0') << std::setw(2) << d.day; return os; } int diffDays(Date date1, Date date2) { int d1m = (date1.getMonth() + 9) % 12; int d1y = date1.getYear() - d1m / 10; int d2m = (date2.getMonth() + 9) % 12; int d2y = date2.getYear() - d2m / 10; int days1 = 365 * d1y + d1y / 4 - d1y / 100 + d1y / 400 + (d1m * 306 + 5) / 10 + (date1.getDay() - 1); int days2 = 365 * d2y + d2y / 4 - d2y / 100 + d2y / 400 + (d2m * 306 + 5) / 10 + (date2.getDay() - 1); return days2 - days1; } int main() { std::string ds1 = "2019-01-01"; std::string ds2 = "2019-12-02"; if (Date::isValidDate(ds1) && Date::isValidDate(ds2)) { Date d1(ds1); Date d2(ds2); std::cout << "Days difference : " << diffDays(d1, d2); } else { std::cout << "Dates are invalid.\n"; } return 0; }
Port the provided F# code into Java while preserving the original functionality.
let n,g=System.DateTime.Parse("1792-9-22"),System.DateTime.Parse("1805-12-31") printfn "There are %d days between %d-%d-%d and %d-%d-%d" (g-n).Days n.Year n.Month n.Day g.Year g.Month g.Day
import java.time.LocalDate; import java.time.temporal.ChronoUnit; public class DaysBetweenDates { public static void main(String[] args) { LocalDate fromDate = LocalDate.parse("2019-01-01"); LocalDate toDate = LocalDate.parse("2019-10-19"); long diff = ChronoUnit.DAYS.between(fromDate, toDate); System.out.printf("Number of days between %s and %s: %d\n", fromDate, toDate, diff); } }
Convert the following code from F# to Java, ensuring the logic remains intact.
let n,g=System.DateTime.Parse("1792-9-22"),System.DateTime.Parse("1805-12-31") printfn "There are %d days between %d-%d-%d and %d-%d-%d" (g-n).Days n.Year n.Month n.Day g.Year g.Month g.Day
import java.time.LocalDate; import java.time.temporal.ChronoUnit; public class DaysBetweenDates { public static void main(String[] args) { LocalDate fromDate = LocalDate.parse("2019-01-01"); LocalDate toDate = LocalDate.parse("2019-10-19"); long diff = ChronoUnit.DAYS.between(fromDate, toDate); System.out.printf("Number of days between %s and %s: %d\n", fromDate, toDate, diff); } }
Translate the given F# code snippet into Python without altering its behavior.
let n,g=System.DateTime.Parse("1792-9-22"),System.DateTime.Parse("1805-12-31") printfn "There are %d days between %d-%d-%d and %d-%d-%d" (g-n).Days n.Year n.Month n.Day g.Year g.Month g.Day
import sys def days( y,m,d ): m = (m + 9) % 12 y = y - m/10 result = 365*y + y/4 - y/100 + y/400 + (m*306 + 5)/10 + ( d - 1 ) return result def diff(one,two): [y1,m1,d1] = one.split('-') [y2,m2,d2] = two.split('-') year2 = days( int(y2),int(m2),int(d2)) year1 = days( int(y1), int(m1), int(d1) ) return year2 - year1 if __name__ == "__main__": one = sys.argv[1] two = sys.argv[2] print diff(one,two)
Rewrite this program in Python while keeping its functionality equivalent to the F# version.
let n,g=System.DateTime.Parse("1792-9-22"),System.DateTime.Parse("1805-12-31") printfn "There are %d days between %d-%d-%d and %d-%d-%d" (g-n).Days n.Year n.Month n.Day g.Year g.Month g.Day
import sys def days( y,m,d ): m = (m + 9) % 12 y = y - m/10 result = 365*y + y/4 - y/100 + y/400 + (m*306 + 5)/10 + ( d - 1 ) return result def diff(one,two): [y1,m1,d1] = one.split('-') [y2,m2,d2] = two.split('-') year2 = days( int(y2),int(m2),int(d2)) year1 = days( int(y1), int(m1), int(d1) ) return year2 - year1 if __name__ == "__main__": one = sys.argv[1] two = sys.argv[2] print diff(one,two)
Generate a VB translation of this F# snippet without changing its computational steps.
let n,g=System.DateTime.Parse("1792-9-22"),System.DateTime.Parse("1805-12-31") printfn "There are %d days between %d-%d-%d and %d-%d-%d" (g-n).Days n.Year n.Month n.Day g.Year g.Month g.Day
Imports System.Globalization Module Module1 Function DateDiff(d1 As String, d2 As String) As Integer Dim a = DateTime.ParseExact(d1, "yyyy-MM-dd", CultureInfo.InvariantCulture) Dim b = DateTime.ParseExact(d2, "yyyy-MM-dd", CultureInfo.InvariantCulture) Return (b - a).TotalDays End Function Sub Main() Console.WriteLine(DateDiff("1970-01-01", "2019-10-18")) End Sub End Module
Write a version of this F# function in VB with identical behavior.
let n,g=System.DateTime.Parse("1792-9-22"),System.DateTime.Parse("1805-12-31") printfn "There are %d days between %d-%d-%d and %d-%d-%d" (g-n).Days n.Year n.Month n.Day g.Year g.Month g.Day
Imports System.Globalization Module Module1 Function DateDiff(d1 As String, d2 As String) As Integer Dim a = DateTime.ParseExact(d1, "yyyy-MM-dd", CultureInfo.InvariantCulture) Dim b = DateTime.ParseExact(d2, "yyyy-MM-dd", CultureInfo.InvariantCulture) Return (b - a).TotalDays End Function Sub Main() Console.WriteLine(DateDiff("1970-01-01", "2019-10-18")) End Sub End Module
Convert this F# snippet to Go and keep its semantics consistent.
let n,g=System.DateTime.Parse("1792-9-22"),System.DateTime.Parse("1805-12-31") printfn "There are %d days between %d-%d-%d and %d-%d-%d" (g-n).Days n.Year n.Month n.Day g.Year g.Month g.Day
package main import ( "fmt" "log" "time" ) const layout = "2006-01-02" func daysBetween(date1, date2 string) int { t1, err := time.Parse(layout, date1) check(err) t2, err := time.Parse(layout, date2) check(err) days := int(t1.Sub(t2).Hours() / 24) if days < 0 { days = -days } return days } func check(err error) { if err != nil { log.Fatal(err) } } func main() { date1, date2 := "2019-01-01", "2019-09-30" days := daysBetween(date1, date2) fmt.Printf("There are %d days between %s and %s\n", days, date1, date2) date1, date2 = "2015-12-31", "2016-09-30" days = daysBetween(date1, date2) fmt.Printf("There are %d days between %s and %s\n", days, date1, date2) }
Port the provided F# code into Go while preserving the original functionality.
let n,g=System.DateTime.Parse("1792-9-22"),System.DateTime.Parse("1805-12-31") printfn "There are %d days between %d-%d-%d and %d-%d-%d" (g-n).Days n.Year n.Month n.Day g.Year g.Month g.Day
package main import ( "fmt" "log" "time" ) const layout = "2006-01-02" func daysBetween(date1, date2 string) int { t1, err := time.Parse(layout, date1) check(err) t2, err := time.Parse(layout, date2) check(err) days := int(t1.Sub(t2).Hours() / 24) if days < 0 { days = -days } return days } func check(err error) { if err != nil { log.Fatal(err) } } func main() { date1, date2 := "2019-01-01", "2019-09-30" days := daysBetween(date1, date2) fmt.Printf("There are %d days between %s and %s\n", days, date1, date2) date1, date2 = "2015-12-31", "2016-09-30" days = daysBetween(date1, date2) fmt.Printf("There are %d days between %s and %s\n", days, date1, date2) }
Ensure the translated C code behaves exactly like the original Factor snippet.
USING: calendar calendar.parser kernel math prettyprint ; : days-between ( ymd-str ymd-str -- n ) [ ymd>timestamp ] bi@ time- duration>days abs ; "2019-01-01" "2019-09-30" days-between . "2016-01-01" "2016-09-30" days-between .
#include<stdbool.h> #include<string.h> #include<stdio.h> typedef struct{ int year, month, day; }date; date extractDate(char* str){ return (date){.year = 1000 * (str[0]-'0') + 100 * (str[1]-'0') + 10 * (str[2]-'0') + (str[3]-'0'), .month = 10*(str[5]-'0') + (str[6]-'0'), .day = 10*(str[8]-'0') + (str[9]-'0')}; } bool isValidDate(char* str){ date newDate; if(strlen(str)!=10 && str[4]!='-' && str[7]!='-'){ return false; } newDate = extractDate(str); if(newDate.year<=0 || newDate.month<=0 || newDate.day<=0 || newDate.month>12 || (newDate.month==2 && newDate.day>29) || ((newDate.month==1 || newDate.month==3 || newDate.month==5 || newDate.month==7 || newDate.month==8 || newDate.month==10 || newDate.month==12) && newDate.day>31) || newDate.day>30 || (newDate.year%4==0 && newDate.month==2 && newDate.month>28)){ return false; } return true; } int diffDays(date date1,date date2){ int days1, days2; date1.month = (date1.month + 9)%12; date1.year = date1.year - date1.month/10; date2.month = (date2.month + 9)%12; date2.year = date2.year - date2.month/10; days1 = 365*date1.year + date1.year/4 - date1.year/100 + date1.year/400 + (date1.month*306 + 5)/10 + ( date1.day - 1 ); days2 = 365*date2.year + date2.year/4 - date2.year/100 + date2.year/400 + (date2.month*306 + 5)/10 + ( date2.day - 1 ); return days2 - days1; } int main(int argc,char** argv) { if(argc!=3){ return printf("Usage : %s <yyyy-mm-dd> <yyyy-mm-dd>",argv[0]); } if(isValidDate(argv[1])&&isValidDate(argv[2]) == false){ return printf("Dates are invalid.\n"); } printf("Days Difference : %d\n", diffDays(extractDate(argv[1]),extractDate(argv[2]))); return 0; }
Translate the given Factor code snippet into C without altering its behavior.
USING: calendar calendar.parser kernel math prettyprint ; : days-between ( ymd-str ymd-str -- n ) [ ymd>timestamp ] bi@ time- duration>days abs ; "2019-01-01" "2019-09-30" days-between . "2016-01-01" "2016-09-30" days-between .
#include<stdbool.h> #include<string.h> #include<stdio.h> typedef struct{ int year, month, day; }date; date extractDate(char* str){ return (date){.year = 1000 * (str[0]-'0') + 100 * (str[1]-'0') + 10 * (str[2]-'0') + (str[3]-'0'), .month = 10*(str[5]-'0') + (str[6]-'0'), .day = 10*(str[8]-'0') + (str[9]-'0')}; } bool isValidDate(char* str){ date newDate; if(strlen(str)!=10 && str[4]!='-' && str[7]!='-'){ return false; } newDate = extractDate(str); if(newDate.year<=0 || newDate.month<=0 || newDate.day<=0 || newDate.month>12 || (newDate.month==2 && newDate.day>29) || ((newDate.month==1 || newDate.month==3 || newDate.month==5 || newDate.month==7 || newDate.month==8 || newDate.month==10 || newDate.month==12) && newDate.day>31) || newDate.day>30 || (newDate.year%4==0 && newDate.month==2 && newDate.month>28)){ return false; } return true; } int diffDays(date date1,date date2){ int days1, days2; date1.month = (date1.month + 9)%12; date1.year = date1.year - date1.month/10; date2.month = (date2.month + 9)%12; date2.year = date2.year - date2.month/10; days1 = 365*date1.year + date1.year/4 - date1.year/100 + date1.year/400 + (date1.month*306 + 5)/10 + ( date1.day - 1 ); days2 = 365*date2.year + date2.year/4 - date2.year/100 + date2.year/400 + (date2.month*306 + 5)/10 + ( date2.day - 1 ); return days2 - days1; } int main(int argc,char** argv) { if(argc!=3){ return printf("Usage : %s <yyyy-mm-dd> <yyyy-mm-dd>",argv[0]); } if(isValidDate(argv[1])&&isValidDate(argv[2]) == false){ return printf("Dates are invalid.\n"); } printf("Days Difference : %d\n", diffDays(extractDate(argv[1]),extractDate(argv[2]))); return 0; }
Port the following code from Factor to C# with equivalent syntax and logic.
USING: calendar calendar.parser kernel math prettyprint ; : days-between ( ymd-str ymd-str -- n ) [ ymd>timestamp ] bi@ time- duration>days abs ; "2019-01-01" "2019-09-30" days-between . "2016-01-01" "2016-09-30" days-between .
using System; using System.Globalization; public class Program { public static void Main() => WriteLine(DateDiff("1970-01-01", "2019-10-18")); public static int DateDiff(string d1, string d2) { var a = DateTime.ParseExact(d1, "yyyy-MM-dd", CultureInfo.InvariantCulture); var b = DateTime.ParseExact(d2, "yyyy-MM-dd", CultureInfo.InvariantCulture); return (int)(b - a).TotalDays; } }
Please provide an equivalent version of this Factor code in C++.
USING: calendar calendar.parser kernel math prettyprint ; : days-between ( ymd-str ymd-str -- n ) [ ymd>timestamp ] bi@ time- duration>days abs ; "2019-01-01" "2019-09-30" days-between . "2016-01-01" "2016-09-30" days-between .
#include <iomanip> #include <iostream> class Date { private: int year, month, day; public: Date(std::string str) { if (isValidDate(str)) { year = atoi(&str[0]); month = atoi(&str[5]); day = atoi(&str[8]); } else { throw std::exception("Invalid date"); } } int getYear() { return year; } int getMonth() { return month; } int getDay() { return day; } static bool isValidDate(std::string str) { if (str.length() != 10 || str[4] != '-' || str[7] != '-') { return false; } if (!isdigit(str[0]) || !isdigit(str[1]) || !isdigit(str[2]) || !isdigit(str[3])) { return false; } if (!isdigit(str[5]) || !isdigit(str[6])) { return false; } if (!isdigit(str[8]) || !isdigit(str[9])) { return false; } int year = atoi(&str[0]); int month = atoi(&str[5]); int day = atoi(&str[8]); if (year <= 0 || month <= 0 || day <= 0) { return false; } if (month > 12) { return false; } switch (month) { case 2: if (day > 29) { return false; } if (!isLeapYear(year) && day == 29) { return false; } break; case 1: case 3: case 5: case 7: case 8: case 10: case 12: if (day > 31) { return false; } break; default: if (day > 30) { return false; } break; } return true; } static bool isLeapYear(int year) { if (year > 1582) { return ((year % 4 == 0) && (year % 100 > 0)) || (year % 400 == 0); } if (year > 10) { return year % 4 == 0; } return false; } friend std::ostream &operator<<(std::ostream &, Date &); }; std::ostream &operator<<(std::ostream &os, Date &d) { os << std::setfill('0') << std::setw(4) << d.year << '-'; os << std::setfill('0') << std::setw(2) << d.month << '-'; os << std::setfill('0') << std::setw(2) << d.day; return os; } int diffDays(Date date1, Date date2) { int d1m = (date1.getMonth() + 9) % 12; int d1y = date1.getYear() - d1m / 10; int d2m = (date2.getMonth() + 9) % 12; int d2y = date2.getYear() - d2m / 10; int days1 = 365 * d1y + d1y / 4 - d1y / 100 + d1y / 400 + (d1m * 306 + 5) / 10 + (date1.getDay() - 1); int days2 = 365 * d2y + d2y / 4 - d2y / 100 + d2y / 400 + (d2m * 306 + 5) / 10 + (date2.getDay() - 1); return days2 - days1; } int main() { std::string ds1 = "2019-01-01"; std::string ds2 = "2019-12-02"; if (Date::isValidDate(ds1) && Date::isValidDate(ds2)) { Date d1(ds1); Date d2(ds2); std::cout << "Days difference : " << diffDays(d1, d2); } else { std::cout << "Dates are invalid.\n"; } return 0; }
Please provide an equivalent version of this Factor code in C++.
USING: calendar calendar.parser kernel math prettyprint ; : days-between ( ymd-str ymd-str -- n ) [ ymd>timestamp ] bi@ time- duration>days abs ; "2019-01-01" "2019-09-30" days-between . "2016-01-01" "2016-09-30" days-between .
#include <iomanip> #include <iostream> class Date { private: int year, month, day; public: Date(std::string str) { if (isValidDate(str)) { year = atoi(&str[0]); month = atoi(&str[5]); day = atoi(&str[8]); } else { throw std::exception("Invalid date"); } } int getYear() { return year; } int getMonth() { return month; } int getDay() { return day; } static bool isValidDate(std::string str) { if (str.length() != 10 || str[4] != '-' || str[7] != '-') { return false; } if (!isdigit(str[0]) || !isdigit(str[1]) || !isdigit(str[2]) || !isdigit(str[3])) { return false; } if (!isdigit(str[5]) || !isdigit(str[6])) { return false; } if (!isdigit(str[8]) || !isdigit(str[9])) { return false; } int year = atoi(&str[0]); int month = atoi(&str[5]); int day = atoi(&str[8]); if (year <= 0 || month <= 0 || day <= 0) { return false; } if (month > 12) { return false; } switch (month) { case 2: if (day > 29) { return false; } if (!isLeapYear(year) && day == 29) { return false; } break; case 1: case 3: case 5: case 7: case 8: case 10: case 12: if (day > 31) { return false; } break; default: if (day > 30) { return false; } break; } return true; } static bool isLeapYear(int year) { if (year > 1582) { return ((year % 4 == 0) && (year % 100 > 0)) || (year % 400 == 0); } if (year > 10) { return year % 4 == 0; } return false; } friend std::ostream &operator<<(std::ostream &, Date &); }; std::ostream &operator<<(std::ostream &os, Date &d) { os << std::setfill('0') << std::setw(4) << d.year << '-'; os << std::setfill('0') << std::setw(2) << d.month << '-'; os << std::setfill('0') << std::setw(2) << d.day; return os; } int diffDays(Date date1, Date date2) { int d1m = (date1.getMonth() + 9) % 12; int d1y = date1.getYear() - d1m / 10; int d2m = (date2.getMonth() + 9) % 12; int d2y = date2.getYear() - d2m / 10; int days1 = 365 * d1y + d1y / 4 - d1y / 100 + d1y / 400 + (d1m * 306 + 5) / 10 + (date1.getDay() - 1); int days2 = 365 * d2y + d2y / 4 - d2y / 100 + d2y / 400 + (d2m * 306 + 5) / 10 + (date2.getDay() - 1); return days2 - days1; } int main() { std::string ds1 = "2019-01-01"; std::string ds2 = "2019-12-02"; if (Date::isValidDate(ds1) && Date::isValidDate(ds2)) { Date d1(ds1); Date d2(ds2); std::cout << "Days difference : " << diffDays(d1, d2); } else { std::cout << "Dates are invalid.\n"; } return 0; }
Port the provided Factor code into Java while preserving the original functionality.
USING: calendar calendar.parser kernel math prettyprint ; : days-between ( ymd-str ymd-str -- n ) [ ymd>timestamp ] bi@ time- duration>days abs ; "2019-01-01" "2019-09-30" days-between . "2016-01-01" "2016-09-30" days-between .
import java.time.LocalDate; import java.time.temporal.ChronoUnit; public class DaysBetweenDates { public static void main(String[] args) { LocalDate fromDate = LocalDate.parse("2019-01-01"); LocalDate toDate = LocalDate.parse("2019-10-19"); long diff = ChronoUnit.DAYS.between(fromDate, toDate); System.out.printf("Number of days between %s and %s: %d\n", fromDate, toDate, diff); } }
Port the following code from Factor to Java with equivalent syntax and logic.
USING: calendar calendar.parser kernel math prettyprint ; : days-between ( ymd-str ymd-str -- n ) [ ymd>timestamp ] bi@ time- duration>days abs ; "2019-01-01" "2019-09-30" days-between . "2016-01-01" "2016-09-30" days-between .
import java.time.LocalDate; import java.time.temporal.ChronoUnit; public class DaysBetweenDates { public static void main(String[] args) { LocalDate fromDate = LocalDate.parse("2019-01-01"); LocalDate toDate = LocalDate.parse("2019-10-19"); long diff = ChronoUnit.DAYS.between(fromDate, toDate); System.out.printf("Number of days between %s and %s: %d\n", fromDate, toDate, diff); } }
Please provide an equivalent version of this Factor code in Python.
USING: calendar calendar.parser kernel math prettyprint ; : days-between ( ymd-str ymd-str -- n ) [ ymd>timestamp ] bi@ time- duration>days abs ; "2019-01-01" "2019-09-30" days-between . "2016-01-01" "2016-09-30" days-between .
import sys def days( y,m,d ): m = (m + 9) % 12 y = y - m/10 result = 365*y + y/4 - y/100 + y/400 + (m*306 + 5)/10 + ( d - 1 ) return result def diff(one,two): [y1,m1,d1] = one.split('-') [y2,m2,d2] = two.split('-') year2 = days( int(y2),int(m2),int(d2)) year1 = days( int(y1), int(m1), int(d1) ) return year2 - year1 if __name__ == "__main__": one = sys.argv[1] two = sys.argv[2] print diff(one,two)
Rewrite this program in Python while keeping its functionality equivalent to the Factor version.
USING: calendar calendar.parser kernel math prettyprint ; : days-between ( ymd-str ymd-str -- n ) [ ymd>timestamp ] bi@ time- duration>days abs ; "2019-01-01" "2019-09-30" days-between . "2016-01-01" "2016-09-30" days-between .
import sys def days( y,m,d ): m = (m + 9) % 12 y = y - m/10 result = 365*y + y/4 - y/100 + y/400 + (m*306 + 5)/10 + ( d - 1 ) return result def diff(one,two): [y1,m1,d1] = one.split('-') [y2,m2,d2] = two.split('-') year2 = days( int(y2),int(m2),int(d2)) year1 = days( int(y1), int(m1), int(d1) ) return year2 - year1 if __name__ == "__main__": one = sys.argv[1] two = sys.argv[2] print diff(one,two)
Translate the given Factor code snippet into VB without altering its behavior.
USING: calendar calendar.parser kernel math prettyprint ; : days-between ( ymd-str ymd-str -- n ) [ ymd>timestamp ] bi@ time- duration>days abs ; "2019-01-01" "2019-09-30" days-between . "2016-01-01" "2016-09-30" days-between .
Imports System.Globalization Module Module1 Function DateDiff(d1 As String, d2 As String) As Integer Dim a = DateTime.ParseExact(d1, "yyyy-MM-dd", CultureInfo.InvariantCulture) Dim b = DateTime.ParseExact(d2, "yyyy-MM-dd", CultureInfo.InvariantCulture) Return (b - a).TotalDays End Function Sub Main() Console.WriteLine(DateDiff("1970-01-01", "2019-10-18")) End Sub End Module
Rewrite this program in VB while keeping its functionality equivalent to the Factor version.
USING: calendar calendar.parser kernel math prettyprint ; : days-between ( ymd-str ymd-str -- n ) [ ymd>timestamp ] bi@ time- duration>days abs ; "2019-01-01" "2019-09-30" days-between . "2016-01-01" "2016-09-30" days-between .
Imports System.Globalization Module Module1 Function DateDiff(d1 As String, d2 As String) As Integer Dim a = DateTime.ParseExact(d1, "yyyy-MM-dd", CultureInfo.InvariantCulture) Dim b = DateTime.ParseExact(d2, "yyyy-MM-dd", CultureInfo.InvariantCulture) Return (b - a).TotalDays End Function Sub Main() Console.WriteLine(DateDiff("1970-01-01", "2019-10-18")) End Sub End Module
Preserve the algorithm and functionality while converting the code from Factor to Go.
USING: calendar calendar.parser kernel math prettyprint ; : days-between ( ymd-str ymd-str -- n ) [ ymd>timestamp ] bi@ time- duration>days abs ; "2019-01-01" "2019-09-30" days-between . "2016-01-01" "2016-09-30" days-between .
package main import ( "fmt" "log" "time" ) const layout = "2006-01-02" func daysBetween(date1, date2 string) int { t1, err := time.Parse(layout, date1) check(err) t2, err := time.Parse(layout, date2) check(err) days := int(t1.Sub(t2).Hours() / 24) if days < 0 { days = -days } return days } func check(err error) { if err != nil { log.Fatal(err) } } func main() { date1, date2 := "2019-01-01", "2019-09-30" days := daysBetween(date1, date2) fmt.Printf("There are %d days between %s and %s\n", days, date1, date2) date1, date2 = "2015-12-31", "2016-09-30" days = daysBetween(date1, date2) fmt.Printf("There are %d days between %s and %s\n", days, date1, date2) }
Transform the following Groovy implementation into C, maintaining the same output and logic.
import java.time.LocalDate def fromDate = LocalDate.parse("2019-01-01") def toDate = LocalDate.parse("2019-10-19") def diff = fromDate - toDate println "Number of days between ${fromDate} and ${toDate}: ${diff}"
#include<stdbool.h> #include<string.h> #include<stdio.h> typedef struct{ int year, month, day; }date; date extractDate(char* str){ return (date){.year = 1000 * (str[0]-'0') + 100 * (str[1]-'0') + 10 * (str[2]-'0') + (str[3]-'0'), .month = 10*(str[5]-'0') + (str[6]-'0'), .day = 10*(str[8]-'0') + (str[9]-'0')}; } bool isValidDate(char* str){ date newDate; if(strlen(str)!=10 && str[4]!='-' && str[7]!='-'){ return false; } newDate = extractDate(str); if(newDate.year<=0 || newDate.month<=0 || newDate.day<=0 || newDate.month>12 || (newDate.month==2 && newDate.day>29) || ((newDate.month==1 || newDate.month==3 || newDate.month==5 || newDate.month==7 || newDate.month==8 || newDate.month==10 || newDate.month==12) && newDate.day>31) || newDate.day>30 || (newDate.year%4==0 && newDate.month==2 && newDate.month>28)){ return false; } return true; } int diffDays(date date1,date date2){ int days1, days2; date1.month = (date1.month + 9)%12; date1.year = date1.year - date1.month/10; date2.month = (date2.month + 9)%12; date2.year = date2.year - date2.month/10; days1 = 365*date1.year + date1.year/4 - date1.year/100 + date1.year/400 + (date1.month*306 + 5)/10 + ( date1.day - 1 ); days2 = 365*date2.year + date2.year/4 - date2.year/100 + date2.year/400 + (date2.month*306 + 5)/10 + ( date2.day - 1 ); return days2 - days1; } int main(int argc,char** argv) { if(argc!=3){ return printf("Usage : %s <yyyy-mm-dd> <yyyy-mm-dd>",argv[0]); } if(isValidDate(argv[1])&&isValidDate(argv[2]) == false){ return printf("Dates are invalid.\n"); } printf("Days Difference : %d\n", diffDays(extractDate(argv[1]),extractDate(argv[2]))); return 0; }
Port the following code from Groovy to C with equivalent syntax and logic.
import java.time.LocalDate def fromDate = LocalDate.parse("2019-01-01") def toDate = LocalDate.parse("2019-10-19") def diff = fromDate - toDate println "Number of days between ${fromDate} and ${toDate}: ${diff}"
#include<stdbool.h> #include<string.h> #include<stdio.h> typedef struct{ int year, month, day; }date; date extractDate(char* str){ return (date){.year = 1000 * (str[0]-'0') + 100 * (str[1]-'0') + 10 * (str[2]-'0') + (str[3]-'0'), .month = 10*(str[5]-'0') + (str[6]-'0'), .day = 10*(str[8]-'0') + (str[9]-'0')}; } bool isValidDate(char* str){ date newDate; if(strlen(str)!=10 && str[4]!='-' && str[7]!='-'){ return false; } newDate = extractDate(str); if(newDate.year<=0 || newDate.month<=0 || newDate.day<=0 || newDate.month>12 || (newDate.month==2 && newDate.day>29) || ((newDate.month==1 || newDate.month==3 || newDate.month==5 || newDate.month==7 || newDate.month==8 || newDate.month==10 || newDate.month==12) && newDate.day>31) || newDate.day>30 || (newDate.year%4==0 && newDate.month==2 && newDate.month>28)){ return false; } return true; } int diffDays(date date1,date date2){ int days1, days2; date1.month = (date1.month + 9)%12; date1.year = date1.year - date1.month/10; date2.month = (date2.month + 9)%12; date2.year = date2.year - date2.month/10; days1 = 365*date1.year + date1.year/4 - date1.year/100 + date1.year/400 + (date1.month*306 + 5)/10 + ( date1.day - 1 ); days2 = 365*date2.year + date2.year/4 - date2.year/100 + date2.year/400 + (date2.month*306 + 5)/10 + ( date2.day - 1 ); return days2 - days1; } int main(int argc,char** argv) { if(argc!=3){ return printf("Usage : %s <yyyy-mm-dd> <yyyy-mm-dd>",argv[0]); } if(isValidDate(argv[1])&&isValidDate(argv[2]) == false){ return printf("Dates are invalid.\n"); } printf("Days Difference : %d\n", diffDays(extractDate(argv[1]),extractDate(argv[2]))); return 0; }