How to subtract minutes from a given time? - linux

I have a given time string 14:00 and want to subtract 5 Minutes.
How can this be done in bash?
Gives me a datetime object: date -d "14:00" +'%H:%M' -> 14:00
I tried subtract: date -d "14:00 - 5min" +'%H:%M'
-> Gives 21:01. But Why?
Desired result if of course: 13:55.

The following works:
$ date -d "14:00 5 minutes ago" +'%H:%M'
13:55
I can't explain why though.

From the info page for gnu date:
Our units of temporal measurement, from seconds on up to months,
are so complicated, asymmetrical and disjunctive so as to make
coherent mental reckoning in time all but impossible. Indeed, had
some tyrannical god contrived to enslave our minds to time, to make
it all but impossible for us to escape subjection to sodden
routines and unpleasant surprises, he could hardly have done better
than handing down our present system. It is like a set of
trapezoidal building blocks, with no vertical or horizontal
surfaces, like a language in which the simplest thought demands
ornate constructions, useless particles and lengthy
circumlocutions. Unlike the more successful patterns of language
and science, which enable us to face experience boldly or at least
level-headedly, our system of temporal calculation silently and
persistently encourages our terror of time.
—Robert Grudin, ‘Time and the Art of Living’.
.
and, more relevantly:
The time may alternatively be followed by a time zone correction,
expressed as ‘SHHMM’, where S is ‘+’ or ‘-’, HH is a number of zone
hours and MM is a number of zone minutes
Your attempt to perform arithmetic is failing, and date is trying to parse the expression "- 5min" as a time zone correction.

Related

How long does it take to crack a hash?

I want to calculate the time it will take to break a SHA-256 hash. So I research and found the following calculation. If I have a password in lower letter with a length of 6 chars, I would have 26^6passwords right?
To calculate the time I have to divide this number by a hashrate, I guess. So if I had one RTX 3090, the hashrate would be 120 MH/s (1.2*10^8 H/s) and than I need to calculate 26^6/(1.2*10^8) to get the time in seconds right?
Is this idea right or wrong?
Yes, but a lowercase-latin 6 character string is also short enough that you would expect to compute this one time and put it into a database so that you could look it up in O(1). It's only a bit over 300M entries. That said, given you're 50% likely to find the answer in the first half of your search, it's so fast to crack that you might not even bother unless you were doing this often. You don't even need a particularly fancy GPU for something on this scale.
Note that in many cases a 6 character string can also be a 5 character string, so you need to add 26^6 + 26^5 + 26^4 + ..., but all of these together only raises this to around 320M hashes. It's a tiny space.
Adding uppercase, numbers and the easily typed symbols gets you up to 96^6 ~ 780B. On the other hand, adding just 3 more lowercase-letters (9 total) gets you to 26^9 ~ 5.4T. For brute force on random strings, longer is much more powerful than complicated.
To your specific question, note that it does matter how you implement this. You won't get these kinds of hash rates if you don't write your code in a way to maximize the GPU. For example, writing simple code that sends one value to the GPU to hash at a time, and then compares the result on the CPU could be incredibly slow (in some cases slower than just doing all the work on a CPU). Setting up your memory efficiently and maximizing things the GPU can do in parallel are very important. If you're not familiar with this kind of programming, I recommend using or studying a tool like John the Ripper.

How to remove a sentence that occurs after 'MORE:' in the given string?

For the news articles which I have scraped, I am trying to omit the sentence that occurs after 'MORE:', as it is a backlink and has nothing to do with the article.
The prime minister announced a new COVID Alert System to track the coronavirus R number -- the rate at which the virus reproduces -- that will determine how the country phases out of lockdown. The R number is now between 0.5 and 0.9 in the U.K., he said. If the data supports it, by June at the earliest some schools could open, and by July, pending “further scientific advice,†the government hope that areas of the hospitality industry may be able to reopen.
MORE: What it's like inside the Oxford trial leading the race for coronavirus vaccine
If we as a nation begin to fulfil the conditions I have set out, then in the next few weeks and months we may be able to go further.....
So for the above example, I want the python program to delete the line which follows 'MORE:' till a new line i.e. '\n' is detected which means that the article follows on from there.
PS - I know that regular expressions would be most likely used in such a case but I am not sure how to do it.
You can achieve this using the regex positive lookahead method.
So your pattern will be
pattern = r'(?=MORE:)(.*)'
Pattern Explanation:
(?=MORE:): Will match any group after the expression MORE:, without including it in the result
(.*): Will group 0 or any character that are not line breaks
Your final code in python will be
import re
pattern = r'(?=MORE:)(.*)'
target_string ='''
The prime minister announced a new COVID Alert System to track the coronavirus R number -- the rate at which the virus reproduces -- that will determine how the country phases out of lockdown. The R number is now between 0.5 and 0.9 in the U.K., he said. If the data supports it, by June at the earliest some schools could open, and by July, pending “further scientific advice,†the government hope that areas of the hospitality industry may be able to reopen.
MORE: What it's like inside the Oxford trial leading the race for coronavirus vaccine
If we as a nation begin to fulfil the conditions I have set out, then in the next few weeks and months we may be able to go further.....'''
without_more = re.sub(r'(?=MORE:)(.*)','',target_string)
print(without_more)
An alternative solution:
without_more=target_string[:target_string.index('MORE')]
print(str(without_more))

Discontinuity in results from Excel's Present Value function PV

