Advertisement
HAR1F

MyDate.cs

Feb 28th, 2019
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.87 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using Realms;
  5.  
  6. namespace Calculator2
  7. {
  8.     public class MyDate : RealmObject
  9.     {
  10.         public int Day { get; set; }
  11.         public int Month { get; set; }
  12.         public int Year { get; set; }
  13.         public int Hour { get; set; }
  14.         public int Minute { get; set; }
  15.         public int Second { get; set; }
  16.         public string Type { get; set; }
  17.  
  18.         public override string ToString()
  19.         {
  20.             return Month + "." + Day + "." + Year + " " + Hour + "." + Minute + "." + Second + " " + Type;
  21.         }
  22.  
  23.         public MyDate(string s)
  24.         {
  25.             string[] str = s.Split(' ');
  26.             string[] Date = str[0].Split(new Char[] { '.', '/', ':' });
  27.             string[] Time = str[1].Split(new Char[] { '.', '/', ':' });
  28.  
  29.             Day = Int32.Parse(Date[1]);
  30.             Month = Int32.Parse(Date[0]);
  31.             Year = Int32.Parse(Date[2]);
  32.  
  33.             Hour = Int32.Parse(Time[0]);
  34.             Minute = Int32.Parse(Time[1]);
  35.             Second = Int32.Parse(Time[2]);
  36.  
  37.             if (str.Length == 3)
  38.                 Type = str[2];
  39.             else
  40.                 Type = "24";
  41.         }
  42.  
  43.         public MyDate()
  44.         {
  45.         }
  46.  
  47.         public static long DateToLong (MyDate a)
  48.         {
  49.             int[] CntDay = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
  50.             long ans = 0;
  51.             ans += 6940 * 24 * 60 * 60;
  52.  
  53.             for (int i = 0; i < 12; ++i) {
  54.                 if (i + 1 < a.Month)
  55.                     ans += CntDay[i] * 24 * 60 * 60;
  56.             }
  57.  
  58.             for (int i = 1; i <= 31; ++i)
  59.             {
  60.                 if (i < a.Day)
  61.                     ans += 24 * 60 * 60;
  62.             }
  63.  
  64.             for (int i = 1; i <= 24; ++i)
  65.             {
  66.                 if (i <= a.Hour)
  67.                     ans += 60 * 60;
  68.             }
  69.  
  70.             for (int i = 1; i < 60; ++i)
  71.             {
  72.                 if (i <= a.Minute)
  73.                     ans += 60;
  74.             }
  75.  
  76.             ans += a.Second;
  77.  
  78.             return ans;
  79.         }
  80.  
  81.         public static string TimeToString(long val, int type)
  82.         {            
  83.             long seconds = 0, hours = 0, minutes = 0, days = 0;
  84.  
  85.             if (type == 1 || type == 2)
  86.             {
  87.                 if (val > 365 * 24 * 60 * 60)
  88.                 {
  89.                     if (type == 1)
  90.                         return "Вы не курили больше года!";
  91.                     else
  92.                         return "Вы не пили больше года!";
  93.                 }
  94.             }
  95.  
  96.             if (val / (60 * 60 * 24) > 0) {
  97.                 days = val / (60 * 60 * 24);
  98.                 val -= days * 60 * 60 * 24;                
  99.             }
  100.  
  101.             if (val / (60 * 60) > 0)
  102.             {
  103.                 hours = val / (60 * 60);
  104.                 val -= hours * 60 * 60;                
  105.             }
  106.  
  107.             if (val / (60) > 0)
  108.             {
  109.                 minutes = val / (60);
  110.                 val -= minutes * 60;                
  111.             }
  112.  
  113.             if (val > 0)
  114.             {                
  115.                 seconds = val;
  116.             }
  117.  
  118.             string StrType = "";
  119.             if (type == 1)
  120.                 StrType = "сигареты:";
  121.             if (type == 2)
  122.                 StrType = "кружки:";
  123.             if (type == 3)
  124.                 StrType = "сигаретами:";
  125.             if (type == 4)
  126.                 StrType = "кружками:";
  127.             if (type == 5)
  128.                 StrType = "сигареты";
  129.             if (type == 6)
  130.                 StrType = "кружки";
  131.  
  132.             if (type == 1 || type == 2)
  133.             {
  134.                 return $"С последней {StrType} {days}д {hours}ч {minutes}м {seconds}с!";
  135.             }
  136.             if (type == 3 || type == 4)
  137.             {
  138.                 return $"Среднее время между {StrType} {days}д {hours}ч {minutes}м {seconds}с!";
  139.             }
  140.             if (type == 5 || type == 6)
  141.             {
  142.                 return $"С первой {StrType} прошло: {days}д {hours}ч {minutes}м {seconds}с!";
  143.             }
  144.             if (type == 7)
  145.             {
  146.                 return $"Максимальный промежуток воздержания: {days}д {hours}ч {minutes}м {seconds}с!";
  147.             }
  148.             if (type == 8)
  149.             {
  150.                 return $"Длительность курения последней сигареты: {days}д {hours}ч {minutes}м {seconds}с!";
  151.             }
  152.             if (type == 9)
  153.             {
  154.                 return $"Длительность последнего принятия алкоголя: {days}д {hours}ч {minutes}м {seconds}с!";
  155.             }
  156.  
  157.             return "";
  158.         }
  159.     }
  160. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement