Using awk, how to convert dates to week and quarter? - date

Using awk, how to convert dates (yyyy-mm-dd) to week and quarter (first day of week set to monday)?
Input:
a;2016-04-25;10
b;2016-07-25;20
c;2016-10-25;30
d;2017-02-25;40
Wanted output:
a;2016-04-25;10;2016-w17;2016-q2
b;2016-07-25;20;2016-w30;2016-q3
c;2016-10-25;30;2016-w43;2016-q4
d;2017-02-25;40;2017-w8;2017-q1

awk solution:
awk -F';' '{ split($2,d,"-"); w = strftime("%W", mktime(d[1]" "d[2]" "d[3]" 00 00 00"));
q = int((d[2]+2)/3);
print $0,d[1]"-w"w,d[1]"-q"q}' OFS=';' file
The output:
a;2016-04-25;10;2016-w17;2016-q2
b;2016-07-25;20;2016-w30;2016-q3
c;2016-10-25;30;2016-w43;2016-q4
d;2017-02-25;40;2017-w08;2017-q1
split($2,d,"-") - split the 2nd field (date) by separator -
mktime(datespec) - turn datespec (date specification) into a timestamp
strftime("%W", mktime(d[1]" "d[2]" "d[3]" 00 00 00")) - format the time returned by mktime() function according to %W (the week number of the year)
q = int((d[2]+2)/3) - calculating the quarter number. The quarter is equivalent to 3 months. So we'll use 3 as a divisor.
https://www.gnu.org/software/gawk/manual/html_node/Time-Functions.html

Related

How to convert 9 digit number into a particular date format?

I want to convert 9 digit number into a particular date format.
Example -
Number - 000007547 ===> Date - 2016/10/05
Number - 000007550 ===> Date - 2016/10/08
Number - 000007559 ===> Date - 2016/10/17
I already have this numbers and it's dates but I'm unable to find the logic behind that conversion. Is anyone aware of this 9 digit date-time format?
It seems that the number is a count of days.
Try this python script:
from datetime import date
d0 = date(1996, 2, 6)
d1 = date(2016, 10, 17)
delta = d1 - d0
print delta.days
To convert this format you can use this script:
from datetime import date,timedelta
d0 = date(1996, 2, 6)
d1 = date(2016, 10, 17)
delta = d0 + timedelta(days=7559)
print delta
Output: 2016-10-17
It is the timestamp.
A timestamp is encoded information, which indicates the date and time at which a particular event has occurred.
import datetime
value=1258094605
date_obj=datetime.date.fromtimestamp(value)
print(date_obj)

How can I set Date format when use power shell to generate date and save to batch variable?

I use this command in batch for generate curentdate -1 and save it to variable in batch but How can I put format in ToString() I already use ' and "
for /f "tokens=*" %a in ('powershell "$date = Get-Date; $date=$date.AddDays(-1);$date.ToString();"') do set var=%a
You can use :
for /f "tokens=*" %a in ('powershell "$date = Get-Date; $date=$date.AddDays(-1);$date.ToString('yyyy:MM:dd');"') do set var=%a
Here are the specificators :
Spécificator Type Example Output Example
dd day dd 10
ddd Name of the day ddd Jeu.
dddd Complet name of the day dddd Jeudi
f, ff, … Fractions of seconds fff 932
gg, … position gg ap. J.-C.
hh Hour two digits hh 10
HH Hour two digits (24 hours) HH 22
mm Minuts 00-59 mm 38
MM Month 01-12 MM 12
MMM Month shortcut MMM Sep.
MMMM complet name of the month MMMM Septembre
ss Seconds 00-59 ss 46
tt AM or PM tt ““
yy Years, 2 digits yy 02
yyyy Years yyyy 2002
zz Time zone, 2 digits zz +02
zzz Complete Time zone zzz +02:00
: Separator hh:mm:ss 10:43:20
/ Separator dd/MM/yyyy 10/12/2002

Average similar time periods MATLAB

I have a matrix with a timestamp and several column variables.
The matrix spans a month of half hourly variables. Here is a sample of four columns of the matrix
11/11/2015 20:15 31.26410236 35.70104634 35.93171056
11/11/2015 20:45 32.10746291 35.48806277 35.9647747
.
.
.
12/11/2015 20:15 32.10746291 35.48806277 35.9647747
12/11/2015 20:45 32.10746291 35.48806277 35.9647747
.
.
.
13/11/2015 20:15 32.68310429 35.58753807 37.26447422
13/11/2015 20:45 33.05141516 34.8432801 36.48033884
.
.
.
14/11/2015 20:15 32.08328579 34.66482668 34.65446868
14/11/2015 20:45 32.19994433 34.40562145 34.34035989
What is the easiest way to find the average of identical times in terms of hours and minutes?
E.g. mean of each variable at time 20:45 for all days of the month.
I know I could achieve this by converting the timestamp to a datenum, taking the fractional part of datenum and sorting the data by the fractional part of datenum. After that I could block average the rows with similar fractional datenums. Is there a more efficient and more elegant way?
With matlab you can work directly with date and times without converting it to timestamp in miliseconds or seconds:
http://es.mathworks.com/help/matlab/date-and-time-operations-1.html
Or an easy way is to convert dates to a date vector like this:
DateVector = datevec(DateString,formatIn)
then compare the columns you want:
[Y,M,D,H,MN,S] = datevec(___)
>> A = datevec('13/11/2015 20:45','dd/mm/yyyy HH:MM')
A =
2015 11 13 20 45 0
>> B = datevec('14/11/2015 20:45','dd/mm/yyyy HH:MM')
B =
2015 11 14 20 45 0
with this is easy to compare dates:
>> A - B
ans =
0 0 -1 0 0 0
exactly one day difference
This is what I ened up doing to solve this problem:
timestamp=linspace(datenum('2015-11-01 00:00', 'yyyy-mm-dd HH:MM'),datenum('2015-12-01 00:00', 'yyyy-mm-dd HH:MM'),1440); % 30 days
timestamp=timestamp';
time_of_day=datetime(datevec(timestamp(1:48)),'Format','HH:mm');
numdays=30;
data=rand(length(timestamp),2);
means=NaN(48,3);
for tt=1:48
means(tt,:)=[datenum(time_of_day(tt)) nanmean(data(tt:48:48*numdays,:),1)];
end
figure;
plot(time_of_day,means(:,2:3));
xlim([timestamp(1) timestamp(48)]);

