Time Does Not Flow Evenly
You are working on some system and you need some kind of timestamp in milli- or nanoseconds. Nothing easier than that.
In Unix you might use clock_gettime
and in Java System.currentTimeMillis()
and of your go.
// Forgive me my C, I probably got things wrong =)
#include <sys/time.h>
#include <stdint.h>
int64_t time_stamp_nanos(){
struct timespec current;
clock_gettime(CLOCK_REALTIME, ¤t);
return current.tv_sec * 1000000000 + current.tv_nsec;
}
Terminal Utilities I Often Use
In this blog post, I just list a few Unixy terminal utilities I often use. If you are a terminal veteran this will be boring for you. If you rarely use the terminal, it might have a utility or two you are not aware of yet.
I’m not giving a tutorial on the commands. Google for more details or read the man pages =). It is just to let you know that these tools exist.
Slack Java API and the Data Non Problem
Recently I had the "joy" to work with the Slack Java API. I felt that this Java API is tedious and an example of an API which create busy work for little benefit. Let me show first how the API is used:
var slack = Slack.getInstance();
var methods = slack.methods();
var channeId = "C424242";
var request = ChatPostMessageRequest.builder()
.channel(channeId)
.text(":wave: Hi from a bot written in Java!")
.build();
var response = methods.chatPostMessage(request);
Keep Time Explicit
In a lot of software is operations are depending on time. For example creating invoices, statistics, showing the latest news so on so forth. In this blog post, I use an example summing up bought items in the last week. We might have multiple layers in our system, like database access, places to do extra processing, and a web interface. I’m using C# here, but it applies to most mainstream languages. As you can see, to get the right database records the cure uses the current time.
public class Purchases
{
public IList<Purchase> LastWeeksPurchases()
{
// Assume more complex queries, let's say joins with users, applied coupons, discounts at the time and god knows what
return Database.InTransaction(conn => conn.Query<Purchase>(@"select * from purchases
where dateTime < current_timestamp
and (current_timestamp + INTERVAL '-7 day') < dateTime
order by dateTime").ToList());
}
}
C# Loves Code Patterns
This post is part of the third annual C# Advent. Check out the home page for up to 50 C# blog posts in December 2019! Thanks, Matthew D. Groves for organizing it.
You might recall that LINQ has two forms. The first way is to call LINQ methods and the second is the special LINQ syntax. Here’s an example of the LINQ syntax.