Commit 502769f1 authored by Chris Lamb's avatar Chris Lamb 👀
Browse files

Update SOURCE_DATE_EPOCH documentation re. datetime.datetime.fromtimestamp....

Update SOURCE_DATE_EPOCH documentation re. datetime.datetime.fromtimestamp. Thanks, James Addison! (Closes: #50)
parent de187090
Loading
Loading
Loading
Loading
+21 −4
Original line number Diff line number Diff line
@@ -94,29 +94,46 @@ We are persuading upstream tools to support this directly. You may help by writi

Or search in all Debian sources: <https://codesearch.debian.net/search?perpkg=1&q=SOURCE_DATE_EPOCH>

### Python
### Python >= 3.x

```
import os
import time
import datetime

build_date = datetime.datetime.utcfromtimestamp(
    int(os.environ.get('SOURCE_DATE_EPOCH', time.time()))
build_date = datetime.datetime.fromtimestamp(
    int(os.environ.get('SOURCE_DATE_EPOCH', time.time())),
    tz=datetime.timezone.utc,
)
```

or with less imports, rendering to a string:
or with fewer imports and rendering to a string:

```
import os
import time

date_str = time.strftime(
    "%Y-%m-%d",
    time.gmtime(int(os.environ.get('SOURCE_DATE_EPOCH', time.time())))
)
```

### Python >= 2.x

If you still require Python 2.x support, you will need to use the non-recommended [`datetime.utcfromtimestamp`](https://docs.python.org/3.8/library/datetime.html#datetime.datetime.utcfromtimestamp) method ([more info](https://blog.ganssle.io/articles/2019/11/utcnow.html)):


```
import os
import time
import datetime

build_date = datetime.datetime.utcfromtimestamp(
    int(os.environ.get('SOURCE_DATE_EPOCH', time.time()))
)
```

### Bash / POSIX shell

For GNU systems: