Skip to main content

GNU date on Windoze

I love the Cygwin package on Windoze. I'm more of a Unix guy and can't stand the Windows command prompt. I much prefer a standard shell such as bash across all platforms. At my current job, I'm doing most of my development on Windows and our setup makes it so that I have to rely on Git bash more but I still have Cygwin installed and use it for things that aren't so tied to work. Git bash pisses me off sometimes with relation to copy and paste, especially inside the vi editor! What's up with that?

So this post's focus is on the date command. One of my few peeves about Cygwin is the default behavior of the command on Windows:

Tue, Apr  3, 2018  7:26:52 AM
The whole reason for using Cygwin was to have a shell environment that looks and acts like Unix. This does not look like Unix. And GNU being what it is, Git bash works the same way! Yes, my OCD is obvious... some people might say Well, that's close but I don't like this behavior! Unix is supposed to bring order to chaos!

Sure, there are options to override the behavior and I could have swore that they had a single simple option (or maybe some kind of environment variable??) to print in Unix form:

$ date +"%a %b %e %H:%M:%S %Z %Y"
Tue Apr  3 07:28:44 EDT 2018
$
This is not simple and it ticks me off that I have to do this. Computers are supposed to make things easier!

It's also easy to create an alias to specify this option but what if you want to specify a different format? I came up with a simple script to invoke the date and conditionally add the Unix-style format option: I probably could have done this in bash but that can be such a pain with strings. Python is just easier and more straight forward. For instance, Python doesn't care that there are blanks imbedded in the format string! I don't have to do any special magic to have it all interpreted as a single string! cmd is already a list of strings so it's already been carved up into tokens.

I plan to store this script as ~/bin/unixdate and use an alias in Windows shells to invoke this whenever I type date.


2019/09/13 update: I came up with a technique to create an alias for Windoze only:

# windoze aliases
if expr match "$(uname -s)" '.*[Ww][Ii][Nn]' >/dev/null 2>&1
then
  alias date=unixdate
fi

I did this because I recently started a new job and after using the Windoze laptop for about a month, I realized I had neglected to create an alias to use my unixdate script.
Yes, I could have been more specific and just checked for cygwin but I mostly only using Cygwin on Windoze so I'm happy with this. I am adding this to misc/.bashrc in my toys repo.

Comments

  1. Shortly after I published this blog, I realized I was only testing for a "+" option where the format can be expressed in excruciating detail. I forgot that that there were other options so I fixed that although I'm not going to bother updating the script shown here. If you're really interested, you can check out the script in my tools & tools repo: https://github.com/pfuntner/toys/blob/master/bin/unixdate

    ReplyDelete
  2. Follow the Gist at https://gist.github.com/pfuntner/ed3a7f9591c214e05ce320bf7a9df59d#file-unixdate for yet another snag in the script. Oh what a tangled web, indeed!

    ReplyDelete

Post a Comment

Popular posts from this blog

Dynamic Python script loading

I have a bunch of toys and tools in a Git repository - I affectionately call this my toys repo . Most are just scripts that I use from a Unix (Cygwin or Git bash on Windoze) command line but there are some Python classes that I sometimes use in another script. Today at work, I was coming up with a new Python script that made use of a couple of my public classes. The script is good enough to share with my colleagues but I'm faced with the problem of my public classes: I imagine that most of my colleagues haven't even heard of my public classes and I don't expect them to download the entire repo just to get a couple of classes If I'm going to distribute the classes as separate files I introduce new problems: It could be confusing to have the files tag along. What is the user supposed to do with them? The answer is nothing - they should leave them alone and make sure they are in the same directory as the main script in case they decide to move th...

New Years Eve 2009

So this is my first blog entry!! It's New Years Eve and I've got a horrible cold so I'm staying in a lot. I had planned to see Avatar at the IMAX theater in Raleigh this afternoon but don't want to risk people exposing people to what I have. Also, I would probably annoy people with lots of nose blowing. I ordered tickets online and, using Craigslist , I arranged to sell them to someone else who can use them since the prime show times have been selling out. So... I have to wait to see Avatar. I still have foster cats Jack and Purrvis from the SPCA ... Jack gets supervised visits but I've had to defuse lots of situations: a couple of fights with Katie in the past week plus Sofi let out this noise I'd never heard before.... I didn't think she had it in her. So usually I just ferry Jack back into the bedroom and let them cool off. I just had to break up a prolonged fight so I may not let Jack out much until I take them back to the SPCA on Saturday. I went i...

Python ArgParse

Maybe it's my use of the getopt() function in C/C++ but in my Python scripts, I've always just made do with the native getopt package and it's served me well. Over time, people have encouraged me to look at using ArgParse but I've always been kind of resistant because it was a little mysterious to me. I finally sat down and figured out how to do basic things I do a lot in scripts using getopt and worked up a simple script using ArgParse to do the same thing. There are definitely some advantages to ArgParse ! Here's the gist with this sample: I've made a few updates to the gist as I discover new features of which I want to take advantage: Mutually-exclusive options Script description etc. I will admit that since I've created this gist, I've been using ArgParse for new scripts. I can't whip out a script using ArgParse from memory yet (which I can do when I use getopt !) but it's becoming easier. I haven't gone back and...