Lua - get time and date of specific time zone - date

I want the time and date of a specific timezone in Lua, formatted in way, os.date("%a %b %d, %H:%M") would return it.
I know that os.date("!%a %b %d, %H:%M") (added exclamation mark "!") gives me UTC time but how do I move from there and offset the requested time?
In my case the desired timezone is UTC+08:00.

os.date accepts two parameters:
os.date ( [format [, time]] )
The time parameter - in seconds - can be used to offset the returned value.
Since os.time() returns the current time in seconds, you can simply add your offset (8), multiplied by seconds in a minute (60), multiplied by minutes in a second (60).
os.date( "!%a %b %d, %H:%M", os.time() + 8 * 60 * 60 )
If you're in UTC+01:00, these are the kinds of output you would receive:
> os.date( "%a %b %d, %H:%M")
Wed Mar 16, 09:33
> os.date( "!%a %b %d, %H:%M")
Wed Mar 16, 08:33
> os.date( "!%a %b %d, %H:%M", os.time() + 8 * 60 * 60 )
Wed Mar 16, 16:33
If your offset is not by full hours, you have to use a decimal number of course.
For example: UTC+07:30 would be 7.5 in the equation.

Related

How can I set Date format when use power shell to generate date and save to batch variable?

I use this command in batch for generate curentdate -1 and save it to variable in batch but How can I put format in ToString() I already use ' and "
for /f "tokens=*" %a in ('powershell "$date = Get-Date; $date=$date.AddDays(-1);$date.ToString();"') do set var=%a
You can use :
for /f "tokens=*" %a in ('powershell "$date = Get-Date; $date=$date.AddDays(-1);$date.ToString('yyyy:MM:dd');"') do set var=%a
Here are the specificators :
Spécificator Type Example Output Example
dd day dd 10
ddd Name of the day ddd Jeu.
dddd Complet name of the day dddd Jeudi
f, ff, … Fractions of seconds fff 932
gg, … position gg ap. J.-C.
hh Hour two digits hh 10
HH Hour two digits (24 hours) HH 22
mm Minuts 00-59 mm 38
MM Month 01-12 MM 12
MMM Month shortcut MMM Sep.
MMMM complet name of the month MMMM Septembre
ss Seconds 00-59 ss 46
tt AM or PM tt ““
yy Years, 2 digits yy 02
yyyy Years yyyy 2002
zz Time zone, 2 digits zz +02
zzz Complete Time zone zzz +02:00
: Separator hh:mm:ss 10:43:20
/ Separator dd/MM/yyyy 10/12/2002

AppleScript countdown

I want to have a string that counts down from current time to alarm time.
I've manage to figer out how to get the current time and how to set the alarm time.
The problem I'm having is that when I take current time - alarm time it gives me a numer witch I then need to format back to a hh:mm:ss string.
i've got this.
set alarmHour to 23
set alarmMinute to 00
set theDate to the current date
set the hours of theDate to alarmHour
set the minutes of theDate to alarmMinute
set the seconds of theDate to 0
theDate
set countdown to theDate - (current date)
set ss to countdown / 60
at this point it gives me 22.283333333333 witch i now need to convert to hh:mm:ss and then put them into a sting that give me 00:22:00
UPDATE:
in swift you have % you can use
countDownTime = (formatterInteger - timeControlInteger)
let interval = Int(countDownTime)
let seconds = interval % 60
let minutes = (interval / 60) % 60
let hours = (interval / 3600)
but how to you do this in applescript?
Answer to second question:
is there a way to format strings like in swift? like
String(format:"%02d",absHour) – Mathias Halén
Yes, but you need to use the Satimage.osax scripting addition, available for free at:
Satimage AppleScript Additions
Satimage strftime() -- Date/Time Format Function
strftime v : format a date using a specification string like in the C
function strftime.
strftime date or list of date
into string : the formatting string. To obtain ISO 8601 dates, use
"%FT%TZ" or "%GW%V-%uT%TZ" (using the 'with GMT' parameter)
[GMT boolean] : if true, output date as GMT. Default: false, the ouput
date is local.
→ string : the formatted date
EXAMPLE: strftime (current date) into “%x” RETURNS: 07/22/14
"%a, %b %d, %Y" RETURNS: Tue, Jul 22, 2014
set d to current date
-- some ISO 8601 formats:
strftime d into "%FT%T%z"
-- "2007-01-15T16:10:56+0100"
strftime d into "%GW%V-%uT%T%z"
-- "2007W03-1T16:10:56+0100"
--if you need to store the date d as UTC:
strftime d into "%FT%TZ" with GMT
-- "2007-01-15T15:10:56Z"
strftime d into "%a, %b %d, %Y %H:%M:%S %z"
-- "Mon, Jan 15, 2007 16:10:56 +0100"

Matlab: Selecting rows related of specfic time

