Question: How do you convert a timestamp column to epoch seconds?

```
var df = sc.parallelize(Seq("2018-07-01T00:00:00Z")).toDF("date_string")
df = df.withColumn("timestamp", $"date_string".cast("timestamp"))
df.show(false)

```

DataFrame:

```
+--------------------+---------------------+
|date_string         |timestamp            |
+--------------------+---------------------+
|2018-07-01T00:00:00Z|2018-07-01 00:00:00.0|
+--------------------+---------------------+

```

Answer: 
If you have a timestamp you can cast it to a long to get the epoch seconds

```
df = df.withColumn("epoch_seconds", $"timestamp".cast("long"))
df.show(false)

```

DataFrame

```
+--------------------+---------------------+-------------+
|date_string         |timestamp            |epoch_seconds|
+--------------------+---------------------+-------------+
|2018-07-01T00:00:00Z|2018-07-01 00:00:00.0|1530403200   |
+--------------------+---------------------+-------------+

```
It can be easily done with `unix_timestamp` function in spark SQL like this:

```
spark.sql("SELECT unix_timestamp(inv_time) AS time_as_long FROM agg_counts LIMIT 10").show()

```

Hope this helps.