Issue: Inserting a timestamp value of 0 or "1970-01-01 00:00:00" followed by a get returns None
Author: dougthor42
State: closed
Created: 2019-03-04 19:24:41

# Summary

The `.get` method on a model with a `TimestampField` field will return `None` if the value stored is the POSIX epoch `1970-01-01 00:00:00` (POSIX timestamp `0`).


# Steps to reproduce:

Here's a short example program:

```python
# example.py
from datetime import datetime
from peewee import *

db = SqliteDatabase(":memory:")

class Example(Model):
    timestamp = TimestampField(utc=True)

    class Meta:
        database = db

    def __str__(self):
        return "<{}, {}>".format(self.id, repr(self.timestamp))

db.connect()
db.create_tables([Example])

# Insert some data
a = Example(timestamp=datetime.utcnow())
a.save()
print("(Instance)  timestamp=utcnow: {}".format(a))
a_get = Example.get(Example.id == 1)
print("(Query)     timestamp=utcnow: {}".format(a_get))

posix_epoch = datetime(1970, 1, 1)
b = Example(timestamp=posix_epoch)
b.save()
print("(Instance)  Timestamp=posix_epoch: {}".format(b))
b_get = Example.get(Example.id == 2)
print("(Query)     Timestamp=posix_epoch: {}".format(b_get))

db.close()
```


# Expected Result

The timestamp value returned by the `.get` method should be `0`:

```shell
$ python example.py
(Instance)  timestamp=utcnow: <1, datetime.datetime(2019, 3, 4, 19, 8, 47, 912755)>
(Query)     timestamp=utcnow: <1, datetime.datetime(2019, 3, 4, 19, 8, 48)>
(Instance)  timestamp=posix_epoch: <2, datetime.datetime(1970, 1, 1, 0, 0, 0, 0)>
(Query)     timestamp=posix_epoch: <2, datetime.datetime(1970, 1, 1, 0, 0, 0)>
```


# Actual Result

The timestamp value returned by `.get` is `None`:

```shell
$ python example.py
(Instance)  timestamp=utcnow: <1, datetime.datetime(2019, 3, 4, 19, 16, 10, 217857)>
(Query)     timestamp=utcnow: <1, datetime.datetime(2019, 3, 4, 19, 16, 10)>
(Instance)  Timestamp=posix_epoch: <2, datetime.datetime(1970, 1, 1, 0, 0)>
(Query)     Timestamp=posix_epoch: <2, None>
```


# Comments

+ I'm testing against an SQLite database. I have not checked MySQL or PostgreSQL.
+ You'll notice that the sub-second resolution is dropped. As you know, this is expected because all backends store the value in an `integer` column. might open another issue to adjust the column type, but that's out of scope.
+ The record instance has the correct value, but the result from `.get` does not

# Versions

+ Python: 3.6.7 64-bit
+ OS: Ubuntu 18.04 on Windows Subsystem for Linux
    ```shell
    $ lsb_release -a
    No LSB modules are available.
    Distributor ID: Ubuntu
    Description:    Ubuntu 18.04.2 LTS
    Release:        18.04
    Codename:       bionic
    $ uname -a
    Linux Thorium 4.4.0-17763-Microsoft #253-Microsoft Mon Dec 31 17:49:00 PST 2018 x86_64 x86_64 x86_64 GNU/Linux
    ```
+ Windows 10 Pro, version 1809
+ Peewee versions tested
    + Master 735ff26b
    + v3.8.2 from pypi

This relates to the following code:

```python
    def python_value(self, value):
        if value is not None and isinstance(value, (int, float, long)):
            if value == 0:
                return
```

I suppose this is a bug, so I've removed the "if value == 0" conditional.

My original thinking was that a zero timestamp is a special-case which was equivalent to "no timestamp". But even if that is true, the use of NULL/None carries consequences that are not present when using a zero value. Should be fixed by df94b2db9fa93b0f56cbca2f95b2bf42ba27bb01

Awesome, thanks! I kinda figured it was something like that.

The only reason that I ran into the issue is because I was testing my own program and noticed that I couldn't fix particular edge case without some hacks.