Python3: convert relative date to UTC timestamp

I needed to convert a relative time string, e.g., “Yesterday at 3:08 am,” to a UTC timestamp. Apparently this is not that easy to find on the Internet.
My research combines at least 3 different sources to accomplish my goal.

My incoming timezone is UTC-0500 (“US/Eastern”). And I realize it is unfortunate that I am doing some string-to-ints and back and forth. Well, please share how you could do better!

Code

#!/usr/bin/env python3
from datetime import timedelta
from parsedatetime import Calendar
from pytz import timezone 

indate = "Yesterday at 3:08 am"
print("indate:",indate)

# long version, for explanation
cal = Calendar()
dto, _ = cal.parseDT(datetimeString=indate, tzinfo=timezone("US/Eastern"))
add_hours = int((str(dto)[-6:])[:3])
outdate = (timedelta(hours=-add_hours) + dto).strftime('%Y-%m-%dT%H:%M:%SZ')
print("outdate:",outdate)

# short form
dto, _ = Calendar().parseDT(datetimeString=indate, tzinfo=timezone("US/Eastern"))
outdate = (timedelta(hours=-int((str(dto)[-6:])[:3])) + dto).strftime('%Y-%m-%dT%H:%M:%SZ')
print("outdate:",outdate)

Sample run

$ ./sample.py 
indate: Yesterday at 3:08 am
outdate: 2020-02-02T08:08:00Z
outdate: 2020-02-02T08:08:00Z

References

Weblinks

  1. python – Convert relative date string to absolute date – Stack Overflow
  2. bear/parsedatetime: Parse human-readable date/time strings [github.com]
  3. python – Convert UTC datetime string to local datetime – Stack Overflow

Prepend output with time to generate each line

To show how long it takes before showing each new line of output, use this neat command.

long_command | ts -i "%.s"
$ ./configure --prefix=/tools | ts -i %.s
0.082337 checking for a BSD-compatible install... /tools/bin/install -c
0.002841 checking whether build environment is sane... yes
0.008164 checking for a thread-safe mkdir -p... /tools/bin/mkdir -p
0.000040 checking for gawk... gawk
0.005892 checking whether make sets $(MAKE)... yes

References

Weblinks

  1. St├ęphane Chazelas at https://unix.stackexchange.com/questions/391210/prepending-each-line-with-how-long-it-took-to-generate-it/391222#391222

System selecting wrong time due to not UTC

The problem

I use kickstart files to configure my CentOS 7 virtual machines. One problem I discovered is that the vms can get the wrong time from the hwclock. I guess I haven’t mastered ntpd or chronyd. I can always just run ntpdate dns1.ipa.example.com but haven’t bothered to set up a cronjob/systemd unit for that.
Anyway, I finally discovered how to get the hwclock and system date to actually grab the time correctly from the host OS. If I don’t set the timezone correctly in the kickstart file with the –utc option:

timezone America/New_York --utc

I can go modify /etc/adjtime myself.
An incorrect file:

0.000000 1499048878 0.000000
1499048878
LOCAL

The correct file replaces the LOCAL with UTC, to tell the system that the hardware clock (hwclock) is showing the UTC time:

0.000000 1499048878 0.000000
1499048878
UTC

A reboot later, and now my vm has the correct time, so I have achieved my ultimate goal of gssapi auth.

References

Weblinks

  1. That such a file exists as /etc/adjtime https://eloquence.marxmeier.com/sdb/html/954237377.html
  2. https://bugzilla.redhat.com/show_bug.cgi?id=672194