Change timestamp variable format or do replacement - datetime

I am currently working on setting up ci/cd pipeline for pushing nuget packages.
I want to use the built-in CI_COMMIT_TIMESTAMP for version suffix however its ISO 8601 format is not valid for this.
Example ISO 8601 (UTC): 2022-03-15T18:34:43Z
Will need to at least replace colon.
Is it possible to format it differently or do some sort of text replacement?

You can't change how the variable is presented, but you can reformat the value in your job.
The unix date command can do this. For example, you can declare any valid format as the desired output format.
MY_JOB:
variables:
DESIRED_FORMAT: "%Y-%m-%dT%H-%M-%S"
script:
- nuget_format="$(date -d "$CI_COMMIT_TIMESTAMP" +"$DESIRED_FORMAT")"
- echo "$nuget_format"
This will have an output like:
2022-03-15T23-43-17
An alternative may be to use sed to replace the occurrences of : with -.
script:
- nuget_time_format="$(sed "s/:/-/g" <<< $CI_COMMIT_TIMESTAMP)"
- echo "$nuget_time_format"

Related

Simplest way to create filename with date and time in command line or batch file

I need to append date/time to some test log files generated multiple times in a day. Some suggest doing it like:
set CUR_YYYY=%date:~10,4%
set CUR_MM=%date:~4,2%
set CUR_DD=%date:~7,2%
set CUR_NN=%time:~3,2%
set CUR_SS=%time:~6,2%
set CUR_MS=%time:~9,2%
set SUBFILENAME=%CUR_YYYY%%CUR_MM%%CUR_DD%-%CUR_HH%%CUR_NN%%CUR_SS%
And then concatenate them together but I feel it's just not right. I wonder if there is a better or more concise way of doing it? I imagine there would be a simple solution because the need is quite common.
Thanks!
Well, first of all, data/time formatting is actually a big thing in any programming languages. Just look at the long list of ToString() methods in C# DateTime class, for example, will give you an idea what it is like.
For your particular task, assuming that your locale date using "-" and your locale time using ":" as separators (you can echo date/time to verify:)
echo %date% %time%
If you are not picky, the simplest way to generate a file name is below, and you may replace the '-' and ':' characters according to your own locale, which will give you a valid file name in most systems.
echo "testResult_%date:-=%_%time::=%.xml"
"testResult_20220409_ 84841.28.xml"
If you want only alphanumeric characters in your naming, then probably a cleaner set of commands:
set shortTime=%time:~0,8% ##eliminate the milliseconds in time string
set shortTime=%shortTime: =0% ##replace empty space with zero in morning hours
echo testResult_%date:-=%_%shortTime::=%.xml ##replace '-' and ':' with nothing
That should give you something like
testResult_20220409_084841.xml
However, if you need to deal with application globalization, it's always best to format date/time to a predefined string format using something like PowerShell (on Windows) as the first step. It will then make any subsequent string manipulations easier.
powershell get-date -format "{dd-MM-yyyy_HH:mm:ss}"
When dealing with timestamps you can retrieve the date as I show here on this demo shell file (using bash):
#!/bin/bash
#gets the current timestamp
current_time=$(date "+%Y%m%d-%H%M%S")
echo "Current Time : $current_time"
#crafts the new filename appending $current_time to the original filename
original_filename="filename.log"
new_fileName=$original_filename.$current_time
echo "New FileName: " "$new_fileName"
#renames the file
#mv $original_filename $new_fileName

Moment JS Date format to remove minus sign from iso format

I am using moment.js in my application and the expected date format is
2017-01-09T17:05:00.000 //Expected Result
Where as if i call
moment().format()
I am getting ISO 8601 format i.e with T and minus sign
(2017-01-14T17:05:00-06:00) // Actual result.
What should i use to get this format of with .000
ISO 8601 is the default representation of moment's format() API. But you can customize it by passing required format pattern.
In your case, moment().format('YYYY-MM-DD[T]HH:mm:ss.SSS') will produce what you're looking
2017-01-10T11:55:56.621
Find more customization options here: http://momentjs.com/docs/#/displaying/
--
Another option to know, though it works only for UTC time : moment().toISOString()
2017-01-10T06:56:18.465Z
[credit: Rajesh]

