Creating a date adder

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
0
down vote

favorite












We are thinking about hiring more developers so I was asked to compose a test. I have been looking online for ideas. I found this question: https://stackoverflow.com/questions/23534528/how-to-add-n-days-to-a-date-in-java-without-importing-date-calendar-from-java-ap. I have attempted this myself as follows:



public class Date : IEquatable<Date>

private readonly static int _daysInMonth = 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ;
private readonly int _month;
private readonly int _day;
private readonly int _year;

public Date(int day, int month, int year)

this._day = day;
this._month = month;
this._year = year;


//static because thinking about using in constructor for validation.
public static int MaxDays(int month, int year)

int maxDays = _daysInMonth[month - 1];
if (month == 2)
((year % 4 == 0) && (year % 100 != 0)))
maxDays = maxDays + 1;

return maxDays;


public Date AddDays(int daysToAdd)

int day = this._day + daysToAdd;
int month = this._month;
int year = this._year;
while (day > MaxDays(month,year))

day = day - MaxDays(month,year);
month++;
if (month > 12)

year++;
month = 1;


return new Date(day, month, year);


public override bool Equals(object obj)

return Equals(obj as Date);


public override int GetHashCode()

unchecked

int hash = 17;
hash = hash * 23 + _day.GetHashCode();
hash = hash * 23 + _month.GetHashCode();
hash = hash * 23 + _year.GetHashCode();
return hash;



public static bool operator ==(Date date1, Date date2)


public static bool operator !=(Date date1, Date date2)

return !(date1 == date2);


public bool Equals(Date other)

if (ReferenceEquals(other, null))
return false;
return _day == other._day && _month == other._month && _year == other._year;




I would be grateful for comments. I reviewed the terms of posting here: https://codereview.stackexchange.com/help/on-topic. This is real code (no stubs) that works and therefore I believe it is on topic.







share|improve this question

















  • 2




    If I were hiring developers, which I have been known to do every few years, I would choose someone who uses what is already tried, true, and tested in the framework, rather than pay them to take a lot of time reinventing the wheel.
    – Rick Davin
    Feb 27 at 13:08






  • 1




    @RickDavin indeed, this class is more than unnecessary. Also the entire implementation could be based on the DateTime.
    – t3chb0t
    Feb 27 at 13:14










  • @Rick Davin, I agree. Do you think the questioner in my linked question was asked to "reinvent the wheel" or do you think they were expected to use DateTime?
    – w0051977
    Feb 27 at 13:47










  • While the original questioner was trying to reinvent the wheel, you should forget about that. Your goal is to hire developers, and an intermediate task for you to do that is to compose a competency test. I would suggest the one posted here is not the right test and that you should keep looking. If I were hiring a .NET developer, I want to make sure they know .NET. If I were interviewing, I'd give bonus points to the candidate who asks what I did not use DateTime or possibly NodaTime.
    – Rick Davin
    Feb 27 at 14:38










  • I tried to code something similar. I think an Int16 based date would be a more interesting question. Here you are 3 Int32.
    – paparazzo
    Feb 28 at 7:46

















up vote
0
down vote

favorite












We are thinking about hiring more developers so I was asked to compose a test. I have been looking online for ideas. I found this question: https://stackoverflow.com/questions/23534528/how-to-add-n-days-to-a-date-in-java-without-importing-date-calendar-from-java-ap. I have attempted this myself as follows:



public class Date : IEquatable<Date>

private readonly static int _daysInMonth = 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ;
private readonly int _month;
private readonly int _day;
private readonly int _year;

public Date(int day, int month, int year)

this._day = day;
this._month = month;
this._year = year;


//static because thinking about using in constructor for validation.
public static int MaxDays(int month, int year)

int maxDays = _daysInMonth[month - 1];
if (month == 2)
((year % 4 == 0) && (year % 100 != 0)))
maxDays = maxDays + 1;

return maxDays;


public Date AddDays(int daysToAdd)

int day = this._day + daysToAdd;
int month = this._month;
int year = this._year;
while (day > MaxDays(month,year))

day = day - MaxDays(month,year);
month++;
if (month > 12)

year++;
month = 1;


return new Date(day, month, year);


public override bool Equals(object obj)

return Equals(obj as Date);


public override int GetHashCode()

unchecked

int hash = 17;
hash = hash * 23 + _day.GetHashCode();
hash = hash * 23 + _month.GetHashCode();
hash = hash * 23 + _year.GetHashCode();
return hash;



public static bool operator ==(Date date1, Date date2)


public static bool operator !=(Date date1, Date date2)

return !(date1 == date2);


public bool Equals(Date other)

if (ReferenceEquals(other, null))
return false;
return _day == other._day && _month == other._month && _year == other._year;




I would be grateful for comments. I reviewed the terms of posting here: https://codereview.stackexchange.com/help/on-topic. This is real code (no stubs) that works and therefore I believe it is on topic.







