convert Calendar date to Julian Date in Unix - shell

I have one requirement. in one of the source file I'm getting calendar date as input and while processing the file it has to convert Julian Date format. I just need a Script.
ex:Date: 10-Nov-2020
Julian Date: 2020314

The easiest way is
date -d 10-Nov-2020 +%Y%j
but it seems to count from 1, not 0, so it returns 2020315.
Perl's Time::Piece can be used to get the expected value:
perl -MTime::Piece -lwe '$t = localtime->strptime(shift, "%d-%b-%Y"); print $t->year, $t->yday' -- 10-Nov-2020
You might need sprintf "%03d", $t->yday instead of just $t->yday if you want 2020000 instead of 20200 for the first day.

Related

Python datetime function - one pending aspect still from my earlier question

Earlier someone answered my question on Python's datetime function which I am grateful to. However my followup comment/ question remains unanswered. Please help.
My followup question:
Thank you so much. Your answer helped. It works. However when you give 2 digit year with %y and you try to print the year with date.year, it prints prefix of 20xx (e.g. 2023 in my example below). If I wanted it to print 19xx what should I do?
Original question:
datetime.strptime not working as portrayed widely. Using Python 3.11
I see several posts talking about datetime.datetime.strptime that can be used to convert date string '12/13/23' to a datetime object. This doesn't work for me.
import datetime dt = datetime.datetime.strptime('12/13/2023', "%m/%d/%y") produces error as follows
Traceback (most recent call last): File "C:\Users\User\IdeaProjects\FirstTest\Scratchpad.py", line 3, in dt = datetime.datetime.strptime('12/13/2023', "%m/%d/%y") ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\User\AppData\Local\Programs\Python\Python311\Lib_strptime.py", line 568, in _strptime_datetime tt, fraction, gmtoff_fraction = _strptime(data_string, format) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\User\AppData\Local\Programs\Python\Python311\Lib_strptime.py", line 352, in _strptime raise ValueError("unconverted data remains: %s" % ValueError: unconverted data remains: 64
Many people show the above code with output working as 2023-12-13 as a datetime object. Doesn't work for me
I was expecting dt.month() to return the month and print(dt) to print yyyy-mm-dd
Answer:
Your code doesn't show the same code that you're running as your traceback.
Your code, which works:
dt = datetime.datetime.strptime('12/13/23', "%m/%d/%y")
Your traceback, which doesn't work:
dt = datetime.datetime.strptime('12/13/2023', "%m/%d/%y")
The traceback is correct to produce an error here, the format string you've given is not valid for the date string you've given. %y parse 2-digit year, and 2023 is not a 2-digit year, you need %Y if you want to parse a 4-digit year.

Add custom letter to timestamp instead of month in shell or perl

So, I am not a coder but i have to write a shell script that can "call" a timestamp in format [A][21][16][30][4] where A is the Month (A for January, B for February, C for March and so on), 21 is the day, 16 is the hours, 30 is the minutes and 4 are the tenth of a milisecond (0-5). Brackets are only for visualization, so the timestamp should be A2116304
This needs to be either a shell script or a perl code, that is part of a shell script (i need to put this is an existing shell script).
I tried searching for solution, but couldnt find anything useful.
The idea is that i need to append this custom timestamp to a file name, like
FILENAME.TIMESTMAP
Thanks !
Assuming that:
time is now,
time zone is GMT,
you want a fixed length timestamp,
the last digit should be tens of seconds.
I suggest something like:
my ($s, $m, $h, $D, $M) = gmtime;
my $prefix = "snapshot";
my $filename = sprintf "%s.%s%02d%02d%02d%d",
$prefix, chr($M+ord "A"), $D, $h, $m, $s/10;
print $filename, "\n";
Output:
snapshot.J2612182
You can use localtime instead of gmtime if you don't want to use GMT.
Both *time functions take a UNIX timestamp as argument, in case you need something other than now.

DATES with awk in UNIX [duplicate]

This question already has an answer here:
take date from file in unix
(1 answer)
Closed 6 years ago.
I want to take two dates as argument from the user ( ) with
$./tool.sh --born-since <dateA> --born-until <dateB>
and from a file print the lines that are between those two dates.For example:
933|Mahinda|Perera|male|1989-12-03|2010-03-17T13:32:10.447+0000|192.248.2.123|Firefox
1129|Carmen|Lepland|female|1984-02-18|2010-02-28T04:39:58.781+0000|81.25.252.111|Internet Explorer
4194|Hồ Chí|Do|male|1988-10-14|2010-03-17T22:46:17.657+0000|103.10.89.118|Internet Explorer
So , i use awk command like this :
awk -F'|' '{print $4} [ file ... ]
to take the dates .. how can i use awk to make the dates from the txt to seconds form ?
if the date variables are in the same format, you can convert everything to numbers and use comparison.
awk -F'|' -v from=$dateA -v to=$dateB '{gsub("-","",$5);
gsub("-","",from); gsub("-","",to)}
from <= $5 && $5 <= to' file
Note, it's the fifth field in your file.
You can either call the /bin/date +"%s" --date="DATESTRING" through system() if the DATESTRING matches a format "/bin/date" understands, or you use the internal mktime() function. But then you need to split your date according to awk(1):
mktime(datespec)
Turn datespec into a time stamp of the same form as returned by systime(), and return the result. The datespec is a string of
the form YYYY MM DD HH MM SS[ DST]. The contents of the string are six or seven numbers representing respectively the full year
including century, the month from 1 to 12, the day of the month from 1 to 31, the hour of the day from 0 to 23, the minute from 0
to 59, the second from 0 to 60, and an optional daylight saving flag. The values of these numbers need not be within the ranges
specified; for example, an hour of -1 means 1 hour before midnight. The origin-zero Gregorian calendar is assumed, with year 0
preceding year 1 and year -1 preceding year 0. The time is assumed to be in the local timezone. If the daylight saving flag is
positive, the time is assumed to be daylight saving time; if zero, the time is assumed to be standard time; and if negative (the
default), mktime() attempts to determine whether daylight saving time is in effect for the specified time. If datespec does not
contain enough elements or if the resulting time is out of range, mktime() returns -1.
So you need to prepare your date fields to use the form given in the documentation.
split($5, D, "-");
DS = sprintf("%4d %2d %2d 00 00 00", D[1], D[2], D[3]);
T = mktime(DS);
should do the job.

