Generating Dates between two date ranges in AWK - date

I need to create a list of days between a date interval.
Say for example from 2001-01-01 to 2009-12-31:
2001-01-01
2001-01-02
2001-01-03
..
2009-12-29
2009-12-30
2009-12-31
I know how to do it but maybe someone has a script already made?
If not, I will make such a script and upload it so others won't waste time on this when they need it.

I do not know awk from GnuWin32, but if the functions "mktime" and "strftime" are available, you can try the following code:
BEGIN {
START_DATE="2001-02-01"
END_DATE="2001-03-05"
S2=START_DATE
gsub("-"," ",S2)
T=mktime(S2 " 01 00 00")
if (T<0)
printf("%s is invalid.\n",START_DATE) >> "/dev/stderr"
else
{
for(S=START_DATE; END_DATE>S ;T+=86440) print S=strftime("%F",T)
}
}
The key is to convert the start date to a number meaning the seconds since the Epoch, add 86400 seconds (one day or 24 x 60 x 60) and convert back to the ISO date format.
After some trials I realized the mktime() function admits wrong dates as good (for instance, 2000-14-03).
Best regards

Related

how to get a week's date with autohotkey

i'm using autohotkey to work with date, i need to catch the day a week ago
example
if today is the 28th then I have to take the 21st of last week
calendar
in the following script I take the current date
FormatTime, date, , dd/MM/yyyy
MsgBox %date%
I even thought of a logic, to take the current day subtract by 7 that will take the day a week ago. I need help to create a better script
28 - 7 = 21
if anyone can help me thanks :)
Just subtracting numbers would be bad when you encountered a change between months.
Would have to create custom logic for that.
Luckily AutoHotkey's += operator supports date/time math.
So this is all you need:
;we're starting off the date1 variable as blank,
;which means the current time will be used.
date1 += -7, days
FormatTime, finalDate, % date1, dd/MM/yyyy ;format the result to our desired format
MsgBox, % finalDate
I did it that way
FormatTime, date_, , dd
sub += date_-7
FormatTime date, , /MM/yyyy
MsgBox,%sub%%date%

Lua 24-hour format of an hour without leading zeros

I need to get the act hour in Lua without the leading zeros(in 24 hour mode)
it's like date("G") in PHP
I actually have this code
os.date("%d-%m-%Y %H")
but it returns me 01 , 02 , 03 .... and I need to get 1 , 2, 3 etc
I found this but it doesn't seem to help me to solve my problem
os.date formats
You can just remove the leading zero:
os.date("%d-%m-%Y %H"):gsub(" 0"," ")
Try to get hour :
local hour = os.date("%H")
And try to format with string.format :
string.format("%d", hour)

Calculating seconds since the Unix Epoch when there is no +"%s" option for the 'date' command available

I wanted to calculate the seconds since the Unix Epoch (1970-01-01 00:00:00). Usually I would use
date +"%s"
Now, on my system the +"%s" option is not available but I easily got around using some other 'date' options and parsed it using bc:
date -u +"scale=0;(((((%Y-1970)*365.2425+%j)*24+%H)*60+%M)*60+%S)/1" | bc
This is short for
years = year_now - 1970
days = years * 365.2425 + day_of_year_now
hours = days * 24 + hour_now
minutes = hours * 60 + minute_now
seconds = minutes * 60 + second_now
So far so good. Then I discovered that the result of this calculation does not match with the result of the +"%s" option. I needed to add a magic number:
date -u +"scale=0;(((((%Y-1970)*365.2425+%j)*24+%H)*60+%M)*60+%S-36936)/1" | bc
Why?
Additionally, several months later, this magic number has changed from -36936 to -99792.
Why?
I'm sure something is wrong with my maths. I do not need better solutions in other script languages but I'd appreciate if somebody could correct my maths, please. Maybe someone has the source code for date and could show me its internal algorithm for +"%s" ... ?
Here is a POSIX way that should then work on all Unix and Unix like systems:
awk 'BEGIN {srand();print srand()}'
If you use ksh93, this should also work:
printf "%(%s)T\n"
Most system will have perl installed:
perl -le 'print time'

18 digit julian timestamp in perl

I need to get 18-digit Julian Timestamp in my perl script. Could anyone help me in this? However I have written a subroutine to achieve this but it does not look good to me since it always gives me a number ending with 6 zeroes. Please help to get a proper 18-digit J-timestamp.
sub GetJulianTimestamp()
{
my $t = `perl -e 'print time, "\n"'`;
return (($t * 1000000 ) + 210866803200000000);
}
Based on the comments, you appear to be asking how to obtain the number of microseconds since the unix epoch.
use Time::HiRes qw( );
my $microsec_time = int( Time::HiRes::time() * 1_000_000 );
return 210866803200000000 + $microsec_time;
I agree with the answer given by ikegami, except the amount to be added to the unix epoch needs to be changed. The value 210866803200000000 corresponds to November 24, 4714 BC, 00:00 Universal Time, Gregorian proleptic calendar. But the epoch of Julian dates is at noon, not midnight. So the amount to be added should be 210,866,760,000,000,000. And of course there is no official name for a Julian date that has been converted to microseconds, so anyone using such a number would have to provide an explanation to anyone who is receiving the data.

Comparing Current Date and Time Against a File

I am looking to compare the last updated date and time of a file against the current date.
To date the date/time of the file I am splitting up the time and date tag like this:
find . -maxdepth 1 -type f -name \*.dat -printf "%Tm,%Td,%TH,%TM\n"
And I am looking to match this against the current date:
date '+%m,%d,%H,%M'
Both are in the same format - DD MM HH MM - and I would like to compare if the time is more than two minutes out.
What would be my best option for coding this - can I use the values I have obtained?
Or even - would there be an easier method?
Yes, there is an easier way.
You can use
find -mmin -2
for finding all the entries which have been changed less than two minutes ago.
Or, of course, use
find -mmin +2
to find the entries which are older than two minutes (have been changed more than two minutes ago).