# Useful code snippets

SIS Desktop has many (data) types, this allows it to support many different types of dataset and offers low level compatibility with databases.

Python is a high level language and has less types.

## Python and datetime

SIS Desktop uses Win32 legacy code for handling dates. This is the reason it passes back the date as a serial date so that it is more a handy hint.

But accurately converting the serial back to a Python date time object is still important (in computing / mathematics / programming the exact treatment of dates is particularly important).

### Python to SIS

Python datetime converts to SIS VARIANT(VT_DATE)

### SIS to Python

DATE value SIS VARIANT(VT_DATE) converts to Python double as serial date.

See the following code extract for converting the Python double (serial) to a standard Python datetime object.

```      from datetime import datetime
def convert_serial_date_to_datetime(serial):
# 86400 = Seconds in a day
# 25569 = Days between 1970/01/01 and 1900/01/01
seconds = (serial - 25569) * 86400.0
return seconds ```

## Python and decimal

For decimal to double data types Python will do conversions on the fly but these are not always accurate.

To avoid this we cast to a string then convert using the decimal() method; this ensures whatever is passed out of SIS Desktop as a decimal is accurately converted to a Python double.

When converting from a SIS decimal to a Python double the following method should be used.

```      from decimal import Decimal
def convert_double_to_decimal(value):
# cast the value to a string and
# and precisely convert to a python
# double using decimal()
return Decimal(str(value))```