share|improve this question

















  • 2




    If I were hiring developers, which I have been known to do every few years, I would choose someone who uses what is already tried, true, and tested in the framework, rather than pay them to take a lot of time reinventing the wheel.
    – Rick Davin
    Feb 27 at 13:08






  • 1




    @RickDavin indeed, this class is more than unnecessary. Also the entire implementation could be based on the DateTime.
    – t3chb0t
    Feb 27 at 13:14










  • @Rick Davin, I agree. Do you think the questioner in my linked question was asked to "reinvent the wheel" or do you think they were expected to use DateTime?
    – w0051977
    Feb 27 at 13:47










  • While the original questioner was trying to reinvent the wheel, you should forget about that. Your goal is to hire developers, and an intermediate task for you to do that is to compose a competency test. I would suggest the one posted here is not the right test and that you should keep looking. If I were hiring a .NET developer, I want to make sure they know .NET. If I were interviewing, I'd give bonus points to the candidate who asks what I did not use DateTime or possibly NodaTime.
    – Rick Davin
    Feb 27 at 14:38










  • I tried to code something similar. I think an Int16 based date would be a more interesting question. Here you are 3 Int32.
    – paparazzo
    Feb 28 at 7:46













up vote
0
down vote

favorite









up vote
0
down vote

favorite











We are thinking about hiring more developers so I was asked to compose a test. I have been looking online for ideas. I found this question: https://stackoverflow.com/questions/23534528/how-to-add-n-days-to-a-date-in-java-without-importing-date-calendar-from-java-ap. I have attempted this myself as follows:



public class Date : IEquatable<Date>

private readonly static int _daysInMonth = 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ;
private readonly int _month;
private readonly int _day;
private readonly int _year;

public Date(int day, int month, int year)

this._day = day;
this._month = month;
this._year = year;


//static because thinking about using in constructor for validation.
public static int MaxDays(int month, int year)

int maxDays = _daysInMonth[month - 1];
if (month == 2)
((year % 4 == 0) && (year % 100 != 0)))
maxDays = maxDays + 1;

return maxDays;


public Date AddDays(int daysToAdd)

int day = this._day + daysToAdd;
int month = this._month;
int year = this._year;
while (day > MaxDays(month,year))

day = day - MaxDays(month,year);
month++;
if (month > 12)

year++;
month = 1;


return new Date(day, month, year);


public override bool Equals(object obj)

return Equals(obj as Date);


public override int GetHashCode()

unchecked

int hash = 17;
hash = hash * 23 + _day.GetHashCode();
hash = hash * 23 + _month.GetHashCode();
hash = hash * 23 + _year.GetHashCode();
return hash;



public static bool operator ==(Date date1, Date date2)


public static bool operator !=(Date date1, Date date2)

return !(date1 == date2);


public bool Equals(Date other)

if (ReferenceEquals(other, null))
return false;
return _day == other._day && _month == other._month && _year == other._year;




I would be grateful for comments. I reviewed the terms of posting here: https://codereview.stackexchange.com/help/on-topic. This is real code (no stubs) that works and therefore I believe it is on topic.







share|improve this question













We are thinking about hiring more developers so I was asked to compose a test. I have been looking online for ideas. I found this question: https://stackoverflow.com/questions/23534528/how-to-add-n-days-to-a-date-in-java-without-importing-date-calendar-from-java-ap. I have attempted this myself as follows:



public class Date : IEquatable<Date>

private readonly static int _daysInMonth = 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ;
private readonly int _month;
private readonly int _day;
private readonly int _year;

public Date(int day, int month, int year)

this._day = day;
this._month = month;
this._year = year;


//static because thinking about using in constructor for validation.
public static int MaxDays(int month, int year)

int maxDays = _daysInMonth[month - 1];
if (month == 2)
((year % 4 == 0) && (year % 100 != 0)))
maxDays = maxDays + 1;

return maxDays;


public Date AddDays(int daysToAdd)

int day = this._day + daysToAdd;
int month = this._month;
int year = this._year;
while (day > MaxDays(month,year))

day = day - MaxDays(month,year);
month++;
if (month > 12)

year++;
month = 1;


return new Date(day, month, year);


public override bool Equals(object obj)

return Equals(obj as Date);


public override int GetHashCode()

unchecked

int hash = 17;
hash = hash * 23 + _day.GetHashCode();
hash = hash * 23 + _month.GetHashCode();
hash = hash * 23 + _year.GetHashCode();
return hash;



public static bool operator ==(Date date1, Date date2)


public static bool operator !=(Date date1, Date date2)

return !(date1 == date2);


public bool Equals(Date other)

if (ReferenceEquals(other, null))
return false;
return _day == other._day && _month == other._month && _year == other._year;




I would be grateful for comments. I reviewed the terms of posting here: https://codereview.stackexchange.com/help/on-topic. This is real code (no stubs) that works and therefore I believe it is on topic.









share|improve this question












share|improve this question




share|improve this question








edited Feb 27 at 8:13
























asked Feb 27 at 8:02









w0051977

389112




