How to compare date in linux for specific format - linux

I want to compare date 08-08-2018 and 20-07-2018.
if (dt1 > $ dt2){
Success
}
Please help me.

The core
Time::Piece module
allows you to convert a date/time string to an object using the strptime class method. The resulting objects can be compared using the standard <, >, <=, >=, and == operators as you describe, and being a core module it is unlikely to need installing
Here's a short program that uses the values in your question, converts them to Time::Piece objects $dt1 and $dt2, and compares them as you describe
use strict;
use warnings 'all';
use feature 'say';
use Time::Piece;
my ($dt1, $dt2) = map { Time::Piece->strptime($_, '%d-%m-%Y') } qw/ 08-08-2018 20-07-2018 /;
if ( $dt1 > $dt2 ) {
say 'Success';
}
else {
say 'Failure';
}
output
Success

If your date formats are static, you have two easy options.
Option 1
Strip out the day, month, and year from each date.
Compare the two years.
If the years are the same, compare the two months.
If the months are the same, compare the two days.
Option 2
Strip out the day, month, and year from each date.
Create a new date string in the YYYY-MM-DD format
Do a standard string comparison on the two resulting strings.
I'm sorry, but I'm not in a position to provide you with sample code at this time.

Related

how to compare dates using relational Operators in dart?

I'm working on a problem which requires to compare the date with other if the selected date is less than given then print Hello.
The date is present In String Format like given in Examaple
Ex:
if('2020-1-13'<'2021-1-5')
{
print(hello);
}
You should try this it should work
````
var date1 = DateTime.parse('2020-01-13').millisecondsSinceEpoch.toInt();
var date2 = DateTime.parse('2021-01-15').millisecondsSinceEpoch.toInt();
if(date1 < date2){
print('true');
}else{
print('false');
}
````
Instead of doing that just use the date's isBefore or isAfter operator
It would look something like the following:
final now = DateTime.now();
final yesterday = DateTime.now().subtract(Duration(days: 1));
print(now.isAfter(yesterday)); //true
Since your dates are in STring format, it you would have to use the DateTime.parse() method to construct your date objects, and then use the operators talked about above
package:basics provides DateTime extensions that can easily compare DateTime objects.
(Disclosure: I contributed those particular extensions.)

How do I write a locale-aware DateTime sensibly into Spreadsheet::WriteExcel?

I have got a DateTime object that is in a specific locale (with a DateTime::Locale object attached to it). I want to write a date string to an XLS file using Spreadsheet::WriteExcel, but I want the output that's visible to user of the Excel file to be of the same locale as the one attached to my DateTime object.
There is some documentation on this matter within Spreadsheet::WriteExcel. It's possible to set formats using a combination of a string, $wb->add_format() and $ws->write_date_time().
I can get locale information from DateTime via DateTime::Locale by looking at the CLDR patterns. There are also the named formats, which are easier to use. Something like $locale->date_format_short is actually quite nice.
use DateTime::Locale;
say DateTime::Locale->load('en_GB')->date_format_short; # dd/MM/y
say DateTime::Locale->load('en_US')->date_format_short; # M/d/yy
Now the problem with this is, that Excel does not know what a single y means. So my workaround has been to just replace a single y with yy, as that seems to roughly be the same.
Excel also doesn't like upper case letters in the format. I have no idea how it distinguishes between minutes and months, but it works.
This example seems to work, but I am sure it's not perfect.
use strict;
use warnings;
use Spreadsheet::WriteExcel;
use DateTime;
use DateTime::Locale;
my $workbook = Spreadsheet::WriteExcel->new("date_time.xls");
my $worksheet = $workbook->add_worksheet();
# Write the column headers
$worksheet->write('A1', 'Formatted date');
$worksheet->write('B1', 'Format');
$worksheet->write('C1', 'Locale');
my $row = 0;
for my $locale (qw/en_GB en_US de_DE ko_KR/) {
$row++;
my $format_string =
lc(DateTime::Locale->load($locale)->date_format_short)
=~ s{
(?<!y) # 2. not preceded by a y
y # 1. a single y
(?!y) # 3. not followed by a y
}{yy}xr; # 4. replaced with two y
my $format = $workbook->add_format(num_format => $format_string);
$worksheet->write_date_time($row, 0, DateTime->now->datetime, $format);
$worksheet->write($row, 1, $format_string);
$worksheet->write($row, 2, $locale);
}
This produces the following Excel file.
They all work, but the code is smelly. Is there something I've overlooked? Maybe someone has written a more correct converter for these format strings that I've not seen yet.
Please note that DateTime::Format::Excel is not helpful as it only works the other way around, turning Excel dates into DateTime objects.

NPOI: Achieve Currency format as if formatted by Excel