How to convert a string to timestamp with milliseconds in Hive

I have a string '20141014123456789' which represents a timestamp with milliseconds that I need to convert to a timestamp in Hive (0.13.0) without losing the milliseconds.
I tried this but unix_timestamp returns an integer, so I lose the milliseconds:
from_unixtime(unix_timestamp('20141014123456789', 'yyyyMMddHHmmssSSS')) >> 2014-10-14 12:34:56
Casting a string works:
cast('2014-10-14 12:34:56.789' as timestamp) >> 2014-10-14 12:34:56.789
but my string isn't in that form.
I think I need to reformat my string from '20141014123456789' to '2014-10-14 12:34:56.789'. My challenge is how to do that without a messy concatenation of substrings.
I found a way to avoid the messy concatenation of substrings using the following code:
select cast(regexp_replace('20141014123456789',
'(\\d{4})(\\d{2})(\\d{2})(\\d{2})(\\d{2})(\\d{2})(\\d{3})',
'$1-$2-$3 $4:$5:$6.$7') as timestamp)
A simple strategy would be to use date_format(arg1, arg2), where arg1 is the timestamp either as formatted string, date, or timestamp and the arg2 is the format of the string (in arg1). Refer to the SimpleDateFormat java documentation for what is acceptable in the format argument.
So, in this case:
date_format('20141014123456789', 'yyyyMMddHHmmssSSS')
would yield the following string: '2014-10-14 12:34:56.789' which can then be cast as timestamp:
cast(date_format('20141014123456789', 'yyyyMMddHHmmssSSS') as timestamp)
The above statement would return timestamp (as desired).
i had the date field in this form 2015-07-22T09:00:32.956443Z(stored as string). i needed to do some date manipulations.
the following command even though little messy worked fine for me:)
select cast(concat(concat(substr(date_created,1,10),' '),substr(date_created,12,15)) as timestamp) from tablename;
this looks confusing but it is quite easy if you break it down.
extracting the date and time with milliseconds and concat a space in between and then concat the whole thing and casting it into timestamp. now this can be used for date or timestamp manipulations.
Let say you have a column 'birth_date' in your table which is in string format,
you should use the following query to filter using birth_date
date_Format(birth_date, 'yyyy-MM-dd HH:mm:ssSSS')
You can use it in a query in the following way
select * from yourtable
where
date_Format(birth_date, 'yyyy-MM-dd HH:mm:ssSSS') = '2019-04-16 07:12:59999';
I don't think this can be done without being messy. Because according to the unix_timestamp() function documentation it returns the time is seconds and hence will omit the milliseconds part.
"Convert time string with given pattern to Unix time stamp (in seconds), return 0 if fail: unix_timestamp('2009-03-20', 'yyyy-MM-dd') = 1237532400."
Best option here would be to write a UDF to handle this is you want to avoid messy concatenations. However the concatenation (though messy) would be better to the job.

CodeIgniter making the date helper output 12 hour format

Using the Ci date helper, how would I turn '06/08/2012 17:10pm' into 12 hour format?
date('m/d/Y H:ia ', strtotime($row->entryCreationDateTime))
That code has nothing to do with CodeIgniter, it's simply PHP functions. For information on the date function, see the manual page.
The small chatacter h puts the hour in 12-hour format and capital chatacter H puts the hour in 24-hour format.
Change the code to this:
date('m/d/Y h:ia ', strtotime($row->entryCreationDateTime))

Resources