Easy Way to Get the Difference of Two Times in Python

This post is quite the short one, but if you ever need the duration/difference between two times, this can come in very handy. But the code may be confusing. So bear with me.

So if you can get the total amount of seconds, which to make it easier, I used the package ‘arrow’.

``````
pip install arrow
pip3 install arrow
``````

Here’s my code, a more stripped down version than what I use.

``````
#! /usr/bin/python3
import arrow
import datetime
def secondsToText(secs):
days = secs//86400
hours = (secs - days*86400)//3600
minutes = (secs - days*86400 - hours*3600)//60
seconds = secs - days*86400 - hours*3600 - minutes*60
result = ("{0} day{1}, ".format(int(days), "s" if days!=1 else "") if days else "") + \
("{0} hour{1}, ".format(int(hours), "s" if hours!=1 else "") if hours else "") + \
("{0} minute{1}, ".format(int(minutes), "s" if minutes!=1 else "") if minutes else "") + \
("{0} second{1}".format(int(seconds), "s" if seconds!=1 else "") if seconds else "")
return result
my_year  = int(input("Year --> "))
my_month = int(input("Month (01-12) --> "))
my_day   = int(input("Day (01-31) --> "))
my_hour  = int(input("Hour (00-23 --> "))
my_min   = int(input("Minute (00-59) --> "))
my_sec   = int(input("Second (00-59) --> "))
my_datetime = datetime.datetime(my_year, my_month, my_day,
my_hour, my_min, my_sec)
now = arrow.now("US/Eastern")
mypasttime = my_datetime
then = arrow.get(mypasttime, "US/Eastern")
time_diff = now - then
time_days = time_diff.days
time_diff = secondsToText(time_diff.total_seconds())

print(time_diff)
``````

So lets say I wanted the exact length of time(in days + rest) since ElectroCode began.

And while I know the date was January 20th 2012, I don’t exactly know the exact time, so we’ll use Noon(12 00 00)

