How can I parse the start time output of the ps command? It can parse if it's in the form, like 06:38PM, but when it looks like Tue01PM it somehow can't. I also tried to add cultureinfo, like Convert.ToDateTime(result[8], CultureInfo.GetCultureInfo("en-US")), and with ParseExact like DateTime.ParseExact(result[8], "dddhhhh", CultureInfo.GetCultureInfo("en-US")).
The ps command can be asked to present specific data, and in some cases in specific formats, which can make it easier to parse the results. You request specific data with the -o command line option, whose argument is a comma-separated list of items in the form: item=HEADER. HEADER is the column header to use; leaving it out means that no header will be printed for that column (but if you also leave out the =, you'll get a default header).
There are at least two common items for start time: start and lstart. These give you the standard and long versions of the time, respectively:
$ ps -ostart= 2875
11:28:13
$ ps -olstart= 2875
Wed Mar 18 11:28:13 2015
On Linux, ps gets its information from the "file" /proc/pid/stat, which is a single line containing space-separated fields (see man 5 proc). The start time is the 22nd item in the list; it is an integer, which represents the number of clock ticks since the system was booted. In bash, you can convert this to something more usable with a bit of work:
start_time() {
local now running ticks uptime
now=$(date +%s) # time in seconds since epoch
running=$(cut -d' ' -f22 /proc/$1/stat)
# start time of process in ticks since boot
ticks=$(getconf CLK_TCK) # ticks per second (usually 100)
uptime=($(</proc/uptime)) # seconds since boot and idle time
bc <<<"$now-${uptime[0]}+$running/$ticks"
}
$ date +"%Y-%m-%d %H:%M:%S" -d#$(start_time 2875)
2015-03-18 11:28:12
In linux, you can override the system date format and give it whatever you'd like. Then you can be sure of the output. That is what I would do.
Gives the starting time in epoc of pid / process $MYPID
MYPID=$PPID
STARTING_EPOC_OF_MYPID=$(date +%s -d "$(ps -olstart= ${MYPID})")
echo $STARTING_EPOC_OF_MYPID
I'm not quite sure, if this differs from above.
Related
The documentation states that "When a build is completed, if nothing else in the build number has changed, the Rev integer value is incremented by one".
However, you can see below that even though something did change, it still incremented.
Does anyone know what the actual behavior is when using $(Rev:r) in the build number?
You have your build number format specified like this:
1.$(Rev:r).$(Year:yy)$(DayOfYear)
You do consequent builds. Everything on the left side from $(Rev:r) stays constant as you hard coded the prefix 1.. The right side of $(Rev:r) doesn't matter here. Hence, your $(Rev:r) keeps incrementing on 1, like this:
1.1.2010 // first build on tenth day of 2020
1.2.2010 // second build, same day
1.3.2115 // third build on fifteenth day of 2021
Now if you change your format to
1.$(Year:yy)$(DayOfYear).$(Rev:r)
you will end up with this behavior
1.2010.1 // first build on tenth day of 2020
1.2010.2 // second build, same day
1.2115.1 // third build on fifteenth day of 2021
I am trying to use TagLib# for sorting some of the tags in my MP3s, as my car audio has problems with content of some tags (irrelevant for showing). Surely i want just sort the tags, not generate new ones or replace existing. In ideal case only sequence of tags in a mp3 file should be changed, so the file size won't change.
Unfortunately i cannot find a way to just move TDRC tag (new definition for recording date): as soon as i set it, TagLib decides to do something for compatibility and generates also TYER aand TDAT tags, and if time is also present in TDRC, then it generates also TIME tag.
Is there a way to disable autogeneration of these tags when setting TDRC? i have tried just copy TDRC as TextInformationFrame, without luck.
I am using latest TagLib, and processing MP3 files with id3v2.3 tags.
You added the information that you use Id3v2.3 with UTF16.
TDRC is officially only available for id3v2.4. If you use it with id3v2.3 you create a tag outside the official standard.
id3.org says for id3v2.4:
TDAT - Date / TIME - Time / TYER - Year
This frame is replaced by the TDRC frame, 'Recording time'
[F:4.2.5]
TDRC
The 'Recording time' frame contains a timestamp describing when the
audio was recorded. Timestamp format is described in the ID3v2
structure document [ID3v2-strct].
The timestamp fields are based on a subset of ISO 8601. When being as
precise as possible the format of a time string is
yyyy-MM-ddTHH:mm:ss (year, "-", month, "-", day, "T", hour (out of
24), ":", minutes, ":", seconds), but the precision may be reduced by
removing as many time indicators as wanted.Hence valid timestamps
are: yyyy, yyyy-MM, yyyy-MM-dd, yyyy-MM-ddTHH, yyyy-MM-ddTHH:mm and
yyyy-MM-ddTHH:mm:ss. All time stamps are UTC. For durations, use
the slash character as described in 8601, and for multiple non-
contiguous dates, use multiple strings, if allowed by the frame
definition.
I've got something like this DateTime.Now.ToString("dd.MM.yy"); In my code, And I need to add 1 week to it, like 5.4.2012 to become 12.4.2012 I tried to convert it to int and then add it up, but there is a problem when it's up to 30.
Can you tell me some clever way how to do it?
You want to leave it as a DateTime until you are ready to convert it to a string.
DateTime.Now.AddDays(7).ToString("dd.MM.yy");
First, always keep the data in it's native type until you are ready to either display it or serialize it (for example, to JSON or to save in a file). You wouldn't convert two int variables to strings before adding or multiplying them, so don't do it with dates either.
Staying in the native type has a few advantages, such as storing the DateTime internally as 8 bytes, which is smaller than most of the string formats. But the biggest advantage is that the .NET Framework gives you a bunch of built in methods for performing date and time calculations, as well as parsing datetime values from a source string. The full list can be found here.
So your answer becomes:
Get the current timestamp from DateTime.Now. Use DateTime.Now.Date if you'd rather use midnight than the current time.
Use AddDays(7) to calculate one week later. Note that this method automatically takes into account rolling over to the next month or year, if applicable. Leap days are also factored in for you.
Convert the result to a string using your desired format
// Current local server time + 7 days
DateTime.Now.AddDays(7).ToString("dd.MM.yy");
// Midnight + 7 days
DateTime.Now.Date.AddDays(7).ToString("dd.MM.yy");
And there are plenty of other methods in the framework to help with:
Internationalization
UTC and timezones (though you might want to check out NodaTime for more advanced applications)
Operator overloading for some basic math calcs
The TimeSpan class for working with time intervals
Any reason you can't use the AddDays method as in
DateTime.Now.AddDays(7)
I need to format the day time using QueryPerformanceCounter Win32 API.
The format, is: HH:mm:ss.ffffff , containing hours minuts seconds and microseconds.
I need to use THIS function, because another process (written in C) is using this function and the purpose is using the same function in both places.
Thanks
The System.Diagnostics.Stopwatch class uses QueryPerformanceCounter(), saves you from having to P/Invoke it.
You should not use QueryPerformanceCounter to determine time of day. It can only be used to determine an elapsed interval with a very high resolution as it returns the number of ticks that passed since the computer was last restarted.
As such, at best, you may only determine how many hours, minutes, and seconds have passed since a previous reading of QueryPerformanceCounter which must not have happened too long in the past.
In order to convert from ticks to seconds you need to determine the frequency (using QueryPerformanceFrequency) of the ticks on the computer you're running the QueryPerformanceCounter function and then divide your reading by that frequency:
// obtain frequency
long freq;
QueryPerformanceFrequency(freq);
// then obtain your first reading
long start_count;
long end_count;
QueryPerformanceCounter(start_count)
// .. do some work
// obatin your second reading
QueryPerformanceCounter(end_count);
// calculate time elapsed
long milliseconds_elapsed = (long)(((double)(end_count - start_count) / freq) * 1000);
// from here on you can format milliseconds_elapsed any way you need to
An alternative to the above example would be to use the TimeSpan structure available in .Net which has a constructor that takes ticks like so:
// then obtain your first reading
long start_count;
long end_count;
QueryPerformanceCounter(start_count)
// .. do some work
// obatin your second reading
QueryPerformanceCounter(end_count);
TimeSpan time_elapsed = new TimeSpan(end_count - start_count);
Console.WriteLine("Time Elapsed: " + time_elapsed.ToString());
Can use :
1) The System.Diagnostics.Stopwatch class uses QueryPerformanceCounter(), saves you from having to P/Invoke it.
2) Can use directly by importing from the Win32 dll . [DLLImport(Win32)] and the name ofthe function
Possibly I misunderstand the question, as for me none of the previous answers are relevant at all.
I had the problem (which sent me here): Given a value from QueryPerformanceCounter, because something out of my control specifies timestamps using that function, how can I convert these values to a normal date / time?
I figured that QueryPerformanceCounter returns the number of seconds since the system booted, multiplied (and extended in resolution) depending on QueryPerformanceFrequency.
Thus, the most simple solution is to get the current date/time, subtract the amount of seconds returned by QueryPerformanceCounter/QueryPerformanceFrequency, and then add the values you like to format as time of day.
I'm having problems deciding on what is the best way is to handle and store time measurements.
I have an app that has a textbox that allows the users to input time in either hh:mm:ss or mm:ss format.
So I was planning on parsing this string, tokenizing it on the colons and creating TimeSpan (or using TimeSpan.Parse() and just adding a "00:" to the mm:ss case) for my business logic. Ok?
How do I store this as in a database though? What would the field type be? DateTime seems wrong. I don't want a time of 00:54:12 to be stored as 1901-01-01 00:54:12 that seems a bit poor?
TimeSpan has an Int64 Ticks property that you can store instead, and a constructor that takes a Ticks value.
I think the simplest is to just convert user input into a integer number of seconds. So 54:12 == 3252 seconds, so store the 3252 in your database or wherever. Then when you need to display it to the user, you can convert it back again.
For periods less than a day, just use seconds as other have said.
For longer periods, it depends on your db engine. If SQL Server, prior to version 2008 you want a datetime. It's okay- you can just ignore the default 1/1/1900 date they'll all have. If you are fortunate enough to have sql server 2008, then there are separate Date and Time datatypes you can use. The advantage with using a real datetime/time type is the use of the DateDiff function for comparing durations.
Most databases have some sort of time interval type. The answer depends on which database you're talking about. For Oracle, it's just a floating point NUMBER that represents the number of days (including fractional days). You can add/subtract that to/from any DATE type and you get the right answer.
As an integer count of seconds (or Milliseconds as appropriate)
Are you collecting both the start time and stop time? If so, you could use the "timestamp" data type, if your DBMS supports that. If not, just as a date/time type. Now, you've said you don't want the date part to be stored - but consider the case where the time period spans midnight - you start at 23:55:01 and end at 00:05:14, for example - unless you also have the date in there. There are standard build in functions to return the elapsed time (in seconds) between two date-time values.
Go with integers for seconds or minutes. Seconds is probably better. you'll never kick yourself for choosing something with too much precision. Also, for your UI, consider using multiple text inputs you don't have to worry about the user actually typing in the ":" properly. It's also much easier to add other constraints such as the minute and second values conting containing 0-59.
and int type should do it, storing it as seconds and parsing it back and forth
http://msdn.microsoft.com/en-us/library/ms187745.aspx