Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle a day that starts from 06:00 and ends at 30:00

Tags:

I am working with a case where the client has a 30 hour day.

The day starts at 6 am and then goes around to 6 am the next day, but when they come to 1 am the next day, they take it as 25:00 hours. 2 am will be 26:00 hours and so forth...

Now, i want to know, is there a way to handle this in c#'s DateTime class or do i need to do it the long way and split it all up?

UPDATE:

It's a Media Agency in Australia. Just to explain again, the day starts at 06:00 am (12 Jan 2012), when it comes to midnight it will be 24:00. Now when it is 01:00 am (13 Jan 2012) the next day, the client takes it as 25:00 hours (12 Jan 2012).

They still have 24 hours in a day. The only difference is that their day starts at 6 am and not 00 hours like us.

UPDATE:

XML representation of a typical program i need to work with. Note: Removed CHANNEL_CODE and CHANNEL_NAME.

 <PROGRAMME>   <PROGRAMME_ID>1</PROGRAMME_ID>   <PROGRAMME_NAME>Mass For You At Home</PROGRAMME_NAME>   <CHANNEL_CODE>SomeCode</CHANNEL_CODE>   <CHANNEL_NAME>SomeChannel</CHANNEL_NAME>   <TX_DATE>20120101</TX_DATE>   <START_TIME>06:00</START_TIME>   <DURATION>1800</DURATION>   <AGENCY_AVAIL>C</AGENCY_AVAIL>   <SALES_AVAIL>90</SALES_AVAIL>   <SSB>N</SSB>  </PROGRAMME> </PROGRAMME>   <PROGRAMME>   <PROGRAMME_ID>2</PROGRAMME_ID>   <PROGRAMME_NAME>Home Shopping</PROGRAMME_NAME>   <CHANNEL_CODE>SomeCode</CHANNEL_CODE>   <CHANNEL_NAME>SomeChannel</CHANNEL_NAME>   <TX_DATE>20120101</TX_DATE>   <START_TIME>26:00</START_TIME>   <DURATION>1800</DURATION>   <AGENCY_AVAIL>C</AGENCY_AVAIL>   <SALES_AVAIL>0</SALES_AVAIL>   <SSB>N</SSB>  </PROGRAMME> 

So, is there maybe a way to adjust the DateTime class to start at 06:00 and end at 30:00?

like image 551
Willem Avatar asked Jan 12 '12 08:01

Willem


2 Answers

This sounds a bit like the situation where you have a business that covers multiple timezones - it's possible, then, to have a continuous day that is longer than 24 hours.

However, this doesn't mean that you have to adjust the length of a day - a day is an international convention, and, unless Jon Skeet actually decides to use the giant gravity gun he has undoubedly built (in a day ;-) and uses it to change the rotational speed of the earth, extending the alternating period of light and darkness we refer to as a day, your best bet is to probably use the concept of a shift or timeslot;

A shift (in your case, a timeslot!) has a work day, a length, and a timezone. You can then:

  • Sum all of the hours an advert was shown for a workday (sum[length] where date = workday)
  • Sum all of the hours an advert was shown for a timezone (sum[length] where timezone = x group by workday
  • Sum all of the hours an advert was on for a particular astronomical date (work out the number of hours between the workday.starttime and midnight versus the length)

It's best not to refer to these as days as it confuses straightforward terminology.

In your instance, you aren't even bothered about the timezone. I think all you really need is the date/time the advert timeslot is due to start, and the number of hours it is due to be shown for.

EDIT: In the case of your XML, you can still use the above concept. You can either:

1) Clean it up when you get the XML and store it as a 'proper' datetime - so work out what the UTC starttime is and use the duration

2) Create a class that just converts this to normal datetime representation with the length. The benefit of this approach is that you can also go the other way, back to the source convention.

Realistically, I think that's really all you need.

For example, in the xml above, you could create a class; something like this should do the trick:

public class AdvertDate{      public DateTime TransmissionDate { get; set;} //Store as 06:00 on the TX_Date      public int AdvertStartTime { get; set; } //Store as 0 - 30      public int Duration { get; set; } //Store as 18 - assuming whole numbers so change if needed             public DateTime RealDate {         get{             return TransmissionDate.AddHours(AdvertStartTime);         }     }       public AdvertDate(){      }      public AdvertDate(DateTime transmissionDate, int advertStartTime, int duration){         TransmissionDate = transmissionDate;         AdvertStartTime = advertStartTime;         Duration = duration;     }       public AdvertDate ConvertRealDateTimeToAdvertDate(DateTime realDateTime, int advertDuration){          if(realDateTime.Hour < 6)         {             DateTime  advertDateTime = realDateTime.AddDays(-1).Date.AddHours(6);              return new AdvertDate(advertDateTime, 24+realDateTime.Hour, advertDuration);         }         else{             return new AdvertDate(realDateTime.Date.AddHours(6), realDateTime.Hour, advertDuration);         }      }       public void LoadFromXml(){         //Xml Loading here (or in a dedicated class or wherever)     }    } 
like image 102
dash Avatar answered Sep 27 '22 23:09

dash


Always store your data in some universal standard format and treat any other requirements as display/output formatting only.

When dealing with date/times that originate from different timezones (different to the server or also between clients), it is best to store them using the C# DateTimeOffset structure along with the datetimeoffset field type of SQL Server 2008 R2.

This gives you the power to determine not just "universal time", but also to know what the times are relative to any given client.

You have not stated the actual uses for the 30 hour days, so I can't provide specifics without more information, but as stated by others you would need to provide a custom class for your funky date output.

like image 38
Gone Coding Avatar answered Sep 28 '22 00:09

Gone Coding