I have a shell script that I want to get the date and time 30 minutes ago in GMT.
I have this working great for full hours, but partial hours don't seem to work:
1 hour ago
TZ=GMT+1 date +%Y-%m-%d" "%H:%M:%S
2010-01-08 17:43:57
2 hours ago
TZ=GMT+2 date +%Y-%m-%d" "%H:%M:%S
2010-01-08 16:44:07
1/2 hour ago
TZ=GMT+.5 date +%Y-%m-%d" "%H:%M:%S
2010-01-08 18:44:38
tried lots of combinations of 0.5 1.5, no partial hours seem to work, which is weird because there are some timezones that are not full offset of an hour.
any suggestions?
cant use perl or ruby needs to be regular shell or mysql call.
date -u --date="-30 minutes"
You can also do this:
TZ='UTC+0:30' date
/usr/bin/env TZ='GMT' date -d '-30 minutes'
This is with the version of the date command that's part of the GNU coreutils. I don't know if it works for other versions of the date program.
Related
I am having trouble with the momentjs library
the line
moment("Mon Oct 14 01:00:00 GMT 2013") parses correctly
but the line
moment("Mon Oct 14 01:00:00 BST 2013") throws an invalid date
I have tried building a format string but the zz format which is what I think I need is deprecated, is there a way to make it skip the BST/GMT bit completely as I am only interested in the date
Thanks in advance.
Time zone abbreviations aren't unique, so they cannot be parsed. You can ignore it by putting any non-format character as a placeholder:
moment("Mon Oct 14 01:00:00 BST 2013","ddd MMM DD HH:mm:ss ? YYYY")
But you should be aware that by ignoring it, you'll be assuming the local time zone of the computer where the code is running. Set your computer for another time zone and call .format() on this and you'll see what I mean.
Perhaps you don't care about time zones and just want to reformat this to something else. That's fine, but what if you provide a date that's invalid because of a daylight saving time transition in the computer's local time zone? Your browser will either skip backward or forward depending on which browser your running. To avoid that, you should work in UTC instead of in the local time. Even though your input value is from some other time zone entirely, working in UTC will ensure it doesn't get mangled.
moment.utc("Mon Oct 14 01:00:00 BST 2013","ddd MMM DD HH:mm:ss ? YYYY")
Just now ("Sun Jun 9 17:19:24 MDT 2013"), I ran this command:
> date -d "first Sunday next month"
Tue Jul 9 00:00:00 MDT 2013
which is neither a Sunday, and isn't even the first Tuesday of next month:
How is date -d interpreting my input?
Is there a verbose option to date (or even "date -d") that shows
how my input is interpreted? It's not -v, and "man date" doesn't
show a verbose option.
I realize there's probably some library handling "date -d". Where
can I find that library and its documentation, limitations, etc?
I realize no program can handle every possible format, but it's
unnerving to see "date -d" give the wrong answer. I'd have much
preferred "date: can not parse 'first Sunday next month'" to the
wrong answer.
EDIT: running this on fedora 11 core:
> date --version
date (GNU coreutils) 7.2
Copyright (C) 2009 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
GNU date options :
-d, --date=string display time described by string, not now. It is a human readable format such as "next Thursday" or "1 month ago". A date string may contain items indicating calendar date, time of day, time zone, day of week, relative time, relative date, and numbers. This is also known as relative GNU date formats.[1] Here are a few examples of relative date:
date --date="1 days ago"
date --date="yesterday"
date --date='10 month ago'
date --date='2 hour ago'
date --date='Second Friday'
i bluntly copied this from the wiki. : http://en.wikipedia.org/wiki/Date_(Unix)
this date -d is stupid -- gave me 16th July as the next first sunday next month
Kaizen ~ $ date -d "first Sunday next month"
+ date -d 'first Sunday next month'
Tue Jul 16 00:00:00 IST 2013
it seems that the string has keywords that are anded and then results given.... Sunday + next month... after todays date ie 10th (in June) in the next month (July) is 16th of July.
if i did use Monday here instead of Sunday , i get 17th July as answer.
Kaizen ~ $ date -d "next month monday"
+ date -d 'next month monday'
Wed Jul 10 00:00:00 IST 2013
like wise if i use "next month monday" then it will go to the next month and look for monday from todays date ie 10th is the result.
does this help ?
Also it's super not portable and dosen't work alike on all servers ... dont know about you but I would stay away from it for programing purposes.
also there was a super user question of similar nature : http://superuser.com/questions/572088/unix-date-command-not-working-for-few-servers you can check that out too
How is date -d interpreting my input?
Via this Bison grammar that's part of Gnulib, on which date (and indeed much of the coreutils package) depends. My days of writing grammars are long gone, but the relative date/time parsing "fun" seems to start around line 892.
Is there a verbose option to date (or even "date -d") that shows how my input is interpreted?
The current version of GNU date as I write this (v8.28) does have a debug mode:
$ date --debug -d "first Sunday next month"
date: parsed day part: next/first Sun (day ordinal=1 number=0)
date: parsed relative part: +1 month(s)
date: input timezone: system default
date: warning: using midnight as starting time: 00:00:00
date: new start date: 'next/first Sun' is '(Y-M-D) 2019-02-03 00:00:00'
date: starting date/time: '(Y-M-D) 2019-02-03 00:00:00'
date: warning: when adding relative months/years, it is recommended to specify the 15th of the months
date: after date adjustment (+0 years, +1 months, +0 days),
date: new date/time = '(Y-M-D) 2019-03-03 00:00:00'
date: '(Y-M-D) 2019-03-03 00:00:00' = 1551542400 epoch-seconds
date: timezone: system default
date: final: 1551542400.000000000 (epoch-seconds)
date: final: (Y-M-D) 2019-03-02 16:00:00 (UTC)
date: final: (Y-M-D) 2019-03-03 00:00:00 (UTC+08)
Sun Mar 3 00:00:00 +08 2019
As you can see, it went left-to-right;
* "first Sunday" resolves forward to the first Sunday of Feb 2019 (2019-Feb-03), then
* "next month" adds one month (2019-Mar-03), which just happens to be a Sunday too, since it's not a leap year.
I realize there's probably some library handling "date -d". Where can I find that library and its documentation, limitations, etc?
Gnulib, as mentioned above. Also, the Date input formats section of the coreutils manual is worth reading in detail.
I realize no program can handle every possible format, but it's unnerving to see "date -d" give the wrong answer. I'd have much preferred "date: can not parse 'first Sunday next month'" to the wrong answer.
I'm sure the authors of the parse_datetime routine would agree. Sadly, your date phrasing seems like it just happens to match some grouping of the existing grammar rules, so date dutifully does the necessary calculation...and comes up with the wrong answer.
Perhaps a polite bug report to the Gnulib bugs mailing list (bug-gnulib#gnu.org) might set some gears turning.
Oh, and you probably already figured it out, but date -d "first Sunday" gets you the right result...as long as you're past the current month's first Sunday. :)
We are trying to convert unix timestamp to human readable time when running mysql commands.
For the unix date we have this working command
select FROM_UNIXTIME(registered) AS "ResolutionDateLine" from tickets
which gives us an readable date like
2012-12-03 09:41:00
But we do also have unix timestamp "seconds" that we need to convert, using the same line as above we get 1970-01-01 01:00:00 but the actual value should be 89 days, 23 hours, 22 minutes and 34 seconds.
Then we tried
select FROM_UNIXTIME(firstresponsetime, "%dd, %Hh, %Im") AS "Response" from tickets
with this result:
01d, 00h, 12m
Does anyone know how to convert this correctly in the mysql command?
Use SEC_TO_TIME (http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_sec-to-time) to convert a duration in seconds to a HH:mm:ss notation.
select sec_to_time(3500);
results in
00:58:20
Your will be like
select FROM_UNIXTIME(firstresponsetime, '%d-%m-%Y %H:%i:%s') AS Response from tickets
or you can customize it by change second parameter.
for more please check below link:-
http://dev.mysql.com/doc/refman/5.7/en/date-and-time-functions.html#function_from-unixtime
The below command is used for getting the yerterdays date in Unix Ksh on HP UX
DATE_STAMP=`TZ=CST+24 date +%m/%d/%Y`
Can somebody let me know what does "CST + 24 date " in above command do?
That command sets the timezone to CST+24 and returns the date in that timezone.
if you are looking for a command to find out yesterday's date, you are better of using the TZ trick esp. if you are in a timezone that observes DST.
use perl one liner instead.
#this takes local time and substracts a day(24*60*60 seconds) and formats the time.
echo `perl -e 'use POSIX; print strftime "%m/%d/%Y%", localtime time-86400;'`
Just a guess on your command - since its yesterday at CST+24 timezone the command returns yesterday's date and if you use CST-24, it retunrs tomorrow's date since the date translates to tomorrows date at CST-24 timezone.
VARIABLE=VALUE COMMAND means that you set the environment variable VARIABLE to VALUE but not persistent but only for the executed command COMMAND.
In your example that means: Execute the date command with the environment variable TZ set to CST+24 (which is Central Standard Time plus 24 hours).
Check out this page http://www.kodkast.com/blogs/unix-shell-scripting/how-to-get-yesterdays-date where you can find out yesterday's date as well as any other previous date in unix shell scripting.
How to get the current date value in epoch i.e., number of days elapsed since 1970-1-1. I need solution in unix shell script.
The Unix Date command will display in epoch time
the command is
date +"%s"
https://linux.die.net/man/1/date
Edit: Some people have observed you asked for days, so it's the result of that command divided by 86,400
Update: The answer previously posted here linked to a custom script that is no longer available, solely because the OP indicated that date +'%s' didn't work for him. Please see UberAlex' answer and cadrian's answer for proper solutions. In short:
For the number of seconds since the Unix epoch use date(1) as follows:
date +'%s'
For the number of days since the Unix epoch divide the result by the number of seconds in a day (mind the double parentheses!):
echo $(($(date +%s) / 60 / 60 / 24))
echo $(($(date +%s) / 60 / 60 / 24))
echo `date +%s`/86400 | bc
Depending on the language you're using it's going to be something simple like
CInt(CDate("1970-1-1") - CDate(Today()))
Ironically enough, yesterday was day 40,000 if you use 1/1/1900 as "day zero" like many computer systems use.