Format hours, minutes and seconds with moment.js

I get this value from my backend service: 171054. It represents hh:mm:ss. But when I use the formatting options from the docs it gives me back 00:00:00.
Things I've tried:
moment('171054').format('hh-mm-ss')
moment('171054').format('HH-mm-ss')
moment('171054').format('HH-MM-SS')
You are confusing format option with parsing option. Since your input string is not in ISO 8601 format, you have to specify format when parsing.
Here a working example for your use case:
var mom = moment('171054', 'HHmmss');
console.log(mom.format());
console.log(mom.format('HH:mm:ss'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.15.1/moment.min.js"></script>
I am not sure if momentjs can read that as date since there is no identifier. I would suggest changing the value for example you have 171054, get each 2 digits since you sure that this is represent as hh:mm:ss then add identifier between then like ":" or "-" then try us momentjs formatting again.

Using SED or other unix command to fix a date format?

I have a large file with date formats such as 2014-3-16
Year-Month-Day with a basic regex pattern of [0-9]{4}-[0-9]{1,2}-[0-9]{1,2}
Some entries are in the incorrect format of Month/Day/Year Hour:Minute:Second AM(or PM)
basic regex pattern of [0-9]{1,2}/[0-9]{1,2}/[0-9]{4} [0-9]{1,2}:[0-9]{2} [A-Z]M
I tried to fix these dates using python and csv reader but csv reader introduced all sorts of fun errors like changing quotes in other fields and adding ^M at the end of every line. So I'm trying to learn how to use SED which I think will have the answer. Any help?
This should do what you want:
sed -i -e 's/\([0-9]\{1,2\}\)\/\([0-9]\{1,2\}\)\/\([0-9]\{2,4\}\)\([ :0-9]*[AP]\{1\}M\)/\3-\1-\2/g' file.txt
Basically there are three of these patterns:
\(\[0-9]\{1,2\}\)\/
Which means that it will find one or two [0-9] followed by a /
Then finally a
\([ :0-9]*[AP]\{1\}M\)
Which finds spaces, colons, and 0-9s that are after the first three patterns, but before the AM or PM. It lumps everything after the third pattern above through the M as one pattern.
The final part:
\3-\1-\2
Means that it substitutes everything between the 's/... and the first / that is not escaped, with the third pattern, first pattern, and second pattern mentioned. Leaving you with a Year, Month, Date formatting.

Where do I find the reference for TO_DATE format string

I need to convert a string formatted as MM/DD/YYYY HH:MI plus AM/PM, but can't find a complete reference to the format string to find how to specify the AM/PM part.
I would certainly appreciate information on how to do this, but would appreciate a link to a good source of documentation for this even more.
:EDIT
SELECT top 1
v.CalendarDateTime
,TO_TIMESTAMP(v.CalendarDateTime,'MM/DD/YYYY HH:MIAM') as CalendarDateTimeTS
--,CAST(TO_TIMESTAMP(v.CalendarDateTime,'MM/DD/YYYY HH:MIAM') AS TIMESTAMP(0) FORMAT 'MM/DD/YYYYBHH:MIBT') AS CalendarDateTimeTS2
12/03/2015 03:00AM 12/3/2015 03:00:00.000000
The commented out line produces a "DateTime field overflow" error.
You probably want TO_TIMESTAMP instead of TO_DATE.
The only bad thing about the Oracle function is the resulting datatype of TIMESTAMP(6) which can't be changed:
TO_TIMESTAMP('12/03/2015 03:00AM', 'MM/DD/YYYY HH:MIAM')
Using Teradata's FORMAT you can specify the timestamp precision, but it's less flexible than Oracle's, the string must match the format exactly:
CAST('12/03/2015 03:00AM' AS TIMESTAMP(0) FORMAT 'MM/DD/YYYYbHH:MIT')
On the Teradata site you'll find the (slow) online docu, e.g. TO_DATE formats or Teradata FORMATs. Of course you should download the full documentation CD for your release.
Please tell us at least which programming language are you using.
Normally it would be something like "MM/DD/YYYY HH:MI a" but we need to know first you language.

Resources