I’ve been working on sending metric data from control systems (namely, Opto22 PAC controllers) into time series databases such as Graphite and InfluxDB. These both want UNIX epoch time in their API.
The conversion is pretty simple:
I use the built-in conversion to NTP time and then bit shift this over to remove the NTP fractional seconds. Then I needed to subtract 2,208,988,800 seconds since the NTP epoch is Jan 1, 1900 and the UNIX epoch is Jan 1, 1970.
The last entry converts the epoch to a string so it’s ready to go out the HTTP API with the metric data.