Verified Commit c41667e6 authored by FC (Fay) Stegerman's avatar FC (Fay) Stegerman 🏳️‍🌈
Browse files

SOURCE_DATE_EPOCH: add missing syntax highlighting

parent aa5e9da9
Loading
Loading
Loading
Loading
+39 −38
Original line number Diff line number Diff line
@@ -13,7 +13,7 @@ Before added support to a tool for reading this variable, you should scan throug

### Python >= 3.x

```
```python
import os
import time
import datetime
@@ -26,7 +26,7 @@ build_date = datetime.datetime.fromtimestamp(

… or with fewer imports and rendering to a string:

```
```python
import os
import time

@@ -41,7 +41,7 @@ date_str = time.strftime(
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)):


```
```python
import os
import time
import datetime
@@ -55,13 +55,13 @@ build_date = datetime.datetime.utcfromtimestamp(

For GNU systems:

```
```bash
BUILD_DATE="$(date --utc --date="@${SOURCE_DATE_EPOCH:-$(date +%s)}" +%Y-%m-%d)"
```

If you need to support BSD date as well you should fallback to trying ther `-r seconds` timestamp variant:

```
```bash
DATE_FMT="+%Y-%m-%d"
SOURCE_DATE_EPOCH="${SOURCE_DATE_EPOCH:-$(date +%s)}"
BUILD_DATE=$(date -u -d "@$SOURCE_DATE_EPOCH" "$DATE_FMT" 2>/dev/null || date -u -r "$SOURCE_DATE_EPOCH" "$DATE_FMT" 2>/dev/null || date -u "$DATE_FMT")
@@ -69,14 +69,14 @@ BUILD_DATE=$(date -u -d "@$SOURCE_DATE_EPOCH" "$DATE_FMT" 2>/dev/null || date -u

### Perl

```
```perl
use POSIX qw(strftime);
my $date = strftime("%Y-%m-%d", gmtime($ENV{SOURCE_DATE_EPOCH} || time));
```

### Makefile

```
```make
DATE_FMT = +%Y-%m-%d
ifdef SOURCE_DATE_EPOCH
    BUILD_DATE ?= $(shell date -u -d "@$(SOURCE_DATE_EPOCH)" "$(DATE_FMT)" 2>/dev/null || date -u -r "$(SOURCE_DATE_EPOCH)" "$(DATE_FMT)" 2>/dev/null || date -u "$(DATE_FMT)")
@@ -89,7 +89,7 @@ The above will work with either GNU or BSD date, and fallback to ignore `SOURCE_

### CMake

```
```cmake
STRING(TIMESTAMP BUILD_DATE "%Y-%m-%d" UTC)
```

@@ -97,7 +97,7 @@ STRING(TIMESTAMP BUILD_DATE "%Y-%m-%d" UTC)

If you would like to support legacy/archival versions of CMake, you can use this less-preferred variant:

```
```cmake
if (DEFINED ENV{SOURCE_DATE_EPOCH})
  execute_process(
    COMMAND "date" "-u" "-d" "@$ENV{SOURCE_DATE_EPOCH}" "+%Y-%m-%d"
@@ -117,7 +117,7 @@ Note that the above will work only with GNU `date`; see the POSIX shell example

By deliberate design, [Meson does not provide access to environment variables in build files](https://github.com/mesonbuild/meson/issues/9#issuecomment-543780613) which makes accessing `SOURCE_DATE_EPOCH` troublesome.

```
```meson
date_exe = find_program('date')
cmd = run_command('sh', '-c', 'echo $SOURCE_DATE_EPOCH')
source_date_epoch = cmd.stdout().strip()
@@ -132,7 +132,7 @@ The above will work only with GNU `date`. See the POSIX shell example on how to

### C

```
```c
#include <errno.h>
#include <limits.h>
#include <stdlib.h>
@@ -173,7 +173,7 @@ build_time = gmtime(&now);

If you want less verbose code and are happy with the assumptions stated below, you can use

```
```c
#include <stdlib.h>

time_t now;
@@ -187,7 +187,7 @@ if ((source_date_epoch = getenv("SOURCE_DATE_EPOCH")) == NULL ||

### C++

```
```cpp
#include <sstream>
#include <iostream>
#include <cstdlib>
@@ -209,7 +209,7 @@ if ((source_date_epoch = getenv("SOURCE_DATE_EPOCH")) == NULL ||

### Go

```
```go
import (
        "fmt"
        "os"
@@ -234,13 +234,13 @@ if source_date_epoch == "" {

### PHP

```
```php
\date('Y', (int)\getenv('SOURCE_DATE_EPOCH') ?: \time())
```

### Emacs-Lisp

```
```elisp
(current-time-string
  (when (getenv "SOURCE_DATE_EPOCH")
    (seconds-to-time
@@ -250,7 +250,7 @@ if source_date_epoch == "" {

### OCaml

```
```ocaml
let build_date =
  try
    float_of_string (Sys.getenv "SOURCE_DATE_EPOCH")
@@ -260,7 +260,7 @@ let build_date =

### Java / gradle

```
```groovy
def buildDate = System.getenv("SOURCE_DATE_EPOCH") == null ?
  new java.util.Date() :
  new java.util.Date(1000 * Long.parseLong(System.getenv("SOURCE_DATE_EPOCH")))
@@ -268,7 +268,7 @@ def buildDate = System.getenv("SOURCE_DATE_EPOCH") == null ?

### Javascript / node.js

```
```javascript
var timestamp = new Date(process.env.SOURCE_DATE_EPOCH ? (process.env.SOURCE_DATE_EPOCH * 1000) : new Date().getTime());

// Alternatively, to ensure a fixed timezone:
@@ -281,7 +281,7 @@ if (process.env.SOURCE_DATE_EPOCH) {

### Coffeescript

```
```coffeescript
now = new Date()
if process.env.SOURCE_DATE_EPOCH
  now = new Date((process.env.SOURCE_DATE_EPOCH * 1000) + (now.getTimezoneOffset() * 60000))
@@ -290,7 +290,7 @@ if process.env.SOURCE_DATE_EPOCH

### Ruby

```
```ruby
if ENV['SOURCE_DATE_EPOCH'].nil?
  now = Time.now
else
@@ -330,6 +330,7 @@ let now = match env::var("SOURCE_DATE_EPOCH") {
};
```
or

```rust
use chrono::{DateTime, NaiveDateTime, Utc};
use std::env;
@@ -348,7 +349,7 @@ let now = match env::var("SOURCE_DATE_EPOCH") {

Add the following property in the `pom.xml` file:

```
```xml
<properties>
    <project.build.outputTimestamp>
        ${env.SOURCE_DATE_EPOCH}
@@ -360,7 +361,7 @@ Add the following property in the `pom.xml` file:

Set the following properties for the Zip Task that creates the `.jar` file:

```
```groovy
// Normalizes the ZIP and JAR archives
tasks.withType(Zip) {
   preserveFileTimestamps = false
@@ -402,7 +403,7 @@ def extendedTimestamp = buildInstant.toString()

`debian/strip-nondeterminism/a2x`:

```
```sh
#!/bin/sh
# Depends: faketime
# Eventually the upstream tool should support SOURCE_DATE_EPOCH internally.
@@ -412,7 +413,7 @@ exec env NO_FAKE_STAT=1 faketime -f "$(TZ=UTC date -d "@$SOURCE_DATE_EPOCH" +'%Y

`debian/rules`:

```
```make
export PATH := $(CURDIR)/debian/strip-nondeterminism:$(PATH)
```

@@ -442,19 +443,19 @@ In Debian, this is automatically set to the same time as the latest entry in `de

4. If none of the above options are good (you should have a ''very good reason'') then package maintainers may set and export this variable manually in `debian/rules`:

 ```
```make
export SOURCE_DATE_EPOCH ?= $(shell dpkg-parsechangelog -STimestamp)
```

If you need/want to support dpkg versions earlier than 1.18.8:

 ```
```make
export SOURCE_DATE_EPOCH ?= $(shell dpkg-parsechangelog -SDate | date -f- +%s)
```

If you need/want to support dpkg versions earlier than 1.17.0:

 ```
```make
export SOURCE_DATE_EPOCH ?= $(shell dpkg-parsechangelog | grep -Po '^Date: \K.*' | date -f- +%s)
```

@@ -464,7 +465,7 @@ In Debian, this is automatically set to the same time as the latest entry in `de

To set `SOURCE_DATE_EPOCH` to the last modification of a Git repository you can use `git log`. For example:

```
```make
export SOURCE_DATE_EPOCH=$(git log -1 --pretty=%ct)
```