AppleScript countdown

I want to have a string that counts down from current time to alarm time.
I've manage to figer out how to get the current time and how to set the alarm time.
The problem I'm having is that when I take current time - alarm time it gives me a numer witch I then need to format back to a hh:mm:ss string.
i've got this.
set alarmHour to 23
set alarmMinute to 00
set theDate to the current date
set the hours of theDate to alarmHour
set the minutes of theDate to alarmMinute
set the seconds of theDate to 0
theDate
set countdown to theDate - (current date)
set ss to countdown / 60
at this point it gives me 22.283333333333 witch i now need to convert to hh:mm:ss and then put them into a sting that give me 00:22:00
UPDATE:
in swift you have % you can use
countDownTime = (formatterInteger - timeControlInteger)
let interval = Int(countDownTime)
let seconds = interval % 60
let minutes = (interval / 60) % 60
let hours = (interval / 3600)
but how to you do this in applescript?
Answer to second question:
is there a way to format strings like in swift? like
String(format:"%02d",absHour) – Mathias Halén
Yes, but you need to use the Satimage.osax scripting addition, available for free at:
Satimage AppleScript Additions
Satimage strftime() -- Date/Time Format Function
strftime v : format a date using a specification string like in the C
function strftime.
strftime date or list of date
into string : the formatting string. To obtain ISO 8601 dates, use
"%FT%TZ" or "%GW%V-%uT%TZ" (using the 'with GMT' parameter)
[GMT boolean] : if true, output date as GMT. Default: false, the ouput
date is local.
→ string : the formatted date
EXAMPLE: strftime (current date) into “%x” RETURNS: 07/22/14
"%a, %b %d, %Y" RETURNS: Tue, Jul 22, 2014
set d to current date
-- some ISO 8601 formats:
strftime d into "%FT%T%z"
-- "2007-01-15T16:10:56+0100"
strftime d into "%GW%V-%uT%T%z"
-- "2007W03-1T16:10:56+0100"
--if you need to store the date d as UTC:
strftime d into "%FT%TZ" with GMT
-- "2007-01-15T15:10:56Z"
strftime d into "%a, %b %d, %Y %H:%M:%S %z"
-- "Mon, Jan 15, 2007 16:10:56 +0100"

How do I convert microseconds into a timestamp?

I took this piece from an unencrypted .DAT file:
Code:
00 e1 27 17 6f e6 69 c0
Which translates to 63,374,851,375,000,000 in decimal. The units for the number are microseconds.
And this huge number cannot bypass the 1st January 1970 00:00:00 format; such a format that most converters use today.
So, yes. Is there such a converter that uses the 1st January of the year 1 format? Or how shall I make one?
And by the way, a timestamp is both date and time.
Thanks in advance!
You do not say what language are you using, if it is a .NET language, you can use: http://msdn.microsoft.com/en-us/library/z2xf7zzk.aspx for that constructor the input is in nanoseconds (are you sure that your number is in milliseconds and not in nanoseconds?).
If you are sure it is in milliseconds, the conversion to nanoseconds should be easy: 1 millisecond = 1 000 000 nanoseconds.
But I have the feeling that those are nanoseconds and not milliseconds...
Now that you have told us that it is in microseconds:
C# Example from decimal to yyyy dd MM hh:mm:ss
long microseconds = 63370738175000000;
long ticks = microseconds * 10;
DateTime timestamp = new DateTime(ticks);
Console.WriteLine(timestamp.ToString("yyyy dd MM hh:mm:ss"));
It prints:
2009 20 02 02:49:35
The other way around from yyyy dd MM hh:mm:ss to decimal
String dateString = "2009 20 02 02:49:35";
DateTime timestamp = DateTime.ParseExact(dateString, "yyyy dd MM hh:mm:ss",CultureInfo.CurrentCulture);
long ticks = timestamp.Ticks;
long microseconds = ticks / 10;
Console.WriteLine(microseconds);
It prints:
63370694975000000
And if you want it in hexadecimal just write:
Console.WriteLine(microseconds.ToString("X"));
Then it will print:
E1234FB3278DC0
If you want the answer in another programming language, please add that to you question.
In JAVA in order to convert microseconds into java.sql.Timestamp:
public static Timestamp getTimestampFromMicros(long pMicros) {
long millis = TimeUnit.MICROSECONDS.toMillis(pMicros);
long shaaritInMicros = pMicros - TimeUnit.MILLISECONDS.toMicros(millis);
Timestamp ts = new Timestamp(millis);
long nanos = ts.getNanos() + TimeUnit.MICROSECONDS.toNanos(shaaritInMicros);
ts.setNanos((int)nanos);
return ts;
}
Use below Java code to covert microseconds to date and time,
long msec = microseconds * 1/1000;
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
dateFormat.format(msec);
Which will returns,
2016-01-27 03:41:12