Created
January 15, 2021 00:29
-
-
Save alireza19/36ff581e9ec7b35928b179ae714e764c to your computer and use it in GitHub Desktop.
Arcurve Summer 2021 Internship Coding Challenge
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
public class Program | |
{ | |
public static void Main() | |
{ | |
Program testProgram = new Program(); | |
DateTime aDate = new DateTime(2010, 2, 14); | |
DateTime utcMonthStart = new DateTime(); | |
DateTime utcNextMonthStart = new DateTime(); | |
testProgram.GetMonthRangeInUtc(aDate, out utcMonthStart, out utcNextMonthStart); | |
Console.WriteLine("The first second of the first day of the month is: {0}", utcMonthStart); | |
Console.WriteLine("The first second of the first day of the next month: {0}", utcNextMonthStart); | |
} | |
/// <summary> | |
/// Given a local date, get UTC date/time values for the first second of the first day | |
/// of the month containing that date and the first second of the first day of next month. | |
/// </summary> | |
/// <example> | |
/// When run in Calgary, an input date of February 14, 2010 yields: | |
/// The first second of the first day of the month is: 7:00:00am, February 1, 2010 (UTC) | |
/// The first second of the first day of the next month: 7:00:00am, March 1, 2010 (UTC) | |
/// </example> | |
/// <param name="aDate">An arbitrary date, in localtime | |
/// <param name="utcMonthStart">Output: First day of the month in which aDate occurs, in UTC | |
/// <param name="utcNextMonthStart">Output: First day of the month after aDate, in UTC | |
void GetMonthRangeInUtc(DateTime aDate, out DateTime utcMonthStart, out DateTime utcNextMonthStart) | |
{ | |
// errors are annotated in the below comments and numbered by error i (1...4) | |
// compute the first day of the month containing aDate and successive months | |
//DateTime[] monthStart = new DateTime[2]; | |
// error 2: Run-time exception: Index outside the bounds of the array. | |
// solution: don't use a loop (why would you need to?) | |
//for (int i = 0; i <= monthStart.Length; i++) | |
//{ | |
// error 1: aDate.Month causes compilation error since it is read only | |
// solution: use a temp variable in the loop | |
// int month = aDate.Month; | |
// monthStart[i] = new DateTime(aDate.Year, month++, 1); | |
//} | |
// Compute the offset from UTC to our local time (UTC + offset = localtime). | |
// error 3: utcOffset is not adjusted to 7 hour offset for Calgary | |
//TimeSpan utcOffset = TimeZone.CurrentTimeZone.GetUtcOffset(aDate); | |
// convert local times to UTC (UTC = localtime - offset) | |
// error 4: does not handle end of month case, ex. Feb 28 11:59PM | |
//utcMonthStart = monthStart[0].Subtract(utcOffset); | |
//utcNextMonthStart = monthStart[1].Subtract(utcOffset); | |
// get the beginning of the month | |
DateTime monthStart = new DateTime(aDate.Year, aDate.Month, 1); | |
// get the local offset | |
TimeSpan localOffset = new TimeSpan(0,7,0,0); | |
// beginning of given month and converting to UTC | |
utcMonthStart = monthStart.Add(localOffset); | |
// beginning of next given month by adding 1 month | |
utcNextMonthStart = monthStart.AddMonths(1).Add(localOffset); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment