FastAPI validate timezones

Photo by Luis Cortes on Unsplash

If you aren’t familiar with Python, FastAPI or Timezones, this might not be the post for you, sorry.

For the rest of you geeks (like me), here is an example how to validate supported timezones in an input (Query, Path etc.). Scratch that, this is about how to validate dynamic lists of values in FastAPI by using timezones as an example.

As you know, in FastAPI you can validate predefined values by using String Enums (A class sub-classed from str and Enum), so how do you validate dynamic lists of values?

According to the docs, this is how you create a dynamic Enum:

>>> from datetime import timedelta
>>> class Period(timedelta, Enum):
...     "different lengths of time"
...     _ignore_ = 'Period i'
...     Period = vars()
...     for i in range(367):
...         Period['day_%d' % i] = i

The _ignore_ part is to remove the variable generated by the property Period and the for loop, of course.

Back to Timezones, according to the docs you can get the list of timezones supported by the locally installed IANA timezone DB:

import zoneinfo
zoneinfo.available_timezones()

And to our mini validation example:

import zoneinfo
from datetime import datetime
from enum import Enum

from fastapi import FastAPI, Query

app = FastAPI()


class Timezone(str, Enum):
    _ignore_ = 'Timezone z'
    Timezone = vars()
    for z in zoneinfo.available_timezones():
        Timezone[z] = z


@app.get('/')
def get_time(zone: Timezone = Query(Timezone.UTC)):
    return {'now': datetime.now(tz=zoneinfo.ZoneInfo(zone))}

This way you can accept a timezone string, but not inserting it into any function without validating it is a string you can trust, and in the same time you don’t have to maintain that list on your own.

Remember #1: that the timezone list is recalculated every time the function is called (app start in this case), so if you run this directly on a machine, remember to restart the app after updating the timezone DB.

Remember #2: that most of the Enum items aren’t accessible in the class instance notation, Timezone.America/New_York, but you can access it using the dict notation Timezone['America/New_York'], The common names of course are accessible, like Timezone.UTC and Timezone.EST.

Short variable swap in PHP > 5.4.x

Following this great old post from David Walsh’s post Tweet for Code #2, here is a PHP adaptation for this JavaScript Var Swap tweet:

$b = [$a, $a = $b][0];

Works on PHP 5.4 and up.

I know this is not very practical, for daily work, but it can come handy in a job interview.

♦ ♦ ♦

[Update:June 16, 2016]

In PHP 7.1.x it will finally be possible to use a cleaner swap short-code:

[$a, $b] = [$b, $a];

[/Update]

Installing ODBTP on Linux with PHP 5.4

Hi,
Lately I encountered several opportunities to try and install the ODBTP library, at first I was working on new systems, so when i encountered errors when simply doing pecl install odbtp I tried installing from source, encountering errors during the make command I came to my senses and managed just fine using the basic MSSQL library in PHP.
Then a few weeks ago I needed to upgrade an existing system relying on ODBTP with tight coupling, so i had no choice but to find a way to overcome the errors.
After research on the Internet I came to solution but had no time to document the final solution, until today when I installed a test zone on a separate system and remembered only vaguely what I was looking for.
Follow the instructions here except for this: After moving the php/ext content, patch the php_odbtp.c file using the attached file.
Sources:
This patch was checked on Gentoo linux, Ubuntu server 12.04.1 and Linux mint 14, the variable as far that I could gather from the complaints is the PHP version.