Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Round Time To Nearest 15 Minutes
Message
From
01/06/2016 01:35:03
 
 
To
31/05/2016 21:15:10
General information
Forum:
Visual FoxPro
Category:
Coding, syntax & commands
Environment versions
Visual FoxPro:
VFP 9 SP2
OS:
Windows 7
Network:
Windows Server 2008 R2
Database:
MS SQL Server
Application:
Desktop
Miscellaneous
Thread ID:
01636901
Message ID:
01636902
Views:
93
>I've been struggling with rounding time to the nearest 15. Any suggestions? I figured I need to convert the minutes to seconds first but can figure out the equation to round to the nearest 15 minutes.
>
>TIA
>Jeff

Interesting breakfast problem.
Generalized to any fraction of an hour, expressed in minutes (1 to 30).
LOCAL ltTime AS Datetime

m.ltTime = DATETIME()

? m.ltTime,NearestMinutes(m.ltTime,15)

* Calculate nearest time, in minutes, for a given time
FUNCTION NearestMinutes (ttTime AS Datetime, tnMinutes AS Integer) AS Datetime

	ASSERT TYPE("m.tnMinutes") = "N" AND BETWEEN(m.tnMinutes,1,30) ;
		MESSAGE "Minutes must be a number between 1 and 30"

	LOCAL lnMinutes AS Integer
	LOCAL lnTarget AS Integer

	* minutes part of time, in seconds
	m.lnMinutes = MINUTE(m.ttTime) * 60 + SEC(m.ttTime)
	* the fraction of an hour we are targeting
	m.lnTarget = INT(m.tnMinutes) * 60

	LOCAL lnNearest AS Integer

	* find the nearest fraction, in seconds
	m.lnNearest = ROUND(m.lnMinutes / m.lnTarget,0) * m.lnTarget

	LOCAL ltNearest AS Datetime

	* add it to the zeroed initial time, and return it as a result
	m.ltNearest = (m.ttTime - m.lnMinutes) + m.lnNearest
	
	RETURN m.ltNearest

ENDFUNC
----------------------------------
António Tavares Lopes
Previous
Reply
Map
View

Click here to load this message in the networking platform