Esbasic reusable. Net class library (01) -- time elapsed time

Source: Internet
Author: User

(If you can read this article against the source code, the effect will be better .)


Assume that our staff Punching System needs to set the company's work time, work time, and determine whether employees are late and leave early.

In what way can we record the time similar to the commuting time, which is only the hour, minute, and second without the year, month, and day? You can use datetime, but is it appropriate? It is awkward to use datetime to indicate the commuting time, because we need to specify the specific day for the commuting time.

I designed esbasic. Clock Time to abstract the concept similar to commuting time. Shorttime has only three required attributes, namely, hour, minute, and second.

The watermark time image is as follows:





2.Applicable scenarios:

Any scenario that only uses time minutes and seconds to represent the time.


3Design ideas and implementation

The Design and Implementation of shorttime are quite simple. The class diagram is as follows: 

Shorttime has three attributes: hour, minute, and second. The Hour value ranges from 0 to 24 hours ~ 23. The value range of minute and second is 0 ~ 59. If the value of these attributes exceeds the value range, the Set Method of the attribute automatically modifies them to the correct value.

Shorttime implements the icomparable generic interface, indicating that the same time objects can be compared with each other. The greater the value of the time indicated by the specified time, the greater the value of the specified time, which is completely consistent.

The processing time is serializable, indicating remote transmission through remoting.

Shorttime provides a very useful method called isontime to determine whether a specified time has been reached. This method accepts two parameters: Target and maxtoleranceinsecs. Target is the target for comparison with the current object, maxtoleranceinsecs indicates that the current object and the target object represent the maximum time difference allowed at the same time point (in seconds ). For example, if the value of maxtoleranceinsecs is 3 seconds, 22:15:00 and 22:15:02 are regarded as the same time. The isontime method returns true.


4.Precautions for use

(1) The accuracy of interval time can only be seconds, and smaller precision such as milliseconds and microseconds cannot be recorded.

(2) The value of the maxtoleranceinsecs parameter of the isontime method is determined by the maximum time-point error allowed by our system requirements. For example, if your polling engine detects the specified time every five seconds, set the maxtoleranceinsecs parameter to 5 instead of greater or smaller.



We can combine the timer time object that represents the work time and the timer time object that represents the work time into a timer timescript object, which is used to represent the work time.

Esbasic. effectimes.pdfIt is used to indicate a time range. Of course, the maximum length of this range is 24 hours (more accurate, it should be 23 hours, 59 minutes, 59 seconds). That is to say, timescript cannot represent a time period across days.

If we want to determine whether a time point is within the time range specified by timessag, we can call the contains method of timessag.

It is difficult to compare the size of the shorttimescript object, but it can be compared to whether it is equal. Therefore, shorttimescript implements the "=" operator.

In addition, the circletaskswitcher (Cyclic task switcher) introduced later uses the processing time mentioned in this section.

Note: The esbasic source code can be downloaded at
Esbasic open source Preface


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.