HecTime Class
HecTime objects are used to manipulate dates and times and to convert dates and times among different formats. To use HecTime objects, the HecTime class must be imported from the hec.heclib.util module (e.g. "from hec.heclib.util import HecTime"). After importing the class, a new HecTime object can be created by calling HecTime() (e.g. "myTime = HecTime()"). *HecTime methods are described below.
HecTime Method | Returns | Description |
add(HecTime increment) | None | Adds the specified increment to the object's date and time. |
add(integer increment) | None | Adds the specified increment in minutes to the object's date and time. |
addDays(integer days) | None | Adds the specified number of days to the time |
addHours (integer hours) | None | Adds the specified number of hours to the time |
addMinutes (integer minutes) | None | Adds the specified number of minutes to the time |
addSeconds (integer seconds) | None | Adds the specified number of seconds to the time |
adjustToIntervalOffset(integer intervalMinutes, integer offset) | None | Changes the time to be at the standard time for that interval, according to HEC-DSS. For example, will adjust daily data to 2400 hours of that day. |
compareTimes(HecTime other) | integer | Returns one of the following values: |
-1 | The object's date and time is less than the other object's | |
0 | The objects' dates and times are equal | |
1 | The object's date and time is greater than the other object's | |
computeNumberIntervals(HecTime otherTime, integer intervalInMins) | integer | Determines the number of periods between the time set and the time passed in, where intervalInMins is the interval length. |
convertTimeZone(HecTime hecTime, TimeZone fromTimeZone, TimeZone toTimeZone) | None | Changes the time passed in from the first time zone to the second time zone. Adjusts for daylight savings time, according to the time given. |
date() | string | Returns a string representation of the object's date. Same as date(2). |
date(integer format) | string | Returns a string representation of the object's date, formatted according to the integer parameter.1 |
dateAndTime() | string | Returns a string representation of the object's date and time. Same as dateAndTime(2). |
dateAndTime(integer format) | string | Returns a string representation of the object's date and time, formatted according to the integer parameter. 1 |
day() | integer | Returns the day portion of the object's date as an integer |
dayOfWeek() | integer | Returns the day of the week with Sunday starting as 1, Monday 2 |
dayOfWeekName() | string | Returns the name of the day of the week, such as "Sunday" |
Method | Returns | Description |
dayOfYear() | integer | Returns the Julian day of the year, with Jan.1 returned as "1". |
equalTo(HecTime other) | Boolean | Returns True if the object's date and time is equal to the other object's date and time. Returns False otherwise. |
greaterThan(HecTime other) | Boolean | Returns True if the object's date and time is greater (later) than the other object's date and time. Returns False otherwise. |
greaterThanEqualTo(HecTime other) | Boolean | Returns True if the object's date and time is greater (later) than or equal to the other object's date and time. Returns False otherwise. |
getJavaDate(integer minutesTimezoneOffset) | Date | Returns the Java Date for this time |
getMinutes() | long | Returns the number of minutes since Jan 1, 1900 |
getTimeInMillis() | long | Returns the number of milliseconds since January 1, 1970, 00:00:00 GMT |
getTimeWindow (String userLine, HecTime startTime, HecTime endTime) | integer Success=0 | Takes a user entered line with start date/time and end date/time and converts to HecTime. |
getXMLDateTime (integer minutesTimezoneOffset) | string | Returns the date in XML format |
hour() | integer | Returns the hours portion of the object's time as an integer |
isDefined() | Boolean | Returns True if the object is set to a valid date and time and False if not. |
isDateDefined() | Boolean | Returns True if the date portion is valid (ignores time) and False if not. |
isTimeDefined() | Boolean | Returns True if the time portion is valid (ignores date) and False if not. |
increment(integer numberPeriods, integer minutesInPeriod) | None | Adds the number of periods to the date |
isoDate() | integer | Returns the date in ISO format (YYMMDD) |
isoTime() | integer | Returns the time in ISO format (HHMMSS) |
lessThan(HecTime other) | Boolean | Returns True if the object's date and time is less (earlier) than the other object's date and time. Returns False otherwise. |
lessThanEqualTo(HecTime other) | Boolean | Returns True if the object's date and time is less (earlier) than or equal to the other object's date and time. Returns False otherwise. |
Method | Returns | Description |
julian() | integer | Returns the number of days since Jan 1, 1900 |
minute() | integer | Returns the minutes portion of the object's time as an integer |
month() | integer | Returns the month portion of the object's date as an integer |
notEqualTo(HecTime other) | Boolean | Returns False if the object's date and time is equal to the other object's date and time. Returns True otherwise. |
second() | integer | Returns the seconds portion of the object's time as an integer |
setCurrent() | None | Sets the date/time to the current time. |
set (integer time) | None | Sets the object to the date and time represented by the integer (minutes since 31Dec1899 00:00) |
set(string dateAndTime) | integer | Sets the object to the date and time represented by the string, and returns zero if successful. |
set (string date, string time) | integer | Sets the object to the date represented by the date string and the time represented by the time string, and returns zero if successful. |
set (HecTime time) | None | Sets the object to the date and time represented by the HecTime parameter. |
setDate(string date) | integer | Sets the object to the date represented by the string, and returns zero if successful. The time portion of the object is not modified. |
setTime(string time) | integer | Sets the object to the time represented by the string, and returns zero if successful. The date portion of the object is not modified. |
setJulian(integer julian) | None | Sets the date to the Julian days (Since Jan 1, 1900) |
setXML (String dateTime) | integer | Sets the date and time according to the XML string |
setUndefined() | None | Sets the object to represent an undefined time, as if the object had just been created. |
showTimeAsBeginningOfDay(Boolean showBeginning) | None | Specifies whether the object is to show midnight times as 00:00 (vs 24:00) |
Method | Returns | Description |
subtract(HecTime increment) | None | Subtracts the specified increment from the object's date and time. |
subtract(integer increment) | None | Subtracts the specified increment in minutes from the object's date and time. |
time() | string | Returns a string representation of the object's time. |
toString() | string | Returns the date and time in string format, for format style 2 |
toString(integer format) | string | Returns the date and time in string format, for format style given |
value() | integer | Returns the object's date and time as in the number of minutes since 31Dec1899 0000. |
year() | integer | Returns the year portion of the object's date as an integer |
1 The format of the string returned by the date(integer format) method and the date portion of the string returned by the dateAndTime(integer format) method are provided below.
HecTime Date Formats
0 | June 2, 1985 | 10 | June 2, 85 | 100 | June 2, 1985 | 110 | June 2, 85 |
1 | Jun 2, 1985 | 11 | Jun 2, 85 | 101 | Jun 2, 1985 | 111 | Jun 2, 85 |
2 | 2 June 1985 | 12 | 2 June 85 | 102 | 2 June 1985 | 112 | 2 June 85 |
3 | June 1985 | 13 | June 85 | 103 | June 1985 | 113 | June 85 |
4 | 02Jun1985 | 14 | 02Jun85 | 104 | 02Jun1985 | 114 | 02Jun85 |
5 | 2Jun1985 | 15 | 2Jun85 | 105 | 2Jun1985 | 115 | 2Jun85 |
6 | Jun1985 | 16 | Jun85 | 106 | Jun1985 | 116 | Jun85 |
7 | 02 Jun 1985 | 17 | 02 Jun 85 | 107 | 02 Jun 1985 | 117 | 02 Jun 85 |
8 | 2 Jun 1985 | 18 | 2 Jun 85 | 108 | 2 Jun 1985 | 118 | 2 Jun 85 |
9 | Jun 1985 | 19 | Jun 85 | 109 | Jun 1985 | 119 | Jun 85 |
Creating HecTime objects can be done with a string constructor with different formats. The table below shows example inputs.
Example |
---|
01Jan2023; 01:15 |
01Jan2023, 0115 |
01Jan2023 0115 |
01Jan2023:0115 |
Here is a Jython example demonstrating an example from the table above:
>>> from hec.heclib.util import HecTime
>>> t = HecTime("01JAN2022; 01:15")
>>> t.toString()
u'1 January 2022, 01:15'
>>> t.dateAndTime(104)
u'01JAN2022, 01:15'