I have seen some questions (like this one) here asking about if a cell in Excel can be formatted by NPOI/POI as if formatted by Excel. As most of you, I have to deal with issues with Currency and DateTime. Here let me ask how the formatting can be achieved as if it has been formatted by Excel? (I will answer this question myself as to demonstrate how to do it.)
Setting: Windows 10, English, Region: Taiwan
Excel format: XLSX (version 2007 and later)
(Sorry about various edit of this question as I have pressed the 'Enter' button at unexpected time.)
If you format a cell as Currency, you have 4 choices:
The internal format of each style is as follow:
-NT$1,234.10
<numFmt formatCode=""NT$"#,##0.00" numFmtId="164"/>
[RED]NT$1,234.10
<numFmt formatCode=""NT$"#,##0.00;[Red]"NT$"#,##0.00" numFmtId="164"/>
-NT$1,234.10
<numFmt formatCode=""NT$"#,##0.00_);("NT$"#,##0.00)" numFmtId="7"/>
[RED]-NT$1,234.10
<numFmt formatCode=""NT$"#,##0.00_);[Red]("NT$"#,##0.00)" numFmtId="8"/>
Note: There is a pair of double quote (") comes before and after NT$.
(To get internal format of XLSX, just unzip it. The Style information is available in <unzip dir>\xl\Styles.xml Check out this answer if you need more information.)
(FYI: In formatCode, the '0' represent a digit. The '#' also represent a digit, but will not appear if the number is not large enough. So any number less than 1000 will not have the comma inside it. The '_' is a space holder. In format 3, '1.75' appears as 'NT$1.75 '. The last one is a space.)
(FYI: In numFmtId, for case 1 and case 2, number 164 is for user-defined. For case 3 and 4, number 7 and 8 are build-in style.)
For developers using POI/NPOI, you may find out if you format your currency column using Build In Format using 0x7 or 0x8, you can get only the third or fourth choice. You cannot get the first or second choice.
To get the first choice, you build upon style 0x7 "$#,##0.00);($#,##0.00)". You need to add the currency symbol and the pair of double quotes in front of it.
styleCurrency.DataFormat = workbook.CreateDataFormat().GetFormat("\"NT$\"#,##0.00");
Apply this format to a cell with number. Once you open the Excel result file, right click to check formatting, you will see the first choice.
Please feel free to comment on this post.
var cell5 = row.CreateCell(5, CellType.Numeric);
cell5.SetCellValue(item.OrderTotal);
var styleCurrency = workbook.CreateCellStyle();
styleCurrency.DataFormat= workbook.CreateDataFormat().GetFormat(string.Format("\"{0}\"#,##0.00", item.CurrencySymbol));//styleCurrency;
cell5.CellStyle = styleCurrency;
styleCurrency = null;
Iterate over loop for multiple currency.
Function to GetCurrencySymbol against currency Code on C#
private string GetCurencySymbol(string isOcurrencyCode)
{
return CultureInfo.GetCultures(CultureTypes.AllCultures).Where(c => !c.IsNeutralCulture)
.Select(culture =>
{
try
{
return new RegionInfo(culture.LCID);
}
catch
{
return null;
}
})
.Where(ri => ri != null && ri.ISOCurrencySymbol == isOcurrencyCode)
.Select(ri => ri.CurrencySymbol).FirstOrDefault();}

How to remove percent character from a string in Cognos?

I have a string field with mostly numeric values like 13.4, but some have 13.4%. I am trying to use the following expression to remove the % symbols and retain just the numeric values to convert the field to integer.
Here is what I have so far in the expression definition of Cognos 8 Report Studio:
IF(POSITION('%' IN [FIELD1]) = NULL) THEN
/*** this captures rows with valid data **/
([FIELD1])
ELSE
/** trying to remove the % sign from rows with data like this 13.4% **/
(SUBSTRING([FIELD1]), 1, POSITION('%' IN [FIELD1])))
Any hints/help is much appreciated.
An easy way to do this is to use the trim() function. The following will remove any trailing % characters:
TRIM(trailing '%',[FIELD1])
The approach you are using is feasable. However, the syntax you are using is not compatible with the version of the ReportStudio that I'm familiar with. Below you will find an updated expression which works for me.
IF ( POSITION( '%'; [FIELD1]) = 0) THEN
( [FIELD1] )
ELSE
( SUBSTRING( [FIELD1]; 1; POSITION( '%'; [FIELD1]) - 1 ) )
Since character positions in strings are 1-based in Cognos it's important to substract 1 from the position returned by POSITION(). Otherwise you would only cut off characters after the percent sign.
Another note: what you are doing here is data cleansing. It's usually more advantageous to push these chores down to a lower level of the data retrieval chain, e.g. the Data Warehouse or at least the Framework Manager model, so that at the reporting level you can use this field as numeric field directly.

In Clojure, what is a good way to compare user input Strings with Longs?

In Clojure I am building a card game. Cards have a suit and a score.
{:suit 1 :score 9}
The cards are created using ranges, e.g. (range suitTotal), so the class of the values of :suit and :score is Long.
Players send command strings, e.g. "discard1.9" is a discard request.
Using a regex to parse this:
(re-seq #"[0-9]+" command)
results in String items "1" and "9". A card created with these results would be
{:suit "1" :score "9"}
I would like this to compare as equal with the original card. At the moment I am using (Integer/parseInt) to convert the strings.
The suit value could be built from a different type, such as a keyword, but the score value is used as a number elsewhere.
use read-string
DEMO
user=> (read-string "1")
1
A good approach would be to parse the strings as numbers and then use = to compare.
user=> (Integer/parseInt "1")
1
The advantage of this over read-string is this is more restricted. This won't parse strings that look like clojure data-structures.

Resources