The PV function of Excel (present value of an investment) seems to make a sudden jump somewhere and is raising an error. For example:
WorksheetFunction.PV(-19, 240, 500000)
will return a value while:
WorksheetFunction.PV(-20, 240, 500000)
will raise an exception. There are other examples where things go wrong. Independently of the semantic meaning of the parameters, the formula to calculate the PV is purely continuous around the jump, so what is causing this?
When calculating things in, for instance, Mathematica this does not happen.
The first term of the formula used to calculate PV is PV * (1+rate) ^ nper (as in your link). Using your first example this gives 4.84861419016704E+304 so close enough to Excel’s limit (of 9.99999999999999E+307) that no element of the first term would have to increase by much for the result to exceed the inbuilt limitation, for which your second example seems to have been enough.
Note that, as #Juliusz has pointed out, the first parameter is the interest rate per period and 19 represents 1900%. For 240 periods (looks like 20 years) the combination is not realistic (ie 1900% per month). Also, the rate should not normally be negative (better to handle whether in or out by signing the payment amount).
Seems that -19.45 is the minimum value, which translates to -1945%. Are you sure you need more?

How to convert epoch time (unix timestamp) in D to standard (year-month-day)

How do you convert epoch time (unix timestamp) to standard time in D? Is there a way to customize the format?
You really should separate questions out, not ask two completely different questions at the same time.
How do you convert epoch time (unix timestamp) to standard time in D?
If you need to convert from unix time to SysTime's "std time," then you use unixTimeToStdTime:
assert(unixTimeToStdTime(0) == (Date(1970, 1, 1) - Date.init).total!"hnsecs");
So, if you do SysTime(unixTimeToStdTime(0)), you'll get a SysTime in your local time zone at the point in time when it was midnight, January 1st 1970 in UTC (i.e. the epoch time). Unlike the other SysTime constructors, it does not treat the time it's given as being in the timezone that it's given. Rather, it just sets its stdTime to the given value, and it's timezone to the given value. So, to construct an identical SysTime with the other constructors, you'd do something like
auto epochInLocalTime = SysTime(Date(1970, 1, 1), UTC()).toLocalTime();
If you want to convert in the opposite direction, stdTimeToUnixTime will convert a std time to a unix time. However, SysTime has toUnixTime on it, making it so that you generally don't need stdTimeToUnixTime.
time_t epoch = epochInLocalTime.toUnixTime();
However, one thing to be aware of is the fact that std.datetime truly deals with unix time - time_t is always considered to be in UTC. The reason that this matters is that for some inexplicable reason, Windows applies the local time's DST to time_t so that the UTC offset never changes, which means that it's wrong for a good chunk of the year. It works with all of the Microsoft's functions which use time_t, because they expect that nonsense, but you'll have interoperability problems if you try and use a Windows time_t with another box or with a library like std.datetime which actually uses time_t correctly.
Is there a way to customize the format?
You mean that you're looking for a way to provide a user-defined format for a string representing the time (like C's strftime)? std.datetime doesn't have that yet. The API still needs to be designed for it. Some discussion has taken place, but it hasn't been settled on yet. So, it'll happen eventually, but it could be a while.
In the interim, you have toISOString, toISOExtString, and toSimpleString, which use the ISO format, the ISO extended format, and Boost's simple string format respectively. In general, I'd suggest using toISOExtString, because it's both easily read by humans and standard. It's also generally best to put it in UTC (e.g. sysTime.toUTC()) when communicating with other computers (as opposed to printing it out for humans), because then the time zone is part of it, unlike with LocalTime, which doesn't append the time zone.
If you haven't read this article on std.datetime yet, then I suggest that you do, since it should give you a good overview of the module and how to use it.
I'm not familiar with D, but according to std.datetime, you could use these steps
long unixTimeToStdTime(time_t)
struct SysTime(long stdTime)
SysTime.dayOfGregorianCal()
struct Date(int day)
Date.toISOExtString()

Is there a Haskell Time Typeclass

I want to build a function with type signature Time t => t -> Bool. When looking at the documentation of Data.Time there are several different types that work on time, such as: UTCTime, LocalTime, and ZonedTime, but I find no typeclass that unifies them. Is there any such one or should I treat time just as a Num? (i.e. a continuum)
The vector-space package has a affine space typeclass.
Diff p is here the time duration type (which should be an instance of VectorSpace), and p is the time point type. You'll need an extra Ord instance for comparisons.
This provides you with linear interpolation between time points for free.
There is no such type class in the standard time library, but it is possible to implement one by yourself.
However, usually you should construct your program logic in such a way that UTCTime is used for all time-based calculations (and this is not Haskell-specific). LocalTime and ZonedTime should just be used to convert back and forth between UTC and a presentation that is showed to the user or for data that comes from external sources. This is probably the reason there are no ready-made functions for calculating time-diffs and time additions for local and zoned time types.
Time is slightly strange.
Time can refer to a specific instant in time (e.g., 09:27 AM, 14 Feb 1821 AD), or a duration of time (e.g., 6 minutes).
It makes sense to add and subtract durations. It doesn't really make sense to find the sum of two instants in time; what would this represent? Adding a duration to an instant would give you another instant; that makes sense. And subtracting one instance from another ought to give you the duration between them.
In summary, temporal arithmetic is not as simple as you might imagine.
Now, what the time package provides? I have no idea. It sounds like all the times you mentioned are instants in time, not time durations...
Take a look at the HasTime class in the time-lens package.
It gives you (both read and write) access to the TimeOfDay component of all those structures. So, if you implement your function for TimeOfDay, it can be easily generalised to LocalTime, ZonedTime and UTCTime.
According to the documentation (http://www.haskell.org/ghc/docs/7.0.2/html/libraries/time-1.2.0.3/Data-Time-Format.html), all of UTCTime, ZonedTime and LocalTime are instances of the typeclasses FormatTime and ParseTime. They should be what you are looking for.

Resources