unix date command with nanosecond precision. Input parameter - date

I'm having difficulty parsing an inputted date with nanosecond precision:
date: invalid date `15-OCT-18 12:40:01:000203570 AM'
Yet when I drop the nanoseconds, it works fine:
$ date -d "15-OCT-18 12:40:01 AM" +"%d-%m-%Y %H:%M:%S %p"
15-10-2018 00:40:01 AM
Looking the docs its suggests uppercase N is to be used for ns
Even when I drop the Ns it generates the ns for me
$ date -d "15-OCT-18 12:40:01 AM" +"%d-%m-%Y %H:%M:%S:%N %p"
15-10-2018 00:40:01:000000000 AM

Should be in the format
date -d "15-OCT-18 12:40:01.000203570 AM" +"%d-%m-%Y %H:%M:%S:%N %p"
Data needs to have . instead of : spearating seconds and ns

You need to replace the : before the Nanoseconds identifier with .(dot)
> date -d"$(echo "15-OCT-18 12:40:01:000203570 AM" | sed 's/:/./3')" +"%d-%m-%Y %H:%M:%S.%N %p"
15-10-2018 00:40:01.000203570 AM
>

Related

Powershell string to unix time with correct timezone

I'm collecting a timestamp value and trying to transform it to a Unix format.
To do that, I'm using ParseExact method, like so:
$FILETIME = "20220709101112"
$EPOCHTIME = [datetime]::ParseExact($FILETIME,"yyyyMMddHHmmss",$null) | Get-Date -UFormat "%s"
echo $EPOCHTIME
1657361472
Get-Date transforms the timestamp to Unix format correctly, but there's an issue.
The returned value uses the local timezone (UTC-3), not UTC-0.
Therefore, in another system that value might be displayed with the wrong timezone.
I've tried to add 3 hours, but it appended the number instead.
$EPOCHTIME = $EPOCHTIME + 10800
echo $EPOCHTIME
165736147210800
How can I convert that timestamp correctly?
Ok, so here's one way to do it (borrowing from https://stackoverflow.com/a/246529/3156906).
The key is to find the TimeZoneInfo for the timezone the $FILETIME string is local to, and use that to convert the local time to UTC before converting to a Unix epoch timestamp.
# datetime string that is local to UTC-3,
# (equivalent to "2022-07-09 13:11:12 UTC")
$FILETIME = "20220709101112";
# because there's no timezone component in the custom
# format string (e.g. "z" or "zz") this gets converted
# to a datetime with "Kind = DateTimeKind.Unspecified"
# (see https://learn.microsoft.com/en-us/dotnet/api/system.datetime.parseexact?view=net-6.0#system-datetime-parseexact(system-string-system-string-system-iformatprovider))
$TIMESTAMP = [datetime]::ParseExact($FILETIME, "yyyyMMddHHmmss", $null);
# DateTime : 09 July 2022 10:11:12
# Kind : Unspecified
# get a reference to the timezone the original date
# string is stored local to. I guessed this by looking
# at the results of "[TimeZoneInfo]::GetSystemTimeZones()"
# and taking a timezone with -3:00 from UTC and no daylight savings
# but maybe there's a better match for your source data
$tz = [TimeZoneInfo]::FindSystemTimeZoneById("SA Eastern Standard Time");
# Id : SA Eastern Standard Time
# DisplayName : (UTC-03:00) Cayenne, Fortaleza
# StandardName : SA Eastern Standard Time
# DaylightName : SA Eastern Summer Time
# BaseUtcOffset : -03:00:00
# SupportsDaylightSavingTime : False
# this is the magic bit - treat $TIMESTAMP as a local time in
# timezone $tz, and convert it to UTC using the BaseUtcOffset
# and daylight saving rules for $tz
$UTCTIME = [TimeZoneInfo]::ConvertTimeToUtc($TIMESTAMP, $tz);
# DateTime : 09 July 2022 13:11:12
# Kind : Utc
# now convert it to a unix epoch timestamp
$EPOCHTIME = $UTCTIME | Get-Date -UFormat "%s";
# 1657372272
Bonus Round
You get the Unix epoch timestamp 1657361472 because the current timezone on the computer where you're running your script is UTC, which is 3 hours offset from the timezone the string is local to.
Notes on DateTime.ParseExact Method
If s does not represent a time in a particular time zone and the parse
operation succeeds, the Kind property of the returned DateTime value is
DateTimeKind.Unspecified. If s does represent the time in a particular time
zone and format allows time zone information to be present (for example, if
format is equal to the "o", "r", or "u" standard format specifiers, or if it
contains the "z", "zz", or "zzz" custom format specifiers), the Kind
property of the returned DateTime value is DateTimeKind.Local.
This question has been answered in this post:
Get formatted universal date / time
Essentially, it depends on the version of PowerShell that you're using. If it's Powershell 7.1+, then you can do:
Get-Date -AsUTC -UFormat "%s"
Otherwise, if it's a lower version, you need to use
Get-Date ([datetime]::UtcNow) -UFormat "%s"

PowerShell time conversion to UTC from a GMT+1 string

i use PowerShell for scripting some stuff.
Actual i got a string from a logfile - this one i prepared a timestamp like this example:
$timestamp = $timestampDate + " " + $timestampTime
#timestamp: 2020.11.16 06:03:27
This timestamp is a GMT+1 timestamp from my timezone but i need it in UTC (wintertime).
So i try:
get-date('2020.11.16 06:03:27') -f "yyyy.MM.dd hh:mm:ss z"
2020.11.16 06:03:27 +1
Get-Date('2020.11.16 06:03:27') -Format FileDateTimeUniversal
20201116T0503270000Z
Now i try how i could format the result to 2020.11.16 05:03:27 in a easy way without string manipulation (my DB field is without timezone)
Thanks a lot.

Current timestamp in ISO 8601 UTC time (ie: 2013-11-03T00:45:54+02:00)

Can someone please help me how to format timestamp in ISO 8601 UTC time?
I want the date to be formatted like this 2020-10-03T00:45:54+02:00
I have tried this in App Script.
Utilities.formatDate(new Date(), "UTC", "yyyy-MM-dd'T'HH:mm:ssZZZ");
It is getting this output 2020-10-03T08:50:18+0000
I want the timezone to be formatted like this TwoDigitHours : Minutes
Use Date.toISOString() for UTC ISO8601 string:
/*<ignore>*/console.config({maximize:true,timeStamps:false,autoScroll:false});/*</ignore>*/
console.log(new Date().toISOString())
<!-- https://meta.stackoverflow.com/a/375985/ --> <script src="https://gh-canon.github.io/stack-snippet-console/console.min.js"></script>
Utilities.formatDate accepts 3 arguments:
Date object
Timezone
SimpleDateFormat string
For, ISO8601 timestring use X instead of Z
For a specific timezone, use that timezone(GMT+2) as second argument instead of UTC
Utilities.formatDate(new Date(),"GMT+2" , "yyyy-MM-dd'T'HH:mm:ssXXX");
Alternatively, Use Session.getScriptTimeZone() or Spreadsheet.getSpreadsheetTimeZone() instead for the second argument:
Utilities.formatDate(new Date(),Session.getScriptTimeZone(), "yyyy-MM-dd'T'HH:mm:ssXXX");
//Or
Utilities.formatDate(new Date(),Spreadsheet.getSpreadsheetTimeZone(), "yyyy-MM-dd'T'HH:mm:ssXXX");

Firefox Bookmarks add_date

One question to firefox exported bookmarks.
How to read this date: ADD_DATE="1375088003"? I think it is date when user add new website to bookmarks but how to read this all string? Where is year, day and month?
It's the Epoch time, ie the number of seconds since Jan 1 1970. To convert it to a human-friendly date, you can pass it through the 'date' unix command:
date -r 1375088003
will return
Mon 29 Jul 2013 18:53:23 AEST
(on my trusty Mac here in eastern Australia, hence the AEST)
Here's what I do. I export bookmarks from my browser. Assume the result file is called "bookmarks.html". I have a one-line executable script that takes a file as input, and creates another file with many long fields stripped. The one-liner I call "icon.sed" contains this:
sed 's/ICON=\"[^\"]*\"//' | sed 's/ICON_URI=\"[^\"]*\"//' | \
sed 's/LAST_CHARSET=\"[^\"]*\"//' | sed 's/HREF=\"[^\"]*\"//'| \
tr -s ' '
I use it like this:
~/icon.sed <bookmarks.html >newbook.txt
I have another script that takes a mm/dd/ccyy parameter, and returns the Epoch time for that date. That executable script is called "epoch.sh":
#!/bin/sh
if [ -z "$1" ]; then echo "You must supply a date"
else date -j -f "%m/%d/%Y" "$1" "+%s"; fi
exit 0
Choose a date you're looking for, and send it through "epoch.sh", such as: ~/epoch.sh 10/30/2015 which outputs this: 1446246082 .
You can then grep your newbook.txt file looking for either ADD_DATE or LAST_MODIFIED date which begins with the first four digits: 1446
grep 'LAST_MODIFIED="1446' newbook.txt
Also, use epoch.sh to get a pair of epoch times for start and stop dates so you can bracket your results by doing multiple greps over the range, using just the first four digits of each epoch time.
To convert a Firefox timestamp to a date with BASIC:
If the timestamp is from an sqlite dump of places.sqlite, you need to truncate it first:
stg0 = Left("1670163289502000", 10) results in "1670163289"
Convert the timestamp:
Dim var0 As double, var1 As Date
var0 = stg0
or
var0 = "1670163289" 'BASIC will do the conversion for you
var1 = (var0 / 86400) + 25569
where 86400 is the # of seconds in a day and 25569 is the BASIC daynumber for 1/1/1970

Validating date in Unix

I have a scenario as below:
I will be getting two dates viz. start_dt and end_dt in yyyyMMdd format as arguments to my Unix Script.
I have to loop through each of the dates starting from start_dt till end_dt incrementing by a day each time.
The script I have written is as below:
start_date=$1
end_date=$2
#verify dates
if ! date -d "$start_date" 2>&1 > /dev/null ;
then echo "start date is invalid" ; exit 1
fi
if ! date -d "$end_date" 2>&1 > /dev/null ;
then echo "end date is invalid" ; exit 1
fi
#set current and end date
curr_dt=$(date -d "$start_date")
end_dt=$(date -d "$end_date +1 hours")
#loop over all dates
while [ "$end_dt" != "$curr_dt" ]
do
echo $curr_dt
# increment the date
curr_dt=$(date -d "$curr_dt +1 hours")
done
However, I am getting below error when I am running with input arguments as 20140128 and 20140130:
date: invalid date `20140130 +1 hours'
Tue Jan 28 00:00:00 EST 2014
date: invalid date `Tue Jan 28 00:00:00 EST 2014 +1 hours'