``````
[email protected]:~\$ ./time.py
Year --> 2012
Month (1-12) --> 01
Day (1-31) --> 20
Hour (0-23 --> 12
Minute (0-59) --> 00
Second (0-59) --> 00
1692 days, 3 minutes, 9 seconds
``````

Now if I wanted to make that able to parse weeks and years, so now I’ll add a few additions to the `secondsToText()` method.

Results

So with those changes, I now get

``4 years, 33 weeks, 1 day, 1 hour, 11 minutes, 38 seconds``

If you think there should be changes to this post, leave a comment down below! I can’t improve if there’s no response of what I need to improve on.

See here for other ways to contact me.

I’m a Help Staff on Rizon, and as such we have the responsibility of staffing RizonBNC, which has a rule that you have to be registered 7 days (1 week) before requesting a BNC, so I made a little script to get me the difference in between them.

Here’s the STDOUT on the shell

```[email protected]:~\$ ./time.rb -t "Aug 04 10:27:26 2016 MDT"
{:time=>"Aug 04 10:27:26 2016 MDT"}
Try again in 6 days, 23 hours, 24 minutes and 46 seconds (7 day wait)
[email protected]:~\$ ./time.rb -n Blah -t "Aug 04 10:27:26 2016 MDT"
{:nick=>"Blah", :time=>"Aug 04 10:27:26 2016 MDT"}
Blah: Try again in 6 days, 23 hours, 24 minutes and 25 seconds (7 day wait)
```

Here’s the code, hosted on GitHub

weechat

So, as weechat is my #1, I thought I would save the best for last.  Its actually pretty easy to get things started in weechat, even adding servers and making weechat connect to them at the start can all be done in one line, granted with some typing.

Let’s Get Our Clients

weechat is great through SSH and when used as your direct client. On Windows, you can use it through putty, or install it in CygWin. If you’re on a Debian derived distribution, you can use ‘`apt-get install weechat`‘, of course, if you aren’t the root user, you will need to add ‘sudo’ to the front of that. Anyways.. Lets get our client downloading.

Linux(debian)

`sudo apt-get install weechat weechat-scripts`

Windows

I’m assuming you already have CygWin. Start up the package downloader, and either, go into Net and find everything, or enter ‘weechat’ into the search field, and select pretty much everything that comes up for it.

Other

If you’ve got something else, or you’re looking for something else, see their download page here.

Let’s Start Connecting

Connecting to a network with the right options the first time is pretty easy for weechat users, as you can give all your options in one line.

Normal Connection

``/connect irc.lamenet.org/6667``

SSL Connection

``/connect irc.lamenet.org/6697 -ssl``

SSL Connection (Don’t Verify Certificates)

``/connect irc.lamenet.org/6697 -ssl -nossl_verify``

Now what if you have a server you want to connect to that has a password?

``/connect irc.lamenet.org/6697 -ssl -nossl_verify -password="ServerPass"``

``/connect irc.lamenet.org/6697 -ssl -nossl_verify -sasl_username="LameUser" -sasl_password="lamepass1234" -sasl_mechanism="PLAIN"``

What if I have a certificate??

``/connect irc.lamenet.org/6697 -ssl -nossl_verify -ssl_cert="%h/ssl/Cert.pem"``

Setting up networks

The format for adding servers/networks and connecting to them directly is basically the same.

``/server add LameNet irc.lamenet.org/6667 -[no]option=""``

Lets try adding LameNet in as having a certificate”

``/server add LameNet irc.lamenet.org/6697 -ssl -ssl_verify -ssl_cert="%h/ssl/Cert.pem"``

What about if you want weechat to connect automatically when it starts?

``/server add LameNet irc.lamenet.org/6697 -ssl -ssl_verify -autoconnect``

“I have a certain RDNS I want shown, what do I do? (IPV6 type)”

If you have a /64, its going to look something like this `2004:2345:2:123::/64`, you’re going to enter something like this

``/server add LameNet irc6.lamenet.org/6697 -ssl -ssl_verify -ipv6 -local_hostname="2004:2345:2:123::4051:0001"``

Given the previous was using IPV6, why don’t we look at a simple line to use it on a hostname that allows both `IPV4` & `IPV6`.

``/server add irc.lamenet.org/6697 -ssl -ssl_verify -ipv6``

Wasn’t too hard was it? I would hope not.

Extra Stuff

Now that we’ve got you connected and roaming, how about we start showing some advanced uses or convenient plugins to have.

buffers.pl

buffers.pl is a perl script(.pl) that gives users a buffer list, which if you’re used to most any other client, then this should be the first script you download/install. There are some extra things you can do with it, including making it look like a regular tree list of windows. See our post here, you’re also going to want to make sure you use `/mouse toggle` if you want to use your mouse to select your buffers.

If you think there should be changes to this post, leave a comment down below! I can’t improve if there’s no response of what I need to improve on.

See here for other ways to contact me.

KVIrc

Connecting to IRC via KVIrc is a bit different than the other clients so far. ZNC doesn’t quite work well, as when KVIrc disconnects from the ZNC server, it can forget the password used to connect, and will cause the client to connect to the default network, which can cause you all sorts of problems.

Lets Get our Clients

You have 3 choices, ‘Stable Packages’, ‘Git Snapshots’, or ‘Bleeding Edge’, Stable is 4.2.0, Git is at 4.9.2, which is just as stable, if not more stable than 4.2.0

When you first install it, you’ll get a start-up wizard, allowing you to set up your basics. If you want to set anything else, you’ll go through the regular settings.

Lets Start Connecting

So, as I said, there are differences in how you connect to IRC networks and bouncers while using KVIrc.

```Normal
`/server irc.lamenet.org 6667`
SSL
`/server -s irc.lamenet.org 6697`
BNC
```/server -s -p="LameUser/lamenet:lamepass1234" bnc.lamenet.org 1338
``````

KVIrc uses switches for its options, much like weechat does, but shorter(kinda).

```IPV6
```/server -i host port
/server --ipv6 host port```
SSL
`/server -s host port`
`/server --ssl host port`
Use vHost
```/server -b="192.168.0.0" host port
Run Command after Connection
```/server -c="/oper LameOper SuperLameOperPass" host port
/server --command="/oper LameOper SuperLameOperPass" host port```

```

Channels & Nicks expired due to restore

Due to an IO failure on the node the services is hosted on, services unexpectedly deleted its own database, thankfully we had a backup, though newer users and users with different nicks may have to re-register, some nicks and channels also expired once the database and services caught wind of the time. So below are the nicks and channels that services expired. If you are missing something let an oper know promptly. You can see this page, to find ways to contact us.

Nicks Lost
Skora

Reaper(fixed)

RoboKill dnsk
Skarner(fixed) Revan
Daniel Youssef_
Pigeon Scott
Seth hentai(Skora)
Firetruck(Funta) Sarah(fixed)
ToXiC iKONN

Alaura

pino_p
Funta
Channels Lost
#nohomo(Seth) #politics(Skora)
#dj(dnsk) #EDM(Skora)
#indietapes(iKONN)

HexChat

Since HexChat seems to be the goto for this type of client, that will be what this tutorial is about.  XChat is old, and mostly closed source, as far as my memory allows. Since HexChat comes from XChat, if you use hexchat, there should be no real big difference in your interface.

Lets get our clients!

Hexchat: https://hexchat.org

XChat: https://xchat.org

Installation is pretty easy, if you’re a beginner, you should be good on just pressing ‘Next’ through the whole thing. Otherwise, you should know what you’re doing with each section.

Let’s Start Connecting!

To start off, when you open HexChat for the first time, you should see a server list dialog box.

Shown here

As you’ll see I have a selected network called “New Network”, we’ll talk about this later in the post, for now, lets go for instant connections.

Ok, so lets say you’ve skipped the network list, if you want to connect to LameNet, then we enter `/server irc.lamenet.org +6697` for SSL. and `/server irc.lamenet.org 6667` for Non-SSL.

If you need to open up a new connection without killing your first, then you need to use the command `/NEWSERVER` with the same input, so that would be `/NEWSERVER chat.freenode.net 6667/+6697`

Here they are for readabilities’ sake.

`/SERVER irc.lamenet.org 6667 -> Non-SSL/SERVER irc.lamenet.org +6697 -> SSL`

``` ```

`/NEWSERVER irc.lamenet.org 6667 -> Non-SSL/NEWSERVER irc.lamenet.org +6697 -> SSL`

You may be thinking, ‘But what do we do if we want to connect to servers when we start XChat though’, unfortunately, the scripting isn’t as helpful in HexChat, so we’re going to have to do this through the Server List GUI, so lets review that.

So you’ve gotten this far, you’ve connected to servers or networks, but want to step up your game.

Lets see that picture of the network list again

Ok, so we have the global settings, your preferred nickname, and your second and third choice, and for the buttons, we have ‘Add’, ‘Remove’, ‘Edit’, ‘Sort’, and ‘Favor’

We’re going to click ‘Add’, then click where ‘New Network’ appeared, edit the name to your liking, then press ‘Enter’ or ‘Return’ on your keyboard, and it will set that as the name. Now click ‘Edit’, making sure that your new network is selected, and it will bring up another little window.

Since we want to connect on the start of the client, we’re going to select the ‘Connect automatically…’ box. You’re going to do this to each network that you want to connect to when HexChat starts.

You’re going to click first up where it says ‘newserver/6667’, then type ‘irc.lamenet.org/6667’, if its SSL, just put ‘irc.lamenet.org/6697’, and then click the ‘Use SSL for all the servers on this network’. If you know them and know they sign their own SSL certificates, click the ‘Accept invalid SSL certificates’.

If you end up making yourself a services account, check out the ‘Login Method’ dropdown, and select what suits your situation. Usually you’re going to use ‘NickServ…’

That should be all that needs to be done, if you haven’t added your channels to NickServ’s ‘AJOIN’, or you don’t have a bnc, you can add channels to autojoin, click the tab ‘AutoJoin Channels’ then ‘Add’, enter the channel name and if it has a key (join password) put that where its supposed to be.

With that, you should be well on your way to having a fine time IRCing.

Scripting Tip

One more thing… If you write your scripts using xchat’s API, they will work in both XChat and HexChat.

Here’s a sample script taken from here

`__module_name__ = "Cancel's HelloBot" `
` __module_version__ = "1.0.0" `
` __module_description__ = "HelloBot by Cancel" `

` print "\0034",__module_name__, __module_version__,"has been loaded\003"`

` import xchat`

` def on_join(word, word_eol, userdata):`
`       triggernick, triggerchannel, triggerhost = word`
`       destination = xchat.get_context()`
`       destination.command("say Hello " + triggernick + " and welcome to " + triggerchannel)`

` xchat.hook_print('Join', on_join)`

If you think there should be changes to this post, leave a comment down below! I can’t improve if there’s no response of what I need to improve on.

See here for other ways to contact me.

Considering I made a post about my top IRC clients, I have been told, and have thought about showing a tutorial based on connecting and using such clients. First one being mIRC and AdiIRC. So without further-ado..

Lets get our clients

One of the first things before connecting to IRC is getting a client.. And for todays, we are using mIRC and AdiIRC.

Once you have them installed, which should be pretty easy, just click next when it asks you to. After that we can go onto the next thing. AdiIRC requires Microsoft’s .NET Framework, which it should install itself, or you can download versions 2.0 or 4.5 yourself, mIRC shouldn’t require anything, if that’s your thing.

Now lets connect!

On mIRC and AdiIRC simple connections and scripted connections are pretty much the same. Lets say you’re connecting to a network named LameNet and its ‘hostname’ or ‘server address’ is “irc.lamenet.org”, and you saw it say it has SSL and Non-SSL ports, 6697 and 6667 respectively.

For a quick connect from AdiIRC or mIRC to their SSL port:

`/server irc.lamenet.org +6697`

Now AdiIRC and mIRC have a switch (–switch/-s) you can use so you don’t have to put +PORT, and their’s is `-e`

If we write that as a command,

`/server -e irc.lamenet.org 6697`

Lets say another group of friends says they are on freenode, you can connect to it without killing your connection to LameNet

`/server -m chat.freenode.net +6697 /server -me chat.freenode.net 6697`

Lets say you have LameNet, freenode, and ElectroCode perhaps. But you don’t want to type that every time you open your client.

In mIRC, Menubar -> Tools -> Script Editor -> Make sure you’re on the remote tab

The code to put down in either client is the following,

`on *:START: {  server -e irc.lamenet.org 6697  server -me chat.freenode.net 6697  server -me irc.electrocode.net 6697}`

After that is put in, you need to save your script so that it works.. Choose a name that describes your script.

After that is done, when you click save and you exit, you will be prompted with a question/warning, this is due to the code being used can either start when loaded, as we are technically loading a new script in; The other time is whenever you start up your client. The dialog box looks like this.

If you’re already connected to your networks, you DO NOT say ‘Ok’, as this will kill your current connection, as well as open up two new connections to the second and third networks, the reason being is the `-m` switch, which as said, opens a new connection, no matter what.

Some Extra things to think of — Stuff to do after connecting

Both of these clients have some options that are beneficial to you, and your continued use of IRC.

One of the things is having some of your events come to the window you’re looking at. So you don’t have to switch windows to check on if a command went through. The few you should be most ‘concerned’ about, is “Whois”, “Notice”, and “CTCP”. On AdiIRC, this may be already set, but you want these set to “In Active” for the most part, if you are a network where ‘notice spam’ ‘ctcp spam’ are prevalent, then feel free to leave them as “In Status”.

In mIRC, Menubar -> Tools -> Options -> IRC and then you should see

on the right.

There is also a scripting way of getting this done, but that will be in a later post.

If you think there should be changes to this post, leave a comment down below! I can’t improve if there’s no response of what I need to improve on.

See here for other ways to contact me.

Bot Assigner

I’ve recently made a little javascript IRC command helper, I’ll be making more. But now people can use this page to get a command to add a bot to their channel, the page has all the public bots we have.  Select the channel name, and enter the bot name you want after looking through the list, then submit and it will show you the command to enter.

New Things

As I said before, there would be some changes and additions, and I’ve added/installed a wiki, and forums for the network. Both are currently under construction, but suggestions may be sent, through any of our contact channels, see on our contact page here.

Wiki: wiki.e-code.in

Forum: forum.electrocode.net

Top 5 IRC Clients (IMO)

I’ve used many IRC clients before,

1. weechat

While weechat is a terminal IRC client, it is my favorite IRC client, mostly due to its configurability, and its large plugin(script) repository. which also shows off how easy it is to play with the plugin API. There are 309 scripts in weechat’s repository as of Apr/25/16, this is mostly from being able to write scripts in multiple languages. Currently, you can write plugins in,

Some of Weechat’s Abilities

• Python
• Perl
• Ruby
• Lua
• JavaScript
• Guile
• TCL
• C

2. KVIrc

KVIrc, or K Visual IRC, is my second because of the lack of forward development on it, not just the client itself, but rather plugins and themes mostly, but that isn’t their fault.  It has a decently fleshed out appearance, although there are some minor graphical glitches sometimes.

Basic KVIrc look

It also allows native scripting, in a language known as KVS, based on perl, which can be used within KVS. All of KVS’s functions, commands, etc are all packed into its help browser, opened by `/help`, its amount of functions seems to be around twice the amount of what mIRC has.

AdiIRC is seen by many as a mIRC clone with some improvments, somethings that were 3rd party addons have been hardcoded into the client, as well as some extras.  One of the extras is the addition of passive popups, not unlike KVIrc’s for channels and nicknames. Another improvement on mIRC is AdiIRC’s more advanced server list, which itself has had multiple versions, as well as the native inclusion of SASL features for networks that support it.

Some of the scripts that have been made for mIRC in the past have been included natively in AdiIRC, such as the use of MTS or mIRC theme standard files to load themes that began as an advanced addon for mIRC.

Some of the extras that came with AdiIRC were the use of native highlight, url, and other ‘monitor’ panels. These allowed easy access to these features without having to manually script something like these in, as well as having them inside the main window, instead of their own ‘window’ in the client.

I myself see AdiIRC as a mIRC+KVIrc hybrid, minus the ability to script in perl of course. Its a very good client for those beginning with IRC, but wanting something that is more intuitive than mIRC, more so when it comes to scripting, or having some features already, instead of having to code each event or window.

4. mIRC

mIRC is a client that has a rich scripting interface, but lacks on the graphical side, it often is very clunky, and its window code gets very slow. It however is very useful in making IRC bots and other IRC helpers, such as context menus and dialogs, as well as being able to load a separate window and use it in many ways, much like what AdiIRC’s monitor windows do natively, but that you can add more uses to them instead of the hard coded actions.  One use for those that own a network, and use mIRC, is that instead of having all of the notices you get, meaning both regular `/notice` and `SNOTICE`s do not flood your status window or your current window. Of course this difference all depends on how you have your settings. However, you can then make a script that then adds all Server NOTICE to a separate window to keep everything else clean.

5. XChat/Hexchat

With XChat, the fault lies in its trouble of configuration, as well as the difficulty in changing the look of the window. Instead of choosing your own themes and such, you mostly have font changing abilities or you can change between certain ‘look&feel’s which are usually between GTK, Tango, and Motif themes, if my memory serves me correctly.
With its configuration, some of the settings that someone coming from mIRC or another client may have is having `/whois` replies come to the current buffer or other events, have to instead first look at the help file, which doesn’t easily tell them of the /set command, which has ~50 options behind it.

While it has some faults, its usefulness is much like that of weechat. Its scripting languages are less than that of weechat, but mostly in the same format, both in the way the files are supposed to look, and in how the methods are called. This means that someone that came from xchat to weechat, or vice-versa could easily pickup or port their scripts. XChat allows some of the same languages allowed in weechat, namely, C, Python, Perl, and TCL.

Now with Hexchat, many things are the same, mainly because Hexchat began as a free fork of XChat, but turned into its own client, but still borrowing most of the things that started with XChat.  Settings are are a bit more fleshed out, some plugins are included with the installer. Most changes are seen through the appearance and menu items, as well as some of the code. Most of the changes with the scripting API just were renaming the functions, xchat->hexchat.

All in all, most of the features in these clients are all the same, but most see a change in how additions can be made, or how they look. With myself, the structured look of weechat and its ease of configuration and scripting, make for an easily like environment. KVIrc on the other hand has a flashy and colorful GUI, which is liked by many that use it, its ease of native scripting is on par with that of mIRC and AdiIRC’s although with some differences. AdiIRC and mIRC would’ve been put together, but the inclusion of some of mIRC’s earlier addons into its native code has put it in its own rank. mIRC, though by far has the most use, and most of the features in other clients, mainly KVIrc and AdiIRC seem to have come from that. HexChat and Xchat though seem to be the easiest to use outside of web clients.

• weechat
• KVIrc