4.11 Working with time-series data
Time-series data need to be handled carefully. If times and dates are specified in local time, then conversions may be necessarily between timezones, especially around the beginning and end of daylight saving time.
Even when this it not an issue, months have different lengths and leap years have an extra day, which mean it is not straightforward to convert a series of calendar dates into elapsed times between the data points.
On a more basic level, even time expressed in hours and minutes are complicated by being expressed as non-decimal fractions of days.
To simplify the process of working with dates and times, Pyxplot has native date object type, together with pre-defined functions in the time module for creating and manipulating such objects. A date object represents a specific moment in time, and can be created from a time and date specified in any arbitrary timezone. It is then possible to read out the time and date components of this date object in any other arbitrary timezone.
The functions for creating date objects are as follows:
This function creates a date object from the specified calendar date. It takes six compulsary numerical inputs: the year, the month number (1–12), the day of the month (1–31), the hour of day (0–24), the number of minutes (0–59), and the number of seconds (0–59). To enter dates before AD 1, a year of should be passed to indicate 1 BC, should be passed to indicate the year 2 BC, and so forth.
A timezone may optionally be specified as the final argument to the function. If no timezone is specified, then the default is used, which may be set using the set timezone command. The timezone should be specified as a location string, of the form Europe/London, America/New_York or Australia/Perth, as used by the tz database. A complete list of available timezones can be found here: http://en.wikipedia.org/wiki/List_of_tz_database_time_zones.
Daylight saving time will be applied as appropriate for the specified location. Note that strings such as GMT, EDT or CEST are not allowed as timezones; a location should be specified.
If universal time is used, the timezone may be specified as UTC.
This function creates a date object from the specified numerical Unix time – i.e. the number of seconds ellapsed since midnight on 1st January 1970 UTC.
This function creates a date object from the specified numerical Julian date.
This function creates a date object from the specified numerical modified Julian date.
This function takes no arguments, returns a date object corresponding to the current system clock time, as in the following example:
pyxplot> print time.now()
Note that the date object created by the time.now() function is identical regardless of timezone, but it is displayed differently depending upon the current timezone.
The following example creates a date object representing midnight on 1st January 2000, in universal time, and in Western Australian time:
pyxplot> print time.fromCalendar(2000,1,1,0,0,0)
Once created, it is possible to add numbers with physical units of time to dates, as in the following example:
pyxplot> myDate = time.fromCalendar(2012,8,1,0,0,0)
Standard string representations of calendar dates can be produced with the print command. It is also possible to use the string substitution operator, as in "%s"%(date), or the str method of date objects, as in date.str().
In addition, the time.string function can be used to choose a custom display format for the date, or to specify the timezone in which the date should be displayed. Its arguments are as follows:
This function returns a string representation of the specified date object . The second argument is optional, and may be used to control the format of the output. If no format string is provided, then the format
The third argument is also optional, and specifies the timezone that the time should be displayed in. As above, this should be specified in the form Europe/London, America/New_York or Australia/Perth, as used by the tz database. A complete list of available timezones can be found here: http://en.wikipedia.org/wiki/List_of_tz_database_time_zones. If universal time is used, the timezone may be specified as UTC. If no timezone is specified, the default is used as set in the set timezone command.
Several functions are provided for converting date objects back into various numerical forms of timekeeping and components of calendar dates, which are listed below. Where appropriate, an optional timezone may be specified to obtain a calendar date for a particular location:
pyxplot> a = time.fromCalendar(2000,1,1,0,0,0)