I have data from a sensor, data were recording for five days non stop. How to select specific rows, related with the certain time, e.g. 11-09-2013 11:20:00 - 11:21:49 PM ? Then, how to divide the obtained data into equal segments 2.56 seconds each?
My data looks like this:
0.135478690266609
0.0537606552243232
-0.0262537784874439
0.157014295458793
0.149360358715057
0.104898564517498
0.0393946692347526
...
and I can read time by using datestr(data(1), 'dd-mm-yyyy HH:MM:SS AM')
If I understand correctly, data is a vector of timestamps in days.
To find the first row that comes after 11-09-2013 11:20:00 PM, you can do:
target = datenum('11-09-2013 11:20:00 PM', 'dd-mm-yyyy HH:MM:SS AM') / 86400;
row = find(data >= target, 1, 'first');
To find all rows between 11-09-2013 11:20:00 PM and 11-09-2013 11:21:49 PM:
start = datenum('11-09-2013 11:20:00 PM', 'dd-mm-yyyy HH:MM:SS AM') / 86400;
stop = datenum('11-09-2013 11:21:49 PM', 'dd-mm-yyyy HH:MM:SS AM') / 86400;
rows = find(data >= start & data <= stop);
To get a segment of 2.56 seconds after the date start, set stop to start + 2.56 / 86400 and use the same trick as above.

In GNU Octave, get the date and time

I want to get the date and time in GNU Octave and print it to screen. What is the best way to do this?
I've checked the Octave documentation, but I don't see a function to print out YYYY-MM-DD HH:MM:SS with a one liner simple command.
http://www.gnu.org/software/octave/doc/interpreter/Timing-Utilities.html
Get the date and time in Octave:
Number of seconds since epoch as integer:
time()
ans = 1.3482e+09
The date(), now() and datestr(...) builtin methods:
date()
ans = 20-Sep-2012
datestr(now(), 'yyyy-mm-dd');
ans = 2022-07-04
str2num(datestr(now(), 'yyyymmddHHMMSS'))
ans = 20220704165727
Add/Subtract days/months interval from now:
datestr(addtodate(now(), -20, 'days'), 'yyyy-mm-dd')
ans = 2022-06-14
datestr(addtodate(now(), -20, 'month'), 'yyyy-mm-dd')
ans = 2020-11-04
Format date/time as string with strftime
strftime ("%r (%Z) %A %e %B %Y", localtime (time ()))
ans = 09:52:42 PM (EDT) Thursday 20 September 2012
Get yyyy-mm-dd hh:mm:ss format
strftime ("%Y-%m-%d %H:%M:%S", localtime (time ()))
ans = 2012-09-20 21:56:07
Sources:
https://octave.sourceforge.io/octave/function/datestr.html
https://octave.sourceforge.io/octave/function/strftime.html

Formatting PowerShell Get-Date inside string

I can't get my head around how formatting a datetime variable inside a string works in PowerShell.
$startTime = Get-Date
Write-Host "The script was started $startTime"
# ...Do stuff...
$endTime = Get-Date
Write-Host "Done at $endTime. Time for the full run was: $( New-TimeSpan $startTime $endTime)."
gives me the US date format while I want ISO 8601.
I could use
$(Get-Date -Format u)
but I want to use $endTime to make the calculation of the timespan correct.
I have tried all permutations of $, (, ), endTime, -format, u, .ToString(...) and .ToShortDate(), but the one that works.
"This is my string with date in specified format $($theDate.ToString('u'))"
or
"This is my string with date in specified format $(Get-Date -format 'u')"
The sub-expression ($(...)) can include arbitrary expressions calls.
Microsoft Documents both standard and custom DateTime format strings.
You can use the -f operator
$a = "{0:D}" -f (get-date)
$a = "{0:dddd}" -f (get-date)
Spécificator Type Example (with [datetime]::now)
d Short date 26/09/2002
D Long date jeudi 26 septembre 2002
t Short Hour 16:49
T Long Hour 16:49:31
f Date and hour jeudi 26 septembre 2002 16:50
F Long Date and hour jeudi 26 septembre 2002 16:50:51
g Default Date 26/09/2002 16:52
G Long default Date and hour 26/09/2009 16:52:12
M Month Symbol 26 septembre
r Date string RFC1123 Sat, 26 Sep 2009 16:54:50 GMT
s Sortable string date 2009-09-26T16:55:58
u Sortable string date universal local hour 2009-09-26 16:56:49Z
U Sortable string date universal GMT hour samedi 26 septembre 2009 14:57:22 (oups)
Y Year symbol septembre 2002
Spécificator Type Example Output Example
dd Jour {0:dd} 10
ddd Name of the day {0:ddd} Jeu.
dddd Complet name of the day {0:dddd} Jeudi
f, ff, … Fractions of seconds {0:fff} 932
gg, … position {0:gg} ap. J.-C.
hh Hour two digits {0:hh} 10
HH Hour two digits (24 hours) {0:HH} 22
mm Minuts 00-59 {0:mm} 38
MM Month 01-12 {0:MM} 12
MMM Month shortcut {0:MMM} Sep.
MMMM complet name of the month {0:MMMM} Septembre
ss Seconds 00-59 {0:ss} 46
tt AM or PM {0:tt} ““
yy Years, 2 digits {0:yy} 02
yyyy Years {0:yyyy} 2002
zz Time zone, 2 digits {0:zz} +02
zzz Complete Time zone {0:zzz} +02:00
: Separator {0:hh:mm:ss} 10:43:20
/ Separator {0:dd/MM/yyyy} 10/12/2002
Instead of using string interpolation you could simply format the DateTime using the ToString("u") method and concatenate that with the rest of the string:
$startTime = Get-Date
Write-Host "The script was started " + $startTime.ToString("u")