Instruction stringlengths 45 106 | input_code stringlengths 1 13.7k | output_code stringlengths 1 13.7k |
|---|---|---|
Translate this program into C# but keep the logic exactly as in Groovy. | 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}"
| 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 the snippet below in C# so it works the same as the original Groovy code. | 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}"
| 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 provided Groovy code into C++ while preserving the original functionality. | 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 <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 Groovy to Java, same semantics. | 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}"
| 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 Groovy block to Java, preserving its control flow 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}"
| 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);
}
}
|
Produce a functionally identical Python code for the snippet given in Groovy. | 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}"
|
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)
|
Port the following code from Groovy to VB 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}"
| 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
|
Transform the following Groovy implementation into VB, 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}"
| 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
|
Produce a functionally identical Go code for the snippet given in Groovy. | 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}"
| 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 algorithm in Go as shown in this Groovy implementation. | 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}"
| 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)
}
|
Preserve the algorithm and functionality while converting the code from Haskell to C. | import Data.Time (Day)
import Data.Time.Calendar (diffDays)
import Data.Time.Format (parseTimeM,defaultTimeLocale)
main = do
putStrLn $ task "2019-01-01" "2019-09-30"
putStrLn $ task "2015-12-31" "2016-09-30"
task :: String -> String -> String
task xs ys = "There are " ++ (show $ betweenDays xs ys) ++ " days between " ++ xs ++ " and " ++ ys ++ "."
betweenDays :: String -> String -> Integer
betweenDays date1 date2 = go (stringToDay date1) (stringToDay date2)
where
go (Just x) (Just y) = diffDays y x
go Nothing _ = error "Exception: Bad format first date"
go _ Nothing = error "Exception: Bad format second date"
stringToDay :: String -> Maybe Day
stringToDay date = parseTimeM True defaultTimeLocale "%Y-%-m-%-d" date
| #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;
}
|
Change the programming language of this snippet from Haskell to C without modifying what it does. | import Data.Time (Day)
import Data.Time.Calendar (diffDays)
import Data.Time.Format (parseTimeM,defaultTimeLocale)
main = do
putStrLn $ task "2019-01-01" "2019-09-30"
putStrLn $ task "2015-12-31" "2016-09-30"
task :: String -> String -> String
task xs ys = "There are " ++ (show $ betweenDays xs ys) ++ " days between " ++ xs ++ " and " ++ ys ++ "."
betweenDays :: String -> String -> Integer
betweenDays date1 date2 = go (stringToDay date1) (stringToDay date2)
where
go (Just x) (Just y) = diffDays y x
go Nothing _ = error "Exception: Bad format first date"
go _ Nothing = error "Exception: Bad format second date"
stringToDay :: String -> Maybe Day
stringToDay date = parseTimeM True defaultTimeLocale "%Y-%-m-%-d" date
| #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;
}
|
Keep all operations the same but rewrite the snippet in C#. | import Data.Time (Day)
import Data.Time.Calendar (diffDays)
import Data.Time.Format (parseTimeM,defaultTimeLocale)
main = do
putStrLn $ task "2019-01-01" "2019-09-30"
putStrLn $ task "2015-12-31" "2016-09-30"
task :: String -> String -> String
task xs ys = "There are " ++ (show $ betweenDays xs ys) ++ " days between " ++ xs ++ " and " ++ ys ++ "."
betweenDays :: String -> String -> Integer
betweenDays date1 date2 = go (stringToDay date1) (stringToDay date2)
where
go (Just x) (Just y) = diffDays y x
go Nothing _ = error "Exception: Bad format first date"
go _ Nothing = error "Exception: Bad format second date"
stringToDay :: String -> Maybe Day
stringToDay date = parseTimeM True defaultTimeLocale "%Y-%-m-%-d" date
| 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 Haskell snippet to C# and keep its semantics consistent. | import Data.Time (Day)
import Data.Time.Calendar (diffDays)
import Data.Time.Format (parseTimeM,defaultTimeLocale)
main = do
putStrLn $ task "2019-01-01" "2019-09-30"
putStrLn $ task "2015-12-31" "2016-09-30"
task :: String -> String -> String
task xs ys = "There are " ++ (show $ betweenDays xs ys) ++ " days between " ++ xs ++ " and " ++ ys ++ "."
betweenDays :: String -> String -> Integer
betweenDays date1 date2 = go (stringToDay date1) (stringToDay date2)
where
go (Just x) (Just y) = diffDays y x
go Nothing _ = error "Exception: Bad format first date"
go _ Nothing = error "Exception: Bad format second date"
stringToDay :: String -> Maybe Day
stringToDay date = parseTimeM True defaultTimeLocale "%Y-%-m-%-d" date
| 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 Haskell. | import Data.Time (Day)
import Data.Time.Calendar (diffDays)
import Data.Time.Format (parseTimeM,defaultTimeLocale)
main = do
putStrLn $ task "2019-01-01" "2019-09-30"
putStrLn $ task "2015-12-31" "2016-09-30"
task :: String -> String -> String
task xs ys = "There are " ++ (show $ betweenDays xs ys) ++ " days between " ++ xs ++ " and " ++ ys ++ "."
betweenDays :: String -> String -> Integer
betweenDays date1 date2 = go (stringToDay date1) (stringToDay date2)
where
go (Just x) (Just y) = diffDays y x
go Nothing _ = error "Exception: Bad format first date"
go _ Nothing = error "Exception: Bad format second date"
stringToDay :: String -> Maybe Day
stringToDay date = parseTimeM True defaultTimeLocale "%Y-%-m-%-d" date
| #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 Haskell version. | import Data.Time (Day)
import Data.Time.Calendar (diffDays)
import Data.Time.Format (parseTimeM,defaultTimeLocale)
main = do
putStrLn $ task "2019-01-01" "2019-09-30"
putStrLn $ task "2015-12-31" "2016-09-30"
task :: String -> String -> String
task xs ys = "There are " ++ (show $ betweenDays xs ys) ++ " days between " ++ xs ++ " and " ++ ys ++ "."
betweenDays :: String -> String -> Integer
betweenDays date1 date2 = go (stringToDay date1) (stringToDay date2)
where
go (Just x) (Just y) = diffDays y x
go Nothing _ = error "Exception: Bad format first date"
go _ Nothing = error "Exception: Bad format second date"
stringToDay :: String -> Maybe Day
stringToDay date = parseTimeM True defaultTimeLocale "%Y-%-m-%-d" date
| #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 Haskell to Java, same semantics. | import Data.Time (Day)
import Data.Time.Calendar (diffDays)
import Data.Time.Format (parseTimeM,defaultTimeLocale)
main = do
putStrLn $ task "2019-01-01" "2019-09-30"
putStrLn $ task "2015-12-31" "2016-09-30"
task :: String -> String -> String
task xs ys = "There are " ++ (show $ betweenDays xs ys) ++ " days between " ++ xs ++ " and " ++ ys ++ "."
betweenDays :: String -> String -> Integer
betweenDays date1 date2 = go (stringToDay date1) (stringToDay date2)
where
go (Just x) (Just y) = diffDays y x
go Nothing _ = error "Exception: Bad format first date"
go _ Nothing = error "Exception: Bad format second date"
stringToDay :: String -> Maybe Day
stringToDay date = parseTimeM True defaultTimeLocale "%Y-%-m-%-d" date
| 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 Java. | import Data.Time (Day)
import Data.Time.Calendar (diffDays)
import Data.Time.Format (parseTimeM,defaultTimeLocale)
main = do
putStrLn $ task "2019-01-01" "2019-09-30"
putStrLn $ task "2015-12-31" "2016-09-30"
task :: String -> String -> String
task xs ys = "There are " ++ (show $ betweenDays xs ys) ++ " days between " ++ xs ++ " and " ++ ys ++ "."
betweenDays :: String -> String -> Integer
betweenDays date1 date2 = go (stringToDay date1) (stringToDay date2)
where
go (Just x) (Just y) = diffDays y x
go Nothing _ = error "Exception: Bad format first date"
go _ Nothing = error "Exception: Bad format second date"
stringToDay :: String -> Maybe Day
stringToDay date = parseTimeM True defaultTimeLocale "%Y-%-m-%-d" date
| 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 Haskell to Python without modifying what it does. | import Data.Time (Day)
import Data.Time.Calendar (diffDays)
import Data.Time.Format (parseTimeM,defaultTimeLocale)
main = do
putStrLn $ task "2019-01-01" "2019-09-30"
putStrLn $ task "2015-12-31" "2016-09-30"
task :: String -> String -> String
task xs ys = "There are " ++ (show $ betweenDays xs ys) ++ " days between " ++ xs ++ " and " ++ ys ++ "."
betweenDays :: String -> String -> Integer
betweenDays date1 date2 = go (stringToDay date1) (stringToDay date2)
where
go (Just x) (Just y) = diffDays y x
go Nothing _ = error "Exception: Bad format first date"
go _ Nothing = error "Exception: Bad format second date"
stringToDay :: String -> Maybe Day
stringToDay date = parseTimeM True defaultTimeLocale "%Y-%-m-%-d" date
|
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 an equivalent Python version of this Haskell code. | import Data.Time (Day)
import Data.Time.Calendar (diffDays)
import Data.Time.Format (parseTimeM,defaultTimeLocale)
main = do
putStrLn $ task "2019-01-01" "2019-09-30"
putStrLn $ task "2015-12-31" "2016-09-30"
task :: String -> String -> String
task xs ys = "There are " ++ (show $ betweenDays xs ys) ++ " days between " ++ xs ++ " and " ++ ys ++ "."
betweenDays :: String -> String -> Integer
betweenDays date1 date2 = go (stringToDay date1) (stringToDay date2)
where
go (Just x) (Just y) = diffDays y x
go Nothing _ = error "Exception: Bad format first date"
go _ Nothing = error "Exception: Bad format second date"
stringToDay :: String -> Maybe Day
stringToDay date = parseTimeM True defaultTimeLocale "%Y-%-m-%-d" date
|
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)
|
Keep all operations the same but rewrite the snippet in VB. | import Data.Time (Day)
import Data.Time.Calendar (diffDays)
import Data.Time.Format (parseTimeM,defaultTimeLocale)
main = do
putStrLn $ task "2019-01-01" "2019-09-30"
putStrLn $ task "2015-12-31" "2016-09-30"
task :: String -> String -> String
task xs ys = "There are " ++ (show $ betweenDays xs ys) ++ " days between " ++ xs ++ " and " ++ ys ++ "."
betweenDays :: String -> String -> Integer
betweenDays date1 date2 = go (stringToDay date1) (stringToDay date2)
where
go (Just x) (Just y) = diffDays y x
go Nothing _ = error "Exception: Bad format first date"
go _ Nothing = error "Exception: Bad format second date"
stringToDay :: String -> Maybe Day
stringToDay date = parseTimeM True defaultTimeLocale "%Y-%-m-%-d" date
| 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
|
Generate an equivalent VB version of this Haskell code. | import Data.Time (Day)
import Data.Time.Calendar (diffDays)
import Data.Time.Format (parseTimeM,defaultTimeLocale)
main = do
putStrLn $ task "2019-01-01" "2019-09-30"
putStrLn $ task "2015-12-31" "2016-09-30"
task :: String -> String -> String
task xs ys = "There are " ++ (show $ betweenDays xs ys) ++ " days between " ++ xs ++ " and " ++ ys ++ "."
betweenDays :: String -> String -> Integer
betweenDays date1 date2 = go (stringToDay date1) (stringToDay date2)
where
go (Just x) (Just y) = diffDays y x
go Nothing _ = error "Exception: Bad format first date"
go _ Nothing = error "Exception: Bad format second date"
stringToDay :: String -> Maybe Day
stringToDay date = parseTimeM True defaultTimeLocale "%Y-%-m-%-d" date
| 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
|
Transform the following Haskell implementation into Go, maintaining the same output and logic. | import Data.Time (Day)
import Data.Time.Calendar (diffDays)
import Data.Time.Format (parseTimeM,defaultTimeLocale)
main = do
putStrLn $ task "2019-01-01" "2019-09-30"
putStrLn $ task "2015-12-31" "2016-09-30"
task :: String -> String -> String
task xs ys = "There are " ++ (show $ betweenDays xs ys) ++ " days between " ++ xs ++ " and " ++ ys ++ "."
betweenDays :: String -> String -> Integer
betweenDays date1 date2 = go (stringToDay date1) (stringToDay date2)
where
go (Just x) (Just y) = diffDays y x
go Nothing _ = error "Exception: Bad format first date"
go _ Nothing = error "Exception: Bad format second date"
stringToDay :: String -> Maybe Day
stringToDay date = parseTimeM True defaultTimeLocale "%Y-%-m-%-d" date
| 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 the following code from Haskell to Go, ensuring the logic remains intact. | import Data.Time (Day)
import Data.Time.Calendar (diffDays)
import Data.Time.Format (parseTimeM,defaultTimeLocale)
main = do
putStrLn $ task "2019-01-01" "2019-09-30"
putStrLn $ task "2015-12-31" "2016-09-30"
task :: String -> String -> String
task xs ys = "There are " ++ (show $ betweenDays xs ys) ++ " days between " ++ xs ++ " and " ++ ys ++ "."
betweenDays :: String -> String -> Integer
betweenDays date1 date2 = go (stringToDay date1) (stringToDay date2)
where
go (Just x) (Just y) = diffDays y x
go Nothing _ = error "Exception: Bad format first date"
go _ Nothing = error "Exception: Bad format second date"
stringToDay :: String -> Maybe Day
stringToDay date = parseTimeM True defaultTimeLocale "%Y-%-m-%-d" date
| 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 Julia to C, same semantics. | using Dates
@show Day(DateTime("2019-09-30") - DateTime("2019-01-01"))
@show Day(DateTime("2019-03-01") - DateTime("2019-02-01"))
@show Day(DateTime("2020-03-01") - DateTime("2020-02-01"))
@show Day(DateTime("2029-03-29") - DateTime("2019-03-29"))
| #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 Julia block to C, preserving its control flow and logic. | using Dates
@show Day(DateTime("2019-09-30") - DateTime("2019-01-01"))
@show Day(DateTime("2019-03-01") - DateTime("2019-02-01"))
@show Day(DateTime("2020-03-01") - DateTime("2020-02-01"))
@show Day(DateTime("2029-03-29") - DateTime("2019-03-29"))
| #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 Julia version. | using Dates
@show Day(DateTime("2019-09-30") - DateTime("2019-01-01"))
@show Day(DateTime("2019-03-01") - DateTime("2019-02-01"))
@show Day(DateTime("2020-03-01") - DateTime("2020-02-01"))
@show Day(DateTime("2029-03-29") - DateTime("2019-03-29"))
| 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 the given Julia code snippet into C# without altering its behavior. | using Dates
@show Day(DateTime("2019-09-30") - DateTime("2019-01-01"))
@show Day(DateTime("2019-03-01") - DateTime("2019-02-01"))
@show Day(DateTime("2020-03-01") - DateTime("2020-02-01"))
@show Day(DateTime("2029-03-29") - DateTime("2019-03-29"))
| 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;
}
}
|
Keep all operations the same but rewrite the snippet in C++. | using Dates
@show Day(DateTime("2019-09-30") - DateTime("2019-01-01"))
@show Day(DateTime("2019-03-01") - DateTime("2019-02-01"))
@show Day(DateTime("2020-03-01") - DateTime("2020-02-01"))
@show Day(DateTime("2029-03-29") - DateTime("2019-03-29"))
| #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 Julia block to C++, preserving its control flow and logic. | using Dates
@show Day(DateTime("2019-09-30") - DateTime("2019-01-01"))
@show Day(DateTime("2019-03-01") - DateTime("2019-02-01"))
@show Day(DateTime("2020-03-01") - DateTime("2020-02-01"))
@show Day(DateTime("2029-03-29") - DateTime("2019-03-29"))
| #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 Java. | using Dates
@show Day(DateTime("2019-09-30") - DateTime("2019-01-01"))
@show Day(DateTime("2019-03-01") - DateTime("2019-02-01"))
@show Day(DateTime("2020-03-01") - DateTime("2020-02-01"))
@show Day(DateTime("2029-03-29") - DateTime("2019-03-29"))
| 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 Julia to Java without modifying what it does. | using Dates
@show Day(DateTime("2019-09-30") - DateTime("2019-01-01"))
@show Day(DateTime("2019-03-01") - DateTime("2019-02-01"))
@show Day(DateTime("2020-03-01") - DateTime("2020-02-01"))
@show Day(DateTime("2029-03-29") - DateTime("2019-03-29"))
| 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);
}
}
|
Write the same code in Python as shown below in Julia. | using Dates
@show Day(DateTime("2019-09-30") - DateTime("2019-01-01"))
@show Day(DateTime("2019-03-01") - DateTime("2019-02-01"))
@show Day(DateTime("2020-03-01") - DateTime("2020-02-01"))
@show Day(DateTime("2029-03-29") - DateTime("2019-03-29"))
|
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 an equivalent Python version of this Julia code. | using Dates
@show Day(DateTime("2019-09-30") - DateTime("2019-01-01"))
@show Day(DateTime("2019-03-01") - DateTime("2019-02-01"))
@show Day(DateTime("2020-03-01") - DateTime("2020-02-01"))
@show Day(DateTime("2029-03-29") - DateTime("2019-03-29"))
|
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 Julia to VB. | using Dates
@show Day(DateTime("2019-09-30") - DateTime("2019-01-01"))
@show Day(DateTime("2019-03-01") - DateTime("2019-02-01"))
@show Day(DateTime("2020-03-01") - DateTime("2020-02-01"))
@show Day(DateTime("2029-03-29") - DateTime("2019-03-29"))
| 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
|
Maintain the same structure and functionality when rewriting this code in VB. | using Dates
@show Day(DateTime("2019-09-30") - DateTime("2019-01-01"))
@show Day(DateTime("2019-03-01") - DateTime("2019-02-01"))
@show Day(DateTime("2020-03-01") - DateTime("2020-02-01"))
@show Day(DateTime("2029-03-29") - DateTime("2019-03-29"))
| 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
|
Produce a functionally identical Go code for the snippet given in Julia. | using Dates
@show Day(DateTime("2019-09-30") - DateTime("2019-01-01"))
@show Day(DateTime("2019-03-01") - DateTime("2019-02-01"))
@show Day(DateTime("2020-03-01") - DateTime("2020-02-01"))
@show Day(DateTime("2029-03-29") - DateTime("2019-03-29"))
| 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 Julia snippet to Go and keep its semantics consistent. | using Dates
@show Day(DateTime("2019-09-30") - DateTime("2019-01-01"))
@show Day(DateTime("2019-03-01") - DateTime("2019-02-01"))
@show Day(DateTime("2020-03-01") - DateTime("2020-02-01"))
@show Day(DateTime("2029-03-29") - DateTime("2019-03-29"))
| 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)
}
|
Please provide an equivalent version of this Lua code in C. | SECONDS_IN_A_DAY = 60 * 60 * 24
function parseDate (str)
local y, m, d = string.match(str, "(%d+)-(%d+)-(%d+)")
return os.time({year = y, month = m, day = d})
end
io.write("Enter date 1: ")
local d1 = parseDate(io.read())
io.write("Enter date 2: ")
local d2 = parseDate(io.read())
local diff = math.ceil(os.difftime(d2, d1) / SECONDS_IN_A_DAY)
print("There are " .. diff .. " days between these dates.")
| #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. | SECONDS_IN_A_DAY = 60 * 60 * 24
function parseDate (str)
local y, m, d = string.match(str, "(%d+)-(%d+)-(%d+)")
return os.time({year = y, month = m, day = d})
end
io.write("Enter date 1: ")
local d1 = parseDate(io.read())
io.write("Enter date 2: ")
local d2 = parseDate(io.read())
local diff = math.ceil(os.difftime(d2, d1) / SECONDS_IN_A_DAY)
print("There are " .. diff .. " days between these dates.")
| #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 functionally identical C# code for the snippet given in Lua. | SECONDS_IN_A_DAY = 60 * 60 * 24
function parseDate (str)
local y, m, d = string.match(str, "(%d+)-(%d+)-(%d+)")
return os.time({year = y, month = m, day = d})
end
io.write("Enter date 1: ")
local d1 = parseDate(io.read())
io.write("Enter date 2: ")
local d2 = parseDate(io.read())
local diff = math.ceil(os.difftime(d2, d1) / SECONDS_IN_A_DAY)
print("There are " .. diff .. " days between these dates.")
| 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 the following code from Lua to C#, ensuring the logic remains intact. | SECONDS_IN_A_DAY = 60 * 60 * 24
function parseDate (str)
local y, m, d = string.match(str, "(%d+)-(%d+)-(%d+)")
return os.time({year = y, month = m, day = d})
end
io.write("Enter date 1: ")
local d1 = parseDate(io.read())
io.write("Enter date 2: ")
local d2 = parseDate(io.read())
local diff = math.ceil(os.difftime(d2, d1) / SECONDS_IN_A_DAY)
print("There are " .. diff .. " days between these dates.")
| 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;
}
}
|
Preserve the algorithm and functionality while converting the code from Lua to C++. | SECONDS_IN_A_DAY = 60 * 60 * 24
function parseDate (str)
local y, m, d = string.match(str, "(%d+)-(%d+)-(%d+)")
return os.time({year = y, month = m, day = d})
end
io.write("Enter date 1: ")
local d1 = parseDate(io.read())
io.write("Enter date 2: ")
local d2 = parseDate(io.read())
local diff = math.ceil(os.difftime(d2, d1) / SECONDS_IN_A_DAY)
print("There are " .. diff .. " days between these dates.")
| #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 Lua code snippet into C++ without altering its behavior. | SECONDS_IN_A_DAY = 60 * 60 * 24
function parseDate (str)
local y, m, d = string.match(str, "(%d+)-(%d+)-(%d+)")
return os.time({year = y, month = m, day = d})
end
io.write("Enter date 1: ")
local d1 = parseDate(io.read())
io.write("Enter date 2: ")
local d2 = parseDate(io.read())
local diff = math.ceil(os.difftime(d2, d1) / SECONDS_IN_A_DAY)
print("There are " .. diff .. " days between these dates.")
| #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 Lua block to Java, preserving its control flow and logic. | SECONDS_IN_A_DAY = 60 * 60 * 24
function parseDate (str)
local y, m, d = string.match(str, "(%d+)-(%d+)-(%d+)")
return os.time({year = y, month = m, day = d})
end
io.write("Enter date 1: ")
local d1 = parseDate(io.read())
io.write("Enter date 2: ")
local d2 = parseDate(io.read())
local diff = math.ceil(os.difftime(d2, d1) / SECONDS_IN_A_DAY)
print("There are " .. diff .. " days between these dates.")
| 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);
}
}
|
Write a version of this Lua function in Java with identical behavior. | SECONDS_IN_A_DAY = 60 * 60 * 24
function parseDate (str)
local y, m, d = string.match(str, "(%d+)-(%d+)-(%d+)")
return os.time({year = y, month = m, day = d})
end
io.write("Enter date 1: ")
local d1 = parseDate(io.read())
io.write("Enter date 2: ")
local d2 = parseDate(io.read())
local diff = math.ceil(os.difftime(d2, d1) / SECONDS_IN_A_DAY)
print("There are " .. diff .. " days between these dates.")
| 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 Lua code snippet into Python without altering its behavior. | SECONDS_IN_A_DAY = 60 * 60 * 24
function parseDate (str)
local y, m, d = string.match(str, "(%d+)-(%d+)-(%d+)")
return os.time({year = y, month = m, day = d})
end
io.write("Enter date 1: ")
local d1 = parseDate(io.read())
io.write("Enter date 2: ")
local d2 = parseDate(io.read())
local diff = math.ceil(os.difftime(d2, d1) / SECONDS_IN_A_DAY)
print("There are " .. diff .. " days between these dates.")
|
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 Lua code snippet into Python without altering its behavior. | SECONDS_IN_A_DAY = 60 * 60 * 24
function parseDate (str)
local y, m, d = string.match(str, "(%d+)-(%d+)-(%d+)")
return os.time({year = y, month = m, day = d})
end
io.write("Enter date 1: ")
local d1 = parseDate(io.read())
io.write("Enter date 2: ")
local d2 = parseDate(io.read())
local diff = math.ceil(os.difftime(d2, d1) / SECONDS_IN_A_DAY)
print("There are " .. diff .. " days between these dates.")
|
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 Lua code into VB without altering its purpose. | SECONDS_IN_A_DAY = 60 * 60 * 24
function parseDate (str)
local y, m, d = string.match(str, "(%d+)-(%d+)-(%d+)")
return os.time({year = y, month = m, day = d})
end
io.write("Enter date 1: ")
local d1 = parseDate(io.read())
io.write("Enter date 2: ")
local d2 = parseDate(io.read())
local diff = math.ceil(os.difftime(d2, d1) / SECONDS_IN_A_DAY)
print("There are " .. diff .. " days between these dates.")
| 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 Lua function in VB with identical behavior. | SECONDS_IN_A_DAY = 60 * 60 * 24
function parseDate (str)
local y, m, d = string.match(str, "(%d+)-(%d+)-(%d+)")
return os.time({year = y, month = m, day = d})
end
io.write("Enter date 1: ")
local d1 = parseDate(io.read())
io.write("Enter date 2: ")
local d2 = parseDate(io.read())
local diff = math.ceil(os.difftime(d2, d1) / SECONDS_IN_A_DAY)
print("There are " .. diff .. " days between these dates.")
| 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
|
Produce a language-to-language conversion: from Lua to Go, same semantics. | SECONDS_IN_A_DAY = 60 * 60 * 24
function parseDate (str)
local y, m, d = string.match(str, "(%d+)-(%d+)-(%d+)")
return os.time({year = y, month = m, day = d})
end
io.write("Enter date 1: ")
local d1 = parseDate(io.read())
io.write("Enter date 2: ")
local d2 = parseDate(io.read())
local diff = math.ceil(os.difftime(d2, d1) / SECONDS_IN_A_DAY)
print("There are " .. diff .. " days between these dates.")
| 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 Lua to Go, same semantics. | SECONDS_IN_A_DAY = 60 * 60 * 24
function parseDate (str)
local y, m, d = string.match(str, "(%d+)-(%d+)-(%d+)")
return os.time({year = y, month = m, day = d})
end
io.write("Enter date 1: ")
local d1 = parseDate(io.read())
io.write("Enter date 2: ")
local d2 = parseDate(io.read())
local diff = math.ceil(os.difftime(d2, d1) / SECONDS_IN_A_DAY)
print("There are " .. diff .. " days between these dates.")
| 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 a version of this Mathematica function in C with identical behavior. | DateDifference["2020-01-01", "2020-03-01"]
DateDifference["2021-01-01", "2021-03-01"]
| #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;
}
|
Transform the following Mathematica implementation into C, maintaining the same output and logic. | DateDifference["2020-01-01", "2020-03-01"]
DateDifference["2021-01-01", "2021-03-01"]
| #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;
}
|
Change the following Mathematica code into C# without altering its purpose. | DateDifference["2020-01-01", "2020-03-01"]
DateDifference["2021-01-01", "2021-03-01"]
| 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 the following code from Mathematica to C#, ensuring the logic remains intact. | DateDifference["2020-01-01", "2020-03-01"]
DateDifference["2021-01-01", "2021-03-01"]
| 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 Mathematica block to C++, preserving its control flow and logic. | DateDifference["2020-01-01", "2020-03-01"]
DateDifference["2021-01-01", "2021-03-01"]
| #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;
}
|
Transform the following Mathematica implementation into C++, maintaining the same output and logic. | DateDifference["2020-01-01", "2020-03-01"]
DateDifference["2021-01-01", "2021-03-01"]
| #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;
}
|
Ensure the translated Java code behaves exactly like the original Mathematica snippet. | DateDifference["2020-01-01", "2020-03-01"]
DateDifference["2021-01-01", "2021-03-01"]
| 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 Mathematica code in Java. | DateDifference["2020-01-01", "2020-03-01"]
DateDifference["2021-01-01", "2021-03-01"]
| 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 Mathematica code snippet into Python without altering its behavior. | DateDifference["2020-01-01", "2020-03-01"]
DateDifference["2021-01-01", "2021-03-01"]
|
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)
|
Can you help me rewrite this code in Python instead of Mathematica, keeping it the same logically? | DateDifference["2020-01-01", "2020-03-01"]
DateDifference["2021-01-01", "2021-03-01"]
|
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 VB while keeping its functionality equivalent to the Mathematica version. | DateDifference["2020-01-01", "2020-03-01"]
DateDifference["2021-01-01", "2021-03-01"]
| 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
|
Produce a language-to-language conversion: from Mathematica to VB, same semantics. | DateDifference["2020-01-01", "2020-03-01"]
DateDifference["2021-01-01", "2021-03-01"]
| 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
|
Produce a functionally identical Go code for the snippet given in Mathematica. | DateDifference["2020-01-01", "2020-03-01"]
DateDifference["2021-01-01", "2021-03-01"]
| 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 functionally identical Go code for the snippet given in Mathematica. | DateDifference["2020-01-01", "2020-03-01"]
DateDifference["2021-01-01", "2021-03-01"]
| 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 Nim snippet to C and keep its semantics consistent. | import times
proc daysBetween(date1, date2: string): int64 =
const Fmt = initTimeFormat("yyyy-MM-dd")
(date2.parse(Fmt, utc()) - date1.parse(Fmt, utc())).inDays
const Dates = [("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")]
for (date1, date2) in Dates:
echo "Days between ", date1, " and ", date2, ": ", daysBetween(date1, date2)
| #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 Nim snippet without changing its computational steps. | import times
proc daysBetween(date1, date2: string): int64 =
const Fmt = initTimeFormat("yyyy-MM-dd")
(date2.parse(Fmt, utc()) - date1.parse(Fmt, utc())).inDays
const Dates = [("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")]
for (date1, date2) in Dates:
echo "Days between ", date1, " and ", date2, ": ", daysBetween(date1, date2)
| #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;
}
|
Can you help me rewrite this code in C# instead of Nim, keeping it the same logically? | import times
proc daysBetween(date1, date2: string): int64 =
const Fmt = initTimeFormat("yyyy-MM-dd")
(date2.parse(Fmt, utc()) - date1.parse(Fmt, utc())).inDays
const Dates = [("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")]
for (date1, date2) in Dates:
echo "Days between ", date1, " and ", date2, ": ", daysBetween(date1, date2)
| 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;
}
}
|
Maintain the same structure and functionality when rewriting this code in C#. | import times
proc daysBetween(date1, date2: string): int64 =
const Fmt = initTimeFormat("yyyy-MM-dd")
(date2.parse(Fmt, utc()) - date1.parse(Fmt, utc())).inDays
const Dates = [("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")]
for (date1, date2) in Dates:
echo "Days between ", date1, " and ", date2, ": ", daysBetween(date1, date2)
| 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 Nim, keeping it the same logically? | import times
proc daysBetween(date1, date2: string): int64 =
const Fmt = initTimeFormat("yyyy-MM-dd")
(date2.parse(Fmt, utc()) - date1.parse(Fmt, utc())).inDays
const Dates = [("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")]
for (date1, date2) in Dates:
echo "Days between ", date1, " and ", date2, ": ", daysBetween(date1, date2)
| #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 Nim code snippet into C++ without altering its behavior. | import times
proc daysBetween(date1, date2: string): int64 =
const Fmt = initTimeFormat("yyyy-MM-dd")
(date2.parse(Fmt, utc()) - date1.parse(Fmt, utc())).inDays
const Dates = [("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")]
for (date1, date2) in Dates:
echo "Days between ", date1, " and ", date2, ": ", daysBetween(date1, date2)
| #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 Nim code snippet into Java without altering its behavior. | import times
proc daysBetween(date1, date2: string): int64 =
const Fmt = initTimeFormat("yyyy-MM-dd")
(date2.parse(Fmt, utc()) - date1.parse(Fmt, utc())).inDays
const Dates = [("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")]
for (date1, date2) in Dates:
echo "Days between ", date1, " and ", date2, ": ", daysBetween(date1, date2)
| 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 Nim to Java with equivalent syntax and logic. | import times
proc daysBetween(date1, date2: string): int64 =
const Fmt = initTimeFormat("yyyy-MM-dd")
(date2.parse(Fmt, utc()) - date1.parse(Fmt, utc())).inDays
const Dates = [("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")]
for (date1, date2) in Dates:
echo "Days between ", date1, " and ", date2, ": ", daysBetween(date1, date2)
| 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 Nim to Python with equivalent syntax and logic. | import times
proc daysBetween(date1, date2: string): int64 =
const Fmt = initTimeFormat("yyyy-MM-dd")
(date2.parse(Fmt, utc()) - date1.parse(Fmt, utc())).inDays
const Dates = [("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")]
for (date1, date2) in Dates:
echo "Days between ", date1, " and ", date2, ": ", daysBetween(date1, date2)
|
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 Nim implementation. | import times
proc daysBetween(date1, date2: string): int64 =
const Fmt = initTimeFormat("yyyy-MM-dd")
(date2.parse(Fmt, utc()) - date1.parse(Fmt, utc())).inDays
const Dates = [("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")]
for (date1, date2) in Dates:
echo "Days between ", date1, " and ", date2, ": ", daysBetween(date1, date2)
|
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 Nim to VB. | import times
proc daysBetween(date1, date2: string): int64 =
const Fmt = initTimeFormat("yyyy-MM-dd")
(date2.parse(Fmt, utc()) - date1.parse(Fmt, utc())).inDays
const Dates = [("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")]
for (date1, date2) in Dates:
echo "Days between ", date1, " and ", date2, ": ", daysBetween(date1, date2)
| 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
|
Generate an equivalent VB version of this Nim code. | import times
proc daysBetween(date1, date2: string): int64 =
const Fmt = initTimeFormat("yyyy-MM-dd")
(date2.parse(Fmt, utc()) - date1.parse(Fmt, utc())).inDays
const Dates = [("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")]
for (date1, date2) in Dates:
echo "Days between ", date1, " and ", date2, ": ", daysBetween(date1, date2)
| 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 Nim to Go with equivalent syntax and logic. | import times
proc daysBetween(date1, date2: string): int64 =
const Fmt = initTimeFormat("yyyy-MM-dd")
(date2.parse(Fmt, utc()) - date1.parse(Fmt, utc())).inDays
const Dates = [("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")]
for (date1, date2) in Dates:
echo "Days between ", date1, " and ", date2, ": ", daysBetween(date1, date2)
| 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 Nim implementation into Go, maintaining the same output and logic. | import times
proc daysBetween(date1, date2: string): int64 =
const Fmt = initTimeFormat("yyyy-MM-dd")
(date2.parse(Fmt, utc()) - date1.parse(Fmt, utc())).inDays
const Dates = [("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")]
for (date1, date2) in Dates:
echo "Days between ", date1, " and ", date2, ": ", daysBetween(date1, date2)
| 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)
}
|
Generate an equivalent C version of this Perl code. | use feature 'say';
use Date::Calc qw(Delta_Days);
say Delta_Days(2018,7,13, 2019,9,13);
say Delta_Days(1900,1,1, 2000,1,1);
say Delta_Days(2000,1,1, 2100,1,1);
say Delta_Days(2020,1,1, 2019,10,1);
say Delta_Days(2019,2,29, 2019,3,1);
| #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 the snippet below in C so it works the same as the original Perl code. | use feature 'say';
use Date::Calc qw(Delta_Days);
say Delta_Days(2018,7,13, 2019,9,13);
say Delta_Days(1900,1,1, 2000,1,1);
say Delta_Days(2000,1,1, 2100,1,1);
say Delta_Days(2020,1,1, 2019,10,1);
say Delta_Days(2019,2,29, 2019,3,1);
| #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 the following code from Perl to C#, ensuring the logic remains intact. | use feature 'say';
use Date::Calc qw(Delta_Days);
say Delta_Days(2018,7,13, 2019,9,13);
say Delta_Days(1900,1,1, 2000,1,1);
say Delta_Days(2000,1,1, 2100,1,1);
say Delta_Days(2020,1,1, 2019,10,1);
say Delta_Days(2019,2,29, 2019,3,1);
| 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 the snippet below in C# so it works the same as the original Perl code. | use feature 'say';
use Date::Calc qw(Delta_Days);
say Delta_Days(2018,7,13, 2019,9,13);
say Delta_Days(1900,1,1, 2000,1,1);
say Delta_Days(2000,1,1, 2100,1,1);
say Delta_Days(2020,1,1, 2019,10,1);
say Delta_Days(2019,2,29, 2019,3,1);
| 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 a version of this Perl function in C++ with identical behavior. | use feature 'say';
use Date::Calc qw(Delta_Days);
say Delta_Days(2018,7,13, 2019,9,13);
say Delta_Days(1900,1,1, 2000,1,1);
say Delta_Days(2000,1,1, 2100,1,1);
say Delta_Days(2020,1,1, 2019,10,1);
say Delta_Days(2019,2,29, 2019,3,1);
| #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 the snippet below in C++ so it works the same as the original Perl code. | use feature 'say';
use Date::Calc qw(Delta_Days);
say Delta_Days(2018,7,13, 2019,9,13);
say Delta_Days(1900,1,1, 2000,1,1);
say Delta_Days(2000,1,1, 2100,1,1);
say Delta_Days(2020,1,1, 2019,10,1);
say Delta_Days(2019,2,29, 2019,3,1);
| #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 Perl to Java, same semantics. | use feature 'say';
use Date::Calc qw(Delta_Days);
say Delta_Days(2018,7,13, 2019,9,13);
say Delta_Days(1900,1,1, 2000,1,1);
say Delta_Days(2000,1,1, 2100,1,1);
say Delta_Days(2020,1,1, 2019,10,1);
say Delta_Days(2019,2,29, 2019,3,1);
| 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 Perl to Java with equivalent syntax and logic. | use feature 'say';
use Date::Calc qw(Delta_Days);
say Delta_Days(2018,7,13, 2019,9,13);
say Delta_Days(1900,1,1, 2000,1,1);
say Delta_Days(2000,1,1, 2100,1,1);
say Delta_Days(2020,1,1, 2019,10,1);
say Delta_Days(2019,2,29, 2019,3,1);
| 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 Perl code into Python while preserving the original functionality. | use feature 'say';
use Date::Calc qw(Delta_Days);
say Delta_Days(2018,7,13, 2019,9,13);
say Delta_Days(1900,1,1, 2000,1,1);
say Delta_Days(2000,1,1, 2100,1,1);
say Delta_Days(2020,1,1, 2019,10,1);
say Delta_Days(2019,2,29, 2019,3,1);
|
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 code in Python as shown below in Perl. | use feature 'say';
use Date::Calc qw(Delta_Days);
say Delta_Days(2018,7,13, 2019,9,13);
say Delta_Days(1900,1,1, 2000,1,1);
say Delta_Days(2000,1,1, 2100,1,1);
say Delta_Days(2020,1,1, 2019,10,1);
say Delta_Days(2019,2,29, 2019,3,1);
|
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 this program into VB but keep the logic exactly as in Perl. | use feature 'say';
use Date::Calc qw(Delta_Days);
say Delta_Days(2018,7,13, 2019,9,13);
say Delta_Days(1900,1,1, 2000,1,1);
say Delta_Days(2000,1,1, 2100,1,1);
say Delta_Days(2020,1,1, 2019,10,1);
say Delta_Days(2019,2,29, 2019,3,1);
| 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
|
Produce a language-to-language conversion: from Perl to VB, same semantics. | use feature 'say';
use Date::Calc qw(Delta_Days);
say Delta_Days(2018,7,13, 2019,9,13);
say Delta_Days(1900,1,1, 2000,1,1);
say Delta_Days(2000,1,1, 2100,1,1);
say Delta_Days(2020,1,1, 2019,10,1);
say Delta_Days(2019,2,29, 2019,3,1);
| 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
|
Can you help me rewrite this code in Go instead of Perl, keeping it the same logically? | use feature 'say';
use Date::Calc qw(Delta_Days);
say Delta_Days(2018,7,13, 2019,9,13);
say Delta_Days(1900,1,1, 2000,1,1);
say Delta_Days(2000,1,1, 2100,1,1);
say Delta_Days(2020,1,1, 2019,10,1);
say Delta_Days(2019,2,29, 2019,3,1);
| 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 Perl snippet to Go and keep its semantics consistent. | use feature 'say';
use Date::Calc qw(Delta_Days);
say Delta_Days(2018,7,13, 2019,9,13);
say Delta_Days(1900,1,1, 2000,1,1);
say Delta_Days(2000,1,1, 2100,1,1);
say Delta_Days(2020,1,1, 2019,10,1);
say Delta_Days(2019,2,29, 2019,3,1);
| 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 COBOL code snippet into C without altering its behavior. | COBOL
identification division.
program-id. days-between.
procedure division.
compute tally =
function integer-of-formatted-date('YYYY-MM-DD', '2019-11-24')
-
function integer-of-formatted-date('YYYY-MM-DD', '2000-01-01')
display tally
compute tally =
function integer-of-formatted-date('YYYYMMDD', '20191124')
-
function integer-of-formatted-date('YYYYMMDD', '20000101')
display tally
goback.
end program 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;
}
|
Generate an equivalent C version of this COBOL code. | COBOL
identification division.
program-id. days-between.
procedure division.
compute tally =
function integer-of-formatted-date('YYYY-MM-DD', '2019-11-24')
-
function integer-of-formatted-date('YYYY-MM-DD', '2000-01-01')
display tally
compute tally =
function integer-of-formatted-date('YYYYMMDD', '20191124')
-
function integer-of-formatted-date('YYYYMMDD', '20000101')
display tally
goback.
end program 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;
}
|
Ensure the translated C# code behaves exactly like the original COBOL snippet. | COBOL
identification division.
program-id. days-between.
procedure division.
compute tally =
function integer-of-formatted-date('YYYY-MM-DD', '2019-11-24')
-
function integer-of-formatted-date('YYYY-MM-DD', '2000-01-01')
display tally
compute tally =
function integer-of-formatted-date('YYYYMMDD', '20191124')
-
function integer-of-formatted-date('YYYYMMDD', '20000101')
display tally
goback.
end program 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;
}
}
|
Rewrite the snippet below in C# so it works the same as the original COBOL code. | COBOL
identification division.
program-id. days-between.
procedure division.
compute tally =
function integer-of-formatted-date('YYYY-MM-DD', '2019-11-24')
-
function integer-of-formatted-date('YYYY-MM-DD', '2000-01-01')
display tally
compute tally =
function integer-of-formatted-date('YYYYMMDD', '20191124')
-
function integer-of-formatted-date('YYYYMMDD', '20000101')
display tally
goback.
end program 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;
}
}
|
Convert the following code from COBOL to C++, ensuring the logic remains intact. | COBOL
identification division.
program-id. days-between.
procedure division.
compute tally =
function integer-of-formatted-date('YYYY-MM-DD', '2019-11-24')
-
function integer-of-formatted-date('YYYY-MM-DD', '2000-01-01')
display tally
compute tally =
function integer-of-formatted-date('YYYYMMDD', '20191124')
-
function integer-of-formatted-date('YYYYMMDD', '20000101')
display tally
goback.
end program 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 COBOL code in C++. | COBOL
identification division.
program-id. days-between.
procedure division.
compute tally =
function integer-of-formatted-date('YYYY-MM-DD', '2019-11-24')
-
function integer-of-formatted-date('YYYY-MM-DD', '2000-01-01')
display tally
compute tally =
function integer-of-formatted-date('YYYYMMDD', '20191124')
-
function integer-of-formatted-date('YYYYMMDD', '20000101')
display tally
goback.
end program 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;
}
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.