How to get local (regional) date format in linux? - c

is it possible (if so, how?) to get local (regional) date format? Ideally in cross-platform way, otherwise at least Linux for start would be enough.
What am I talking about: For example this line when executed in terminal returns date (and time) formatted in local (regional) manner:
date +"%c"
What I would like to have instead of the numbers is the form in which this is displayed, for example if I set my regional setting to Lithuanian ones I get:
2016 m. birželio 27 d. 19:06:11
So I would like to get this instead of the above:
YYYY MM DD
If I set regional settings to US ones:
Mon 27 Jun 2016 07:09:24 PM EEST
In this case instead of the above I would like to get:
DD MM YYYY
Meaning - not the actual numbers, but how local(regional) date is formatted.
I later want to use this information for input/output operations facing user

While Joachims hint is correct, here a solution for your original question.
Just enter in bash:
locale -k LC_TIME | grep ^d_fmt | cut -d= -f2
If you need the time format instead of the date format, use t_fmt instead of d_fmt, and for the combined date/time format use d_t_fmt

Related

Change Git Log Plus/Minus Signs to Anything Custom?

Git CMD line noob here, how do I change the default plus/minus (+/-) signs to something more unique, such as (>>>/<<<) or (|/~). Or any other symbol not as common as (+/-)!
Reason: I am trying to automate a report that collects all the changes to our schema.sql files. I have the line below that does an adequate job:
git log -p --since="14 days ago" -- *Schema*.sql
My only real issue with the output is the plus/minus (+/-) signs which are used to show what has been added or removed:
+ This line was added
- This line was removed
Comments in SQL (t-SQL) are two minus signs (--), so when a comment is removed, I end up with this:
--- This comment was removed
If I can substitute the (+/-) with a unique value I can format the results and make a nice, pretty report for the people that want to see things like that. Thanks in advance!
--output-indicator-new=<char>
--output-indicator-old=<char>
--output-indicator-context=<char>
Specify the character wanted for -old.
https://git-scm.com/docs/git-log#_common_diff_options
I don't know if git can do this natively, but you can certainly achieve what you want by piping the output of git log into sed. For example to change the plus to '$' and the minus to '%' in your report you could use the following command:
git log -p --since="14 days ago" -- *Schema*.sql | sed 's/^+/$/g' | sed 's/^-/%/g'

Modifying elements in bash array

I have an array ${timearray[*]} that contains a number of times in this format
20:56 20:57 20:59 21:01 21:04
There is a second array ${productarray[*]} that contains different times
20:54 20:56 20:58 21:00 21:02
I need to get a difference between the two by subtracting time minus product. To do this I believe I need to convert these times into epoch time before subtracting, I'll then divide by 60 and round to the nearest minute. I attempted using a for loop like this to do the conversion.
arraylength=`expr ${#timearray[#]} -1`
for ((l=0; l<=$arraylength; l++))
do
epochtimearray=(`date --date="${timearray[$l]}" +%s`)
done
However the resulting epochtimearray only contains the epoch value of the last time
echo ${epochtimearray[*]}
1472331840
Does anyone see what I'm missing here or is there a better way to subtract time times.
To add an element to an array, use the += operator:
epochtimearray+=(`date --date="${timearray[$l]}" +%s`)
or set the element at the given index:
epochtimearray[l]=(`date --date="${timearray[$l]}" +%s`)
This diffs a bunch of times - I don't recommend it for large numbers of values but it's definitely better than running date in a loop
# populate couple arrays
declare -a timearray=(20:56 20:57 20:59 21:01 21:04)
declare -a productarray=(20:54 20:56 20:58 21:00 21:02)
# convert multiple times for today into epoch seconds
IFS=$'\n'
timeepochs=($(echo "${timearray[*]}"|date -f- +%s))
prodepochs=($(echo "${productarray[*]}"|date -f- +%s))
unset IFS
for ((i=0; i < ${#timeepochs[*]}; ++i)); do
echo "$i: ${timearray[i]} - ${productarray[i]} = $((timeepochs[i] - prodepochs[i])) seconds"
done
It is usually much easier to loop over array elements than to try to construct the indices:
for d in "${timearray[#]}"; do
epochtimearray+=($(date "$d" +%s))
done
But if you are using Gnu grep (which apparently you are), you can use the -f option to process all the times with a single call:
epochtimearray=($(date +%s -f-<<<"$(IFS=$'\n'; echo "${timearray[*]}")"))
But you don't actually need to construct the temporary arrays; you can put the whole thing together using a couple of standard Unix utilities:
paste -d- <(date +%s -f-<<<"$(IFS=$'\n'; echo "${timearray[*]}")") \
<(date +%s -f-<<<"$(IFS=$'\n'; echo "${productarray[*]}")") |
bc
That uses paste to combine the two lists into two vertical columns separated by a dash (-d-) and then feeds the resulting lines (which look a lot like subtractions :) ) into the calculator bc, which calculates the value of each line.

Awk and double quoting

I need some help with awk and double quoting.
I have this text file with separated by tab values (multiple lines).
Ex.
22-03-2016 11:25 25 Session reconnection succeeded user 10.10.10.10
Now I want to change the date notation.
I want above example to be
2016-03-22 11:25 (as in %Y-%m-%d %H:%M)
I am trying to use awk (on my mac)
I manually can change the date with:
date -j -u -f "%d-%m-%Y %H:%M" "22-03-2016 11:25" "+%Y-%m-%d %H:%M"
result: 2016-03-22 11:25
I am struggling with awk to do this. Having problems with the quoting.
Any other ways of doing this are appreciated!
Regards,
Ronald
Ok,
So I found a solution using sed.. (had to switch to a real linux enviroment.. but ok)
Using this command:
sed -i.bak -r 's/([0-9]{2})-([0-9]{2})-([0-9]{4})/\3-\2-\1/g' TextFile
Always funny how I find the answer right after I post a question....

Bash display files by date

I am just creating bash script to: Print a long listing of the files in the login directory for a specific month. The user is prompted to enter the first 3 letters of a month name, starting with a capital, and the program will display a long list of all files that were last modified in that month.
For example, if user entered “Jul”, all the files that were last modified in July will be listed.
Is it possible to sort files by date and then limit them? or can it be done differently?
Take a look at this answer: https://stackoverflow.com/a/5289636/851273
It covers both month and year, though you can remove the match against the year.
read mon
la -la | grep $mon
You can grep -i for case insensitive grep. So user inputs can become case insensitive.
Note: This is crude because it returns results that have the text matching the month name. Ex: it will return files that are name after month. TO refine this you will have to just look at the date column
Here is the script that should do it
Month=Dec
ls -ltr |awk '$6 ~ /'$Month'/ {print $9}'
This will have awk look at the date field from the ls field ($6), ls -ltr will sort it by date. This will then expand the variable $Month and use that to search the $6 field, and print out the file name (the 9th field $9).

How to print time in "dd mm yyyy hh mm sec" format in LINUX / C

I am generating an XML file using libxml2. In one of the tags i want to put the current time in "dd mm yyyy hh mm sec" format.
My dev environment is C/LINUX.
Thanks !
Use strftime. Check the manual page for details.

Resources