Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Compare UTC to selected time

Status
Not open for further replies.

ralphtrent

Programmer
Jun 2, 2003
958
US
Hi
I have a service that processes request based on a date and time. The two are seperate equations. The user can select that starting date and the ending date they want to be notified and between a start time and end time. The values are saved to a db seperatly. The time is saved as a military time converted to GMT. So 9am eastern becomes 1300 in the database.

The problem I am having is if a users END TIME is less then the users Start Time (ie 9:00 am to 8:30am, to process with in 24 hours, I read the db every 30 minutes to see what needs to be processed, hence the 30 minutes difference). This is stored in the db as 1300 and 1000 (with a -4 difference for GMT to Eastern).

When I go to compare the current time against the users selected time, I use this code :

Code:
if ((DateTime.UtcNow.TimeOfDay >= ltsStartTime && DateTime.UtcNow.TimeOfDay <= ltsEndTime))

since DateTime.UtcNow.TimeOfDay <= ltsEndTime seems to always return false (at least when testing the values I am testing with), this request will never get processed. Does anyone have idea how I can get around this?
 
The problem I am having is if a users END TIME is less then the users Start Time
this is basic input validation. don't let the user enter an end time before the start time.

if you cannot control the input validation, then you need another process to search the system where starttime > endtime and handle these situations (hopefully correcting the user input).

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
 
the time selections are more a time of day, dates are not part of this drop down so there is not saying that what they are selecting is wrong really.
 
maybe you need to explain the context and domain more. in your first post the problem was end time was before start time. in your latest post it's not a problem.

this could be because of the context of the process, but I have never come across a situation where an end time before a start time is valid.

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
 
ill rephrase it, more like time of daye. if a person wants to be notified mon-fri 5:00 pm throuh 9:00am. I store in the db an int. That int is the military time of the requested start and the requested "til" time. Does that make more sense? I need to have a scheduled task to allow a user received notifications between their requested time so they do not get emails all day. Only during the times they specify. So End Time may not be the best choice of terms, ill try through time.
 
the terminology makes sense, it was the context I was not understanding. I'm not sure saving the time as an integer is the best solution though. the problem you are having is 5pm to 9am crosses dates, but your data has not concept of dates, only numbers. you may need to include a "days offset by" column in the database as well. then you could perform the calculation
Code:
var beginTime = 1200;
var start = DateTime.Today;
        .AddHours(int.Parse(beginTime.ToString("0000").Left(2)))
        .AddMinutes(int.Parse(beginTime.ToString("0000").(2)));

var offSet = 1;
var endTime = 900;
var end = DateTime.Today;
        .AddDays(offSet)
        .AddHours(int.Parse(endTime.ToString("0000").Left(2)))
        .AddMinutes(int.Parse(endTime.ToString("0000").Right(2)));

var current = DateTime.Now;

return start <= current && current <= end;
where 1200, 900 and 1 would be values pulled from the database

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top