389112







  • 2




    If I were hiring developers, which I have been known to do every few years, I would choose someone who uses what is already tried, true, and tested in the framework, rather than pay them to take a lot of time reinventing the wheel.
    – Rick Davin
    Feb 27 at 13:08






  • 1




    @RickDavin indeed, this class is more than unnecessary. Also the entire implementation could be based on the DateTime.
    – t3chb0t
    Feb 27 at 13:14










  • @Rick Davin, I agree. Do you think the questioner in my linked question was asked to "reinvent the wheel" or do you think they were expected to use DateTime?
    – w0051977
    Feb 27 at 13:47










  • While the original questioner was trying to reinvent the wheel, you should forget about that. Your goal is to hire developers, and an intermediate task for you to do that is to compose a competency test. I would suggest the one posted here is not the right test and that you should keep looking. If I were hiring a .NET developer, I want to make sure they know .NET. If I were interviewing, I'd give bonus points to the candidate who asks what I did not use DateTime or possibly NodaTime.
    – Rick Davin
    Feb 27 at 14:38










  • I tried to code something similar. I think an Int16 based date would be a more interesting question. Here you are 3 Int32.
    – paparazzo
    Feb 28 at 7:46













  • 2




    If I were hiring developers, which I have been known to do every few years, I would choose someone who uses what is already tried, true, and tested in the framework, rather than pay them to take a lot of time reinventing the wheel.
    – Rick Davin
    Feb 27 at 13:08






  • 1




    @RickDavin indeed, this class is more than unnecessary. Also the entire implementation could be based on the DateTime.
    – t3chb0t
    Feb 27 at 13:14










  • @Rick Davin, I agree. Do you think the questioner in my linked question was asked to "reinvent the wheel" or do you think they were expected to use DateTime?
    – w0051977
    Feb 27 at 13:47










  • While the original questioner was trying to reinvent the wheel, you should forget about that. Your goal is to hire developers, and an intermediate task for you to do that is to compose a competency test. I would suggest the one posted here is not the right test and that you should keep looking. If I were hiring a .NET developer, I want to make sure they know .NET. If I were interviewing, I'd give bonus points to the candidate who asks what I did not use DateTime or possibly NodaTime.
    – Rick Davin
    Feb 27 at 14:38










  • I tried to code something similar. I think an Int16 based date would be a more interesting question. Here you are 3 Int32.
    – paparazzo
    Feb 28 at 7:46








2




2




If I were hiring developers, which I have been known to do every few years, I would choose someone who uses what is already tried, true, and tested in the framework, rather than pay them to take a lot of time reinventing the wheel.
– Rick Davin
Feb 27 at 13:08




If I were hiring developers, which I have been known to do every few years, I would choose someone who uses what is already tried, true, and tested in the framework, rather than pay them to take a lot of time reinventing the wheel.
– Rick Davin
Feb 27 at 13:08




1




1




@RickDavin indeed, this class is more than unnecessary. Also the entire implementation could be based on the DateTime.
– t3chb0t
Feb 27 at 13:14




@RickDavin indeed, this class is more than unnecessary. Also the entire implementation could be based on the DateTime.
– t3chb0t
Feb 27 at 13:14












@Rick Davin, I agree. Do you think the questioner in my linked question was asked to "reinvent the wheel" or do you think they were expected to use DateTime?
– w0051977
Feb 27 at 13:47




@Rick Davin, I agree. Do you think the questioner in my linked question was asked to "reinvent the wheel" or do you think they were expected to use DateTime?
– w0051977
Feb 27 at 13:47












While the original questioner was trying to reinvent the wheel, you should forget about that. Your goal is to hire developers, and an intermediate task for you to do that is to compose a competency test. I would suggest the one posted here is not the right test and that you should keep looking. If I were hiring a .NET developer, I want to make sure they know .NET. If I were interviewing, I'd give bonus points to the candidate who asks what I did not use DateTime or possibly NodaTime.
– Rick Davin
Feb 27 at 14:38




While the original questioner was trying to reinvent the wheel, you should forget about that. Your goal is to hire developers, and an intermediate task for you to do that is to compose a competency test. I would suggest the one posted here is not the right test and that you should keep looking. If I were hiring a .NET developer, I want to make sure they know .NET. If I were interviewing, I'd give bonus points to the candidate who asks what I did not use DateTime or possibly NodaTime.
– Rick Davin
Feb 27 at 14:38












I tried to code something similar. I think an Int16 based date would be a more interesting question. Here you are 3 Int32.
– paparazzo
Feb 28 at 7:46





I tried to code something similar. I think an Int16 based date would be a more interesting question. Here you are 3 Int32.
– paparazzo
Feb 28 at 7:46
















active

oldest

votes











Your Answer




StackExchange.ifUsing("editor", function ()
return StackExchange.using("mathjaxEditing", function ()
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
);
);
, "mathjax-editing");

StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "196"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);








 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f188425%2fcreating-a-date-adder%23new-answer', 'question_page');

);

Post as a guest



































active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes










 

draft saved


draft discarded


























 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f188425%2fcreating-a-date-adder%23new-answer', 'question_page');

);

Post as a guest













































































Popular posts from this blog

Greedy Best First Search implementation in Rust

Function to Return a JSON Like Objects Using VBA Collections and Arrays

C++11 CLH Lock Implementation